[英]Serialize JSON to Scala case class
I need to serialize JSON to scala case class. 我需要将JSON序列化为scala case类。 It is not a question about JSON serialization, but is a question about class mapping in scala.
这不是有关JSON序列化的问题,而是有关scala中的类映射的问题。
The example of JSON: JSON的示例:
{
"id": 98433,
"name": "Santa Cruz Bronson",
"vendor": {
"id": 344,
"name": "Santa Cruz"
},
"category": {
"id": 132,
"name": "Bicycles"
},
"annotation": "The best downhill cycle",
"description": "Rich text is here",
"classification": {
"id": 12,
"name": "138-cycles"
},
"properties": [{
"id": 84436,
"group": {
"id": 19433,
"name": "Suspension"
},
"name": "Fork turn",
"description": "Fork turn defines bike suspension",
"value": "129mm"
}, {
"id": 84436,
"group": {
"id": 19433,
"name": "Suspension"
},
"name": "Fork turn",
"description": "Fork turn defines bike suspension",
"value": "129mm"
}, {
"id": 84436,
"group": {
"id": 19433,
"name": "Suspension"
},
"name": "Fork turn",
"description": "Fork turn defines bike suspension",
"value": "129mm"
}, {
"id": 84436,
"group": {
"id": 19433,
"name": "Suspension"
},
"name": "Fork turn",
"description": "Fork turn defines bike suspension",
"value": "129mm"
}],
"isGroup": true
}
I know how to build a case class for top level map: 我知道如何为顶级地图构建案例类:
case class ProductDocument(id: Long, name: String, annotation: String, description: String, isGroup: String) extends DocumentMap {
...
}
But I have no idea how to build values for vendor, category, properties, etc. 但是我不知道如何为供应商,类别,属性等建立价值。
I want to define map for this JSON into one class file. 我想将此JSON的映射定义为一个类文件。
Each of the nested json objects should be defined as their own case classes, such that: 每个嵌套的json对象都应定义为自己的case类,这样:
case class Vendor(id: Long, name: String)
case class ProductDocument(id: Long, ..., vendor: Vendor)
The properties
would become a List
of a Property
case class: 这些
properties
将成为Property
案例类的List
:
case class Property(id: Long, group: PropertyGroup, name: String, description: String, value: String)
case class PropertyGroup(id: Long, name: String)
case class ProductDocument(id: Long, ..., properties: List[Property])
This assumes that you are using json4s serialization. 假设您正在使用json4s序列化。
For scala-json mapping you have at least two options: 对于scala-json映射,您至少有两个选择:
Salat - which originally provided ORM-like functionality for Mongo but also works for just JSON serialization (disclaimer: I work at Novus) Salat-最初为Mongo提供类似于ORM的功能,但仅适用于JSON序列化(免责声明:我在Novus工作)
json4s - see section "Serialization" json4s-请参见“序列化”部分
The good news is that because the structure of Scala case classes are well-understood, you generally do not have to define "mapping" between your JSON fields and case class fields. 好消息是,由于Scala案例类的结构已经很容易理解,因此您通常不必在JSON字段和案例类字段之间定义“映射”。 You just write your case classes to match the structure / fields of your JSON document, as Arne Claassen has sketched out.
正如Arne Claassen所概述的那样,您只需编写案例类以匹配JSON文档的结构/字段即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.