java 嵌套对象序列化,从JSON反序列化复杂的嵌套Java对象

So I have a java class Workspace that has field of type List of Product which contains field of type List of Module which contains field of type List with Objects.

So I have like 3 levels nested objects with lists. What is the most painless way to deal with this in regards to JSON serialization and deserialization?

Currentlly I'm doing serialization with GSON and that works fine but it seems there is no elegant way to do deserialization.

I read all the answers on SO about this topics but have not found anything that solves my question.

This is the issue

class Workspace{

List products;

}

class Product{

List modules;

}

class Module{

List parameters;

}

class Parameter{

String name;

String type;

}

Is there a way to do deserialization with few lines of code?

解决方案

If it's worth the change and if you are willing to I suggest you have a look to Jackson (https://github.com/FasterXML/jackson). It might do the trick out-of-the-box with few or no annotations or customization.

"Jackson is one of the several available libraries for processing JSON. Some others are Boon, GSON, and Java API for JSON Processing.

One advantage that Jackson has over other libraries is its maturity. Jackson has evolved enough to become the preferred JSON processing library of some major web services frameworks (...)"

This good article of DZone may give you some highlights: https://dzone.com/articles/processing-json-with-jackson

Nonetheless since you didn't define what "elegant" means to you and as you are speaking about GSON I agree this answer may not suit you (and might disappear ;)).

I read all the answers on SO about this topics but have not found anything that solves my question

And yep if you need more you will also need to provide us with details about your actual issue.

EDIT: added sample code with Jackson.

Here's how elegant it could look with Jackson (and of course it can even be better with pre-configured mappers through factories, builders, helpers...).

public class Workspace {

final ObjectMapper objMapper = new ObjectMapper();

List products;

public Workspace() {

}

public Workspace(List products) {

this.products = products;

}

public List getProducts() {

return products;

}

public void setProducts(List products) {

this.products = products;

}

public String serialize() throws IOException {

try {

return objMapper.writerWithDefaultPrettyPrinter().writeValueAsString(this);

}catch (IOException e) {

e.printStackTrace();

}

return null;

}

public static Workspace deserialize(String json) throws IOException {

return new ObjectMapper().readValue(json, Workspace.class);

}

}

public class Product {

List modules;

public Product() {

}

public Product(List modules) {

this.modules = modules;

}

public List getModules() {

return modules;

}

public void setModules(List modules) {

this.modules = modules;

}

}

public class Module {

List parameters;

public Module() {

}

public Module(List parameters) {

this.parameters = parameters;

}

public List getParameters() {

return parameters;

}

public void setParameters(List parameters) {

this.parameters = parameters;

}

}

public class Parameter {

String name;

String type;

public Parameter() {

}

public Parameter(String name, String type) {

this.name = name;

this.type = type;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getType() {

return type;

}

public void setType(String type) {

this.type = type;

}

}

public class WorkspaceTest {

/**

* Test of serialize method, of class Workspace.

*/

@Test

public void testSerDeserialize() throws Exception {

// Build sample:

Workspace instance = new Workspace(

Collections.singletonList(new Product(

Collections.singletonList(new Module(

Collections.singletonList(new Parameter("Hello","World")))))));

// SER

String serialized = instance.serialize();

assertNotNull(serialized);

System.out.println("Serialized JSON: \n"+serialized);

// DSER

Workspace deserialized = Workspace.deserialize(serialized);

// MATCH

assertEquals(serialized, deserialized.serialize());

System.out.println("Match!");

}

}

Output:

Serialized JSON:

{

"products" : [ {

"modules" : [ {

"parameters" : [ {

"name" : "Hello",

"type" : "World"

} ]

} ]

} ]

}

Match!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值