From 2ef955f24b27e19e5404668f60c1dbd9371e4dfd Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Mon, 22 Oct 2018 14:07:02 +0100 Subject: [PATCH] Jackson deserialization for AbstractOperation --- .../history/ApplyOperationsCommand.java | 2 +- .../refine/model/AbstractOperation.java | 7 ++++ .../refine/operations/OperationRegistry.java | 34 +++++++++---------- 3 files changed, 24 insertions(+), 19 deletions(-) diff --git a/main/src/com/google/refine/commands/history/ApplyOperationsCommand.java b/main/src/com/google/refine/commands/history/ApplyOperationsCommand.java index 6ea66a1f2..136109acf 100644 --- a/main/src/com/google/refine/commands/history/ApplyOperationsCommand.java +++ b/main/src/com/google/refine/commands/history/ApplyOperationsCommand.java @@ -78,7 +78,7 @@ public class ApplyOperationsCommand extends Command { } } - protected void reconstructOperation(Project project, JSONObject obj) { + protected void reconstructOperation(Project project, JSONObject obj) throws IOException { AbstractOperation operation = OperationRegistry.reconstruct(project, obj); if (operation != null) { try { diff --git a/main/src/com/google/refine/model/AbstractOperation.java b/main/src/com/google/refine/model/AbstractOperation.java index 414bbe453..16fd68741 100644 --- a/main/src/com/google/refine/model/AbstractOperation.java +++ b/main/src/com/google/refine/model/AbstractOperation.java @@ -36,6 +36,8 @@ package com.google.refine.model; import java.util.Properties; import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; import com.google.refine.history.HistoryEntry; import com.google.refine.operations.OperationRegistry; @@ -46,6 +48,11 @@ import com.google.refine.process.QuickHistoryEntryProcess; * An abstract operation can be applied to different but similar * projects. */ +@JsonTypeInfo( + use=JsonTypeInfo.Id.CUSTOM, + include=JsonTypeInfo.As.PROPERTY, + property="op") +@JsonTypeIdResolver(OperationResolver.class) abstract public class AbstractOperation { public Process createProcess(Project project, Properties options) throws Exception { return new QuickHistoryEntryProcess(project, getBriefDescription(null)) { diff --git a/main/src/com/google/refine/operations/OperationRegistry.java b/main/src/com/google/refine/operations/OperationRegistry.java index 604439f11..e947c510c 100644 --- a/main/src/com/google/refine/operations/OperationRegistry.java +++ b/main/src/com/google/refine/operations/OperationRegistry.java @@ -33,6 +33,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package com.google.refine.operations; +import java.io.IOException; import java.lang.reflect.Method; import java.util.HashMap; import java.util.LinkedList; @@ -41,8 +42,12 @@ import java.util.Map; import org.json.JSONObject; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; + import com.google.refine.model.AbstractOperation; import com.google.refine.model.Project; +import com.google.refine.util.ParsingUtilities; import edu.mit.simile.butterfly.ButterflyModule; @@ -67,25 +72,18 @@ public abstract class OperationRegistry { classes.add(klass); } - static public AbstractOperation reconstruct(Project project, JSONObject obj) { - try { - String op = obj.getString("op"); - if (!op.contains("/")) { - op = "core/" + op; // backward compatible - } - - List> classes = s_opNameToClass.get(op); - if (classes != null && classes.size() > 0) { - Class klass = classes.get(classes.size() - 1); - - Method reconstruct = klass.getMethod("reconstruct", Project.class, JSONObject.class); - if (reconstruct != null) { - return (AbstractOperation) reconstruct.invoke(null, project, obj); - } - } - } catch (Exception e) { - e.printStackTrace(); + static public Class resolveOperationId(String op) { + if (!op.contains("/")) { + op = "core/" + op; // backward compatible + } + List> classes = s_opNameToClass.get(op); + if (classes != null && classes.size() > 0) { + return classes.get(classes.size() - 1); } return null; } + + static public AbstractOperation reconstruct(Project project, JSONObject obj) throws IOException { + return ParsingUtilities.mapper.readValue(obj.toString(), AbstractOperation.class); + } }