问题
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<Product> products;
}
class Product{
List<Module> modules;
}
class Module{
List<Parameter> parameters;
}
class Parameter{
String name;
String type;
}
Is there a way to do deserialization with few lines of code?
回答1:
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<Product> products;
public Workspace() {
}
public Workspace(List<Product> products) {
this.products = products;
}
public List<Product> getProducts() {
return products;
}
public void setProducts(List<Product> 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<Module> modules;
public Product() {
}
public Product(List<Module> modules) {
this.modules = modules;
}
public List<Module> getModules() {
return modules;
}
public void setModules(List<Module> modules) {
this.modules = modules;
}
}
public class Module {
List<Parameter> parameters;
public Module() {
}
public Module(List<Parameter> parameters) {
this.parameters = parameters;
}
public List<Parameter> getParameters() {
return parameters;
}
public void setParameters(List<Parameter> 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!
来源:https://stackoverflow.com/questions/47497266/deserializing-complex-nested-java-objects-from-json