Jackson deserialization for Row

This commit is contained in:
Antonin Delpeuch 2018-10-21 17:45:05 +01:00
parent adb2e13874
commit 487f6bc131
3 changed files with 22 additions and 95 deletions

View File

@ -48,9 +48,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore;
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.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.InjectableValues; import com.fasterxml.jackson.databind.InjectableValues;
import com.google.refine.expr.EvalError; import com.google.refine.expr.EvalError;
@ -193,60 +190,6 @@ public class Cell implements HasFields {
return new Cell((Serializable)value, recon); return new Cell((Serializable)value, recon);
} }
static public Cell loadStreaming(JsonParser jp, Pool pool) throws Exception {
JsonToken t = jp.getCurrentToken();
if (t == JsonToken.VALUE_NULL || t != JsonToken.START_OBJECT) {
return null;
}
Serializable value = null;
String type = null;
Recon recon = null;
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldName = jp.getCurrentName();
jp.nextToken();
if ("r".equals(fieldName)) {
if (jp.getCurrentToken() == JsonToken.VALUE_STRING) {
String reconID = jp.getText();
recon = pool.getRecon(reconID);
} else {
// legacy
recon = Recon.loadStreaming(jp, pool);
}
} else if ("e".equals(fieldName)) {
value = new EvalError(jp.getText());
} else if ("v".equals(fieldName)) {
JsonToken token = jp.getCurrentToken();
if (token == JsonToken.VALUE_STRING) {
value = jp.getText();
} else if (token == JsonToken.VALUE_NUMBER_INT) {
value = jp.getLongValue();
} else if (token == JsonToken.VALUE_NUMBER_FLOAT) {
value = jp.getDoubleValue();
} else if (token == JsonToken.VALUE_TRUE) {
value = true;
} else if (token == JsonToken.VALUE_FALSE) {
value = false;
}
} else if ("t".equals(fieldName)) {
type = jp.getText();
}
}
if (value != null) {
if (type != null && "date".equals(type)) {
value = ParsingUtilities.stringToDate((String) value);
}
return new Cell(value, recon);
} else {
return null;
}
}
@Override @Override
public String toString() { public String toString() {
return StringUtils.toString(value); return StringUtils.toString(value);

View File

@ -36,14 +36,17 @@ package com.google.refine.model;
import java.io.IOException; import java.io.IOException;
import java.io.Writer; import java.io.Writer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken; import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.databind.InjectableValues;
import com.google.refine.expr.CellTuple; import com.google.refine.expr.CellTuple;
import com.google.refine.expr.HasFields; import com.google.refine.expr.HasFields;
@ -206,40 +209,25 @@ public class Row implements HasFields {
loadStreaming(s, pool); loadStreaming(s, pool);
} }
@JsonCreator
static public Row deserialize(
@JsonProperty(STARRED)
boolean starred,
@JsonProperty(FLAGGED)
boolean flagged,
@JsonProperty("cells")
List<Cell> cells) {
if (cells == null) {
cells = new ArrayList<>();
}
return new Row(cells, flagged, starred);
}
static public Row loadStreaming(String s, Pool pool) throws Exception { static public Row loadStreaming(String s, Pool pool) throws Exception {
JsonFactory jsonFactory = new JsonFactory(); InjectableValues injectableValues = new InjectableValues.Std()
JsonParser jp = jsonFactory.createJsonParser(s); .addValue("pool", pool);
return ParsingUtilities.mapper.setInjectableValues(injectableValues)
if (jp.nextToken() != JsonToken.START_OBJECT) { .readValue(s, Row.class);
return null;
}
List<Cell> cells = new ArrayList<Cell>();
boolean starred = false;
boolean flagged = false;
while (jp.nextToken() != JsonToken.END_OBJECT) {
String fieldName = jp.getCurrentName();
jp.nextToken();
if (STARRED.equals(fieldName)) {
starred = jp.getBooleanValue();
} else if (FLAGGED.equals(fieldName)) {
flagged = jp.getBooleanValue();
} else if ("cells".equals(fieldName)) {
if (jp.getCurrentToken() != JsonToken.START_ARRAY) {
return null;
}
while (jp.nextToken() != JsonToken.END_ARRAY) {
Cell cell = Cell.loadStreaming(jp, pool);
cells.add(cell);
}
}
}
return (cells.size() > 0) ? new Row(cells, flagged, starred) : new Row(0);
} }
@Override @Override

View File

@ -1,14 +1,10 @@
package com.google.refine.tests.model; package com.google.refine.tests.model;
import org.testng.annotations.Test;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import java.util.Properties; import org.testng.annotations.Test;
import com.google.refine.model.Cell; import com.google.refine.model.Cell;
import com.google.refine.model.Recon; import com.google.refine.model.Recon;