diff --git a/main/src/com/google/refine/grel/ast/FieldAccessorExpr.java b/main/src/com/google/refine/grel/ast/FieldAccessorExpr.java index 9677106ea..73f45c7c1 100644 --- a/main/src/com/google/refine/grel/ast/FieldAccessorExpr.java +++ b/main/src/com/google/refine/grel/ast/FieldAccessorExpr.java @@ -35,9 +35,9 @@ package com.google.refine.grel.ast; import java.util.Properties; -import org.json.JSONException; -import org.json.JSONObject; - +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.google.common.collect.Lists; import com.google.refine.expr.Evaluable; import com.google.refine.expr.ExpressionUtils; import com.google.refine.expr.HasFields; @@ -65,12 +65,23 @@ public class FieldAccessorExpr implements Evaluable { return null; } else if (o instanceof HasFields) { return ((HasFields) o).getField(_fieldName, bindings); - } else if (o instanceof JSONObject) { - try { - return ((JSONObject) o).get(_fieldName); - } catch (JSONException e) { - return null; - } + } else if (o instanceof ObjectNode) { + JsonNode value = ((ObjectNode) o).get(_fieldName); + if (value.isArray()) { + return Lists.newArrayList(value.elements()); + } else if (value.isBigDecimal() || value.isDouble() || value.isFloat()) { + return value.asDouble(); + } else if (value.isBigInteger() || value.isInt()) { + return value.asLong(); + } else if (value.isBinary() || value.isTextual()) { + return value.asText(); + } else if (value.isBoolean()) { + return value.asBoolean(); + } else if (value.isNull()) { + return null; + } else { + return null; + } } else { return null; } diff --git a/main/src/com/google/refine/grel/ast/LiteralExpr.java b/main/src/com/google/refine/grel/ast/LiteralExpr.java index 8c04ade8f..d6ac35748 100644 --- a/main/src/com/google/refine/grel/ast/LiteralExpr.java +++ b/main/src/com/google/refine/grel/ast/LiteralExpr.java @@ -35,8 +35,7 @@ package com.google.refine.grel.ast; import java.util.Properties; -import org.json.JSONObject; - +import com.fasterxml.jackson.databind.node.TextNode; import com.google.refine.expr.Evaluable; /** @@ -56,6 +55,6 @@ public class LiteralExpr implements Evaluable { @Override public String toString() { - return _value instanceof String ? JSONObject.quote((String) _value) : _value.toString(); + return _value instanceof String ? new TextNode((String) _value).toString() : _value.toString(); } } diff --git a/main/tests/server/src/com/google/refine/tests/grel/ast/LiteralExprTest.java b/main/tests/server/src/com/google/refine/tests/grel/ast/LiteralExprTest.java new file mode 100644 index 000000000..789ce9ee9 --- /dev/null +++ b/main/tests/server/src/com/google/refine/tests/grel/ast/LiteralExprTest.java @@ -0,0 +1,21 @@ +package com.google.refine.tests.grel.ast; + +import static org.testng.Assert.assertEquals; + +import org.testng.annotations.Test; + +import com.google.refine.grel.ast.LiteralExpr; + +public class LiteralExprTest { + @Test + public void intLiteralToString() { + LiteralExpr expr = new LiteralExpr(42); + assertEquals("42", expr.toString()); + } + + @Test + public void stringLiteralToString() { + LiteralExpr expr = new LiteralExpr("string with \"\\backslash\""); + assertEquals("\"string with \\\"\\\\backslash\\\"\"", expr.toString()); + } +}