Given lift-json
2.0 and the following Scala classes & sealed trait:
sealed trait Location
case class Coordinate(latitude: Double,
longitude: Double) extends Location
case class Address(...) extends Location
I'd like to be able to deserialize a Location object without determining the concrete implementation:
deserialize[Location](""" { "latitude":0.0, "longitude":0.0 } """)
should produce the equivalent of:
val location: Location = Coordinate(0.0, 0.0)
Any way of doing this?
This may not be what you want, but with:
implicit val formats = net.liftweb.json.DefaultFormats
.withHints(ShortTypeHints(List(classOf[Geo], classOf[Address])))
enables you to write
val loc: Location = read(write(Geo(0.0, 0.0)))
However your json then gets a TypeHint:
{"jsonClass":"Geo","latitude":0.0,"longitude":0.0}
This formats can be played with somewhat, here is a nice post about type hints.