io.burt.jmespath.jackson.JacksonRuntime Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jmespath-jackson Show documentation
Show all versions of jmespath-jackson Show documentation
A JMESPath implementation for Java
package io.burt.jmespath.jackson;
import java.util.AbstractList;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Iterator;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import io.burt.jmespath.BaseRuntime;
import io.burt.jmespath.JmesPathType;
import io.burt.jmespath.RuntimeConfiguration;
public class JacksonRuntime extends BaseRuntime {
private final ObjectMapper jsonParser;
public JacksonRuntime() {
this(RuntimeConfiguration.defaultConfiguration());
}
public JacksonRuntime(RuntimeConfiguration configuration) {
this(configuration, new ObjectMapper());
}
public JacksonRuntime(RuntimeConfiguration configuration, ObjectMapper jsonParser) {
super(configuration);
this.jsonParser = jsonParser;
}
@Override
public JsonNode parseString(String string) {
try {
return jsonParser.readTree(string);
} catch (IOException ioe) {
throw new IllegalStateException(ioe);
}
}
private static class ArrayNodeListWrapper extends AbstractList {
private final ArrayNode array;
ArrayNodeListWrapper(ArrayNode array) {
this.array = array;
}
@Override
public JsonNode get(int index) {
return array.get(index);
}
@Override
public int size() {
return array.size();
}
}
@Override
public List toList(JsonNode value) {
if (value.isArray()) {
return new ArrayNodeListWrapper((ArrayNode) value);
} else if (value.isObject()) {
List elements = new ArrayList<>(value.size());
for (JsonNode element : value) {
elements.add(element);
}
return elements;
} else {
return Collections.emptyList();
}
}
@Override
public String toString(JsonNode str) {
if (str.isTextual()) {
return str.textValue();
} else {
return str.toString();
}
}
@Override
public Number toNumber(JsonNode n) {
return n.numberValue();
}
@Override
public boolean isTruthy(JsonNode value) {
switch (value.getNodeType()) {
case ARRAY:
case BINARY:
case OBJECT:
return value.size() > 0;
case STRING:
return value.textValue().length() > 0;
case BOOLEAN:
return value.booleanValue();
case MISSING:
case NULL:
return false;
case NUMBER:
case POJO:
return true;
default:
throw new IllegalStateException(String.format("Unknown node type encountered: %s", value.getNodeType()));
}
}
@Override
public JmesPathType typeOf(JsonNode value) {
switch (value.getNodeType()) {
case ARRAY:
return JmesPathType.ARRAY;
case POJO:
case OBJECT:
return JmesPathType.OBJECT;
case BINARY:
case STRING:
return JmesPathType.STRING;
case BOOLEAN:
return JmesPathType.BOOLEAN;
case MISSING:
case NULL:
return JmesPathType.NULL;
case NUMBER:
return JmesPathType.NUMBER;
default:
throw new IllegalStateException(String.format("Unknown node type encountered: %s", value.getNodeType()));
}
}
@Override
public JsonNode getProperty(JsonNode value, JsonNode name) {
return nodeOrNullNode(value.get(name.textValue()));
}
@Override
public Collection getPropertyNames(JsonNode value) {
if (value.isObject()) {
List names = new ArrayList<>(value.size());
Iterator fieldNames = value.fieldNames();
while (fieldNames.hasNext()) {
names.add(createString(fieldNames.next()));
}
return names;
} else {
return Collections.emptyList();
}
}
@Override
public JsonNode createNull() {
return nodeOrNullNode(null);
}
@Override
public JsonNode createArray(Collection elements) {
ArrayNode array = JsonNodeFactory.instance.arrayNode();
array.addAll(elements);
return array;
}
@Override
public JsonNode createString(String str) {
return JsonNodeFactory.instance.textNode(str);
}
@Override
public JsonNode createBoolean(boolean b) {
return JsonNodeFactory.instance.booleanNode(b);
}
@Override
public JsonNode createObject(Map obj) {
ObjectNode object = new ObjectNode(JsonNodeFactory.instance);
for (Map.Entry entry : obj.entrySet()) {
object.set(entry.getKey().textValue(), entry.getValue());
}
return object;
}
@Override
public JsonNode createNumber(double n) {
return JsonNodeFactory.instance.numberNode(n);
}
@Override
public JsonNode createNumber(long n) {
return JsonNodeFactory.instance.numberNode(n);
}
private JsonNode nodeOrNullNode(JsonNode node) {
if (node == null) {
return JsonNodeFactory.instance.nullNode();
} else {
return node;
}
}
}