Skip to content

Commit b7abc27

Browse files
committed
source code and tests for jackson node operations
1 parent 2e5b1ac commit b7abc27

4 files changed

Lines changed: 160 additions & 0 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"name":
3+
{
4+
"first": "Tatu",
5+
"last": "Saloranta"
6+
},
7+
8+
"title": "Jackson founder",
9+
"company": "FasterXML"
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.baeldung.jackson.node;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import com.fasterxml.jackson.databind.JsonNode;
7+
import com.fasterxml.jackson.databind.ObjectMapper;
8+
9+
public class ExampleStructure {
10+
private static ObjectMapper mapper = new ObjectMapper();
11+
12+
static JsonNode getExampleRoot() throws IOException {
13+
InputStream exampleInput = ExampleStructure.class.getClassLoader().getResourceAsStream("node_example.json");
14+
JsonNode rootNode = mapper.readTree(exampleInput);
15+
return rootNode;
16+
}
17+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.baeldung.jackson.node;
2+
3+
public class MyBean {
4+
private int id;
5+
private String name;
6+
7+
public MyBean() {
8+
}
9+
10+
public MyBean(int id, String name) {
11+
this.id = id;
12+
this.name = name;
13+
}
14+
15+
public int getId() {
16+
return id;
17+
}
18+
19+
public void setId(int id) {
20+
this.id = id;
21+
}
22+
23+
public String getName() {
24+
return name;
25+
}
26+
27+
public void setName(String name) {
28+
this.name = name;
29+
}
30+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package org.baeldung.jackson.node;
2+
3+
import static org.hamcrest.CoreMatchers.containsString;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertFalse;
6+
import static org.junit.Assert.assertThat;
7+
import static org.junit.Assert.assertTrue;
8+
9+
import java.io.FileReader;
10+
import java.io.FileWriter;
11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.nio.file.Paths;
14+
15+
import org.junit.Test;
16+
17+
import com.fasterxml.jackson.core.JsonProcessingException;
18+
import com.fasterxml.jackson.databind.JsonNode;
19+
import com.fasterxml.jackson.databind.ObjectMapper;
20+
import com.fasterxml.jackson.databind.node.ObjectNode;
21+
22+
public class JacksonNodeOperationTest {
23+
private static ObjectMapper mapper = new ObjectMapper();
24+
25+
@Test
26+
public void giveAnObject_whenConvertingIntoANode_thenCorrect() {
27+
MyBean fromValue = new MyBean(2016, "baeldung.com");
28+
29+
JsonNode node = mapper.valueToTree(fromValue);
30+
31+
assertEquals(2016, node.get("id").intValue());
32+
assertEquals("baeldung.com", node.get("name").textValue());
33+
}
34+
35+
@Test
36+
public void givenANode_whenWritingOutAsAJsonString_thenCorrect() throws IOException {
37+
final String pathToTestFile = "node_to_json_test.json";
38+
char[] characterBuffer = new char[50];
39+
40+
JsonNode node = mapper.createObjectNode();
41+
((ObjectNode) node).put("id", 2016);
42+
((ObjectNode) node).put("name", "baeldung.com");
43+
44+
try (FileWriter outputStream = new FileWriter(pathToTestFile)) {
45+
mapper.writeValue(outputStream, node);
46+
}
47+
48+
try (FileReader inputStreamForAssertion = new FileReader(pathToTestFile)) {
49+
inputStreamForAssertion.read(characterBuffer);
50+
}
51+
String textContentOfTestFile = new String(characterBuffer);
52+
53+
assertThat(textContentOfTestFile, containsString("2016"));
54+
assertThat(textContentOfTestFile, containsString("baeldung.com"));
55+
56+
Files.delete(Paths.get(pathToTestFile));
57+
}
58+
59+
@Test
60+
public void givenANode_whenConvertingIntoAnObject_thenCorrect() throws JsonProcessingException {
61+
JsonNode node = mapper.createObjectNode();
62+
((ObjectNode) node).put("id", 2016);
63+
((ObjectNode) node).put("name", "baeldung.com");
64+
65+
MyBean toValue = mapper.treeToValue(node, MyBean.class);
66+
67+
assertEquals(2016, toValue.getId());
68+
assertEquals("baeldung.com", toValue.getName());
69+
}
70+
71+
@Test
72+
public void givenANode_whenAddingIntoATree_thenCorrect() throws IOException {
73+
JsonNode rootNode = ExampleStructure.getExampleRoot();
74+
ObjectNode addedNode = ((ObjectNode) rootNode).putObject("address");
75+
addedNode.put("city", "Seattle").put("state", "Washington").put("country", "United States");
76+
77+
assertFalse(rootNode.path("address").isMissingNode());
78+
assertEquals("Seattle", rootNode.path("address").path("city").textValue());
79+
assertEquals("Washington", rootNode.path("address").path("state").textValue());
80+
assertEquals("United States", rootNode.path("address").path("country").textValue());
81+
}
82+
83+
@Test
84+
public void givenANode_whenModifyingIt_thenCorrect() throws IOException {
85+
String newString = "{\"nick\": \"cowtowncoder\"}";
86+
JsonNode newNode = mapper.readTree(newString);
87+
88+
JsonNode rootNode = ExampleStructure.getExampleRoot();
89+
((ObjectNode) rootNode).set("name", newNode);
90+
91+
assertFalse(rootNode.path("name").path("nick").isMissingNode());
92+
assertEquals("cowtowncoder", rootNode.path("name").path("nick").textValue());
93+
}
94+
95+
@Test
96+
public void givenANode_whenRemovingFromATree_thenCorrect() throws IOException {
97+
JsonNode rootNode = ExampleStructure.getExampleRoot();
98+
((ObjectNode) rootNode).remove("company");
99+
100+
assertTrue(rootNode.path("company").isMissingNode());
101+
}
102+
103+
}

0 commit comments

Comments
 (0)