Migrate PreviewExpressionCommand to Jackson

This commit is contained in:
Antonin Delpeuch 2018-11-19 18:46:26 +00:00
parent f48cdb0c13
commit 472dc7f2ac

View File

@ -44,14 +44,14 @@ import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue; import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.refine.commands.Command; import com.google.refine.commands.Command;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.Evaluable; import com.google.refine.expr.Evaluable;
@ -139,8 +139,8 @@ public class PreviewExpressionCommand extends Command {
} }
} }
JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString); List<Integer> rowIndices = ParsingUtilities.mapper.readValue(rowIndicesString, new TypeReference<List<Integer>>() {});
int length = rowIndices.length(); int length = rowIndices.size();
try { try {
Evaluable eval = MetaParser.parse(expression); Evaluable eval = MetaParser.parse(expression);
@ -150,7 +150,7 @@ public class PreviewExpressionCommand extends Command {
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
Object result = null; Object result = null;
int rowIndex = rowIndices.getInt(i); int rowIndex = rowIndices.get(i);
if (rowIndex >= 0 && rowIndex < project.rows.size()) { if (rowIndex >= 0 && rowIndex < project.rows.size()) {
Row row = project.rows.get(rowIndex); Row row = project.rows.get(rowIndex);
Cell cell = row.getCell(cellIndex); Cell cell = row.getCell(cellIndex);
@ -202,7 +202,7 @@ public class PreviewExpressionCommand extends Command {
} }
} }
static protected void writeValue(StringBuffer sb, Object v, boolean quote) throws JSONException { static protected void writeValue(StringBuffer sb, Object v, boolean quote) {
if (ExpressionUtils.isError(v)) { if (ExpressionUtils.isError(v)) {
sb.append("[error: " + ((EvalError) v).message + "]"); sb.append("[error: " + ((EvalError) v).message + "]");
} else { } else {
@ -213,10 +213,10 @@ public class PreviewExpressionCommand extends Command {
sb.append("[object Cell]"); sb.append("[object Cell]");
} else if (v instanceof WrappedRow) { } else if (v instanceof WrappedRow) {
sb.append("[object Row]"); sb.append("[object Row]");
} else if (v instanceof JSONObject) { } else if (v instanceof ObjectNode) {
sb.append(((JSONObject) v).toString()); sb.append(((ObjectNode) v).toString());
} else if (v instanceof JSONArray) { } else if (v instanceof ArrayNode) {
sb.append(((JSONArray) v).toString()); sb.append(((ArrayNode) v).toString());
} else if (ExpressionUtils.isArray(v)) { } else if (ExpressionUtils.isArray(v)) {
Object[] a = (Object[]) v; Object[] a = (Object[]) v;
sb.append("[ "); sb.append("[ ");
@ -244,7 +244,11 @@ public class PreviewExpressionCommand extends Command {
ParsingUtilities.dateToString((OffsetDateTime) v) +"]"); ParsingUtilities.dateToString((OffsetDateTime) v) +"]");
} else if (v instanceof String) { } else if (v instanceof String) {
if (quote) { if (quote) {
sb.append(JSONObject.quote((String) v)); try {
sb.append(ParsingUtilities.mapper.writeValueAsString(((String) v)));
} catch (JsonProcessingException e) {
// will not happen
}
} else { } else {
sb.append((String) v); sb.append((String) v);
} }