Migrate GREL AST to Jackson

This commit is contained in:
Antonin Delpeuch 2018-11-15 16:55:26 +00:00
parent ebfa591190
commit ccb1ac84c1
3 changed files with 43 additions and 12 deletions

View File

@ -35,9 +35,9 @@ package com.google.refine.grel.ast;
import java.util.Properties; import java.util.Properties;
import org.json.JSONException; import com.fasterxml.jackson.databind.JsonNode;
import org.json.JSONObject; import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
import com.google.refine.expr.Evaluable; import com.google.refine.expr.Evaluable;
import com.google.refine.expr.ExpressionUtils; import com.google.refine.expr.ExpressionUtils;
import com.google.refine.expr.HasFields; import com.google.refine.expr.HasFields;
@ -65,12 +65,23 @@ public class FieldAccessorExpr implements Evaluable {
return null; return null;
} else if (o instanceof HasFields) { } else if (o instanceof HasFields) {
return ((HasFields) o).getField(_fieldName, bindings); return ((HasFields) o).getField(_fieldName, bindings);
} else if (o instanceof JSONObject) { } else if (o instanceof ObjectNode) {
try { JsonNode value = ((ObjectNode) o).get(_fieldName);
return ((JSONObject) o).get(_fieldName); if (value.isArray()) {
} catch (JSONException e) { return Lists.newArrayList(value.elements());
return null; } 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 { } else {
return null; return null;
} }

View File

@ -35,8 +35,7 @@ package com.google.refine.grel.ast;
import java.util.Properties; import java.util.Properties;
import org.json.JSONObject; import com.fasterxml.jackson.databind.node.TextNode;
import com.google.refine.expr.Evaluable; import com.google.refine.expr.Evaluable;
/** /**
@ -56,6 +55,6 @@ public class LiteralExpr implements Evaluable {
@Override @Override
public String toString() { public String toString() {
return _value instanceof String ? JSONObject.quote((String) _value) : _value.toString(); return _value instanceof String ? new TextNode((String) _value).toString() : _value.toString();
} }
} }

View File

@ -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());
}
}