Cleaner double serialization emulation, application wide.

This commit is contained in:
Antonin Delpeuch 2018-09-27 18:52:50 +01:00
parent 1fa101c334
commit 761b748faa
3 changed files with 34 additions and 13 deletions

View File

@ -37,13 +37,14 @@ import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.json.JSONException;
import org.json.JSONWriter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import org.json.JSONException;
import org.json.JSONWriter;
import com.google.refine.Jsonizable;
import com.google.refine.expr.HasFields;
@ -55,7 +56,7 @@ public class ReconCandidate implements HasFields, Jsonizable {
final public String name;
@JsonProperty("types")
final public String[] types;
@JsonIgnore
@JsonProperty("score")
final public double score;
public ReconCandidate(String topicID, String topicName, String[] typeIDs, double score) {
@ -65,15 +66,6 @@ public class ReconCandidate implements HasFields, Jsonizable {
this.score = score;
}
// Serialize doubles that are ints without trailing ".0" for consistency with previous serialization.
@JsonProperty("score")
public Object getJsonScore() {
if ((double)(int)score == score) {
return (int)score;
}
return score;
}
@Override
public Object getField(String name, Properties bindings) {
if ("id".equals(name)) {

View File

@ -61,12 +61,20 @@ import org.json.JSONTokener;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
public class ParsingUtilities {
public static final ObjectMapper mapper = new ObjectMapper();
static {
SimpleModule module = new SimpleModule();
module.addSerializer(Double.class, new SerializationFilters.DoubleSerializer());
module.addSerializer(double.class, new SerializationFilters.DoubleSerializer());
mapper.registerModule(module);
}
public static final FilterProvider defaultFilters = new SimpleFilterProvider()
.addFilter("reconCandidateFilter", SerializationFilters.reconCandidateFilter);
public static final FilterProvider saveFilters = new SimpleFilterProvider()
@ -74,7 +82,7 @@ public class ParsingUtilities {
public static final ObjectWriter saveWriter = mapper.writerWithView(JsonViews.SaveMode.class).with(saveFilters);
public static final ObjectWriter defaultWriter = mapper.writerWithView(JsonViews.NonSaveMode.class).with(defaultFilters);
public static final DateTimeFormatter ISO8601 = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss'Z'");
static public Properties parseUrlParameters(HttpServletRequest request) {

View File

@ -1,11 +1,14 @@
package com.google.refine.util;
import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;
import com.google.refine.model.Recon;
import com.google.refine.model.Recon.Judgment;
@ -52,4 +55,22 @@ public class SerializationFilters {
}
}
};
public static class DoubleSerializer extends StdSerializer<Double> {
private static final long serialVersionUID = 132345L;
public DoubleSerializer() {
super(Double.class);
}
@Override
public void serialize(Double arg0, JsonGenerator gen, SerializerProvider s)
throws IOException {
if (new Double(arg0.intValue()).equals(arg0)) {
gen.writeNumber(arg0.intValue());
} else {
gen.writeNumber(arg0);
}
}
}
}