Occasionally I will write Java code ... (No, it is the main language in my business), so I will post what I researched in the case I am currently in charge of.
Do you use Spring Framework? Recently, the server side is often built with Node.js, Ruby, etc., and the PaaS side has a mechanism that can easily provide Web API, so it is quite rare to implement it in Java. I'm wondering, but I'm still active, so I'm still coding Java in the current project.
This time it is a little unusual specification, and instead of mapping the JSON received by the Web API built in Spring Framework to POJO, we decided to process it as it is (dynamically) and bridge it to another Web API. Then, when I looked at the Javadoc, I wondered how I should have created JSON without using POJO in Jackson, and found that I should do the following.
ʻObjectMapper provided a method called
createObjectNode () `.
You can use it to create an empty JSON object.
Use put (fieldName, value)
to add a simple value to this object.
Use set (fieldName, value)
when adding an object or array.
Below is an example.
ObjectMapper mapper = new ObjectMapper();
ObjectNode root = mapper.createObjectNode();
root.put("stringValue", "test");
root.set("brankObject", mapper.createObjectNode());
When dealing with arrays, ʻObjectMapper provided a method called
createArrayNode () . You can use it to create an empty JSON array. Use ʻadd (value)
to add elements to this array.
Below is an example.
ObjectMapper mapper = new ObjectMapper();
ArrayNode array = mapper.createArrayNode();
array.add("value");
array.add(mapper.createObjectNode());
It's a waste, but if you assemble JSON from POJO by yourself, it will be like this.
public class Test {
public List<Item> items = Arrays.asList(
new Item("Item 1", "$19.99", Arrays.asList(new Feature("New!"), new Feature("Awesome!"))),
new Item("Item 2","$29.99", Arrays.asList(new Feature("Old."), new Feature("Ugly."))));
public static class Item {
Item(String name, String price, List<Feature> features) {
this.name = name;
this.price = price;
this.features = features;
}
public String name, price;
public List<Feature> features;
}
public static class Feature {
Feature(String description) {
this.description = description;
}
public String description;
}
public static void main(String[] args) throws IOException {
Test test = new Test();
ObjectMapper mapper = new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT);
ObjectNode root = mapper.createObjectNode();
ArrayNode items = mapper.createArrayNode();
for (Item itemsObj : test.items) {
ObjectNode item = mapper.createObjectNode();
item.put("name", itemsObj.name);
item.put("price", itemsObj.price);
ArrayNode features = mapper.createArrayNode();
for (Feature featuresObj : itemsObj.features) {
ObjectNode feature = mapper.createObjectNode();
feature.put("description", featuresObj.description);
features.add(feature);
}
item.set("features", features);
items.add(item);
}
root.set("items", items);
System.out.println(mapper.writeValueAsString(root));
//After all, the output result will be the same as below
// System.out.println(mapper.writeValueAsString(test));
}
}
Output result
{
"items" : [ {
"name" : "Item 1",
"price" : "$19.99",
"features" : [ {
"description" : "New!"
}, {
"description" : "Awesome!"
} ]
}, {
"name" : "Item 2",
"price" : "$29.99",
"features" : [ {
"description" : "Old."
}, {
"description" : "Ugly."
} ]
} ]
}
Performance etc. are not considered so much, but please refer to it when building JSON directly with Java + Jackson.
Recommended Posts