I need to deserialize some JSON that looks like the following:
{ "states":
{ "Position" : { "x": 1, "y": 2, "z": 3 },
"Timestamp" : { "value" : 123 } }
}
The fields named Position and Timestamp are the name of the class to be serialized.
The only way I have been able to deserialize this at the moment is to transform this JSON this into a format that lift web JSON understands. For example:
{ "states": [
{ "jsonClass": "Position", "x": 1, "y": 2, "z": 3 },
{ "jsonClass": "Timestamp", "value" : 123 }
]}
With formats
as follows
implicit val formats = new DefaultFormats {
override val typeHintFieldName = "type"
override val typeHints = ShortTypeHints(List(classOf[Position], classOf[Timestamp]))
}
Is it possible to deserialize the top form?
Using Jackson, then:
Gives
List(States(List(State(Position(1,2,3),Timestamp(123)))))
Note1: The configure lines allow Jackson to treat an {} as a single element array, in cases where the Json uses badgerfish notation
Note2: If you have upper case field names, rename the field names in the case class to match, eg
case class State(Position: Position, Timestamp: Timestamp)