我正在尝试使用Lift-Json自动反序列化json对象到scala类,其中包含用于存储GeoJson信息的坐标类。
case class Request(name:String, geometry:Geometry)
sealed abstract class Geometry
case class Point(coordinates:(Double,Double)) extends Geometry
case class LineString(coordinates:List[Point]) extends Geometry
case class Polygon(coordinates:List[LineString]) extends Geometry
我想这样反序列化json字符串:
{
name:"test",
geometry:{
"type": "LineString",
"coordinates": [ [100.0, 0.0], [101.0, 1.0] ]
}
}
转换为Request case类,并在Geometry字段中使用正确的LineString运行时类。我想我应该使用TypeHint,但怎么用呢。这是正确的方法,还是我应该创建三个不同的请求(RequestPoint,RequestLineString和RequestPolygon)?下面是要反序列化的Scala代码:
val json = parse(message)
json.extract[Request]
是的,您需要为和类型(如Geometry)使用类型提示。下面是一个例子:
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}}]}
}
不是你想要的。由于要更改point的默认序列化方案,因此需要为该类型提供自定义序列化器。
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]]
}
}
更好,但如果你需要将默认字段名为'jsonClass'更改为'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]]
}
}