Jackson serialization for history classes

This commit is contained in:
Antonin Delpeuch 2018-09-28 16:24:01 +01:00
parent aba8cd8430
commit 418b21dda2
6 changed files with 72 additions and 8 deletions

View File

@ -49,6 +49,8 @@ import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.Jsonizable; import com.google.refine.Jsonizable;
import com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.RefineServlet; import com.google.refine.RefineServlet;
@ -113,7 +115,9 @@ public class History implements Jsonizable {
} }
protected long _projectID; protected long _projectID;
@JsonProperty("past")
protected List<HistoryEntry> _pastEntries; // done changes, can be undone protected List<HistoryEntry> _pastEntries; // done changes, can be undone
@JsonProperty("future")
protected List<HistoryEntry> _futureEntries; // undone changes, can be redone protected List<HistoryEntry> _futureEntries; // undone changes, can be redone
public History(Project project) { public History(Project project) {

View File

@ -44,11 +44,16 @@ import org.json.JSONWriter;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import com.google.refine.Jsonizable; import com.google.refine.Jsonizable;
import com.google.refine.ProjectManager; import com.google.refine.ProjectManager;
import com.google.refine.model.AbstractOperation; import com.google.refine.model.AbstractOperation;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.operations.OperationRegistry; import com.google.refine.operations.OperationRegistry;
import com.google.refine.util.JsonViews;
import com.google.refine.util.ParsingUtilities; import com.google.refine.util.ParsingUtilities;
/** /**
@ -57,18 +62,26 @@ import com.google.refine.util.ParsingUtilities;
*/ */
public class HistoryEntry implements Jsonizable { public class HistoryEntry implements Jsonizable {
final static Logger logger = LoggerFactory.getLogger("HistoryEntry"); final static Logger logger = LoggerFactory.getLogger("HistoryEntry");
@JsonProperty("id")
final public long id; final public long id;
@JsonIgnore
final public long projectID; final public long projectID;
@JsonProperty("description")
final public String description; final public String description;
@JsonProperty("time")
final public OffsetDateTime time; final public OffsetDateTime time;
// the manager (deals with IO systems or databases etc.) // the manager (deals with IO systems or databases etc.)
@JsonIgnore
final public HistoryEntryManager _manager; final public HistoryEntryManager _manager;
// the abstract operation, if any, that results in the change // the abstract operation, if any, that results in the change
@JsonProperty("operation")
@JsonView(JsonViews.SaveMode.class)
final public AbstractOperation operation; final public AbstractOperation operation;
// the actual change, loaded on demand // the actual change, loaded on demand
@JsonIgnore
private transient Change _change; private transient Change _change;
private final static String OPERATION = "operation"; private final static String OPERATION = "operation";
@ -77,6 +90,7 @@ public class HistoryEntry implements Jsonizable {
this._change = _change; this._change = _change;
} }
@JsonIgnore
public Change getChange() { public Change getChange() {
return _change; return _change;
} }

View File

@ -38,6 +38,9 @@ import java.util.Properties;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONWriter; import org.json.JSONWriter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.refine.model.Project; import com.google.refine.model.Project;
import com.google.refine.process.Process; import com.google.refine.process.Process;
import com.google.refine.process.ProcessManager; import com.google.refine.process.ProcessManager;
@ -67,6 +70,12 @@ public class HistoryProcess extends Process {
} }
} }
@Override
@JsonIgnore
public long getId() {
return super.getId();
}
@Override @Override
public void cancel() { public void cancel() {
throw new RuntimeException(WARN); throw new RuntimeException(WARN);
@ -97,9 +106,19 @@ public class HistoryProcess extends Process {
writer.object(); writer.object();
writer.key("description"); writer.value(_description); writer.key("description"); writer.value(_description);
writer.key("immediate"); writer.value(true); writer.key("immediate"); writer.value(true);
writer.key("status"); writer.value(_done ? "done" : "pending"); writer.key("status"); writer.value(getStatus());
writer.endObject(); writer.endObject();
} }
@JsonProperty("status")
public String getStatus() {
return _done ? "done" : "pending";
}
@JsonProperty("description")
public String getDescription() {
return _description;
}
@Override @Override
public boolean isDone() { public boolean isDone() {

View File

@ -72,6 +72,7 @@ public class ParsingUtilities {
SimpleModule module = new SimpleModule(); SimpleModule module = new SimpleModule();
module.addSerializer(Double.class, new SerializationFilters.DoubleSerializer()); module.addSerializer(Double.class, new SerializationFilters.DoubleSerializer());
module.addSerializer(double.class, new SerializationFilters.DoubleSerializer()); module.addSerializer(double.class, new SerializationFilters.DoubleSerializer());
module.addSerializer(OffsetDateTime.class, new SerializationFilters.DateSerializer());
mapper.registerModule(module); mapper.registerModule(module);
} }

View File

@ -1,6 +1,7 @@
package com.google.refine.util; package com.google.refine.util;
import java.io.IOException; import java.io.IOException;
import java.time.OffsetDateTime;
import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider; import com.fasterxml.jackson.databind.SerializerProvider;
@ -13,6 +14,10 @@ import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.google.refine.model.Recon; import com.google.refine.model.Recon;
import com.google.refine.model.Recon.Judgment; import com.google.refine.model.Recon.Judgment;
/**
* Series of classes which configure JSON serialization at application level.
* @author Antonin Delpeuch
*/
public class SerializationFilters { public class SerializationFilters {
static class BaseFilter extends SimpleBeanPropertyFilter { static class BaseFilter extends SimpleBeanPropertyFilter {
@Override @Override
@ -37,6 +42,11 @@ public class SerializationFilters {
} }
public static PropertyFilter noFilter = new BaseFilter(); public static PropertyFilter noFilter = new BaseFilter();
/**
* Filter out reconciliation candidates when rendering a matched recon
* in view mode. (In save mode, render them all the time.)
*/
public static PropertyFilter reconCandidateFilter = new BaseFilter() { public static PropertyFilter reconCandidateFilter = new BaseFilter() {
@Override @Override
public void serializeAsField(Object obj, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) public void serializeAsField(Object obj, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer)
@ -55,7 +65,10 @@ public class SerializationFilters {
} }
} }
}; };
/**
* Serialize double values as integers if they happen to round to an integer.
*/
public static class DoubleSerializer extends StdSerializer<Double> { public static class DoubleSerializer extends StdSerializer<Double> {
private static final long serialVersionUID = 132345L; private static final long serialVersionUID = 132345L;
@ -73,4 +86,21 @@ public class SerializationFilters {
} }
} }
} }
/**
* Serialize dates by ISO format.
*/
public static class DateSerializer extends StdSerializer<OffsetDateTime> {
private static final long serialVersionUID = 93872874L;
public DateSerializer() {
super(OffsetDateTime.class);
}
@Override
public void serialize(OffsetDateTime arg0, JsonGenerator gen, SerializerProvider s)
throws IOException {
gen.writeString(ParsingUtilities.dateToString(arg0));
}
}
} }

View File

@ -2,8 +2,6 @@ package com.google.refine.tests.history;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import java.util.Properties;
import org.testng.annotations.BeforeMethod; import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest; import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -58,9 +56,7 @@ public class HistoryEntryTests extends RefineTest {
+ "\"time\":\"2018-08-07T09:06:37Z\"}"; + "\"time\":\"2018-08-07T09:06:37Z\"}";
HistoryEntry historyEntry = HistoryEntry.load(project, json); HistoryEntry historyEntry = HistoryEntry.load(project, json);
TestUtils.isSerializedTo(historyEntry, jsonSimple); TestUtils.isSerializedTo(historyEntry, jsonSimple, false);
Properties options = new Properties(); TestUtils.isSerializedTo(historyEntry, json, true);
options.setProperty("mode", "save");
TestUtils.isSerializedTo(historyEntry, json, options);
} }
} }