Hey,
I saw this question on SO:
http://stackoverflow.com/questions/36202938/scala-play-reads-parse-nested-json/36210676
From times to times I find myself in the same position, having some
problems correctly parsing JSON to some Scala case class (often with
Sequences or DateTime).
1) So, how could one answer to the question on SO? The problem is in
parsing the JSON Array of 'Friends' into a Sequence of 'ids' (Longs).
Using a case class for the 'Friend' it would be easy:
https://gist.github.com/pedrorijo91/e8b89b0ae7a1e8d16938
But what about without the case class? How would the Response reader be like?
2) Another typical use case I face is when the JSON contains a long
(let's say json = { "date": 1458860074 }, and I want to map it to a
DateTime using the constructor that receives the long
Note: org.joda.time.DateTime
I would expect something like this to work:
case class Exp(datetime: DateTime)
object Exp {
implicit val reader: Reads[Exp] = (JsPath \ "date").read[Long].map(d
=> new DateTime(d)) (Exp.apply _)
}
But I get a:
type mismatch;
found : org.joda.time.DateTime => plugins.results.traits.config.Exp
required: play.api.libs.json.Reads[?]
3) Is there any good info on this json parse subject? I found
https://www.playframework.com/documentation/2.4.x/ScalaJson but it
doesn't seem very clear to me on how to do such not-so-basic
operations
Thanks,
Pedro