Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: java
jdk:
- oraclejdk7
- oraclejdk8
cache:
directories:
- $HOME/.m2
Expand Down
35 changes: 35 additions & 0 deletions jmespath-gson/pom.xml
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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>jmespath</artifactId>
<groupId>io.burt</groupId>
<version>0.2.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>jmespath-gson</artifactId>

<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>
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>

</project>
188 changes: 188 additions & 0 deletions jmespath-gson/src/main/java/io/burt/jmespath/gson/GsonRuntime.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package io.burt.jmespath.gson;

import com.google.gson.*;
import io.burt.jmespath.BaseRuntime;
import io.burt.jmespath.JmesPathType;
import io.burt.jmespath.function.FunctionRegistry;

import java.util.*;

public class GsonRuntime extends BaseRuntime<JsonElement> {
private final JsonParser parser;

public GsonRuntime(FunctionRegistry functionRegistry) {
super(functionRegistry);
this.parser = new JsonParser();
}

public GsonRuntime() {
this(null);
}

@Override
public JsonElement parseString(String str) {
return parser.parse(str);
}

@Override
public List<JsonElement> toList(JsonElement value) {
List<JsonElement> list;
if(value.isJsonObject()) {
list = new ArrayList<>(value.getAsJsonObject().size());
for(Map.Entry<String, JsonElement> entry : value.getAsJsonObject().entrySet()) {
list.add(entry.getValue());
}

return list;
}

if(value.isJsonArray()) {
list = new ArrayList<>(value.getAsJsonArray().size());
for(JsonElement e : value.getAsJsonArray()) {
list.add(e);
}

return list;
}

return Collections.emptyList();
}

@Override
public String toString(JsonElement value) {
if(value.isJsonPrimitive() && value.getAsJsonPrimitive().isString()) {
return value.getAsJsonPrimitive().getAsString();
} else {
return value.toString();
}
}

@Override
public Number toNumber(JsonElement value) {
return (value.isJsonPrimitive() && value.getAsJsonPrimitive().isNumber()) ? value.getAsNumber() : null;
}

@Override
public boolean isTruthy(JsonElement value) {
switch (typeOf(value)) {
case NULL:
return false;
case BOOLEAN:
return value.getAsBoolean();
case STRING:
return value.getAsString().length() > 0;
case NUMBER:
return true;
case ARRAY:
return value.getAsJsonArray().size() > 0;
case OBJECT:
return value.getAsJsonObject().size() > 0;
}

throw new IllegalStateException(String.format("Unknown node type encountered: %s", value.getClass()));
}

@Override
public JmesPathType typeOf(JsonElement value) {
if(value.isJsonArray()) {
return JmesPathType.ARRAY;
}

if(value.isJsonObject()) {
return JmesPathType.OBJECT;
}

if(value.isJsonPrimitive()) {
if(value.getAsJsonPrimitive().isBoolean()) {
return JmesPathType.BOOLEAN;
}

if(value.getAsJsonPrimitive().isNumber()) {
return JmesPathType.NUMBER;
}

if(value.getAsJsonPrimitive().isString()) {
return JmesPathType.STRING;
}
}

if(value.isJsonNull()) {
return JmesPathType.NULL;
}

throw new IllegalStateException(String.format("Unknown node type encountered: %s", value.getClass()));
}

@Override
public JsonElement getProperty(JsonElement value, JsonElement name) {
return nodeOrNullNode(
value.isJsonObject() ? value.getAsJsonObject().get(name.getAsString()) : null
);
}

@Override
public Collection<JsonElement> getPropertyNames(JsonElement value) {
if(value.isJsonObject()) {
JsonObject object = (JsonObject) value;
List<JsonElement> names = new ArrayList<>((object.size()));

for (String s : object.keySet()) {
names.add(createString(s));
}
return names;
} else {
return Collections.emptyList();
}
}

@Override
public JsonElement createNull() {
return JsonNull.INSTANCE;
}

@Override
public JsonElement createArray(Collection<JsonElement> elements) {
JsonArray array = new JsonArray();
for(JsonElement e : elements) {
array.add(e);
}
return array;
}

@Override
public JsonElement createString(String str) {
return new JsonPrimitive(str);
}

@Override
public JsonElement createBoolean(boolean b) {
return new JsonPrimitive(b);
}

@Override
public JsonElement createObject(Map<JsonElement, JsonElement> obj) {
JsonElement object = new JsonObject();
for(Map.Entry<JsonElement, JsonElement> entry : obj.entrySet()) {
((JsonObject) object).add(entry.getKey().getAsString(), entry.getValue());
}
return object;
}

@Override
public JsonElement createNumber(double n) {
return new JsonPrimitive(n);
}

@Override
public JsonElement createNumber(long n) {
return new JsonPrimitive(n);
}

private JsonElement nodeOrNullNode(JsonElement node) {
if (node == null) {
return JsonNull.INSTANCE;
} else {
return node;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.burt.jmespath.gson;

import com.google.gson.JsonElement;
import io.burt.jmespath.Adapter;
import io.burt.jmespath.JmesPathComplianceTest;

public class GsonComplianceTest extends JmesPathComplianceTest<JsonElement> {
private Adapter<JsonElement> runtime = new GsonRuntime();

@Override
protected Adapter<JsonElement> runtime() {
return runtime;
}
}
14 changes: 14 additions & 0 deletions jmespath-gson/src/test/java/io/burt/jmespath/gson/GsonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.burt.jmespath.gson;

import com.google.gson.JsonElement;
import io.burt.jmespath.Adapter;
import io.burt.jmespath.JmesPathRuntimeTest;

public class GsonTest extends JmesPathRuntimeTest<JsonElement> {
private Adapter<JsonElement> runtime = new GsonRuntime();

@Override
protected Adapter<JsonElement> runtime() {
return runtime;
}
}
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<modules>
<module>jmespath-core</module>
<module>jmespath-jackson</module>
<module>jmespath-gson</module>
</modules>

<dependencies>
Expand Down