Jackson deserialization for AbstractOperation

This commit is contained in:
Antonin Delpeuch 2018-10-22 14:07:02 +01:00
parent 8f9004f7ee
commit 2ef955f24b
3 changed files with 24 additions and 19 deletions

View File

@ -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 {

View File

@ -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)) {

View File

@ -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<Class<? extends AbstractOperation>> classes = s_opNameToClass.get(op);
if (classes != null && classes.size() > 0) {
Class<? extends AbstractOperation> 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<? extends AbstractOperation> resolveOperationId(String op) {
if (!op.contains("/")) {
op = "core/" + op; // backward compatible
}
List<Class<? extends AbstractOperation>> 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);
}
}