我有一个这样的案例类:
case class Product(ean: Long, name: String, description: String, purchasePrice: Option[BigDecimal] = None, sellingPrice: Option[BigDecimal] = None)
我有一个像这样的非隐式写:
val adminProductWrites = (
(JsPath \ "ean").write[Long] and
(JsPath \ "name").write[String] and
(JsPath \ "description").write[String] and
(JsPath \ "purchase_price").writeNullable[BigDecimal] and
(JsPath \ "selling_price").writeNullable[BigDecimal]
)(unlift(Product.unapply))
然后我有一个 Option[Product] 的实例:
val prod = Some(Product(5018206244611L, "Zebra Paperclips", "Zebra Length 28mm Assorted 150 Pack"))
当我试图序列化它时......:
val jsonStr = Json.toJson(prod) (adminProductWrites)
我收到这样的错误:
<console>:21: error: type mismatch;
found : play.api.libs.json.OWrites[Product]
required: play.api.libs.json.Writes[Some[Product]]
val jsonStr = Json.toJson(prod) (adminProductWrites)
所以,首先(只是为了比较)我试过这个:
val jsonStr = Json.toJson(prod.get) (adminProductWrites)
有用:
jsonStr: play.api.libs.json.JsValue = {"ean":5018206244611,"name":"Zebra Paperclips","description":"Zebra Length 28mm Assorted 150 Pack"}
但我不想这样做(调用.get)。当 Writes 被声明为隐式时,我需要按照它的方式工作:
implicit object ProductWrites extends Writes[Product] {
def writes(p: Product) = Json.obj(
"ean" -> Json.toJson(p.ean),
"name" -> Json.toJson(p.name),
"description" -> Json.toJson(p.description)
)
}
(使用隐式写入,此行有效):
scala> val jsonStr = Json.toJson(prod)
jsonStr: play.api.libs.json.JsValue = {"ean":5018206244611,"name":"Zebra Paperclips","description":"Zebra Length 28mm Assorted 150 Pack"}
我错过了什么?
附加说明:
我的隐式写入不完整,我故意删除了最后两个字段(purchasePrice 和 sellPrice)。原因:此代码无法编译:
implicit object ProductWrites extends Writes[Product] {
def writes(p: Product) = Json.obj(
"ean" -> Json.toJson(p.ean),
"name" -> Json.toJson(p.name),
"description" -> Json.toJson(p.description),
"purchase_price" -> p.purchasePrice.getOrElse(None),
"selling_price" -> p.sellingPrice.getOrElse(None)
)
}
我给出了这个错误:
<console>:24: error: No Json serializer found for type Serializable. Try to implement an implicit Writes or Format for this type.
"purchase_price" -> Json.toJson(p.purchasePrice.getOrElse(None)),
提前致谢, 拉卡