Skip to content

Commit ec0388f

Browse files
GH-20 JsonApi initial commit
1 parent cfe2f7e commit ec0388f

File tree

9 files changed

+323
-3
lines changed

9 files changed

+323
-3
lines changed

json-api/pom.xml

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>json-library</artifactId>
7+
<groupId>io.github.xmljim.json</groupId>
8+
<version>1.0.2</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
12+
<artifactId>json-api</artifactId>
13+
14+
<properties>
15+
<maven.compiler.source>17</maven.compiler.source>
16+
<maven.compiler.target>17</maven.compiler.target>
17+
</properties>
18+
19+
<dependencies>
20+
<dependency>
21+
<groupId>io.github.xmljim.json</groupId>
22+
<artifactId>json-parser</artifactId>
23+
<version>${project.version}</version>
24+
</dependency>
25+
<dependency>
26+
<groupId>io.github.xmljim.json</groupId>
27+
<artifactId>elementfactory</artifactId>
28+
<version>${project.version}</version>
29+
</dependency>
30+
<dependency>
31+
<groupId>io.github.xmljim.json</groupId>
32+
<artifactId>json-merger</artifactId>
33+
<version>${project.version}</version>
34+
</dependency>
35+
<dependency>
36+
<groupId>io.github.xmljim.json</groupId>
37+
<artifactId>json-mapper</artifactId>
38+
<version>${project.version}</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>io.github.xmljim.json</groupId>
42+
<artifactId>jsonpath</artifactId>
43+
<version>${project.version}</version>
44+
<scope>compile</scope>
45+
</dependency>
46+
</dependencies>
47+
48+
</project>
Lines changed: 257 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
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+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module io.github.xmljim.json.api {
2+
3+
requires transitive io.github.xmljim.json.elementfactory;
4+
requires transitive io.github.xmljim.json.parser;
5+
requires transitive io.github.xmljim.json.merger;
6+
requires transitive io.github.xmljim.json.mapper;
7+
requires transitive io.github.xmljim.json.jsonpath;
8+
9+
exports io.github.xmljim.json.api;
10+
}

merger/src/main/java/io/github/xmljim/json/merger/MergeConfigImpl.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import io.github.xmljim.json.factory.merge.strategy.ArrayConflictStrategy;
55
import io.github.xmljim.json.factory.merge.strategy.MergeResultStrategy;
66
import io.github.xmljim.json.factory.merge.strategy.ObjectConflictStrategy;
7+
import io.github.xmljim.json.merger.conflict.ArrayConflictStrategies;
8+
import io.github.xmljim.json.merger.conflict.ObjectConflictStrategies;
79

810
class MergeConfigImpl implements MergeConfig {
911
private ArrayConflictStrategy arrayConflictStrategy = ArrayConflictStrategies.APPEND;

merger/src/main/java/io/github/xmljim/json/merger/ArrayConflictStrategies.java renamed to merger/src/main/java/io/github/xmljim/json/merger/conflict/ArrayConflictStrategies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.xmljim.json.merger;
1+
package io.github.xmljim.json.merger.conflict;
22

33
import io.github.xmljim.json.factory.merge.MergeProcessor;
44
import io.github.xmljim.json.factory.merge.strategy.ArrayConflictStrategy;

merger/src/main/java/io/github/xmljim/json/merger/ObjectConflictStrategies.java renamed to merger/src/main/java/io/github/xmljim/json/merger/conflict/ObjectConflictStrategies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.xmljim.json.merger;
1+
package io.github.xmljim.json.merger.conflict;
22

33
import io.github.xmljim.json.factory.merge.MergeProcessor;
44
import io.github.xmljim.json.factory.merge.strategy.ObjectConflictStrategy;

merger/src/main/java/module-info.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
requires transitive io.github.xmljim.json.factory;
66
opens io.github.xmljim.json.merger to io.github.xmljim.json.factory;
77
exports io.github.xmljim.json.merger to io.github.xmljim.json.merger.test;
8+
exports io.github.xmljim.json.merger.conflict;
9+
opens io.github.xmljim.json.merger.conflict to io.github.xmljim.json.factory;
810

911
provides MergeFactory with MergeFactoryImpl;
1012
}

parser/src/main/java/module-info.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module io.github.xmljim.json.parser {
66
requires transitive io.github.xmljim.json.factory;
7-
opens io.github.xmljim.json.parser;
7+
opens io.github.xmljim.json.parser to io.github.xmljim.json.factory;
88
provides ParserFactory with ParserFactoryImpl;
99

1010
uses ElementFactory;

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<module>mapper</module>
1717
<module>merger</module>
1818
<module>jsonpath</module>
19+
<module>json-api</module>
1920
</modules>
2021

2122
<name>${project.groupId}:${project.artifactId}</name>

0 commit comments

Comments
 (0)