|
| 1 | +package io.github.xmljim.json.api; |
| 2 | + |
| 3 | +import io.github.xmljim.json.factory.jsonpath.JsonPathBuilder; |
| 4 | +import io.github.xmljim.json.factory.jsonpath.JsonPathFactory; |
| 5 | +import io.github.xmljim.json.factory.jsonpath.ResultType; |
| 6 | +import io.github.xmljim.json.factory.mapper.MapperBuilder; |
| 7 | +import io.github.xmljim.json.factory.mapper.MapperFactory; |
| 8 | +import io.github.xmljim.json.factory.merge.MergeFactory; |
| 9 | +import io.github.xmljim.json.factory.merge.strategy.MergeResultStrategy; |
| 10 | +import io.github.xmljim.json.factory.model.ElementFactory; |
| 11 | +import io.github.xmljim.json.factory.parser.InputData; |
| 12 | +import io.github.xmljim.json.factory.parser.JsonParserException; |
| 13 | +import io.github.xmljim.json.factory.parser.ParserBuilder; |
| 14 | +import io.github.xmljim.json.factory.parser.ParserFactory; |
| 15 | +import io.github.xmljim.json.merger.conflict.ArrayConflictStrategies; |
| 16 | +import io.github.xmljim.json.merger.conflict.ObjectConflictStrategies; |
| 17 | +import io.github.xmljim.json.model.JsonArray; |
| 18 | +import io.github.xmljim.json.model.JsonNode; |
| 19 | +import io.github.xmljim.json.model.JsonObject; |
| 20 | +import io.github.xmljim.json.model.JsonValue; |
| 21 | +import io.github.xmljim.json.service.ServiceManager; |
| 22 | + |
| 23 | +import java.io.InputStream; |
| 24 | +import java.io.Reader; |
| 25 | +import java.nio.file.Files; |
| 26 | +import java.nio.file.Path; |
| 27 | +import java.util.Collection; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +public final class JsonApi { |
| 32 | + |
| 33 | + public JsonPathApi JsonPath = new JsonPathApi(); |
| 34 | + public JsonParserApi JsonParser = new JsonParserApi(); |
| 35 | + |
| 36 | + public ElementApi JsonElement = new ElementApi(); |
| 37 | + |
| 38 | + public JsonMergeApi JsonMerge = new JsonMergeApi(); |
| 39 | + |
| 40 | + public JsonMapperApi JsonMapper = new JsonMapperApi(); |
| 41 | + |
| 42 | + /** |
| 43 | + * API for JsonPath functionality |
| 44 | + */ |
| 45 | + public static final class JsonPathApi { |
| 46 | + private final JsonPathFactory jsonPathFactory = ServiceManager.getProvider(JsonPathFactory.class); |
| 47 | + |
| 48 | + private JsonPathApi() { |
| 49 | + //no-op |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Return the underlying JsonPathBuilder to modify settings |
| 54 | + * for a new {@link io.github.xmljim.json.factory.jsonpath.JsonPath} instance |
| 55 | + * |
| 56 | + * @return a new JsonPathBuilder |
| 57 | + */ |
| 58 | + public JsonPathBuilder getBuilder() { |
| 59 | + return jsonPathFactory.newJsonPathBuilder(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Return a JsonArray of values selected from a JsonPath expression. |
| 64 | + * This uses the default settings and properties for a JsonPath instance. |
| 65 | + * If you need to modify settings (e.g., properties, or set variables), |
| 66 | + * use {@link #getBuilder()} to apply these settings and return a |
| 67 | + * {@link io.github.xmljim.json.factory.jsonpath.JsonPath} instance |
| 68 | + * |
| 69 | + * <p>This is the syntactic equivalent of: </p> |
| 70 | + * <pre> |
| 71 | + * JsonPathFactory jsonPathFactory = ServiceManager.getProvider(JsonPathFactory.class); |
| 72 | + * JsonPath jsonPath = jsonPathFactory.newJsonPath(); |
| 73 | + * JsonArray result = jsonPath.select(node, pathExpression); |
| 74 | + * </pre> |
| 75 | + * |
| 76 | + * @param node The context node that will be used to evaluate and select values |
| 77 | + * @param pathExpression the JsonPath expression to query the context node |
| 78 | + * @return a JsonArray of values that represent the select expression |
| 79 | + */ |
| 80 | + public JsonArray select(JsonNode node, String pathExpression) { |
| 81 | + return jsonPathFactory.newJsonPath().select(node, pathExpression); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Return a List of values selected from a JsonPath expression. |
| 86 | + * This uses the default settings and properties for a JsonPath instance. |
| 87 | + * If you need to modify settings (e.g., properties, or set variables), |
| 88 | + * use {@link #getBuilder()} to apply these settings and return a |
| 89 | + * {@link io.github.xmljim.json.factory.jsonpath.JsonPath} instance |
| 90 | + * |
| 91 | + * <p>This is the syntactic equivalent of: </p> |
| 92 | + * <pre> |
| 93 | + * JsonPathFactory jsonPathFactory = ServiceManager.getProvider(JsonPathFactory.class); |
| 94 | + * JsonPath jsonPath = jsonPathFactory.newJsonPath(); |
| 95 | + * JsonArray result = jsonPath.select(node, pathExpression); |
| 96 | + * |
| 97 | + * MapperFactory mapperFactory = ServiceManager.getProvider(MapperFactory.class); |
| 98 | + * Mapper mapper = mapperFactory.newMapper(); |
| 99 | + * return mapper.toList(result); |
| 100 | + * </pre> |
| 101 | + * |
| 102 | + * @param node The context node that will be used to evaluate and select values |
| 103 | + * @param pathExpression the JsonPath expression to query the context node |
| 104 | + * @return a JsonArray of values that represent the select expression |
| 105 | + */ |
| 106 | + @SuppressWarnings("unchecked") |
| 107 | + public List<Object> selectList(JsonNode node, String pathExpression) { |
| 108 | + JsonMapperApi jsonMapperApi = new JsonMapperApi(); |
| 109 | + return (List<Object>) jsonMapperApi.toList(select(node, pathExpression)); |
| 110 | + } |
| 111 | + |
| 112 | + public JsonArray selectPath(JsonNode node, String pathExpression) { |
| 113 | + return jsonPathFactory.newJsonPath().select(node, pathExpression, ResultType.PATH); |
| 114 | + } |
| 115 | + |
| 116 | + @SuppressWarnings("unchecked") |
| 117 | + public List<String> selectPathList(JsonNode node, String pathExpression) { |
| 118 | + JsonMapperApi jsonMapperApi = new JsonMapperApi(); |
| 119 | + return (List<String>) jsonMapperApi.toList(select(node, pathExpression)); |
| 120 | + } |
| 121 | + |
| 122 | + public <T> T selectValue(JsonNode node, String pathExpression) { |
| 123 | + return jsonPathFactory.newJsonPath().selectValue(node, pathExpression); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Parser API |
| 129 | + */ |
| 130 | + public static final class JsonParserApi { |
| 131 | + private final ParserFactory parserFactory = ServiceManager.getProvider(ParserFactory.class); |
| 132 | + |
| 133 | + private JsonParserApi() { |
| 134 | + //no-op |
| 135 | + } |
| 136 | + |
| 137 | + public <T extends JsonNode> T parse(String jsonString) { |
| 138 | + return parserFactory.newParser().parse(InputData.of(jsonString)); |
| 139 | + } |
| 140 | + |
| 141 | + public <T extends JsonNode> T parse(Path path) { |
| 142 | + if (path.getFileSystem().isOpen()) { |
| 143 | + if (!Files.exists(path)) { |
| 144 | + throw new JsonParserException("File does not exist: " + path.getFileName().toString()); |
| 145 | + } |
| 146 | + } |
| 147 | + return parserFactory.newParser().parse(InputData.of(path)); |
| 148 | + } |
| 149 | + |
| 150 | + public <T extends JsonNode> T parse(InputStream inputStream) { |
| 151 | + return parserFactory.newParser().parse(InputData.of(inputStream)); |
| 152 | + } |
| 153 | + |
| 154 | + public <T extends JsonNode> T parse(Reader reader) { |
| 155 | + return parserFactory.newParser().parse(InputData.of(reader)); |
| 156 | + } |
| 157 | + |
| 158 | + public ParserBuilder getParserBuilder() { |
| 159 | + return parserFactory.newParserBuilder(); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + public static final class ElementApi { |
| 164 | + private final ElementFactory elementFactory = ServiceManager.getProvider(ElementFactory.class); |
| 165 | + |
| 166 | + private ElementApi() { |
| 167 | + //no-op |
| 168 | + } |
| 169 | + |
| 170 | + public JsonObject newObject() { |
| 171 | + return elementFactory.newObject(); |
| 172 | + } |
| 173 | + |
| 174 | + public JsonArray newArray() { |
| 175 | + return elementFactory.newArray(); |
| 176 | + } |
| 177 | + |
| 178 | + public <T> JsonValue<T> newValue(T value) { |
| 179 | + return elementFactory.newValue(value); |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + public static final class JsonMergeApi { |
| 184 | + private final MergeFactory mergeFactory = ServiceManager.getProvider(MergeFactory.class); |
| 185 | + |
| 186 | + private JsonMergeApi() { |
| 187 | + // no-op |
| 188 | + } |
| 189 | + |
| 190 | + public <T extends JsonNode> T merge(T primary, T secondary) { |
| 191 | + return mergeFactory.newMergeProcessor().merge(primary, secondary); |
| 192 | + } |
| 193 | + |
| 194 | + public <T extends JsonNode> T merge(T primary, T secondary, ArrayConflictStrategies arrayConflictStrategies, |
| 195 | + ObjectConflictStrategies objectConflictStrategies) { |
| 196 | + return mergeFactory.newMergeBuilder() |
| 197 | + .setArrayConflictStrategy(arrayConflictStrategies) |
| 198 | + .setObjectConflictStrategy(objectConflictStrategies) |
| 199 | + .build() |
| 200 | + .merge(primary, secondary); |
| 201 | + } |
| 202 | + |
| 203 | + public <T extends JsonNode> T merge(T primary, T secondary, ArrayConflictStrategies arrayConflictStrategies, |
| 204 | + ObjectConflictStrategies objectConflictStrategies, |
| 205 | + MergeResultStrategy mergeResultStrategy, |
| 206 | + String mergeAppendKey) { |
| 207 | + return mergeFactory.newMergeBuilder() |
| 208 | + .setArrayConflictStrategy(arrayConflictStrategies) |
| 209 | + .setObjectConflictStrategy(objectConflictStrategies) |
| 210 | + .setMergeAppendKey(mergeAppendKey) |
| 211 | + .setMergeResultStrategy(mergeResultStrategy) |
| 212 | + .build() |
| 213 | + .merge(primary, secondary); |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + public static final class JsonMapperApi { |
| 218 | + private final MapperFactory mapperFactory = ServiceManager.getProvider(MapperFactory.class); |
| 219 | + |
| 220 | + private JsonMapperApi() { |
| 221 | + //no-op |
| 222 | + } |
| 223 | + |
| 224 | + public MapperBuilder getMapperBuilder() { |
| 225 | + |
| 226 | + return mapperFactory.newBuilder(); |
| 227 | + } |
| 228 | + |
| 229 | + public JsonObject toJsonObject(Object object) { |
| 230 | + return mapperFactory.newMapper().toJson(object); |
| 231 | + } |
| 232 | + |
| 233 | + public JsonObject toJsonObject(Map<String, Object> objectMap) { |
| 234 | + return mapperFactory.newMapper().toJson(objectMap); |
| 235 | + } |
| 236 | + |
| 237 | + public JsonArray toJsonArray(Collection<Object> collection) { |
| 238 | + return mapperFactory.newMapper().toJson(collection); |
| 239 | + } |
| 240 | + |
| 241 | + public <T> T toClass(JsonObject jsonObject, Class<T> targetClass) { |
| 242 | + return mapperFactory.newMapper().toClass(jsonObject, targetClass); |
| 243 | + } |
| 244 | + |
| 245 | + public Map<String, Object> toMap(JsonObject jsonObject) { |
| 246 | + return mapperFactory.newMapper().toMap(jsonObject); |
| 247 | + } |
| 248 | + |
| 249 | + public List<?> toList(JsonArray jsonArray) { |
| 250 | + return mapperFactory.newMapper().toList(jsonArray); |
| 251 | + } |
| 252 | + |
| 253 | + public JsonValue<?> toValue(Object value) { |
| 254 | + return mapperFactory.newMapper().toValue(value); |
| 255 | + } |
| 256 | + } |
| 257 | +} |
0 commit comments