Skip to content

Commit

Permalink
Merge pull request #56 from paulogaspar7/master
Browse files Browse the repository at this point in the history
Jakarta JSON-P support.
  • Loading branch information
iconara committed Sep 17, 2019
2 parents 4a40466 + 752ecdb commit 5646461
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 0 deletions.
35 changes: 35 additions & 0 deletions jmespath-jakarta-jsonp/pom.xml
@@ -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>
@@ -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;
}
}
}
@@ -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; }
}
@@ -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); }
}
2 changes: 2 additions & 0 deletions pom.xml
Expand Up @@ -16,6 +16,7 @@
<junit.version>4.12</junit.version>
<hamcrest.version>1.3</hamcrest.version>
<jackson.version>2.9.9</jackson.version>
<jakarta.jsonp.version>1.1.6</jakarta.jsonp.version>
<gson.version>2.8.5</gson.version>
<antlr.version>4.7.2</antlr.version>
<vertx.version>3.7.1</vertx.version>
Expand Down Expand Up @@ -56,6 +57,7 @@
<modules>
<module>jmespath-core</module>
<module>jmespath-jackson</module>
<module>jmespath-jakarta-jsonp</module>
<module>jmespath-gson</module>
<module>jmespath-vertx</module>
</modules>
Expand Down

0 comments on commit 5646461

Please sign in to comment.