BAEL-2037 - Code for mini-article (#4923)
* BAEL-2037 - Code for mini-article * BAEL-2037 - Fixes after editors review * Fixing dependencies * Change File Reader * Formatting * Fix imports * Rename test class
This commit is contained in:
parent
fd009dca02
commit
5eea4fc6a6
14
json/pom.xml
14
json/pom.xml
|
@ -33,6 +33,20 @@
|
|||
<artifactId>json</artifactId>
|
||||
<version>20171018</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- https://search.maven.org/search?q=g:org.glassfish%20AND%20a:javax.json&core=gav -->
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.1.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
|
|
|
@ -0,0 +1,95 @@
|
|||
package com.baeldung.jsonpointer;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import javax.json.Json;
|
||||
import javax.json.JsonObject;
|
||||
import javax.json.JsonPointer;
|
||||
import javax.json.JsonReader;
|
||||
import javax.json.JsonString;
|
||||
import javax.json.JsonStructure;
|
||||
|
||||
public class JsonPointerCrud {
|
||||
|
||||
private JsonStructure jsonStructure = null;
|
||||
|
||||
public JsonPointerCrud(String fileName) throws IOException {
|
||||
|
||||
try (JsonReader reader = Json.createReader(Files.newBufferedReader(Paths.get(fileName)))) {
|
||||
jsonStructure = reader.read();
|
||||
} catch (FileNotFoundException e) {
|
||||
System.out.println("Error to open json file: " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public JsonPointerCrud(InputStream stream) {
|
||||
|
||||
JsonReader reader = Json.createReader(stream);
|
||||
jsonStructure = reader.read();
|
||||
reader.close();
|
||||
|
||||
}
|
||||
|
||||
public JsonStructure insert(String key, String value) {
|
||||
|
||||
JsonPointer jsonPointer = Json.createPointer("/" + key);
|
||||
JsonString jsonValue = Json.createValue(value);
|
||||
jsonStructure = jsonPointer.add(jsonStructure, jsonValue);
|
||||
|
||||
return jsonStructure;
|
||||
|
||||
}
|
||||
|
||||
public JsonStructure update(String key, String newValue) {
|
||||
|
||||
JsonPointer jsonPointer = Json.createPointer("/" + key);
|
||||
JsonString jsonNewValue = Json.createValue(newValue);
|
||||
jsonStructure = jsonPointer.replace(jsonStructure, jsonNewValue);
|
||||
|
||||
return jsonStructure;
|
||||
}
|
||||
|
||||
public JsonStructure delete(String key) {
|
||||
|
||||
JsonPointer jsonPointer = Json.createPointer("/" + key);
|
||||
jsonPointer.getValue(jsonStructure);
|
||||
jsonStructure = jsonPointer.remove(jsonStructure);
|
||||
|
||||
return jsonStructure;
|
||||
|
||||
}
|
||||
|
||||
public String fetchValueFromKey(String key) {
|
||||
JsonPointer jsonPointer = Json.createPointer("/" + key);
|
||||
JsonString jsonString = (JsonString) jsonPointer.getValue(jsonStructure);
|
||||
|
||||
return jsonString.getString();
|
||||
}
|
||||
|
||||
public String fetchListValues(String key) {
|
||||
JsonPointer jsonPointer = Json.createPointer("/" + key);
|
||||
JsonObject jsonObject = (JsonObject) jsonPointer.getValue(jsonStructure);
|
||||
|
||||
return jsonObject.toString();
|
||||
}
|
||||
|
||||
public String fetchFullJSON() {
|
||||
JsonPointer jsonPointer = Json.createPointer("");
|
||||
JsonObject jsonObject = (JsonObject) jsonPointer.getValue(jsonStructure);
|
||||
|
||||
return jsonObject.toString();
|
||||
|
||||
}
|
||||
|
||||
public boolean check(String key) {
|
||||
JsonPointer jsonPointer = Json.createPointer("/" + key);
|
||||
boolean found = jsonPointer.containsValue(jsonStructure);
|
||||
|
||||
return found;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.jsonpointer;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JsonPointerCrudUnitTest {
|
||||
|
||||
@Test
|
||||
public void testJsonPointerCrudForAddress() {
|
||||
|
||||
JsonPointerCrud jsonPointerCrud = new JsonPointerCrud(JsonPointerCrudUnitTest.class.getResourceAsStream("/address.json"));
|
||||
|
||||
assertFalse(jsonPointerCrud.check("city"));
|
||||
|
||||
// insert a value
|
||||
jsonPointerCrud.insert("city", "Rio de Janeiro");
|
||||
|
||||
assertTrue(jsonPointerCrud.check("city"));
|
||||
|
||||
// fetch full json
|
||||
String fullJSON = jsonPointerCrud.fetchFullJSON();
|
||||
|
||||
assertTrue(fullJSON.contains("name"));
|
||||
|
||||
assertTrue(fullJSON.contains("city"));
|
||||
|
||||
// fetch value
|
||||
String cityName = jsonPointerCrud.fetchValueFromKey("city");
|
||||
|
||||
assertEquals(cityName, "Rio de Janeiro");
|
||||
|
||||
// update value
|
||||
jsonPointerCrud.update("city", "Sao Paulo");
|
||||
|
||||
// fetch value
|
||||
cityName = jsonPointerCrud.fetchValueFromKey("city");
|
||||
|
||||
assertEquals(cityName, "Sao Paulo");
|
||||
|
||||
// delete
|
||||
jsonPointerCrud.delete("city");
|
||||
|
||||
assertFalse(jsonPointerCrud.check("city"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testJsonPointerCrudForBooks() {
|
||||
|
||||
JsonPointerCrud jsonPointerCrud = new JsonPointerCrud(JsonPointerCrudUnitTest.class.getResourceAsStream("/books.json"));
|
||||
|
||||
// fetch value
|
||||
String book = jsonPointerCrud.fetchListValues("books/1");
|
||||
|
||||
assertEquals(book, "{\"title\":\"Title 2\",\"author\":\"John Doe\"}");
|
||||
|
||||
}
|
||||
}
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"name": "Customer 01",
|
||||
"street name": "Street 01"
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
{
|
||||
"library": "My Personal Library",
|
||||
"books": [
|
||||
{ "title":"Title 1", "author":"Jane Doe" },
|
||||
{ "title":"Title 2", "author":"John Doe" }
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue