I'm going crazy trying to parse this JSON structure in Play Framework 2.2:
val jsonStr = """{ personFirstName: "FirstName",
personLastName: "LastName"
positionLat: null,
positionLon: null }"""
I have 2 case classes:
case class Position( val lat: Double, val lon: Double)
case class Person( firstName: String, lastName: String, p: Option[Position] )
As you can see, Position is not mandatory in Person case class.
I was trying to get an instance of Person using something like this
implicit val reader = (
(__ \ 'personFirstName ).read[String] ~
(__ \ 'personLastName ).read[String] ~
( (__ \ 'positionLat ).read[Double] ~
(__ \ 'positionLon ).read[Double] )(Position)
)(Person)
but I soon realized I have no idea how to deal with the Option[Position]
object: the intention would be to instantiate a Some(Position(lat,lon))
if both 'lat' and 'lon' are specified and not null, otherwise instantiate None
.
How would you deal with that?