4

I'm using implicit val reads to map Json like:

{
   "id": 1
   "friends": [
    {
      "id": 1,
      "since": ...
    },
    {
      "id": 2,
      "since": ...
    },
    {
      "id": 3,
      "since": ...
    }
  ]
}

to a case class

case class Response(id: Long, friend_ids: Seq[Long])

I can only make it work with an intermediate class that reflects the JSON friends structure. But I never use it in my app. Is there a way to write a Reads[Response] object so that my Response class would map directly to the JSON given?

2
  • something like case class Response(id: Long, friends: Seq[Friend])?
    – mfirry
    Mar 24, 2016 at 14:53
  • that will work, but I don't have and don't want to create Friend class. I only need their ids Mar 24, 2016 at 14:57

3 Answers 3

5

You only need simple Reads[Response] with explicit Reads.seq() for friend_ids such as

val r: Reads[Response] = (
  (__ \ "id").read[Long] and
    (__ \ "friends").read[Seq[Long]](Reads.seq((__ \ "id").read[Long]))
  )(Response.apply _)

and result will be:

r.reads(json)

scala> res2: play.api.libs.json.JsResult[Response] = JsSuccess(Response(1,List(1, 2, 3)),)
2

The easy way could be:

import play.api.libs.functional.syntax._
import play.api.libs.json.{JsValue, Json, _}


case class Response(id: Long, friend_ids: Seq[Friends])

object Response {

  implicit val userReads: Reads[Response] = (
    (JsPath \ "id").read[Long] and
      (JsPath \ "friends").read[Seq[Friends]]
    ) (Response.apply _)
}

case class Friends(id: Long, since: String)
object Friends {
  implicit val fmt = Json.format[Friends]
}

without the case class Friends I'm finding it harder to find a solution but will post if I can find one

Edit: Added link for answer on Scala reedit

So, I wanted to understand a bit more about how to parse json to models, and decided to ask on Reedit. Received some pretty cool links, have a look:

https://www.reddit.com/r/scala/comments/4bz89a/how_to_correctly_parse_json_to_scala_case_class/

1

You can try the following

@annotation.tailrec
def go(json: Seq[JsValue], parsed: Seq[Long]): JsResult[Seq[Long]] =
  json.headOption match {
    case Some(o @ JsObject(_)) => (o \ "id").validate[Long] match {
      case JsError(cause) => JsError(cause)
      case JsSuccess(id)  => go(json.tail, parsed :+ id)
    }
    case Some(js) => JsError(s"invalid friend JSON (expected JsObject): $js")
    case _ => JsSuccess(parsed) // nothing more to read (success)
  }

implicit val friendIdReader = Reads[Seq[Long]] {
  case JsArray(values) => go(values, Nil)
  case json => JsError(s"unexpected JSON: $json")
}

implicit val responseReader = Json.reads[Response]
// responseReader will use friendIdReader as Reads[Seq[Long]],
// for the property friend_ids
1
  • Thanks for your suggestion! That probably will work, but I'd rather go with an utility class (e.g. Friend), if there is no easier way of doing it. And I suppose there is none. Mar 24, 2016 at 15:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.