使用Scala和Play框架将自定义类型映射序列化为JSON
我使用以下信息创建了自定义类型的映射: 我需要将使用Scala和Play框架将自定义类型映射序列化为JSON,json,scala,serialization,playframework,Json,Scala,Serialization,Playframework,我使用以下信息创建了自定义类型的映射: 我需要将Map[Product,List[StockItem]]序列化/反序列化为JSON,但我真的不知道怎么做。我已经为这两种自定义类型(Product和StockItem)提供了可用的序列化程序,我使用Play的原生JSON库编写了这些序列化程序,但如果更好的话,我可以使用另一个。有人能给我一些指点吗 更新:List/StockItem示例不是一个好的示例,但它只是一个抽象。最后,我有一个表示1到n关系的结构。 因此,考虑到每个产品都有一个Stock
Map[Product,List[StockItem]]
序列化/反序列化为JSON,但我真的不知道怎么做。我已经为这两种自定义类型(Product和StockItem)提供了可用的序列化程序,我使用Play的原生JSON库编写了这些序列化程序,但如果更好的话,我可以使用另一个。有人能给我一些指点吗
更新:List/StockItem示例不是一个好的示例,但它只是一个抽象。最后,我有一个表示1到n关系的结构。 因此,考虑到每个产品都有一个StockItem列表,我希望JSON看起来类似于以下内容:
{
"products": [
{
"id": 1
"productName": "coffee",
"stockitems": [
{
"id":1,
"brandName": "Crazycoffee",
"price": 5
"quantity": 3000
},
{
"id":2,
"brandName": "Badcoffee",
"price": 1
"quantity": 2000
}
]
},
{
"id": 1
"productName": "ice cream",
"stockitems": [
{
"id":1,
"brandName": "Sleazyice",
"price": 2
"quantity": 300
}
]
}
]
}
我还没有测试它,但它是从我的部分工作java代码编写的。使用Jackson库(包括在第2场比赛中):
import com.fasterxml.jackson.core.JsonFactory;
导入com.fasterxml.jackson.core.JsonGenerator;
导入com.fasterxml.jackson.databind.ObjectMapper;
公共静态字符串toJson(映射lst)
{
JsonFactory工厂=新的JsonFactory();
CharArrayWriter writer=新的CharArrayWriter();
试一试{
jsongGenerator gen=factory.createGenerator(writer);
gen.writeStartObject();
gen.writeFieldName(“信息”);
gen.writeRaw(“:”+listToJson(lst));
writeEndObject将军();
关将军();
}捕获(可丢弃的e){
Logger.error(“AjaxRequest.generateResponse:+e.getMessage());
e、 printStackTrace();
}
返回新字符串(writer.tocharray());
}
/**
*将列表转换为json字符串。
*/
公共静态字符串listToJson(List lst)引发IOException
{
ObjectMapper mapper=新的ObjectMapper();
JsonFactory工厂=新的JsonFactory();
CharArrayWriter writer=新的CharArrayWriter();
jsongGenerator gen=factory.createGenerator(writer);
gen.writeRaw(mapper.writeValueAsString(lst));
关将军();
返回新字符串(writer.tocharray());
}
杰克逊图书馆工作得很好,但远不容易理解。无论如何,只要修改一下,这个代码就行了。 你可以考虑使用,这样你就可以节省一些时间,而不用手工写所有的作者和读者。您可以基于顶级JSON元素“products”创建一个“包装器”实体,例如ProductsList。下面是一个示例,说明了如何做到这一点:
case class ProductList (
list: Vector[Product]
)
object ProductList {
/**
* Mapping to and from JSON.
*/
implicit val documentFormatter: Format[ProductList] = (
(__ \ "products").format[Vector[Product]]
) (ProductList.apply, unlift(ProductList.unapply))
}
您希望生成的json看起来如何?这看起来像您提到的一个列表[产品],您有正在工作的序列化程序。我们能看看他们长什么样吗?
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
public static String toJson(Map<Product, List<StockItem>> lst)
{
JsonFactory factory = new JsonFactory();
CharArrayWriter writer = new CharArrayWriter();
try{
JsonGenerator gen = factory.createGenerator(writer);
gen.writeStartObject();
gen.writeFieldName("infos");
gen.writeRaw(":" + listToJson(lst));
gen.writeEndObject();
gen.close();
}catch(Throwable e){
Logger.error("AjaxRequest.generateResponse: " + e.getMessage());
e.printStackTrace();
}
return new String(writer.toCharArray());
}
/**
* Convert a list into a json string.
*/
public static String listToJson(List<?> lst) throws IOException
{
ObjectMapper mapper = new ObjectMapper();
JsonFactory factory = new JsonFactory();
CharArrayWriter writer = new CharArrayWriter();
JsonGenerator gen = factory.createGenerator(writer);
gen.writeRaw(mapper.writeValueAsString(lst));
gen.close();
return new String(writer.toCharArray());
}
case class ProductList (
list: Vector[Product]
)
object ProductList {
/**
* Mapping to and from JSON.
*/
implicit val documentFormatter: Format[ProductList] = (
(__ \ "products").format[Vector[Product]]
) (ProductList.apply, unlift(ProductList.unapply))
}