-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #56 from paulogaspar7/master
Jakarta JSON-P support.
- Loading branch information
Showing
5 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<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"> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>jmespath-jakarta-jsonp</artifactId> | ||
<name>JMESPath Jakarta JSON-P</name> | ||
<description>A JMESPath implementation for Java</description> | ||
|
||
<parent> | ||
<groupId>io.burt</groupId> | ||
<artifactId>jmespath</artifactId> | ||
<version>0.4.1-SNAPSHOT</version> | ||
</parent> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>jmespath-core</artifactId> | ||
<version>${project.parent.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>${project.groupId}</groupId> | ||
<artifactId>jmespath-core</artifactId> | ||
<version>${project.parent.version}</version> | ||
<type>test-jar</type> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.glassfish</groupId> | ||
<artifactId>jakarta.json</artifactId> | ||
<version>${jakarta.jsonp.version}</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
220 changes: 220 additions & 0 deletions
220
jmespath-jakarta-jsonp/src/main/java/io/burt/jmespath/jakarta/jsonp/JsonpRuntime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,220 @@ | ||
package io.burt.jmespath.jakarta.jsonp; | ||
|
||
import java.io.StringReader; | ||
import java.util.AbstractList; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import io.burt.jmespath.BaseRuntime; | ||
import io.burt.jmespath.JmesPathType; | ||
import io.burt.jmespath.RuntimeConfiguration; | ||
|
||
import javax.json.Json; | ||
import javax.json.JsonArray; | ||
import javax.json.JsonArrayBuilder; | ||
import javax.json.JsonNumber; | ||
import javax.json.JsonObject; | ||
import javax.json.JsonObjectBuilder; | ||
import javax.json.JsonReaderFactory; | ||
import javax.json.JsonString; | ||
import javax.json.JsonValue; | ||
import javax.json.JsonValue.ValueType; | ||
|
||
import static javax.json.JsonValue.ValueType.ARRAY; | ||
import static javax.json.JsonValue.ValueType.NUMBER; | ||
import static javax.json.JsonValue.ValueType.OBJECT; | ||
import static javax.json.JsonValue.ValueType.STRING; | ||
|
||
public class JsonpRuntime extends BaseRuntime<JsonValue> { | ||
private final JsonReaderFactory jsonReaderFactory; | ||
|
||
public JsonpRuntime() { | ||
this(RuntimeConfiguration.defaultConfiguration()); | ||
} | ||
|
||
public JsonpRuntime(RuntimeConfiguration configuration) { | ||
this(configuration, Json.createReaderFactory(null)); | ||
} | ||
|
||
public JsonpRuntime(RuntimeConfiguration configuration, JsonReaderFactory jsonReaderFactory) { | ||
super(configuration); | ||
this.jsonReaderFactory = jsonReaderFactory; | ||
} | ||
|
||
@Override | ||
public JsonValue parseString(String string) { | ||
return jsonReaderFactory.createReader(new StringReader(string)).readValue(); | ||
} | ||
|
||
private static class JsonArrayListWrapper extends AbstractList<JsonValue> { | ||
private final JsonArray array; | ||
|
||
JsonArrayListWrapper(JsonArray array) { | ||
this.array = array; | ||
} | ||
|
||
@Override | ||
public JsonValue get(int index) { | ||
return array.get(index); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return array.size(); | ||
} | ||
} | ||
|
||
@Override | ||
public List<JsonValue> toList(JsonValue value) { | ||
ValueType valueType = value.getValueType(); | ||
if (valueType == ARRAY) { | ||
return new JsonArrayListWrapper((JsonArray) value); | ||
} else if (valueType == OBJECT) { | ||
JsonObject obj = (JsonObject) value; | ||
if (!obj.isEmpty()) { | ||
List<JsonValue> elements = new ArrayList<>(obj.size()); | ||
for (JsonValue v : obj.values()) { | ||
elements.add(nodeOrNullNode(v)); | ||
} | ||
return elements; | ||
} | ||
} | ||
return Collections.emptyList(); | ||
} | ||
|
||
@Override | ||
public String toString(JsonValue str) { | ||
if (str.getValueType() == STRING) { | ||
return ((JsonString) str).getString(); | ||
} else { | ||
return str.toString(); | ||
} | ||
} | ||
|
||
@Override | ||
public Number toNumber(JsonValue n) { | ||
return (n.getValueType() == NUMBER) ? ((JsonNumber) n).numberValue() : null; | ||
} | ||
|
||
@Override | ||
public boolean isTruthy(JsonValue value) { | ||
switch (value.getValueType()) { | ||
case FALSE: | ||
case NULL: | ||
return false; | ||
case NUMBER: | ||
case TRUE: | ||
return true; | ||
case ARRAY: | ||
return ((JsonArray) value).size() > 0; | ||
case OBJECT: | ||
return ((JsonObject) value).size() > 0; | ||
case STRING: | ||
return ((JsonString) value).getString().length() > 0; | ||
default: | ||
throw new IllegalStateException(String.format("Unknown node type encountered: %s", value.getValueType())); | ||
} | ||
} | ||
|
||
@Override | ||
public JmesPathType typeOf(JsonValue value) { | ||
switch (value.getValueType()) { | ||
case ARRAY: | ||
return JmesPathType.ARRAY; | ||
case OBJECT: | ||
return JmesPathType.OBJECT; | ||
case STRING: | ||
return JmesPathType.STRING; | ||
case FALSE: | ||
case TRUE: | ||
return JmesPathType.BOOLEAN; | ||
case NULL: | ||
return JmesPathType.NULL; | ||
case NUMBER: | ||
return JmesPathType.NUMBER; | ||
default: | ||
throw new IllegalStateException(String.format("Unknown node type encountered: %s", value.getValueType())); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonValue getProperty(JsonValue value, JsonValue name) { | ||
if (value.getValueType() == OBJECT) { | ||
return nodeOrNullNode(((JsonObject) value).get(toString(name))); | ||
} else { | ||
return JsonValue.NULL; | ||
} | ||
} | ||
|
||
@Override | ||
public Collection<JsonValue> getPropertyNames(JsonValue value) { | ||
if (value.getValueType() == OBJECT) { | ||
Set<String> nameSet = ((JsonObject) value).keySet(); | ||
List<JsonValue> names = new ArrayList<>(nameSet.size()); | ||
for(String n : nameSet) { | ||
names.add(createString(n)); | ||
} | ||
return names; | ||
} else { | ||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
@Override | ||
public JsonValue createNull() { | ||
return nodeOrNullNode(null); | ||
} | ||
|
||
@Override | ||
public JsonValue createArray(Collection<JsonValue> elements) { | ||
JsonArrayBuilder builder = Json.createArrayBuilder(); | ||
for(JsonValue element : elements) { | ||
builder.add(nodeOrNullNode(element)); | ||
} | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public JsonValue createString(String str) { | ||
return nodeOrNullNode(Json.createValue(str)); | ||
} | ||
|
||
@Override | ||
public JsonValue createBoolean(boolean b) { | ||
return b ? JsonValue.TRUE : JsonValue.FALSE; | ||
} | ||
|
||
@Override | ||
public JsonValue createObject(Map<JsonValue, JsonValue> obj) { | ||
JsonObjectBuilder builder = Json.createObjectBuilder(); | ||
for (Map.Entry<JsonValue, JsonValue> entry : obj.entrySet()) { | ||
String key = toString(entry.getKey()); | ||
if (key != null) { | ||
builder.add(key, nodeOrNullNode(entry.getValue())); | ||
} | ||
} | ||
return builder.build(); | ||
} | ||
|
||
@Override | ||
public JsonValue createNumber(double n) { | ||
return Json.createValue(n); | ||
} | ||
|
||
@Override | ||
public JsonValue createNumber(long n) { | ||
return Json.createValue(n); | ||
} | ||
|
||
private JsonValue nodeOrNullNode(JsonValue node) { | ||
if (node == null) { | ||
return JsonValue.NULL; | ||
} else { | ||
return node; | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
jmespath-jakarta-jsonp/src/test/java/io/burt/jmespath/jakarta/jsonp/JsonpComplianceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package io.burt.jmespath.jakarta.jsonp; | ||
|
||
import io.burt.jmespath.JmesPathComplianceTest; | ||
import io.burt.jmespath.Adapter; | ||
|
||
import javax.json.JsonValue; | ||
|
||
public class JsonpComplianceTest extends JmesPathComplianceTest<JsonValue> { | ||
private Adapter<JsonValue> runtime = new JsonpRuntime(); | ||
|
||
@Override | ||
protected Adapter<JsonValue> runtime() { return runtime; } | ||
} |
12 changes: 12 additions & 0 deletions
12
jmespath-jakarta-jsonp/src/test/java/io/burt/jmespath/jakarta/jsonp/JsonpTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package io.burt.jmespath.jakarta.jsonp; | ||
|
||
import io.burt.jmespath.Adapter; | ||
import io.burt.jmespath.JmesPathRuntimeTest; | ||
import io.burt.jmespath.RuntimeConfiguration; | ||
|
||
import javax.json.JsonValue; | ||
|
||
public class JsonpTest extends JmesPathRuntimeTest<JsonValue> { | ||
@Override | ||
protected Adapter<JsonValue> createRuntime(RuntimeConfiguration configuration) { return new JsonpRuntime(configuration); } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters