Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit 5646461

Browse files
authored
Merge pull request #56 from paulogaspar7/master
Jakarta JSON-P support.
2 parents 4a40466 + 752ecdb commit 5646461

File tree

5 files changed

+282
-0
lines changed

5 files changed

+282
-0
lines changed

jmespath-jakarta-jsonp/pom.xml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<artifactId>jmespath-jakarta-jsonp</artifactId>
7+
<name>JMESPath Jakarta JSON-P</name>
8+
<description>A JMESPath implementation for Java</description>
9+
10+
<parent>
11+
<groupId>io.burt</groupId>
12+
<artifactId>jmespath</artifactId>
13+
<version>0.4.1-SNAPSHOT</version>
14+
</parent>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>${project.groupId}</groupId>
19+
<artifactId>jmespath-core</artifactId>
20+
<version>${project.parent.version}</version>
21+
</dependency>
22+
<dependency>
23+
<groupId>${project.groupId}</groupId>
24+
<artifactId>jmespath-core</artifactId>
25+
<version>${project.parent.version}</version>
26+
<type>test-jar</type>
27+
<scope>test</scope>
28+
</dependency>
29+
<dependency>
30+
<groupId>org.glassfish</groupId>
31+
<artifactId>jakarta.json</artifactId>
32+
<version>${jakarta.jsonp.version}</version>
33+
</dependency>
34+
</dependencies>
35+
</project>
Lines changed: 220 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,220 @@
1+
package io.burt.jmespath.jakarta.jsonp;
2+
3+
import java.io.StringReader;
4+
import java.util.AbstractList;
5+
import java.util.ArrayList;
6+
import java.util.Collection;
7+
import java.util.Collections;
8+
import java.util.List;
9+
import java.util.Map;
10+
import java.util.Set;
11+
12+
import io.burt.jmespath.BaseRuntime;
13+
import io.burt.jmespath.JmesPathType;
14+
import io.burt.jmespath.RuntimeConfiguration;
15+
16+
import javax.json.Json;
17+
import javax.json.JsonArray;
18+
import javax.json.JsonArrayBuilder;
19+
import javax.json.JsonNumber;
20+
import javax.json.JsonObject;
21+
import javax.json.JsonObjectBuilder;
22+
import javax.json.JsonReaderFactory;
23+
import javax.json.JsonString;
24+
import javax.json.JsonValue;
25+
import javax.json.JsonValue.ValueType;
26+
27+
import static javax.json.JsonValue.ValueType.ARRAY;
28+
import static javax.json.JsonValue.ValueType.NUMBER;
29+
import static javax.json.JsonValue.ValueType.OBJECT;
30+
import static javax.json.JsonValue.ValueType.STRING;
31+
32+
public class JsonpRuntime extends BaseRuntime<JsonValue> {
33+
private final JsonReaderFactory jsonReaderFactory;
34+
35+
public JsonpRuntime() {
36+
this(RuntimeConfiguration.defaultConfiguration());
37+
}
38+
39+
public JsonpRuntime(RuntimeConfiguration configuration) {
40+
this(configuration, Json.createReaderFactory(null));
41+
}
42+
43+
public JsonpRuntime(RuntimeConfiguration configuration, JsonReaderFactory jsonReaderFactory) {
44+
super(configuration);
45+
this.jsonReaderFactory = jsonReaderFactory;
46+
}
47+
48+
@Override
49+
public JsonValue parseString(String string) {
50+
return jsonReaderFactory.createReader(new StringReader(string)).readValue();
51+
}
52+
53+
private static class JsonArrayListWrapper extends AbstractList<JsonValue> {
54+
private final JsonArray array;
55+
56+
JsonArrayListWrapper(JsonArray array) {
57+
this.array = array;
58+
}
59+
60+
@Override
61+
public JsonValue get(int index) {
62+
return array.get(index);
63+
}
64+
65+
@Override
66+
public int size() {
67+
return array.size();
68+
}
69+
}
70+
71+
@Override
72+
public List<JsonValue> toList(JsonValue value) {
73+
ValueType valueType = value.getValueType();
74+
if (valueType == ARRAY) {
75+
return new JsonArrayListWrapper((JsonArray) value);
76+
} else if (valueType == OBJECT) {
77+
JsonObject obj = (JsonObject) value;
78+
if (!obj.isEmpty()) {
79+
List<JsonValue> elements = new ArrayList<>(obj.size());
80+
for (JsonValue v : obj.values()) {
81+
elements.add(nodeOrNullNode(v));
82+
}
83+
return elements;
84+
}
85+
}
86+
return Collections.emptyList();
87+
}
88+
89+
@Override
90+
public String toString(JsonValue str) {
91+
if (str.getValueType() == STRING) {
92+
return ((JsonString) str).getString();
93+
} else {
94+
return str.toString();
95+
}
96+
}
97+
98+
@Override
99+
public Number toNumber(JsonValue n) {
100+
return (n.getValueType() == NUMBER) ? ((JsonNumber) n).numberValue() : null;
101+
}
102+
103+
@Override
104+
public boolean isTruthy(JsonValue value) {
105+
switch (value.getValueType()) {
106+
case FALSE:
107+
case NULL:
108+
return false;
109+
case NUMBER:
110+
case TRUE:
111+
return true;
112+
case ARRAY:
113+
return ((JsonArray) value).size() > 0;
114+
case OBJECT:
115+
return ((JsonObject) value).size() > 0;
116+
case STRING:
117+
return ((JsonString) value).getString().length() > 0;
118+
default:
119+
throw new IllegalStateException(String.format("Unknown node type encountered: %s", value.getValueType()));
120+
}
121+
}
122+
123+
@Override
124+
public JmesPathType typeOf(JsonValue value) {
125+
switch (value.getValueType()) {
126+
case ARRAY:
127+
return JmesPathType.ARRAY;
128+
case OBJECT:
129+
return JmesPathType.OBJECT;
130+
case STRING:
131+
return JmesPathType.STRING;
132+
case FALSE:
133+
case TRUE:
134+
return JmesPathType.BOOLEAN;
135+
case NULL:
136+
return JmesPathType.NULL;
137+
case NUMBER:
138+
return JmesPathType.NUMBER;
139+
default:
140+
throw new IllegalStateException(String.format("Unknown node type encountered: %s", value.getValueType()));
141+
}
142+
}
143+
144+
@Override
145+
public JsonValue getProperty(JsonValue value, JsonValue name) {
146+
if (value.getValueType() == OBJECT) {
147+
return nodeOrNullNode(((JsonObject) value).get(toString(name)));
148+
} else {
149+
return JsonValue.NULL;
150+
}
151+
}
152+
153+
@Override
154+
public Collection<JsonValue> getPropertyNames(JsonValue value) {
155+
if (value.getValueType() == OBJECT) {
156+
Set<String> nameSet = ((JsonObject) value).keySet();
157+
List<JsonValue> names = new ArrayList<>(nameSet.size());
158+
for(String n : nameSet) {
159+
names.add(createString(n));
160+
}
161+
return names;
162+
} else {
163+
return Collections.emptyList();
164+
}
165+
}
166+
167+
@Override
168+
public JsonValue createNull() {
169+
return nodeOrNullNode(null);
170+
}
171+
172+
@Override
173+
public JsonValue createArray(Collection<JsonValue> elements) {
174+
JsonArrayBuilder builder = Json.createArrayBuilder();
175+
for(JsonValue element : elements) {
176+
builder.add(nodeOrNullNode(element));
177+
}
178+
return builder.build();
179+
}
180+
181+
@Override
182+
public JsonValue createString(String str) {
183+
return nodeOrNullNode(Json.createValue(str));
184+
}
185+
186+
@Override
187+
public JsonValue createBoolean(boolean b) {
188+
return b ? JsonValue.TRUE : JsonValue.FALSE;
189+
}
190+
191+
@Override
192+
public JsonValue createObject(Map<JsonValue, JsonValue> obj) {
193+
JsonObjectBuilder builder = Json.createObjectBuilder();
194+
for (Map.Entry<JsonValue, JsonValue> entry : obj.entrySet()) {
195+
String key = toString(entry.getKey());
196+
if (key != null) {
197+
builder.add(key, nodeOrNullNode(entry.getValue()));
198+
}
199+
}
200+
return builder.build();
201+
}
202+
203+
@Override
204+
public JsonValue createNumber(double n) {
205+
return Json.createValue(n);
206+
}
207+
208+
@Override
209+
public JsonValue createNumber(long n) {
210+
return Json.createValue(n);
211+
}
212+
213+
private JsonValue nodeOrNullNode(JsonValue node) {
214+
if (node == null) {
215+
return JsonValue.NULL;
216+
} else {
217+
return node;
218+
}
219+
}
220+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package io.burt.jmespath.jakarta.jsonp;
2+
3+
import io.burt.jmespath.JmesPathComplianceTest;
4+
import io.burt.jmespath.Adapter;
5+
6+
import javax.json.JsonValue;
7+
8+
public class JsonpComplianceTest extends JmesPathComplianceTest<JsonValue> {
9+
private Adapter<JsonValue> runtime = new JsonpRuntime();
10+
11+
@Override
12+
protected Adapter<JsonValue> runtime() { return runtime; }
13+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package io.burt.jmespath.jakarta.jsonp;
2+
3+
import io.burt.jmespath.Adapter;
4+
import io.burt.jmespath.JmesPathRuntimeTest;
5+
import io.burt.jmespath.RuntimeConfiguration;
6+
7+
import javax.json.JsonValue;
8+
9+
public class JsonpTest extends JmesPathRuntimeTest<JsonValue> {
10+
@Override
11+
protected Adapter<JsonValue> createRuntime(RuntimeConfiguration configuration) { return new JsonpRuntime(configuration); }
12+
}

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<junit.version>4.12</junit.version>
1717
<hamcrest.version>1.3</hamcrest.version>
1818
<jackson.version>2.9.9</jackson.version>
19+
<jakarta.jsonp.version>1.1.6</jakarta.jsonp.version>
1920
<gson.version>2.8.5</gson.version>
2021
<antlr.version>4.7.2</antlr.version>
2122
<vertx.version>3.7.1</vertx.version>
@@ -56,6 +57,7 @@
5657
<modules>
5758
<module>jmespath-core</module>
5859
<module>jmespath-jackson</module>
60+
<module>jmespath-jakarta-jsonp</module>
5961
<module>jmespath-gson</module>
6062
<module>jmespath-vertx</module>
6163
</modules>

0 commit comments

Comments
 (0)