Synchronized setting of embedType and typeInfo for GraphSON. Deprecated embedType - users should prefer use of typeInfo setting as it allows for future flexibility.
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/51c78223 Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/51c78223 Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/51c78223 Branch: refs/heads/graphson-2.0 Commit: 51c7822357b4b0917229a8e1e6f6a3b600a92c2c Parents: 6f9e60a Author: Stephen Mallette <sp...@genoprime.com> Authored: Tue Jul 12 07:31:24 2016 -0400 Committer: Stephen Mallette <sp...@genoprime.com> Committed: Tue Jul 12 07:31:24 2016 -0400 ---------------------------------------------------------------------- .../structure/io/graphson/GraphSONMapper.java | 19 +++++++++-- .../GraphSONMapperEmbeddedTypeTest.java | 36 ++++++++++++++++++++ ...aphSONMapperV2d0PartialEmbeddedTypeTest.java | 34 ++++++++++++++++++ 3 files changed, 87 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/51c78223/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java index d74a852..e099797 100644 --- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java +++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapper.java @@ -217,20 +217,35 @@ public class GraphSONMapper implements Mapper<ObjectMapper> { } /** - * Embeds Java types into generated JSON to clarify their origins. + * Embeds Java types into generated JSON to clarify their origins. Setting this value will override the value + * of {@link #typeInfo(TypeInfo)} where true will set it to {@link TypeInfo#PARTIAL_TYPES} and false will set + * it to {@link TypeInfo#NO_TYPES}. + * + * @deprecated As of release 3.2.1, replaced by {@link #typeInfo(TypeInfo)}. */ + @Deprecated public Builder embedTypes(final boolean embedTypes) { this.embedTypes = embedTypes; + this.typeInfo = embedTypes ? TypeInfo.PARTIAL_TYPES : TypeInfo.NO_TYPES; return this; } /** - * Specify if the values are going to be typed or not, and at which level. + * Specify if the values are going to be typed or not, and at which level. Setting this value will override + * the value of {@link #embedTypes(boolean)} where {@link TypeInfo#PARTIAL_TYPES} will set it to true and + * {@link TypeInfo#NO_TYPES} will set it to false. * * The level can be NO_TYPES or PARTIAL_TYPES, and could be extended in the future. */ public Builder typeInfo(final TypeInfo typeInfo) { this.typeInfo = typeInfo; + if (typeInfo.equals(TypeInfo.PARTIAL_TYPES)) + this.embedTypes = true; + else if (typeInfo.equals(TypeInfo.NO_TYPES)) + this.embedTypes = false; + else + throw new IllegalArgumentException("This value can only be set to PARTIAL_TYPES and NO_TYPES"); + return this; } http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/51c78223/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 1bae35d..f0bdaa8 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 @@ -40,6 +40,8 @@ import java.time.YearMonth; import java.time.ZoneOffset; import java.time.ZonedDateTime; import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; import static org.junit.Assert.assertEquals; @@ -135,6 +137,40 @@ public class GraphSONMapperEmbeddedTypeTest { assertEquals(o, serializeDeserialize(o, ZoneOffset.class)); } + @Test + public void shouldHandleMapWithTypesUsingEmbedTypeSetting() throws Exception { + final ObjectMapper mapper = GraphSONMapper.build() + .version(GraphSONVersion.V1_0) + .typeInfo(GraphSONMapper.TypeInfo.PARTIAL_TYPES) + .create() + .createMapper(); + + final Map<String,Object> m = new HashMap<>(); + m.put("test", 100L); + + final String json = mapper.writeValueAsString(m); + final Map read = mapper.readValue(json, HashMap.class); + + assertEquals(100L, read.get("test")); + } + + @Test + public void shouldNotHandleMapWithTypesUsingEmbedTypeSetting() throws Exception { + final ObjectMapper mapper = GraphSONMapper.build() + .version(GraphSONVersion.V1_0) + .typeInfo(GraphSONMapper.TypeInfo.NO_TYPES) + .create() + .createMapper(); + + final Map<String,Object> m = new HashMap<>(); + m.put("test", 100L); + + final String json = mapper.writeValueAsString(m); + final Map read = mapper.readValue(json, HashMap.class); + + assertEquals(100, read.get("test")); + } + public <T> T serializeDeserialize(final Object o, final Class<T> clazz) throws Exception { try (final ByteArrayOutputStream stream = new ByteArrayOutputStream()) { mapper.writeValue(stream, o); http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/51c78223/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java ---------------------------------------------------------------------- diff --git a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java index eceb157..385fa39 100644 --- a/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java +++ b/gremlin-core/src/test/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONMapperV2d0PartialEmbeddedTypeTest.java @@ -230,6 +230,40 @@ public class GraphSONMapperV2d0PartialEmbeddedTypeTest { } @Test + public void shouldHandleMapWithTypesUsingEmbedTypeSetting() throws Exception { + final ObjectMapper mapper = GraphSONMapper.build() + .version(GraphSONVersion.V2_0) + .embedTypes(true) + .create() + .createMapper(); + + final Map<String,Object> m = new HashMap<>(); + m.put("test", 100L); + + final String json = mapper.writeValueAsString(m); + final Map read = mapper.readValue(json, HashMap.class); + + assertEquals(100L, read.get("test")); + } + + @Test + public void shouldNotHandleMapWithTypesUsingEmbedTypeSetting() throws Exception { + final ObjectMapper mapper = GraphSONMapper.build() + .version(GraphSONVersion.V2_0) + .embedTypes(false) + .create() + .createMapper(); + + final Map<String,Object> m = new HashMap<>(); + m.put("test", 100L); + + final String json = mapper.writeValueAsString(m); + final Map read = mapper.readValue(json, HashMap.class); + + assertEquals(100, read.get("test")); + } + + @Test public void shouldLooseTypesInfoWithGraphSONNoType() throws Exception { ObjectMapper mapper = GraphSONMapper.build() .version(GraphSONVersion.V2_0)