Jackson deserialization for RangeFacetConfig

This commit is contained in:
Antonin Delpeuch 2018-10-21 19:57:13 +01:00
parent 39498233fb
commit d8bc841dae
2 changed files with 44 additions and 8 deletions

View File

@ -35,6 +35,7 @@ package com.google.refine.browsing.facets;
import org.json.JSONObject; import org.json.JSONObject;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include; 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 protected boolean _selected; // false if we're certain that all rows will match
// and there isn't any filtering to do // 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 @Override
public void initializeFromJSON(JSONObject o) { public void initializeFromJSON(JSONObject o) {
_name = o.getString("name"); _name = o.getString("name");
@ -124,7 +157,7 @@ public class RangeFacet implements Facet {
return "range"; return "range";
} }
} }
RangeFacetConfig _config = new RangeFacetConfig(); RangeFacetConfig _config = null;
/* /*
* Derived configuration data * Derived configuration data

View File

@ -1,8 +1,12 @@
package com.google.refine.tests.browsing.facets; package com.google.refine.tests.browsing.facets;
import org.json.JSONObject; import java.io.IOException;
import org.testng.annotations.Test; 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.Engine;
import com.google.refine.browsing.facets.RangeFacet; import com.google.refine.browsing.facets.RangeFacet;
import com.google.refine.browsing.facets.RangeFacet.RangeFacetConfig; 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.model.Project;
import com.google.refine.tests.RefineTest; import com.google.refine.tests.RefineTest;
import com.google.refine.tests.util.TestUtils; import com.google.refine.tests.util.TestUtils;
import com.google.refine.util.ParsingUtilities;
public class RangeFacetTests extends RefineTest { public class RangeFacetTests extends RefineTest {
public static String configJson = "{\n" + public static String configJson = "{\n" +
@ -46,14 +51,13 @@ public class RangeFacetTests extends RefineTest {
+ "\"errorCount\":0}"; + "\"errorCount\":0}";
@Test @Test
public void serializeRangeFacetConfig() { public void serializeRangeFacetConfig() throws JsonParseException, JsonMappingException, IOException {
RangeFacetConfig config = new RangeFacetConfig(); RangeFacetConfig config = ParsingUtilities.mapper.readValue(configJson, RangeFacetConfig.class);
config.initializeFromJSON(new JSONObject(configJson));
TestUtils.isSerializedTo(config, configJson); TestUtils.isSerializedTo(config, configJson);
} }
@Test @Test
public void serializeRangeFacet() { public void serializeRangeFacet() throws JsonParseException, JsonMappingException, IOException {
Project project = createCSVProject("my column\n" Project project = createCSVProject("my column\n"
+ "89.2\n" + "89.2\n"
+ "-45.9\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(1).cells.set(0, new Cell(-45.9, null));
project.rows.get(3).cells.set(0, new Cell(0.4, null)); project.rows.get(3).cells.set(0, new Cell(0.4, null));
Engine engine = new Engine(project); Engine engine = new Engine(project);
RangeFacetConfig config = new RangeFacetConfig(); RangeFacetConfig config = ParsingUtilities.mapper.readValue(configJson, RangeFacetConfig.class);
config.initializeFromJSON(new JSONObject(configJson));
RangeFacet facet = config.apply(project); RangeFacet facet = config.apply(project);
facet.computeChoices(project, engine.getAllFilteredRows()); facet.computeChoices(project, engine.getAllFilteredRows());
TestUtils.isSerializedTo(facet, facetJson); TestUtils.isSerializedTo(facet, facetJson);