Skip to content

Commit 5eea4fc

Browse files
cmlaviladaoire
authored andcommitted
BAEL-2037 - Code for mini-article (eugenp#4923)
* BAEL-2037 - Code for mini-article * BAEL-2037 - Fixes after editors review * Fixing dependencies * Change File Reader * Formatting * Fix imports * Rename test class
1 parent fd009dc commit 5eea4fc

5 files changed

Lines changed: 179 additions & 0 deletions

File tree

‎json/pom.xml‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,20 @@
3333
<artifactId>json</artifactId>
3434
<version>20171018</version>
3535
</dependency>
36+
37+
<dependency>
38+
<groupId>junit</groupId>
39+
<artifactId>junit</artifactId>
40+
<version>4.12</version>
41+
<scope>test</scope>
42+
</dependency>
43+
<!-- https://search.maven.org/search?q=g:org.glassfish%20AND%20a:javax.json&core=gav -->
44+
<dependency>
45+
<groupId>org.glassfish</groupId>
46+
<artifactId>javax.json</artifactId>
47+
<version>1.1.2</version>
48+
</dependency>
49+
3650
</dependencies>
3751

3852
<properties>
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package com.baeldung.jsonpointer;
2+
3+
import java.io.FileNotFoundException;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.nio.file.Files;
7+
import java.nio.file.Paths;
8+
9+
import javax.json.Json;
10+
import javax.json.JsonObject;
11+
import javax.json.JsonPointer;
12+
import javax.json.JsonReader;
13+
import javax.json.JsonString;
14+
import javax.json.JsonStructure;
15+
16+
public class JsonPointerCrud {
17+
18+
private JsonStructure jsonStructure = null;
19+
20+
public JsonPointerCrud(String fileName) throws IOException {
21+
22+
try (JsonReader reader = Json.createReader(Files.newBufferedReader(Paths.get(fileName)))) {
23+
jsonStructure = reader.read();
24+
} catch (FileNotFoundException e) {
25+
System.out.println("Error to open json file: " + e.getMessage());
26+
}
27+
28+
}
29+
30+
public JsonPointerCrud(InputStream stream) {
31+
32+
JsonReader reader = Json.createReader(stream);
33+
jsonStructure = reader.read();
34+
reader.close();
35+
36+
}
37+
38+
public JsonStructure insert(String key, String value) {
39+
40+
JsonPointer jsonPointer = Json.createPointer("/" + key);
41+
JsonString jsonValue = Json.createValue(value);
42+
jsonStructure = jsonPointer.add(jsonStructure, jsonValue);
43+
44+
return jsonStructure;
45+
46+
}
47+
48+
public JsonStructure update(String key, String newValue) {
49+
50+
JsonPointer jsonPointer = Json.createPointer("/" + key);
51+
JsonString jsonNewValue = Json.createValue(newValue);
52+
jsonStructure = jsonPointer.replace(jsonStructure, jsonNewValue);
53+
54+
return jsonStructure;
55+
}
56+
57+
public JsonStructure delete(String key) {
58+
59+
JsonPointer jsonPointer = Json.createPointer("/" + key);
60+
jsonPointer.getValue(jsonStructure);
61+
jsonStructure = jsonPointer.remove(jsonStructure);
62+
63+
return jsonStructure;
64+
65+
}
66+
67+
public String fetchValueFromKey(String key) {
68+
JsonPointer jsonPointer = Json.createPointer("/" + key);
69+
JsonString jsonString = (JsonString) jsonPointer.getValue(jsonStructure);
70+
71+
return jsonString.getString();
72+
}
73+
74+
public String fetchListValues(String key) {
75+
JsonPointer jsonPointer = Json.createPointer("/" + key);
76+
JsonObject jsonObject = (JsonObject) jsonPointer.getValue(jsonStructure);
77+
78+
return jsonObject.toString();
79+
}
80+
81+
public String fetchFullJSON() {
82+
JsonPointer jsonPointer = Json.createPointer("");
83+
JsonObject jsonObject = (JsonObject) jsonPointer.getValue(jsonStructure);
84+
85+
return jsonObject.toString();
86+
87+
}
88+
89+
public boolean check(String key) {
90+
JsonPointer jsonPointer = Json.createPointer("/" + key);
91+
boolean found = jsonPointer.containsValue(jsonStructure);
92+
93+
return found;
94+
}
95+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.jsonpointer;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class JsonPointerCrudUnitTest {
8+
9+
@Test
10+
public void testJsonPointerCrudForAddress() {
11+
12+
JsonPointerCrud jsonPointerCrud = new JsonPointerCrud(JsonPointerCrudUnitTest.class.getResourceAsStream("/address.json"));
13+
14+
assertFalse(jsonPointerCrud.check("city"));
15+
16+
// insert a value
17+
jsonPointerCrud.insert("city", "Rio de Janeiro");
18+
19+
assertTrue(jsonPointerCrud.check("city"));
20+
21+
// fetch full json
22+
String fullJSON = jsonPointerCrud.fetchFullJSON();
23+
24+
assertTrue(fullJSON.contains("name"));
25+
26+
assertTrue(fullJSON.contains("city"));
27+
28+
// fetch value
29+
String cityName = jsonPointerCrud.fetchValueFromKey("city");
30+
31+
assertEquals(cityName, "Rio de Janeiro");
32+
33+
// update value
34+
jsonPointerCrud.update("city", "Sao Paulo");
35+
36+
// fetch value
37+
cityName = jsonPointerCrud.fetchValueFromKey("city");
38+
39+
assertEquals(cityName, "Sao Paulo");
40+
41+
// delete
42+
jsonPointerCrud.delete("city");
43+
44+
assertFalse(jsonPointerCrud.check("city"));
45+
46+
}
47+
48+
@Test
49+
public void testJsonPointerCrudForBooks() {
50+
51+
JsonPointerCrud jsonPointerCrud = new JsonPointerCrud(JsonPointerCrudUnitTest.class.getResourceAsStream("/books.json"));
52+
53+
// fetch value
54+
String book = jsonPointerCrud.fetchListValues("books/1");
55+
56+
assertEquals(book, "{\"title\":\"Title 2\",\"author\":\"John Doe\"}");
57+
58+
}
59+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "Customer 01",
3+
"street name": "Street 01"
4+
}

‎json/src/test/resources/books.json‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"library": "My Personal Library",
3+
"books": [
4+
{ "title":"Title 1", "author":"Jane Doe" },
5+
{ "title":"Title 2", "author":"John Doe" }
6+
]
7+
}

0 commit comments

Comments
 (0)