Jackson deserialization for Cell

This commit is contained in:
Antonin Delpeuch 2018-10-21 17:28:43 +01:00
parent 8758ed2cff
commit adb2e13874

View File

@ -42,6 +42,8 @@ import java.time.OffsetDateTime;
import java.time.ZoneOffset; import java.time.ZoneOffset;
import java.util.Properties; import java.util.Properties;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; 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;
@ -49,6 +51,7 @@ 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.EvalError; import com.google.refine.expr.EvalError;
import com.google.refine.expr.ExpressionUtils; import com.google.refine.expr.ExpressionUtils;
@ -159,14 +162,35 @@ public class Cell implements HasFields {
} }
static public Cell loadStreaming(String s, Pool pool) throws Exception { static public Cell 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, Cell.class);
return null;
} }
return loadStreaming(jp, pool); @JsonCreator
static public Cell deserialize(
@JsonProperty("v")
Object value,
@JsonProperty("t")
String type,
@JsonProperty("r")
String reconId,
@JsonProperty("e")
String error,
@JacksonInject("pool")
Pool pool) {
Recon recon = null;
if(reconId != null) {
recon = pool.getRecon(reconId);
}
if (type != null && "date".equals(type)) {
value = ParsingUtilities.stringToDate((String) value);
}
if (error != null) {
value = new EvalError(error);
}
return new Cell((Serializable)value, recon);
} }
static public Cell loadStreaming(JsonParser jp, Pool pool) throws Exception { static public Cell loadStreaming(JsonParser jp, Pool pool) throws Exception {