【发布时间】:2017-03-09 10:56:24
【问题描述】:
我的案例类有 30 个字段。为简单起见,我使用了 4 个字段,
case class Person(id: Long, name: String, age: Int, sex: Sex)
val personFormat1: OFormat[(Long, String)] = ((__ \ "id").format[Long] ~ (__ \ "name").format[String]).tupled
val personFormat2: OFormat[(Int, Sex)] = ((__ \ "age").format[Int] ~ (__ \ "sex").format[Sex]).tupled
implicit val personFormat: Format[Person] = (personFormat1 ~ personFormat2)({
case ((id, name), (age, sex)) => new Person(id, name, age, sex)
}, (person: Format) => ((person.id, person.name), (person.age, person.sex)))
但是,即使在使用 format1 作为一组 22 个字段和 format2 作为一组 8 个字段编写格式化程序之后,我在尝试解析这个案例类的 json 时也会出错。
错误是 没有为 Person 类型找到作为 JsObject 的 Json 序列化程序。尝试为此类型实现隐式 OWrites 或 OFormat。
如何编写隐式 Owrite 或 OFormat?或者如何解决这个问题?
【问题讨论】:
标签: json scala playframework