简体   繁体   中英

Scala play-json - doesn't parse/deserialize to single argument case class

In Scala, I want to parse JSON responses retrieved using the Travis CI REST API. For that, I've decided to use the play-json library. Along with this, and in accordance to the REST API 'schema', I've defined the following case classes:

case class Build(id : Int,
             repository_id : Int,
             commit_id : Int,
             number : String,
             event_type : String,
             pull_request : Boolean,
             pull_request_title : String,
             pull_request_number : String,
             state : String,
             started_at : String,
             finished_at : String,
             duration : Int,
             job_ids : List[Int]) extends Serializable{
}

case class Builds(builds : List[Build])

The implementation should be self-explanatory by itself, but if any further description is required, I'll update the question.

Along with this, I've defined the following Reads for the play-json library:

implicit val buildReads : Reads[Build] = (
      (JsPath \ "id").read[Int] and
      (JsPath \ "repository_id").read[Int] and
      (JsPath \ "commit_id").read[Int] and
      (JsPath \ "number").read[String] and
      (JsPath \ "event_type").read[String] and
      (JsPath \ "pull_request").read[Boolean] and
      (JsPath \ "pull_request_title").read[String] and
      (JsPath \ "pull_request_number").read[String] and
      (JsPath \ "state").read[String] and
      (JsPath \ "started_at").read[String] and
      (JsPath \ "finished_at").read[String] and
      (JsPath \ "duration").read[Int] and
      (JsPath \ "job_ids").read[List[Int]]
    )(Build.apply _)

implicit val buildsReads : Reads[Builds] = (__ \ "builds").read[List[Build]].map(Builds(_))

The code does compile, the schema ie the case classes do confirm to the REST API response, however, when validating the response, I get the following error:

JsError(List((/builds(0)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(0)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(1)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(1)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(2)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(2)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(3)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(3)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(4)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(4)/pull_request_number,List(ValidationError(List(error.expected.j sstring),WrappedArray()))), (/builds(5)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(5)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(6)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(7)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(7)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(8)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(8)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(9)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(9)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(10)/pull_request_title,List(ValidationError(List( error.expected.jsstring),WrappedArray()))), (/builds(10)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(11)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(12)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(12)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(13)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(13)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(14)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(14)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(15)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(15)/pull_request_number, List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(16)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(17)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(18)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(18)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(19)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(19)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(20)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(20)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(21)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/buil ds(22)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(22)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(23)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(23)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(24)/pull_request_title,List(ValidationError(List(error.expected.jsstring),WrappedArray()))), (/builds(24)/pull_request_number,List(ValidationError(List(error.expected.jsstring),WrappedArray())))))

Any clue onto what the problem might be? Thanks in advance!

With play Json if the case class which you intend to serialize is not using any custom types you don't need to hand write your readers or writers, you can use the Json.Writes[T]:

implicit val buildFormat = Json.Reads[Build]

This will automatically generate the Reads for you (you can also use Json.Writes[T] or Json.Format[T]). Play is clever enough to be able to use this to deserialize a List[T] also.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM