From 8758ed2cffd3be146fc964552545a1cf1bee2706 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Sun, 21 Oct 2018 16:37:51 +0100 Subject: [PATCH] Jackson deserialization for ReconCandidate --- .../google/refine/model/ReconCandidate.java | 24 ++++++++++++------- .../model/recon/DataExtensionReconConfig.java | 18 -------------- 2 files changed, 15 insertions(+), 27 deletions(-) diff --git a/main/src/com/google/refine/model/ReconCandidate.java b/main/src/com/google/refine/model/ReconCandidate.java index 6b2e86f3a..d81e0f760 100644 --- a/main/src/com/google/refine/model/ReconCandidate.java +++ b/main/src/com/google/refine/model/ReconCandidate.java @@ -37,12 +37,14 @@ import java.util.ArrayList; import java.util.List; 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; import com.google.refine.expr.HasFields; +import com.google.refine.util.ParsingUtilities; public class ReconCandidate implements HasFields { @JsonProperty("id") @@ -54,10 +56,19 @@ public class ReconCandidate implements HasFields { @JsonProperty("score") final public double score; - public ReconCandidate(String topicID, String topicName, String[] typeIDs, double score) { + @JsonCreator + public ReconCandidate( + @JsonProperty("id") + String topicID, + @JsonProperty("name") + String topicName, + @JsonProperty("types") + String[] typeIDs, + @JsonProperty("score") + double score) { this.id = topicID; this.name = topicName; - this.types = typeIDs; + this.types = typeIDs == null ? new String[0] : typeIDs; this.score = score; } @@ -81,15 +92,10 @@ public class ReconCandidate implements HasFields { } static public ReconCandidate loadStreaming(String s) throws Exception { - JsonFactory jsonFactory = new JsonFactory(); - JsonParser jp = jsonFactory.createJsonParser(s); - - if (jp.nextToken() != JsonToken.START_OBJECT) { - return null; - } - return loadStreaming(jp); + return ParsingUtilities.mapper.readValue(s, ReconCandidate.class); } + @Deprecated static public ReconCandidate loadStreaming(JsonParser jp) throws Exception { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.VALUE_NULL || t != JsonToken.START_OBJECT) { diff --git a/main/src/com/google/refine/model/recon/DataExtensionReconConfig.java b/main/src/com/google/refine/model/recon/DataExtensionReconConfig.java index 29c8c3590..7cf6dbce0 100644 --- a/main/src/com/google/refine/model/recon/DataExtensionReconConfig.java +++ b/main/src/com/google/refine/model/recon/DataExtensionReconConfig.java @@ -36,8 +36,6 @@ package com.google.refine.model.recon; import java.util.ArrayList; import java.util.List; -import org.json.JSONObject; - import com.google.refine.model.Cell; import com.google.refine.model.Project; import com.google.refine.model.Recon; @@ -49,22 +47,6 @@ public class DataExtensionReconConfig extends StandardReconConfig { private final static String WARN = "Not implemented"; - static public ReconConfig reconstruct(JSONObject obj) throws Exception { - JSONObject type = obj.getJSONObject("type"); - - ReconType typ = null; - if(obj.has("id")) { - typ = new ReconType(obj.getString("id"), - obj.has("name") ? obj.getString("name") : obj.getString("id")); - } - - return new DataExtensionReconConfig( - obj.getString("service"), - obj.has("identifierSpace") ? obj.getString("identifierSpace") : null, - obj.has("schemaSpace") ? obj.getString("schemaSpace") : null, - typ); - } - public DataExtensionReconConfig( String service, String identifierSpace,