Yes, you need to use type hints for sum types such as Geometry. Here is one example:
implicit val formats = DefaultFormats.withHints(ShortTypeHints(List(classOf[Point], classOf[LineString], classOf[Polygon]))) val r = Request("test", LineString(List(Point(100.0, 0.0), Point(101.0, 1.0)))) Serialization.write(r) { "name":"test", "geometry":{ "jsonClass":"LineString", "coordinates":[{"jsonClass":"Point","coordinates":{"_1$mcD$sp":100.0,"_2$mcD$sp":0.0}},{"jsonClass":"Point","coordinates":{"_1$mcD$sp":101.0,"_2$mcD$sp":1.0}}]} }
Not exactly what you wanted. Since you want to change the default serialization scheme for Points, you need to provide your own serializer for this type.
class PointSerializer extends Serializer[Point] { private val Class = classOf[Point] def deserialize(implicit format: Formats) = { case (TypeInfo(Class, _), json) => json match { case JArray(JDouble(x) :: JDouble(y) :: Nil) => Point(x, y) case x => throw new MappingException("Can't convert " + x + " to Point") } } def serialize(implicit format: Formats) = { case p: Point => JArray(JDouble(p.coordinates._1) :: JDouble(p.coordinates._2) :: Nil) } } // Configure it implicit val formats = DefaultFormats.withHints(ShortTypeHints(List(classOf[Point], classOf[LineString], classOf[Polygon]))) + new PointSerializer Serialization.write(r) { "name":"test", "geometry":{ "jsonClass":"LineString", "coordinates":[[100.0,0.0],[101.0,1.0]] } }
Better, but you need one more configuration if you need to change the default field named "jsonClass" to "type":
implicit val formats = new DefaultFormats { override val typeHintFieldName = "type" override val typeHints = ShortTypeHints(List(classOf[Point], classOf[LineString], classOf[Polygon])) } + new PointSerializer Serialization.write(r) { "name":"test", "geometry":{ "type":"LineString", "coordinates":[[100.0,0.0],[101.0,1.0]] } }