[英]Play2 Scala Json custom Format Combinator
I've the following model: 我有以下模型:
case class Person(name: String, age: Int, job: Option[String])
object PersonJsonFormats {
implicit val personFormat = Json.format[Person]
}
Converting a Person
object into Json (eg with Json.toJson(person)
) produces the following Json object. 将
Person
对象转换为Json(例如,使用Json.toJson(person)
)将生成以下Json对象。
{
"name": "John",
"age": 10,
"job": "gardener"
}
What should I change in order to produce an object like the following instead? 为了生成如下所示的对象,我应该改变什么?
[
{
"name": "name",
"value" : "John"
},
{
"name": "age",
"value": 10
},
{
"name": "job",
"value": "gardener"
}
]
I know I could write custom Reads
and Writes
but I want to write something I can apply to every case class
我知道我可以编写自定义的
Reads
和Writes
,但我想写的东西,我可以适用于每一个case class
If you don't want to write custom Reads and Writes and want something that you can apply to every case class
, you probably should use macros to to this. 如果您不想编写自定义的Reads and Writes,并且想要可以应用于每个
case class
,则可能应该使用宏。
That's how the original automatic implicit converters are build. 这就是原始自动隐式转换器的构建方式。
Places to look: 看的地方:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.