From eb666198400b8aa9811dee180630d901a12f5524 Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Mon, 22 Oct 2018 10:11:23 +0100 Subject: [PATCH] Jackson deserialization for ExtendDataOperation --- .../commands/recon/ExtendDataCommand.java | 3 +-- .../recon/PreviewExtendDataCommand.java | 2 +- .../recon/ReconciledDataExtensionJob.java | 4 ++-- .../operations/recon/ExtendDataOperation.java | 24 +++++++++---------- .../recon/ExtendDataOperationTests.java | 12 +++++----- 5 files changed, 21 insertions(+), 24 deletions(-) diff --git a/main/src/com/google/refine/commands/recon/ExtendDataCommand.java b/main/src/com/google/refine/commands/recon/ExtendDataCommand.java index 30adcb526..760c69e62 100644 --- a/main/src/com/google/refine/commands/recon/ExtendDataCommand.java +++ b/main/src/com/google/refine/commands/recon/ExtendDataCommand.java @@ -41,7 +41,6 @@ import com.google.refine.model.AbstractOperation; import com.google.refine.model.Project; import com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionConfig; import com.google.refine.operations.recon.ExtendDataOperation; -import com.google.refine.util.ParsingUtilities; public class ExtendDataCommand extends EngineDependentCommand { @Override @@ -55,7 +54,7 @@ public class ExtendDataCommand extends EngineDependentCommand { String schemaSpace = request.getParameter("schemaSpace"); String jsonString = request.getParameter("extension"); - DataExtensionConfig extension = DataExtensionConfig.reconstruct(ParsingUtilities.evaluateJsonStringToObject(jsonString)); + DataExtensionConfig extension = DataExtensionConfig.reconstruct(jsonString); return new ExtendDataOperation( engineConfig, diff --git a/main/src/com/google/refine/commands/recon/PreviewExtendDataCommand.java b/main/src/com/google/refine/commands/recon/PreviewExtendDataCommand.java index c8d13a2aa..f42675fa0 100644 --- a/main/src/com/google/refine/commands/recon/PreviewExtendDataCommand.java +++ b/main/src/com/google/refine/commands/recon/PreviewExtendDataCommand.java @@ -93,7 +93,7 @@ public class PreviewExtendDataCommand extends Command { } String jsonString = request.getParameter("extension"); - DataExtensionConfig config = DataExtensionConfig.reconstruct(ParsingUtilities.evaluateJsonStringToObject(jsonString)); + DataExtensionConfig config = DataExtensionConfig.reconstruct(jsonString); JSONArray rowIndices = ParsingUtilities.evaluateJsonStringToArray(rowIndicesString); int length = rowIndices.length(); diff --git a/main/src/com/google/refine/model/recon/ReconciledDataExtensionJob.java b/main/src/com/google/refine/model/recon/ReconciledDataExtensionJob.java index 47f575600..13a630e5c 100644 --- a/main/src/com/google/refine/model/recon/ReconciledDataExtensionJob.java +++ b/main/src/com/google/refine/model/recon/ReconciledDataExtensionJob.java @@ -107,10 +107,10 @@ public class ReconciledDataExtensionJob { this.properties = properties; } - public static DataExtensionConfig reconstruct(JSONObject obj) throws JSONException { + public static DataExtensionConfig reconstruct(String json) throws JSONException { ObjectMapper mapper = new ObjectMapper(); try { - return mapper.readValue(obj.toString(), DataExtensionConfig.class); + return mapper.readValue(json, DataExtensionConfig.class); } catch(IOException e) { throw new JSONException(e.toString()); } diff --git a/main/src/com/google/refine/operations/recon/ExtendDataOperation.java b/main/src/com/google/refine/operations/recon/ExtendDataOperation.java index fac3243ed..e0aa039e4 100644 --- a/main/src/com/google/refine/operations/recon/ExtendDataOperation.java +++ b/main/src/com/google/refine/operations/recon/ExtendDataOperation.java @@ -44,6 +44,7 @@ import java.util.Set; import org.json.JSONException; import org.json.JSONObject; +import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; import com.google.refine.browsing.Engine; @@ -67,6 +68,7 @@ import com.google.refine.model.recon.ReconciledDataExtensionJob.DataExtensionCon import com.google.refine.operations.EngineDependentOperation; import com.google.refine.process.LongRunningProcess; import com.google.refine.process.Process; +import com.google.refine.util.ParsingUtilities; public class ExtendDataOperation extends EngineDependentOperation { @JsonProperty("baseColumnName") @@ -84,28 +86,24 @@ public class ExtendDataOperation extends EngineDependentOperation { static public AbstractOperation reconstruct(Project project, JSONObject obj) throws Exception { - JSONObject engineConfig = obj.getJSONObject("engineConfig"); - - DataExtensionConfig dataExtensionConfig = DataExtensionConfig.reconstruct(obj.getJSONObject("extension")); - - return new ExtendDataOperation( - EngineConfig.reconstruct(engineConfig), - obj.getString("baseColumnName"), - obj.getString("endpoint"), - obj.getString("identifierSpace"), - obj.getString("schemaSpace"), - dataExtensionConfig, - obj.getInt("columnInsertIndex") - ); + return ParsingUtilities.mapper.readValue(obj.toString(), ExtendDataOperation.class); } + @JsonCreator public ExtendDataOperation( + @JsonProperty("engineConfig") EngineConfig engineConfig, + @JsonProperty("baseColumnName") String baseColumnName, + @JsonProperty("endpoint") String endpoint, + @JsonProperty("identifierSpace") String identifierSpace, + @JsonProperty("schemaSpace") String schemaSpace, + @JsonProperty("extension") DataExtensionConfig extension, + @JsonProperty("columnInsertIndex") int columnInsertIndex ) { super(engineConfig); diff --git a/main/tests/server/src/com/google/refine/tests/operations/recon/ExtendDataOperationTests.java b/main/tests/server/src/com/google/refine/tests/operations/recon/ExtendDataOperationTests.java index 02df8cfb5..17cdb98ba 100644 --- a/main/tests/server/src/com/google/refine/tests/operations/recon/ExtendDataOperationTests.java +++ b/main/tests/server/src/com/google/refine/tests/operations/recon/ExtendDataOperationTests.java @@ -177,12 +177,12 @@ public class ExtendDataOperationTests extends RefineTest { @Test public void serializeDataExtensionConfig() { - TestUtils.isSerializedTo(DataExtensionConfig.reconstruct(new JSONObject(dataExtensionConfigJson)), dataExtensionConfigJson); + TestUtils.isSerializedTo(DataExtensionConfig.reconstruct(dataExtensionConfigJson), dataExtensionConfigJson); } @Test public void testFormulateQuery() throws IOException { - DataExtensionConfig config = DataExtensionConfig.reconstruct(new JSONObject(dataExtensionConfigJson)); + DataExtensionConfig config = DataExtensionConfig.reconstruct(dataExtensionConfigJson); Set ids = Collections.singleton("Q2"); String json = "{\"ids\":[\"Q2\"],\"properties\":[{\"id\":\"P571\"},{\"id\":\"P159\"},{\"id\":\"P625\"}]}"; ReconciledDataExtensionJobStub stub = new ReconciledDataExtensionJobStub(config, "http://endpoint"); @@ -214,7 +214,7 @@ public class ExtendDataOperationTests extends RefineTest { @Test public void testFetchStrings() throws Exception { - DataExtensionConfig extension = DataExtensionConfig.reconstruct(new JSONObject("{\"properties\":[{\"id\":\"P297\",\"name\":\"ISO 3166-1 alpha-2 code\"}]}")); + DataExtensionConfig extension = DataExtensionConfig.reconstruct("{\"properties\":[{\"id\":\"P297\",\"name\":\"ISO 3166-1 alpha-2 code\"}]}"); EngineDependentOperation op = new ExtendDataOperation(engine_config, "country", @@ -252,7 +252,7 @@ public class ExtendDataOperationTests extends RefineTest { @Test public void testFetchCounts() throws Exception { DataExtensionConfig extension = DataExtensionConfig.reconstruct( - new JSONObject("{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"count\":\"on\",\"rank\":\"any\"}}]}")); + "{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"count\":\"on\",\"rank\":\"any\"}}]}"); EngineDependentOperation op = new ExtendDataOperation(engine_config, "country", @@ -287,7 +287,7 @@ public class ExtendDataOperationTests extends RefineTest { @Test public void testFetchCurrent() throws Exception { DataExtensionConfig extension = DataExtensionConfig.reconstruct( - new JSONObject("{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"rank\":\"best\"}}]}")); + "{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"rank\":\"best\"}}]}"); EngineDependentOperation op = new ExtendDataOperation(engine_config, "country", @@ -328,7 +328,7 @@ public class ExtendDataOperationTests extends RefineTest { @Test public void testFetchRecord() throws Exception { DataExtensionConfig extension = DataExtensionConfig.reconstruct( - new JSONObject("{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"rank\":\"any\"}}]}")); + "{\"properties\":[{\"id\":\"P38\",\"name\":\"currency\",\"settings\":{\"rank\":\"any\"}}]}"); EngineDependentOperation op = new ExtendDataOperation(engine_config, "country",