|
| 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 | +} |
0 commit comments