From 62bcc80dcc161bc8f18f55277f32fcf2d58217c5 Mon Sep 17 00:00:00 2001 From: Owen Stephens Date: Thu, 4 Apr 2019 11:41:35 +0100 Subject: [PATCH 1/5] Add tests for serialisation of Booleans and Numbers in cells --- .../com/google/refine/tests/model/CellTests.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/main/tests/server/src/com/google/refine/tests/model/CellTests.java b/main/tests/server/src/com/google/refine/tests/model/CellTests.java index 3872e68bc..d8cf868cb 100644 --- a/main/tests/server/src/com/google/refine/tests/model/CellTests.java +++ b/main/tests/server/src/com/google/refine/tests/model/CellTests.java @@ -95,4 +95,18 @@ public class CellTests { String json = "{\"v\":\"2018-03-04T08:09:10Z\",\"t\":\"date\"}"; TestUtils.isSerializedTo(Cell.loadStreaming(json, pool), json); } + + @Test + public void serializeNumberCell() throws Exception { + String json = "{\"v\": 1}"; + Cell c = Cell.loadStreaming(json, pool); + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeBooleanCell() throws Exception { + String json = "{\"v\": true}"; + Cell c = Cell.loadStreaming(json, pool); + TestUtils.isSerializedTo(c, json); + } } From a881dc80aaf1faacd98d50c727608282d3379a74 Mon Sep 17 00:00:00 2001 From: Owen Stephens Date: Thu, 4 Apr 2019 11:42:01 +0100 Subject: [PATCH 2/5] Don't write Booleans and Numbers as strings in Cell serialisation --- main/src/com/google/refine/model/Cell.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/main/src/com/google/refine/model/Cell.java b/main/src/com/google/refine/model/Cell.java index d6767ff50..bcab93a2c 100644 --- a/main/src/com/google/refine/model/Cell.java +++ b/main/src/com/google/refine/model/Cell.java @@ -102,7 +102,7 @@ public class Cell implements HasFields { @JsonProperty("v") @JsonInclude(Include.NON_NULL) - public String getValueAsString() { + public Object getValue() { if (value != null && !ExpressionUtils.isError(value)) { Instant instant = null; if (value instanceof OffsetDateTime) { @@ -119,8 +119,9 @@ public class Cell implements HasFields { return ((Double)value).toString(); } else if (value instanceof Float && (((Float)value).isNaN() || ((Float)value).isInfinite())) { - // TODO: Skip? Write as string? return ((Float)value).toString(); + } else if (value instanceof Boolean || value instanceof Number){ + return value; } else { return value.toString(); } From a79e0f31878d84cee3431aab7b91fc0e6ecb7629 Mon Sep 17 00:00:00 2001 From: Owen Stephens Date: Thu, 4 Apr 2019 15:09:56 +0100 Subject: [PATCH 3/5] Only compute recon features if cell contains a string --- .../src/org/openrefine/wikidata/editing/NewItemLibrary.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java b/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java index ba8c0ee03..5f0b0590e 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java @@ -134,7 +134,9 @@ public class NewItemLibrary { ReconConfig config = column.getReconConfig(); if (config instanceof StandardReconConfig) { StandardReconConfig stdConfig = (StandardReconConfig)config; - stdConfig.computeFeatures(recon, cell.getValueAsString()); + if (cell instanceof (String)) { + stdConfig.computeFeatures(recon, cell.getValue()); + } } } } From 318bb541859c9af22610816a207155745d27a13e Mon Sep 17 00:00:00 2001 From: Owen Stephens Date: Thu, 4 Apr 2019 19:09:05 +0100 Subject: [PATCH 4/5] Use getValue when getting value of a cell --- .../src/org/openrefine/wikidata/editing/NewItemLibrary.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java b/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java index 5f0b0590e..534beafea 100644 --- a/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java +++ b/extensions/wikidata/src/org/openrefine/wikidata/editing/NewItemLibrary.java @@ -134,8 +134,8 @@ public class NewItemLibrary { ReconConfig config = column.getReconConfig(); if (config instanceof StandardReconConfig) { StandardReconConfig stdConfig = (StandardReconConfig)config; - if (cell instanceof (String)) { - stdConfig.computeFeatures(recon, cell.getValue()); + if (cell.getValue() instanceof String) { + stdConfig.computeFeatures(recon, (String) cell.getValue()); } } } From 81a82499b52f70f4ad699c83eaaacae66d2533e4 Mon Sep 17 00:00:00 2001 From: Owen Stephens Date: Thu, 4 Apr 2019 23:00:56 +0100 Subject: [PATCH 5/5] Added tests for serializing cells containing dates and numbers --- .../google/refine/tests/model/CellTests.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/main/tests/server/src/com/google/refine/tests/model/CellTests.java b/main/tests/server/src/com/google/refine/tests/model/CellTests.java index d8cf868cb..94391d1bb 100644 --- a/main/tests/server/src/com/google/refine/tests/model/CellTests.java +++ b/main/tests/server/src/com/google/refine/tests/model/CellTests.java @@ -26,6 +26,10 @@ ******************************************************************************/ package com.google.refine.tests.model; +import java.time.format.DateTimeFormatter; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; + import static org.junit.Assert.assertNull; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -109,4 +113,52 @@ public class CellTests { Cell c = Cell.loadStreaming(json, pool); TestUtils.isSerializedTo(c, json); } + + @Test + public void serializeDatewithOffset() throws Exception { + OffsetDateTime dateTimeValue = OffsetDateTime.parse("2017-05-12T05:45:00+01:00", DateTimeFormatter.ISO_OFFSET_DATE_TIME); + Cell c = new Cell(dateTimeValue, null); + String json = "{\"v\":\"2017-05-12T04:45:00Z\",\"t\":\"date\"}"; + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeLocalDate() throws Exception { + LocalDateTime dateTimeValue = LocalDateTime.of(2017,5,12,0,0,0); + Cell c = new Cell(dateTimeValue, null); + String json = "{\"v\":\"2017-05-12T00:00:00Z\",\"t\":\"date\"}"; + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeDoubleNan() throws Exception { + double dn = Double.NaN; + Cell c = new Cell(dn, null); + String json = "{\"v\":\"NaN\"}"; + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeFloatNan() throws Exception { + Float fn = Float.NaN; + Cell c = new Cell(fn, null); + String json = "{\"v\":\"NaN\"}"; + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeDoubleInfinity() throws Exception { + double di = Double.POSITIVE_INFINITY; + Cell c = new Cell(di, null); + String json = "{\"v\":\"Infinity\"}"; + TestUtils.isSerializedTo(c, json); + } + + @Test + public void serializeFloatInfinity() throws Exception { + Float fi = Float.POSITIVE_INFINITY; + Cell c = new Cell(fi, null); + String json = "{\"v\":\"Infinity\"}"; + TestUtils.isSerializedTo(c, json); + } }