简体   繁体   English

如何在Play Framework 2.x中实现嵌入对象的隐式Json Writes

[英]How to implement implicit Json Writes of embedded object in Play Framework 2.x

there are two classes Foo and Bar . FooBar有两个类。 Foo contains a field of Bar . Foo包含一个Bar字段。 The question is, how do I implement an implicit json Writes for class Foo ? 问题是,如何为类Foo实现隐式json Writes

Here is the code: 这是代码:

package models

import play.api.libs.json._

case class Foo(id: String, bar: Bar)

object Foo {
  implicit val implicitFooWrites = new Writes[Foo] {
    def writes(foo: Foo): JsValue = {
      Json.obj(
        "id" -> foo.id,
        "bar" -> foo.bar
      )
    }
  }
}

case class Bar(x: String, y: Int)

object Bar {
  implicit val implicitBarWrites = new Writes[Bar] {
    def writes(bar: Bar): JsValue = {
      Json.obj(
        "x" -> bar.x,
        "y" -> bar.y
      )
    }
  }
}

When I try to compile, I get the following error: 当我尝试编译时,我收到以下错误:

No Json deserializer found for type models.Bar. 没有找到类型模型的Json反序列化器.Bar。 Try to implement an implicit Writes or Format for this type. 尝试为此类型实现隐式写入或格式。

I don't understand this compiler error, since I implemented an implicit Writes for models.Bar class. 我不明白这个编译器错误,因为我为models.Bar类实现了一个隐式Writes。 What is the problem here? 这里有什么问题?

It's a question of visibility, when declaring the implicit Writes[Foo] you are not making visible the implicit Writes[Bar] to it: 这是一个可见性的问题,当声明隐式Writes [Foo]时,你没有看到它的隐式Writes [Bar]:

scala> :paste
// Entering paste mode (ctrl-D to finish)

import play.api.libs.json._

case class Bar(x: String, y: Int)

object Bar {
  implicit val implicitBarWrites = new Writes[Bar] {
    def writes(bar: Bar): JsValue = {
      Json.obj(
        "x" -> bar.x,
        "y" -> bar.y
      )
    }
  }
}

case class Foo(id: String, bar: Bar)

object Foo {

  import Bar._

  implicit val implicitFooWrites = new Writes[Foo] {
    def writes(foo: Foo): JsValue = {
      Json.obj(
        "id" -> foo.id,
        "bar" -> foo.bar
      ) 
    } 
  }     
}

// Exiting paste mode, now interpreting.

import play.api.libs.json._
defined class Bar
defined module Bar
defined class Foo
defined module Foo

scala> Json.prettyPrint(Json.toJson(Foo("23", Bar("x", 1))))
res0: String = 
{
  "id" : "23",
  "bar" : {
    "x" : "x",
    "y" : 1
  }
}

Also, if you're using Play 2.1+ make sure to check out the brand new use of 2.10's macros: http://www.playframework.com/documentation/2.1.0/ScalaJsonInception 此外,如果您使用的是Play 2.1+,请务必查看2.10宏的全新用法: http//www.playframework.com/documentation/2.1.0/ScalaJsonInception

If you're happy with the use of the case classes and the val/vars' names being used as keys in the json output, as in your case BTW, then you can use the two one-liners: 如果您对使用case类和val / vars的名称作为json输出中的键使用感到满意,就像你的BTW一样,那么你可以使用两个单行:

implicit val barFormat = Json.writes[Bar]
implicit val fooFormat = Json.writes[Foo]

That will give you the exact equivalent: 这将给你完全相同的:

scala> import play.api.libs.json._
import play.api.libs.json._

scala> case class Bar(x: String, y: Int)
defined class Bar

scala> case class Foo(id: String, bar: Bar)
defined class Foo

scala> implicit val barWrites = Json.writes[Bar]
barWrites: play.api.libs.json.OWrites[Bar] = play.api.libs.json.OWrites$$anon$2@257cae95

scala> implicit val fooWrites = Json.writes[Foo]
fooWrites: play.api.libs.json.OWrites[Foo] = play.api.libs.json.OWrites$$anon$2@48f97e2a

scala> Json.prettyPrint(Json.toJson(Foo("23", Bar("x", 1))))
res0: String = 
{
  "id" : "23",
  "bar" : {
    "x" : "x",
    "y" : 1
  }
}

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

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