From d8bc841daed5f1d2d7425abeb216608eec9281af Mon Sep 17 00:00:00 2001 From: Antonin Delpeuch Date: Sun, 21 Oct 2018 19:57:13 +0100 Subject: [PATCH] Jackson deserialization for RangeFacetConfig --- .../refine/browsing/facets/RangeFacet.java | 35 ++++++++++++++++++- .../browsing/facets/RangeFacetTests.java | 17 +++++---- 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/main/src/com/google/refine/browsing/facets/RangeFacet.java b/main/src/com/google/refine/browsing/facets/RangeFacet.java index ce0c68161..0915c4f2b 100644 --- a/main/src/com/google/refine/browsing/facets/RangeFacet.java +++ b/main/src/com/google/refine/browsing/facets/RangeFacet.java @@ -35,6 +35,7 @@ package com.google.refine.browsing.facets; import org.json.JSONObject; +import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude.Include; @@ -92,6 +93,38 @@ public class RangeFacet implements Facet { protected boolean _selected; // false if we're certain that all rows will match // and there isn't any filtering to do + @JsonCreator + public RangeFacetConfig( + @JsonProperty("name") + String name, + @JsonProperty("expression") + String expression, + @JsonProperty("columnName") + String columnName, + @JsonProperty(FROM) + Double from, + @JsonProperty(TO) + Double to, + @JsonProperty("selectNumeric") + Boolean selectNumeric, + @JsonProperty("selectNonNumeric") + Boolean selectNonNumeric, + @JsonProperty("selectBlank") + Boolean selectBlank, + @JsonProperty("selectError") + Boolean selectError) { + _name = name; + _expression = expression; + _columnName = columnName; + _from = from == null ? 0 : from; + _to = to == null ? 0 : to; + _selectNumeric = selectNumeric == null ? true : selectNumeric; + _selectNonNumeric = selectNonNumeric == null ? true : selectNonNumeric; + _selectBlank = selectBlank == null ? true : selectBlank; + _selectError = selectError == null ? true : selectError; + _selected = !_selectNumeric || !_selectNonNumeric || !_selectBlank || !_selectError || from != null || to != null; + } + @Override public void initializeFromJSON(JSONObject o) { _name = o.getString("name"); @@ -124,7 +157,7 @@ public class RangeFacet implements Facet { return "range"; } } - RangeFacetConfig _config = new RangeFacetConfig(); + RangeFacetConfig _config = null; /* * Derived configuration data diff --git a/main/tests/server/src/com/google/refine/tests/browsing/facets/RangeFacetTests.java b/main/tests/server/src/com/google/refine/tests/browsing/facets/RangeFacetTests.java index 6ad39158d..21374439d 100644 --- a/main/tests/server/src/com/google/refine/tests/browsing/facets/RangeFacetTests.java +++ b/main/tests/server/src/com/google/refine/tests/browsing/facets/RangeFacetTests.java @@ -1,8 +1,12 @@ package com.google.refine.tests.browsing.facets; -import org.json.JSONObject; +import java.io.IOException; + import org.testng.annotations.Test; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.databind.JsonMappingException; + import com.google.refine.browsing.Engine; import com.google.refine.browsing.facets.RangeFacet; import com.google.refine.browsing.facets.RangeFacet.RangeFacetConfig; @@ -10,6 +14,7 @@ import com.google.refine.model.Cell; import com.google.refine.model.Project; import com.google.refine.tests.RefineTest; import com.google.refine.tests.util.TestUtils; +import com.google.refine.util.ParsingUtilities; public class RangeFacetTests extends RefineTest { public static String configJson = "{\n" + @@ -46,14 +51,13 @@ public class RangeFacetTests extends RefineTest { + "\"errorCount\":0}"; @Test - public void serializeRangeFacetConfig() { - RangeFacetConfig config = new RangeFacetConfig(); - config.initializeFromJSON(new JSONObject(configJson)); + public void serializeRangeFacetConfig() throws JsonParseException, JsonMappingException, IOException { + RangeFacetConfig config = ParsingUtilities.mapper.readValue(configJson, RangeFacetConfig.class); TestUtils.isSerializedTo(config, configJson); } @Test - public void serializeRangeFacet() { + public void serializeRangeFacet() throws JsonParseException, JsonMappingException, IOException { Project project = createCSVProject("my column\n" + "89.2\n" + "-45.9\n" @@ -63,8 +67,7 @@ public class RangeFacetTests extends RefineTest { project.rows.get(1).cells.set(0, new Cell(-45.9, null)); project.rows.get(3).cells.set(0, new Cell(0.4, null)); Engine engine = new Engine(project); - RangeFacetConfig config = new RangeFacetConfig(); - config.initializeFromJSON(new JSONObject(configJson)); + RangeFacetConfig config = ParsingUtilities.mapper.readValue(configJson, RangeFacetConfig.class); RangeFacet facet = config.apply(project); facet.computeChoices(project, engine.getAllFilteredRows()); TestUtils.isSerializedTo(facet, facetJson);