当前位置: 首页 > 面试题库 >

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

公冶俊达
2023-03-14
问题内容

有两个课FooBarFoo包含的字段Bar。问题是,如何Writes为类实现隐式json Foo

这是代码:

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
      )
    }
  }
}

当我尝试编译时,出现以下错误:

没有为类型模型找到Json反序列化器。尝试为此类型实现隐式的Writes或Format。

我不了解此编译器错误,因为我为models.Bar类实现了隐式Writes。这里有什么问题?


问题答案:

这是可见性的问题,当您声明隐式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
  }
}

另外,如果您使用的是Play
2.1+,请确保检查出2.10宏的全新用法:http
:
//www.playframework.com/documentation/2.1.0/ScalaJsonInception


如果您对案例类的使用感到满意,并且将val / vars名称用作json输出中的键(例如在案例BTW中),则可以使用两个单行代码:

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

这将为您提供完全相同的内容:

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
  }
}


 类似资料:
  • 问题内容: 我正在尝试使用Jackson 2.0-RC3读取旧版JSON代码,但是我陷入了“嵌入式”对象的困境。 给定以下JSON: 如何将其映射到以下结构: 我试图这样做,但似乎我必须以这种方式映射整个对象。 问题答案: 要处理“嵌入式”对象,您应该使用它-等同于Hibernate的/ 。

  • JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明。JSP隐式对象也被称为预定义变量。 JSP所支持的九大隐式对象: 对象 描述 request HttpServletRequest 接口的实例 response HttpServletResponse 接口的实例 out JspWriter类的实例,用于把结果输出至网页上 session HttpSess

  • 问题内容: 假设我有以下文件, 现在,如果我跑步,它说。 这是有道理的,并且按照此链接中的以下语句运行: “它将首先在包的目录中查找” 假设我稍微修改了文件结构(添加了一个核心目录): 现在,如果我运行,它将加载内置模块。 同样在第二种情况下,如果必须遵守“ 它将首先在软件包的目录中查找 ” 这样 的语句 ,是否 应该加载本地文件,因为它是“软件包的目录”? 我的术语“包目录”的意义是 明确 的

  • 本文向大家介绍接口的隐式实现是什么?何时在C#中使用接口的隐式实现?,包括了接口的隐式实现是什么?何时在C#中使用接口的隐式实现?的使用技巧和注意事项,需要的朋友参考一下 C#接口成员可以显式或隐式实现。 隐式实现在成员名称之前不包括要实现的接口的名称,因此编译器会推断出该名称。成员将公开显示,并且在将对象转换为混凝土类型时可以访问。 方法的调用也没有不同。只需创建该类的对象并调用它即可。 如果在

  • 我有一个猫鼬图式: 当我看到一个新的时,它是这样存储的:

  • 问题内容: 我有一个想要有效地进行JSON编码的结构: 该结构包含一个已知形式的元数据和一个未知形式的目录。目录列表在运行时填充,因此我并没有真正的控制权。为了提高Go的编组速度,我想在Meta结构上实现Marshaller接口。Marshaller界面如下所示: 请记住,元结构并不像这里所示的那么简单。我尝试过在Meta结构上实现Marshaler接口,但似乎当我再将JSON编组MyStruct