From d4bdd37bda7ad99049821c088356b091dddb3f32 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Sun, 21 Oct 2018 18:35:23 +0100 Subject: [PATCH] Jackson deserialization for Recon --- main/src/com/google/refine/model/Recon.java | 159 ++++++------------ .../google/refine/model/ReconCandidate.java | 1 - main/src/com/google/refine/model/Row.java | 4 - .../refine/model/changes/MassReconChange.java | 2 +- main/src/com/google/refine/util/Pool.java | 2 +- .../google/refine/tests/model/CellTests.java | 2 +- .../google/refine/tests/model/ReconTests.java | 6 +- .../google/refine/tests/model/RowTests.java | 2 +- 8 files changed, 59 insertions(+), 119 deletions(-) diff --git a/main/src/com/google/refine/model/Recon.java b/main/src/com/google/refine/model/Recon.java index f96125ed3..caa1373e6 100644 --- a/main/src/com/google/refine/model/Recon.java +++ b/main/src/com/google/refine/model/Recon.java @@ -46,13 +46,10 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonView; -import com.fasterxml.jackson.core.JsonFactory; -import com.fasterxml.jackson.core.JsonParser; -import com.fasterxml.jackson.core.JsonToken; import com.google.refine.expr.HasFields; import com.google.refine.util.JsonViews; -import com.google.refine.util.Pool; +import com.google.refine.util.ParsingUtilities; @JsonFilter("reconCandidateFilter") public class Recon implements HasFields { @@ -111,20 +108,32 @@ public class Recon implements HasFields { s_featureMap.put("nameWordDistance", Feature_nameWordDistance); } + @JsonIgnore final public long id; + @JsonIgnore public String service = "unknown"; + @JsonIgnore public String identifierSpace = null; + @JsonIgnore public String schemaSpace = null; + @JsonIgnore public Object[] features = new Object[Feature_max]; + @JsonIgnore public List candidates; + @JsonIgnore public Judgment judgment = Judgment.None; + @JsonIgnore public String judgmentAction = "unknown"; + @JsonIgnore public long judgmentHistoryEntry; + @JsonIgnore public int judgmentBatchSize = 0; + @JsonIgnore public ReconCandidate match = null; + @JsonIgnore public int matchRank = -1; @Deprecated @@ -362,110 +371,46 @@ public class Recon implements HasFields { return null; } - static public Recon loadStreaming(String s, Pool pool) throws Exception { - JsonFactory jsonFactory = new JsonFactory(); - JsonParser jp = jsonFactory.createJsonParser(s); - - if (jp.nextToken() != JsonToken.START_OBJECT) { - return null; - } - return loadStreaming(jp, pool); + static public Recon loadStreaming(String s) throws Exception { + return ParsingUtilities.mapper.readValue(s, Recon.class); } - static public Recon loadStreaming(JsonParser jp, Pool pool) throws Exception { - JsonToken t = jp.getCurrentToken(); - if (t == JsonToken.VALUE_NULL || t != JsonToken.START_OBJECT) { - return null; - } - - Recon recon = null; - long id = -1; - long judgmentHistoryEntry = -1; - - while (jp.nextToken() != JsonToken.END_OBJECT) { - String fieldName = jp.getCurrentName(); - jp.nextToken(); - - if ("id".equals(fieldName)) { - id = jp.getLongValue(); - } else if ("judgmentHistoryEntry".equals(fieldName)) { - judgmentHistoryEntry = jp.getLongValue(); - if (recon != null) { - recon.judgmentHistoryEntry = judgmentHistoryEntry; - } - } else { - if (recon == null) { - recon = new Recon(id, judgmentHistoryEntry); - } - - if ("j".equals(fieldName)) { - recon.judgment = stringToJudgment(jp.getText()); - } else if ("m".equals(fieldName)) { - if (jp.getCurrentToken() == JsonToken.VALUE_STRING) { - // legacy case - String candidateID = jp.getText(); - recon.match = pool.getReconCandidate(candidateID); - } else { - recon.match = ReconCandidate.loadStreaming(jp); - } - } else if ("f".equals(fieldName)) { - if (jp.getCurrentToken() != JsonToken.START_ARRAY) { - return null; - } - - int feature = 0; - while (jp.nextToken() != JsonToken.END_ARRAY) { - if (feature < recon.features.length) { - JsonToken token = jp.getCurrentToken(); - if (token == JsonToken.VALUE_STRING) { - recon.features[feature++] = jp.getText(); - } else if (token == JsonToken.VALUE_NUMBER_INT) { - recon.features[feature++] = jp.getLongValue(); - } else if (token == JsonToken.VALUE_NUMBER_FLOAT) { - recon.features[feature++] = jp.getDoubleValue(); - } else if (token == JsonToken.VALUE_FALSE) { - recon.features[feature++] = false; - } else if (token == JsonToken.VALUE_TRUE) { - recon.features[feature++] = true; - } - } - } - } else if ("c".equals(fieldName)) { - if (jp.getCurrentToken() != JsonToken.START_ARRAY) { - return null; - } - - while (jp.nextToken() != JsonToken.END_ARRAY) { - if (jp.getCurrentToken() == JsonToken.VALUE_STRING) { - // legacy case - String candidateID = jp.getText(); - recon.addCandidate(pool.getReconCandidate(candidateID)); - } else { - recon.addCandidate(ReconCandidate.loadStreaming(jp)); - } - } - } else if ("service".equals(fieldName)) { - recon.service = jp.getText(); - } else if ("identifierSpace".equals(fieldName)) { - recon.identifierSpace = jp.getText(); - if ("null".equals(recon.identifierSpace)) { - recon.identifierSpace = FREEBASE_IDENTIFIER_SPACE; - } - } else if ("schemaSpace".equals(fieldName)) { - recon.schemaSpace = jp.getText(); - if ("null".equals(recon.schemaSpace)) { - recon.schemaSpace = FREEBASE_SCHEMA_SPACE; - } - } else if ("judgmentAction".equals(fieldName)) { - recon.judgmentAction = jp.getText(); - } else if ("judgmentBatchSize".equals(fieldName)) { - recon.judgmentBatchSize = jp.getIntValue(); - } else if ("matchRank".equals(fieldName)) { - recon.matchRank = jp.getIntValue(); - } - } - } - - return recon; + public Recon( + @JsonProperty("id") + long id, + @JsonProperty("judgmentHistoryEntry") + long judgmentHistoryEntry, + @JsonProperty("j") + Judgment judgment, + @JsonProperty("m") + ReconCandidate match, + @JsonProperty("f") + Object[] features, + @JsonProperty("c") + List candidates, + @JsonProperty("service") + String service, + @JsonProperty("identifierSpace") + String identifierSpace, + @JsonProperty("schemaSpace") + String schemaSpace, + @JsonProperty("judgmentAction") + String judgmentAction, + @JsonProperty("judgmentBatchSize") + Integer judgmentBatchSize, + @JsonProperty("matchRank") + Integer matchRank) { + this.id = id; + this.judgmentHistoryEntry = judgmentHistoryEntry; + this.judgment = judgment != null ? judgment : Judgment.None; + this.match = match; + this.features = features != null ? features : new Object[Feature_max]; + this.candidates = candidates != null ? candidates : new ArrayList<>(); + this.service = service != null ? service : "unknown"; + this.identifierSpace = identifierSpace; + this.schemaSpace = schemaSpace; + this.judgmentAction = judgmentAction != null ? judgmentAction : "unknown"; + this.judgmentBatchSize = judgmentBatchSize != null ? judgmentBatchSize : 0; + this.matchRank = matchRank != null ? matchRank : -1; } } diff --git a/main/src/com/google/refine/model/ReconCandidate.java b/main/src/com/google/refine/model/ReconCandidate.java index d81e0f760..6e2a1e3da 100644 --- a/main/src/com/google/refine/model/ReconCandidate.java +++ b/main/src/com/google/refine/model/ReconCandidate.java @@ -39,7 +39,6 @@ import java.util.Properties; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonToken; diff --git a/main/src/com/google/refine/model/Row.java b/main/src/com/google/refine/model/Row.java index 846271f41..3e161f3f7 100644 --- a/main/src/com/google/refine/model/Row.java +++ b/main/src/com/google/refine/model/Row.java @@ -36,16 +36,12 @@ package com.google.refine.model; import java.io.IOException; import java.io.Writer; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import java.util.Properties; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; 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.google.refine.expr.CellTuple; diff --git a/main/src/com/google/refine/model/changes/MassReconChange.java b/main/src/com/google/refine/model/changes/MassReconChange.java index 41869af00..3b5541699 100644 --- a/main/src/com/google/refine/model/changes/MassReconChange.java +++ b/main/src/com/google/refine/model/changes/MassReconChange.java @@ -140,7 +140,7 @@ public class MassReconChange implements Change { for (int i = 0; i < count; i++) { String line = reader.readLine(); - Recon recon = Recon.loadStreaming(line, pool); + Recon recon = Recon.loadStreaming(line); recons.put(recon.id, recon); } diff --git a/main/src/com/google/refine/util/Pool.java b/main/src/com/google/refine/util/Pool.java index ddb9ea39e..d91ca0f9a 100644 --- a/main/src/com/google/refine/util/Pool.java +++ b/main/src/com/google/refine/util/Pool.java @@ -145,7 +145,7 @@ public class Pool { for (int i = 0; i < count; i++) { line = reader2.readLine(); if (line != null) { - Recon recon = Recon.loadStreaming(line, this); + Recon recon = Recon.loadStreaming(line); if (recon != null) { pool(recon); } diff --git a/main/tests/server/src/com/google/refine/tests/model/CellTests.java b/main/tests/server/src/com/google/refine/tests/model/CellTests.java index ded99c68b..bbe58fb2e 100644 --- a/main/tests/server/src/com/google/refine/tests/model/CellTests.java +++ b/main/tests/server/src/com/google/refine/tests/model/CellTests.java @@ -28,7 +28,7 @@ public class CellTests { @Test public void serializeCellWithRecon() throws Exception { - recon = Recon.loadStreaming(reconJson, pool); + recon = Recon.loadStreaming(reconJson); when(pool.getRecon("1533649346002675326")).thenReturn(recon); String json = "{\"v\":\"http://www.wikidata.org/entity/Q41522540\",\"r\":\"1533649346002675326\"}"; diff --git a/main/tests/server/src/com/google/refine/tests/model/ReconTests.java b/main/tests/server/src/com/google/refine/tests/model/ReconTests.java index fbfcfbfa0..dda2ce67c 100644 --- a/main/tests/server/src/com/google/refine/tests/model/ReconTests.java +++ b/main/tests/server/src/com/google/refine/tests/model/ReconTests.java @@ -32,13 +32,13 @@ public class ReconTests { @Test public void serializeReconSaveMode() throws Exception { - Recon r = Recon.loadStreaming(fullJson, null); + Recon r = Recon.loadStreaming(fullJson); TestUtils.isSerializedTo(r, fullJson, true); } @Test public void serializeReconViewMode() throws Exception { - Recon r = Recon.loadStreaming(fullJson, null); + Recon r = Recon.loadStreaming(fullJson); String shortJson = "{\"id\":1533651559492945033," + "\"service\":\"https://tools.wmflabs.org/openrefine-wikidata/en/api\"," + "\"identifierSpace\":\"http://www.wikidata.org/entity/\"," @@ -66,7 +66,7 @@ public class ReconTests { + " {\"id\":\"Q30284245\",\"name\":\"Baylor College of Medicine Children\\u2019s Foundation\",\"score\":48.57142857142858,\"types\":[\"Q163740\"]}" + "]" + "}"; - Recon r = Recon.loadStreaming(fullJson, null); + Recon r = Recon.loadStreaming(fullJson); r.match = null; r.judgment = Judgment.None; TestUtils.isSerializedTo(r, json); diff --git a/main/tests/server/src/com/google/refine/tests/model/RowTests.java b/main/tests/server/src/com/google/refine/tests/model/RowTests.java index 5241be71e..4108ccfc8 100644 --- a/main/tests/server/src/com/google/refine/tests/model/RowTests.java +++ b/main/tests/server/src/com/google/refine/tests/model/RowTests.java @@ -136,7 +136,7 @@ public class RowTests extends RefineTest { + "\"c\":[{\"id\":\"Q551479\",\"name\":\"La Monnaie\",\"score\":100,\"types\":[\"Q153562\"]}]," + "\"f\":[false,false,34,0],\"judgmentAction\":\"auto\",\"judgmentBatchSize\":1,\"matchRank\":0}"; Pool pool = mock(Pool.class); - Recon recon = Recon.loadStreaming(reconJson, pool); + Recon recon = Recon.loadStreaming(reconJson); when(pool.getRecon("1533649346002675326")).thenReturn(recon); String json = "{\"flagged\":false,"