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, the position is not required for the Person case class.
I tried 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 that I had no idea how to deal with the Option[Position] object: the intention was to create an instance of Some(Position(lat,lon)) if both βlatβ and βlonβ are specified, not null, otherwise an instance of None .
How do you handle this?
source share