Skip to content

Commit d2102a7

Browse files
pcoates33pivovarit
authored andcommitted
BAEL-2890 (eugenp#6905)
1 parent 0c0f148 commit d2102a7

9 files changed

Lines changed: 118 additions & 11 deletions

File tree

jackson-2/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
77

88
### Relevant Articles:
99
- [Mapping Multiple JSON Fields to a Single Java Field](https://www.baeldung.com/json-multiple-fields-single-java-field)
10+
- [Working with Tree Model Nodes in Jackson](https://www.baeldung.com/jackson-json-node-tree-model)
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung.jackson.node;
2+
3+
import java.util.Iterator;
4+
import java.util.Map.Entry;
5+
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
8+
public class JsonNodeIterator {
9+
10+
private static final String NEW_LINE = "\n";
11+
private static final String FIELD_DELIMITER = ": ";
12+
private static final String ARRAY_PREFIX = "- ";
13+
private static final String YAML_PREFIX = " ";
14+
15+
public String toYaml(JsonNode root) {
16+
StringBuilder yaml = new StringBuilder();
17+
processNode(root, yaml, 0);
18+
return yaml.toString();
19+
}
20+
21+
private void processNode(JsonNode jsonNode, StringBuilder yaml, int depth) {
22+
if (jsonNode.isValueNode()) {
23+
yaml.append(jsonNode.asText());
24+
}
25+
else if (jsonNode.isArray()) {
26+
for (JsonNode arrayItem : jsonNode) {
27+
appendNodeToYaml(arrayItem, yaml, depth, true);
28+
}
29+
}
30+
else if (jsonNode.isObject()) {
31+
appendNodeToYaml(jsonNode, yaml, depth, false);
32+
}
33+
}
34+
35+
private void appendNodeToYaml(JsonNode node, StringBuilder yaml, int depth, boolean isArrayItem) {
36+
Iterator<Entry<String, JsonNode>> fields = node.fields();
37+
boolean isFirst = true;
38+
while (fields.hasNext()) {
39+
Entry<String, JsonNode> jsonField = fields.next();
40+
addFieldNameToYaml(yaml, jsonField.getKey(), depth, isArrayItem && isFirst);
41+
processNode(jsonField.getValue(), yaml, depth+1);
42+
isFirst = false;
43+
}
44+
45+
}
46+
47+
private void addFieldNameToYaml(StringBuilder yaml, String fieldName, int depth, boolean isFirstInArray) {
48+
if (yaml.length()>0) {
49+
yaml.append(NEW_LINE);
50+
int requiredDepth = (isFirstInArray) ? depth-1 : depth;
51+
for(int i = 0; i < requiredDepth; i++) {
52+
yaml.append(YAML_PREFIX);
53+
}
54+
if (isFirstInArray) {
55+
yaml.append(ARRAY_PREFIX);
56+
}
57+
}
58+
yaml.append(fieldName);
59+
yaml.append(FIELD_DELIMITER);
60+
}
61+
62+
}

jackson/src/test/java/com/baeldung/jackson/node/ExampleStructure.java renamed to jackson-2/src/test/java/com/baeldung/jackson/node/ExampleStructure.java

File renamed without changes.
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.jackson.node;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.io.IOException;
6+
7+
import org.junit.Test;
8+
9+
import com.fasterxml.jackson.databind.JsonNode;
10+
11+
public class JsonNodeIteratorUnitTest {
12+
13+
private JsonNodeIterator onTest = new JsonNodeIterator();
14+
private static String expectedYaml = "name: \n" +
15+
" first: Tatu\n" +
16+
" last: Saloranta\n" +
17+
"title: Jackson founder\n" +
18+
"company: FasterXML\n" +
19+
"pets: \n" +
20+
"- type: dog\n" +
21+
" number: 1\n" +
22+
"- type: fish\n" +
23+
" number: 50";
24+
25+
@Test
26+
public void givenANodeTree_whenIteratingSubNodes_thenWeFindExpected() throws IOException {
27+
final JsonNode rootNode = ExampleStructure.getExampleRoot();
28+
29+
String yaml = onTest.toYaml(rootNode);
30+
System.out.println(yaml.toString());
31+
32+
assertEquals(expectedYaml, yaml);
33+
34+
}
35+
36+
37+
}

jackson/src/test/java/com/baeldung/jackson/node/NodeBean.java renamed to jackson-2/src/test/java/com/baeldung/jackson/node/NodeBean.java

File renamed without changes.

jackson/src/test/java/com/baeldung/jackson/node/NodeOperationUnitTest.java renamed to jackson-2/src/test/java/com/baeldung/jackson/node/NodeOperationUnitTest.java

File renamed without changes.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": {
3+
"first": "Tatu",
4+
"last": "Saloranta"
5+
},
6+
"title": "Jackson founder",
7+
"company": "FasterXML",
8+
"pets": [
9+
{
10+
"type": "dog",
11+
"number": 1
12+
},
13+
{
14+
"type": "fish",
15+
"number": 50
16+
}
17+
]
18+
}

jackson/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
1515
- [Jackson JSON Tutorial](http://www.baeldung.com/jackson)
1616
- [Jackson – Working with Maps and nulls](http://www.baeldung.com/jackson-map-null-values-or-null-key)
1717
- [Jackson – Decide What Fields Get Serialized/Deserialized](http://www.baeldung.com/jackson-field-serializable-deserializable-or-not)
18-
- [Working with Tree Model Nodes in Jackson](http://www.baeldung.com/jackson-json-node-tree-model)
1918
- [Jackson vs Gson](http://www.baeldung.com/jackson-vs-gson)
2019
- [XML Serialization and Deserialization with Jackson](http://www.baeldung.com/jackson-xml-serialization-and-deserialization)
2120
- [More Jackson Annotations](http://www.baeldung.com/jackson-advanced-annotations)

jackson/src/main/resources/node_example.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)