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 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;
}

View File

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

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