TINKERPOP-2055 Added support for special Double values Includes Nan and the infinity values.
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/afc12bd2 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/afc12bd2 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/afc12bd2 Branch: refs/heads/TINKERPOP-2053 Commit: afc12bd27bc9c4c26b3ba2594c4c0810d5d76421 Parents: e7af98b Author: Stephen Mallette <sp...@genoprime.com> Authored: Tue Oct 2 15:35:53 2018 -0400 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Tue Oct 2 15:35:53 2018 -0400 ---------------------------------------------------------------------- CHANGELOG.asciidoc | 1 + .../io/graphson/GraphSONSerializersV2d0.java | 19 ++++++++++++++++--- .../graphson/GraphSONMapperEmbeddedTypeTest.java | 15 +++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/afc12bd2/CHANGELOG.asciidoc ---------------------------------------------------------------------- diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index e93c1c9..33d35d3 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -37,6 +37,7 @@ image::https://raw.githubusercontent.com/apache/tinkerpop/master/docs/static/ima * Added support for GraphSON serialization of `Date` in Javascript. * Added synchronized `Map` to Gryo 1.0 registrations. * Added `Triple` to Gryo 1.0 registrations. +* Added support for `Double.NaN`, `Double.POSITIVE_INFINITY` and `Double.NEGATIVE_INFINITY`. * Improved escaping of special characters in strings passed to the `GroovyTranslator`. * Added better internal processing of `Column` in `by(Function)`. * Added `hasNext()` support on `Traversal` for `gremlin-python`. http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/afc12bd2/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java index 2ddb37a..177e2d0 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializersV2d0.java @@ -677,7 +677,7 @@ class GraphSONSerializersV2d0 { } @Override - public Integer deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + public Integer deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { return jsonParser.getIntValue(); } @@ -694,8 +694,21 @@ class GraphSONSerializersV2d0 { } @Override - public Double deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { - return jsonParser.getDoubleValue(); + public Double deserialize(final JsonParser jsonParser, final DeserializationContext deserializationContext) throws IOException, JsonProcessingException { + if (jsonParser.getCurrentToken().isNumeric()) + return jsonParser.getDoubleValue(); + else { + final String numberText = jsonParser.getValueAsString(); + if ("NaN".equalsIgnoreCase(numberText)) + return Double.NaN; + else if ("-Infinity".equals(numberText) || "-INF".equalsIgnoreCase(numberText)) + return Double.NEGATIVE_INFINITY; + else if ("Infinity".equals(numberText) || "INF".equals(numberText)) + return Double.POSITIVE_INFINITY; + else + throw new IllegalStateException("Double value unexpected: " + numberText); + } + } @Override http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/afc12bd2/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java index 9079c8a..e5f2693 100644 --- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java +++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperEmbeddedTypeTest.java @@ -42,7 +42,9 @@ import java.time.Year; import java.time.YearMonth; import java.time.ZoneOffset; import java.time.ZonedDateTime; +import java.util.ArrayList; import java.util.Arrays; +import java.util.List; import static org.hamcrest.core.StringStartsWith.startsWith; import static org.junit.Assert.assertEquals; @@ -72,6 +74,19 @@ public class GraphSONMapperEmbeddedTypeTest extends AbstractGraphSONTest { public String version; @Test + public void shouldHandleNumberConstants() throws Exception { + assumeThat(version, startsWith("v2")); + + final List<Object> o = new ArrayList<>(); + o.add(123.321d); + o.add(Double.NaN); + o.add(Double.NEGATIVE_INFINITY); + o.add(Double.POSITIVE_INFINITY); + + assertEquals(o, serializeDeserialize(mapper, o, List.class)); + } + + @Test public void shouldHandleBiFunctionLambda() throws Exception { assumeThat(version, startsWith("v2"));