This commit is contained in:
pcoates33 2019-05-07 05:37:38 +01:00 committed by Grzegorz Piwowarek
parent 0c0f148320
commit d2102a76ff
9 changed files with 118 additions and 11 deletions

View File

@ -7,3 +7,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles:
- [Mapping Multiple JSON Fields to a Single Java Field](https://www.baeldung.com/json-multiple-fields-single-java-field)
- [Working with Tree Model Nodes in Jackson](https://www.baeldung.com/jackson-json-node-tree-model)

View File

@ -0,0 +1,62 @@
package com.baeldung.jackson.node;
import java.util.Iterator;
import java.util.Map.Entry;
import com.fasterxml.jackson.databind.JsonNode;
public class JsonNodeIterator {
private static final String NEW_LINE = "\n";
private static final String FIELD_DELIMITER = ": ";
private static final String ARRAY_PREFIX = "- ";
private static final String YAML_PREFIX = " ";
public String toYaml(JsonNode root) {
StringBuilder yaml = new StringBuilder();
processNode(root, yaml, 0);
return yaml.toString();
}
private void processNode(JsonNode jsonNode, StringBuilder yaml, int depth) {
if (jsonNode.isValueNode()) {
yaml.append(jsonNode.asText());
}
else if (jsonNode.isArray()) {
for (JsonNode arrayItem : jsonNode) {
appendNodeToYaml(arrayItem, yaml, depth, true);
}
}
else if (jsonNode.isObject()) {
appendNodeToYaml(jsonNode, yaml, depth, false);
}
}
private void appendNodeToYaml(JsonNode node, StringBuilder yaml, int depth, boolean isArrayItem) {
Iterator<Entry<String, JsonNode>> fields = node.fields();
boolean isFirst = true;
while (fields.hasNext()) {
Entry<String, JsonNode> jsonField = fields.next();
addFieldNameToYaml(yaml, jsonField.getKey(), depth, isArrayItem && isFirst);
processNode(jsonField.getValue(), yaml, depth+1);
isFirst = false;
}
}
private void addFieldNameToYaml(StringBuilder yaml, String fieldName, int depth, boolean isFirstInArray) {
if (yaml.length()>0) {
yaml.append(NEW_LINE);
int requiredDepth = (isFirstInArray) ? depth-1 : depth;
for(int i = 0; i < requiredDepth; i++) {
yaml.append(YAML_PREFIX);
}
if (isFirstInArray) {
yaml.append(ARRAY_PREFIX);
}
}
yaml.append(fieldName);
yaml.append(FIELD_DELIMITER);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.jackson.node;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import org.junit.Test;
import com.fasterxml.jackson.databind.JsonNode;
public class JsonNodeIteratorUnitTest {
private JsonNodeIterator onTest = new JsonNodeIterator();
private static String expectedYaml = "name: \n" +
" first: Tatu\n" +
" last: Saloranta\n" +
"title: Jackson founder\n" +
"company: FasterXML\n" +
"pets: \n" +
"- type: dog\n" +
" number: 1\n" +
"- type: fish\n" +
" number: 50";
@Test
public void givenANodeTree_whenIteratingSubNodes_thenWeFindExpected() throws IOException {
final JsonNode rootNode = ExampleStructure.getExampleRoot();
String yaml = onTest.toYaml(rootNode);
System.out.println(yaml.toString());
assertEquals(expectedYaml, yaml);
}
}

View File

@ -0,0 +1,18 @@
{
"name": {
"first": "Tatu",
"last": "Saloranta"
},
"title": "Jackson founder",
"company": "FasterXML",
"pets": [
{
"type": "dog",
"number": 1
},
{
"type": "fish",
"number": 50
}
]
}

View File

@ -15,7 +15,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [Jackson JSON Tutorial](http://www.baeldung.com/jackson)
- [Jackson Working with Maps and nulls](http://www.baeldung.com/jackson-map-null-values-or-null-key)
- [Jackson Decide What Fields Get Serialized/Deserialized](http://www.baeldung.com/jackson-field-serializable-deserializable-or-not)
- [Working with Tree Model Nodes in Jackson](http://www.baeldung.com/jackson-json-node-tree-model)
- [Jackson vs Gson](http://www.baeldung.com/jackson-vs-gson)
- [XML Serialization and Deserialization with Jackson](http://www.baeldung.com/jackson-xml-serialization-and-deserialization)
- [More Jackson Annotations](http://www.baeldung.com/jackson-advanced-annotations)

View File

@ -1,10 +0,0 @@
{
"name":
{
"first": "Tatu",
"last": "Saloranta"
},
"title": "Jackson founder",
"company": "FasterXML"
}