Jackson deserialization for HistoryEntry

This commit is contained in:
Antonin Delpeuch 2018-11-19 18:31:05 +00:00
parent 86d1159926
commit 0d14df1427
3 changed files with 41 additions and 17 deletions

View File

@ -33,18 +33,22 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
package com.google.refine.history;
import java.io.IOException;
import java.io.Writer;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.util.Properties;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JacksonInject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import com.fasterxml.jackson.databind.InjectableValues;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.refine.ProjectManager;
import com.google.refine.model.AbstractOperation;
import com.google.refine.model.Project;
@ -55,7 +59,7 @@ import com.google.refine.util.ParsingUtilities;
* This is the metadata of a Change. It's small, so we can load it in order to
* obtain information about a change without actually loading the change.
*/
public class HistoryEntry {
public class HistoryEntry {
final static Logger logger = LoggerFactory.getLogger("HistoryEntry");
@JsonProperty("id")
final public long id;
@ -93,6 +97,19 @@ public class HistoryEntry {
static public long allocateID() {
return Math.round(Math.random() * 1000000) + System.currentTimeMillis();
}
@JsonCreator
protected HistoryEntry(
@JsonProperty("id")
long id,
@JacksonInject("projectID")
long projectID,
@JsonProperty("description")
String description,
@JsonProperty(OPERATION)
AbstractOperation operation) {
this(id,projectID,description,operation,OffsetDateTime.now(ZoneId.of("Z")));
}
public HistoryEntry(long id, Project project, String description, AbstractOperation operation, Change change) {
this(id,project.id,description,operation,OffsetDateTime.now(ZoneId.of("Z")));
@ -152,21 +169,13 @@ public class HistoryEntry {
getChange().revert(project);
}
static public HistoryEntry load(Project project, String s) throws Exception {
JSONObject obj = ParsingUtilities.evaluateJsonStringToObject(s);
AbstractOperation operation = null;
if (obj.has(OPERATION) && !obj.isNull(OPERATION)) {
operation = ParsingUtilities.mapper.readValue(obj.getJSONObject(OPERATION).toString(), AbstractOperation.class);
}
return new HistoryEntry(
obj.getLong("id"),
project.id,
obj.getString("description"),
operation,
ParsingUtilities.stringToDate(obj.getString("time"))
);
static public HistoryEntry load(Project project, String s) throws IOException {
ObjectMapper mapper = ParsingUtilities.mapper.copy();
InjectableValues injection = new InjectableValues.Std()
.addValue("projectID", project.id);
mapper.setInjectableValues(injection);
return mapper.readValue(s, HistoryEntry.class);
}
public void delete(){

View File

@ -84,6 +84,7 @@ public class ParsingUtilities {
module.addSerializer(double.class, new SerializationFilters.DoubleSerializer());
module.addSerializer(OffsetDateTime.class, new SerializationFilters.OffsetDateSerializer());
module.addSerializer(LocalDateTime.class, new SerializationFilters.LocalDateSerializer());
module.addDeserializer(OffsetDateTime.class, new SerializationFilters.OffsetDateDeserializer());
module.addDeserializer(LocalDateTime.class, new SerializationFilters.LocalDateDeserializer());
mapper.registerModule(module);

View File

@ -125,6 +125,20 @@ public class SerializationFilters {
}
}
public static class OffsetDateDeserializer extends StdDeserializer<OffsetDateTime> {
private static final long serialVersionUID = 93872874L;
public OffsetDateDeserializer() {
super(OffsetDateTime.class);
}
@Override
public OffsetDateTime deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
return ParsingUtilities.stringToDate(p.getValueAsString());
}
}
public static class LocalDateDeserializer extends StdDeserializer<LocalDateTime> {
private static final long serialVersionUID = 93872874L;