Playframework - JSON parsing object with single field - definition issue

29 views
Skip to first unread message

Jakub Stransky

unread,
Mar 26, 2015, 11:56:29ā€ÆAM3/26/15
to play-fr...@googlegroups.com

I cannot find a way how to make it work when de-serialized object has single field - I cannot compile the code. Seems that and operator does some transformation and I cannot find a method to call to do the same.

I have following json:

{"total": 53, "max_score": 3.2948244, "hits": [
                                 {
                                     "_index": "h",
                                     "_type": "B",
                                     "_id": "3413569628",
                                     "_score": 3.2948244,
                                     "_source": {
                                         "fotky": [
                                             {
                                                 "popisek":" ",
                                                 "localFileSystemLocation":" ",
                                                 "isMain": true,
                                                 "originalLocation": ""
                                             }
                                         ]
                                     }
                                 }
                             ]
}

I try the following data model to de serialize to:

case class SearchLikeThisResult(total: Int, max_score: Double, hits: Seq[Hits])

case class Hits(_index: String, _type: String, _id: String, _score: Double, _source: Source)

case class Source(fotky: Seq[Photo])

case class Photo(isMain: Boolean, originalLocation: Option[String], localFileSystemLocation: Option[String], popisek: Option[String])

Implicit reads as follows:

object SearchLikeThisHits {

  import play.api.libs.functional.syntax._

  implicit val photoReads: Reads[Photo] = (
    (JsPath \ "isMain").read[Boolean] and
      (JsPath \ "originalLocation").readNullable[String] and
      (JsPath \ "localFileSystemLocation").readNullable[String] and
      (JsPath \ "popisek").readNullable[String]
    )(Photo.apply _)    

  implicit val sourceReads: Reads[Source] = (
    (JsPath \ "fotky").read[Seq[Photo]]
    )(Source.apply _)

  implicit val hitsReads: Reads[Hits] = (
    (JsPath \ "_index").read[String] and
      (JsPath \ "_type").read[String] and
      (JsPath \ "_id").read[String] and
      (JsPath \ "_score").read[Double] and
      (JsPath \ "_source").read[Source]
    )(Hits.apply _)

  implicit val searchLikeThisResult: Reads[SearchLikeThisResult] = (
    (JsPath \ "total").read[Int] and
      (JsPath \ "max_score").read[Double] and
      (JsPath \ "hits").read[Seq[Hits]]
    )(SearchLikeThisResult.apply _)
}

What I am really struggling with is under the _source:

implicit val sourceReads: Reads[Source] = ( (JsPath \ "fotky").read[Seq[Photo]] )(Source.apply _)

where read is reported as unkown symbol - in other cases "and" performs some transformation. Inline definition doesn't help either.

Does anybody faced this before?

jeff...@rallyhealth.com

unread,
Mar 26, 2015, 5:36:47ā€ÆPM3/26/15
to play-fr...@googlegroups.com
You just have to useĀ 
(JsPath \ "fotky").read[Seq[Photo]].map(Source.apply)
Reply all
Reply to author
Forward
0 new messages