diff --git a/jmespath-core/src/main/java/io/burt/jmespath/BaseRuntime.java b/jmespath-core/src/main/java/io/burt/jmespath/BaseRuntime.java index 85cabe75..f448898c 100644 --- a/jmespath-core/src/main/java/io/burt/jmespath/BaseRuntime.java +++ b/jmespath-core/src/main/java/io/burt/jmespath/BaseRuntime.java @@ -1,7 +1,6 @@ package io.burt.jmespath; import java.util.List; -import java.util.Iterator; import java.util.Collection; import io.burt.jmespath.parser.ExpressionParser; @@ -10,7 +9,6 @@ import io.burt.jmespath.function.ArgumentTypeException; import io.burt.jmespath.node.NodeFactory; import io.burt.jmespath.node.StandardNodeFactory; -import io.burt.jmespath.util.StringEscapeHelper; /** * This class can be extended instead of implementing {@link Adapter} directly, @@ -20,16 +18,6 @@ * these methods if they have more efficient means to perform the same job. */ public abstract class BaseRuntime implements Adapter { - private static final StringEscapeHelper jsonEscapeHelper = new StringEscapeHelper( - true, - 'b', '\b', - 't', '\t', - 'n', '\n', - 'f', '\f', - 'r', '\r', - '\\', '\\', - '\"', '\"' - ); private final FunctionRegistry functionRegistry; private final NodeFactory nodeFactory; @@ -173,82 +161,6 @@ public int hashCode() { return 31; } - /** - * Helper method to render a value as JSON. - * - * Assumes that null, number and boolean - * render themseves correctly with toString, and that - * string renders itself as an unquoted string. - */ - protected String unparse(T object) { - switch (typeOf(object)) { - case NUMBER: - return unparseNumber(object); - case BOOLEAN: - return unparseBoolean(object); - case NULL: - return unparseNull(object); - case STRING: - return unparseString(object); - case OBJECT: - return unparseObject(object); - case ARRAY: - return unparseArray(object); - default: - throw new IllegalStateException(); - } - } - - protected String unparseNumber(T object) { - return object.toString(); - } - - protected String unparseBoolean(T object) { - return object.toString(); - } - - protected String unparseNull(T object) { - return "null"; - } - - protected String unparseString(T object) { - return String.format("\"%s\"", escapeString(toString(object))); - } - - protected String escapeString(String str) { - return jsonEscapeHelper.escape(str); - } - - protected String unparseObject(T object) { - StringBuilder str = new StringBuilder("{"); - Iterator keys = getPropertyNames(object).iterator(); - while (keys.hasNext()) { - T key = keys.next(); - T value = getProperty(object, key); - str.append(unparseString(key)); - str.append(':'); - str.append(unparse(value)); - if (keys.hasNext()) { - str.append(','); - } - } - str.append('}'); - return str.toString(); - } - - protected String unparseArray(T array) { - StringBuilder str = new StringBuilder("["); - Iterator elements = toList(array).iterator(); - while (elements.hasNext()) { - str.append(unparse(elements.next())); - if (elements.hasNext()) { - str.append(','); - } - } - str.append(']'); - return str.toString(); - } - @Override @Deprecated public T getProperty(T value, String name) { diff --git a/jmespath-core/src/main/java/io/burt/jmespath/jcf/JcfRuntime.java b/jmespath-core/src/main/java/io/burt/jmespath/jcf/JcfRuntime.java index b0de826a..f0a7a6f5 100644 --- a/jmespath-core/src/main/java/io/burt/jmespath/jcf/JcfRuntime.java +++ b/jmespath-core/src/main/java/io/burt/jmespath/jcf/JcfRuntime.java @@ -4,17 +4,30 @@ import java.util.ArrayList; import java.util.Map; import java.util.Collection; +import java.util.Iterator; import java.util.Collections; import io.burt.jmespath.BaseRuntime; import io.burt.jmespath.JmesPathType; import io.burt.jmespath.RuntimeConfiguration; +import io.burt.jmespath.util.StringEscapeHelper; import static io.burt.jmespath.JmesPathType.*; public class JcfRuntime extends BaseRuntime { + + private static final StringEscapeHelper jsonEscapeHelper = new StringEscapeHelper( + true, + 'b', '\b', + 't', '\t', + 'n', '\n', + 'f', '\f', + 'r', '\r', + '\\', '\\', + '\"', '\"' + ); + public JcfRuntime() { - super(); } public JcfRuntime(RuntimeConfiguration configuration) { @@ -160,4 +173,57 @@ public Object createNumber(double n) { public Object createNumber(long n) { return n; } + + /** + * Helper method to render a value as JSON. + * + * Assumes that null, number and boolean + * render themseves correctly with toString, and that + * string renders itself as an unquoted string. + */ + private String unparse(Object object) { + switch (typeOf(object)) { + case NUMBER: + case BOOLEAN: + return object.toString(); + case NULL: + return "null"; + case STRING: + return '"' + jsonEscapeHelper.escape(toString(object)) + '"'; + case OBJECT: + return unparseObject(object); + case ARRAY: + return unparseArray(object); + default: + throw new IllegalStateException(); + } + } + + private String unparseObject(Object object) { + StringBuilder str = new StringBuilder("{"); + Collection propertyNames = getPropertyNames(object); + for (Object key: propertyNames) { + Object value = getProperty(object, key); + str.append('"').append(jsonEscapeHelper.escape(toString(key))).append("\":"); + str.append(unparse(value)); + str.append(','); + } + if (!propertyNames.isEmpty()) { + str.setLength(str.length() - 1); + } + return str.append('}').toString(); + } + + private String unparseArray(Object array) { + StringBuilder str = new StringBuilder("["); + List elements = toList(array); + for (Object element : elements) { + str.append(unparse(element)).append(','); + } + if (!elements.isEmpty()) { + str.setLength(str.length() - 1); + + } + return str.append(']').toString(); + } }