diff --git a/main/src/com/google/refine/model/ReconCandidate.java b/main/src/com/google/refine/model/ReconCandidate.java index b25b0354c..7c3f4c9d0 100644 --- a/main/src/com/google/refine/model/ReconCandidate.java +++ b/main/src/com/google/refine/model/ReconCandidate.java @@ -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)) { diff --git a/main/src/com/google/refine/util/ParsingUtilities.java b/main/src/com/google/refine/util/ParsingUtilities.java index 1cb62bc47..3404b79af 100644 --- a/main/src/com/google/refine/util/ParsingUtilities.java +++ b/main/src/com/google/refine/util/ParsingUtilities.java @@ -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) { diff --git a/main/src/com/google/refine/util/SerializationFilters.java b/main/src/com/google/refine/util/SerializationFilters.java index 606d463d6..c8752c22f 100644 --- a/main/src/com/google/refine/util/SerializationFilters.java +++ b/main/src/com/google/refine/util/SerializationFilters.java @@ -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 { + 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); + } + } + } }