简体   繁体   English

Scala play-json-不解析/反序列化为单参数案例类

[英]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. 在Scala中,我想解析使用Travis CI REST API检索到的JSON响应。 For that, I've decided to use the play-json library. 为此,我决定使用play-json库。 Along with this, and in accordance to the REST API 'schema', I've defined the following case classes: 同时,根据REST API的“模式”,我定义了以下案例类:

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: 同时,我为play-json库定义了以下Reads:

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: 代码会编译,架构即案例类确实会确认REST API响应,但是,在验证响应时,出现以下错误:

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 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( 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, 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 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()))))) 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]: 如果您要序列化的case类没有使用任何不需要您自己编写读者或作家的自定义类型,则使用play Json可以使用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]). 这将自动为您生成读取(您也可以使用Json.Writes [T]或Json.Format [T])。 Play is clever enough to be able to use this to deserialize a List[T] also. Play足够聪明,可以使用它来反序列化List [T]。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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