我们说我有一个这样的课程:
abstract class SomeSuperClass(name: String)
case class SomeClass(someString: String, opt: Option[String]) extends SomeSuperClass("someName")
我想序列化这个类并能够添加name
字段,这是我的第一个方法:
implicit def serialize: Writes[SomeClass] = new Writes[SomeClass] {
override def writes(o: SomeClass): JsValue = Json.obj(
"someString" -> o.someString,
"opt" -> o.opt,
"name" -> o.name
)
}
如果有null
,则返回None
,因此我将实施following the documentation更改为:
implicit def serialize: Writes[SomeClass] = (
(JsPath \ "someString").write[String] and
(JsPath \ "opt").writeNullable[String] and
(JsPath \ "name").write[String]
)(unlift(SomeClass.unapply))
这不会编译,只有在删除名称字段时才有效:
[error] [B](f: B => (String, Option[String], String))(implicit fu: play.api.libs.functional.ContravariantFunctor[play.api.libs.json.OWrites])play.api.libs.json.OWrites[B] <and>
[error] [B](f: (String, Option[String], String) => B)(implicit fu: play.api.libs.functional.Functor[play.api.libs.json.OWrites])play.api.libs.json.OWrites[B]
[error] cannot be applied to (api.babylon.bridge.messaging.Command.SomeClass => (String, Option[String]))
[error] (JsPath \ "opt").writeNullable[String] and
如何添加一个不严格存在于案例类中且具有可选字段的字段?
我正在使用play-json 2.3.0。
答案 0 :(得分:4)
您可以为JSON写入编写自己的提取器,而不是使用默认的(编译器生成的)unapply方法,该方法对您的继承值一无所知,它采用SomeClass
实例并返回(String, Option[String], String)
元组,即:
implicit def serialize: Writes[SomeClass] = (
(JsPath \ "someString").write[String] and
(JsPath \ "opt").writeNullable[String] and
(JsPath \ "name").write[String]
)(s => (s.someString, s.opt, s.name))
这会给你:
Json.toJson(SomeClass("foo", None))
// {"someString":"foo","name":"someName"}
Jspm.toJson(SomeClass("foo", Some("bar")))
// {"someString":"foo","opt":"bar","name":"someName"}