tinkerpop-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From spmalle...@apache.org
Subject [02/14] incubator-tinkerpop git commit: TINKERPOP-732 added support for tree() results to be serialized over GraphSON
Date Fri, 25 Mar 2016 11:55:46 GMT
TINKERPOP-732 added support for tree() results to be serialized over GraphSON


Project: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/commit/c01f1970
Tree: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/tree/c01f1970
Diff: http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/diff/c01f1970

Branch: refs/heads/master
Commit: c01f19708a56452c368919133bee38c0e00bd3a2
Parents: e8b6f8e
Author: Dylan Millikin <dmill@apache.org>
Authored: Wed Mar 16 12:32:44 2016 -0400
Committer: Dylan Millikin <dmill@apache.org>
Committed: Wed Mar 16 12:32:44 2016 -0400

----------------------------------------------------------------------
 .../structure/io/graphson/GraphSONModule.java   |  2 +
 .../io/graphson/GraphSONSerializers.java        | 38 ++++++++++++++
 gremlin-shaded/pom.xml                          |  2 +-
 .../gremlin/structure/SerializationTest.java    | 53 ++++++++++++++++++++
 4 files changed, 94 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c01f1970/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
index 821a5dd..075c73b 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONModule.java
@@ -42,6 +42,7 @@ import java.time.YearMonth;
 import java.time.ZoneOffset;
 import java.time.ZonedDateTime;
 import java.util.Map;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
 
 /**
  * The set of serializers that handle the core graph interfaces.  These serializers support
normalization which
@@ -77,6 +78,7 @@ abstract class GraphSONModule extends SimpleModule {
             addSerializer(TraversalMetrics.class, new GraphSONSerializers.TraversalMetricsJacksonSerializer());
             addSerializer(TraversalExplanation.class, new GraphSONSerializers.TraversalExplanationJacksonSerializer());
             addSerializer(Path.class, new GraphSONSerializers.PathJacksonSerializer());
+            addSerializer(Tree.class, new GraphSONSerializers.TreeJacksonSerializer());
             addSerializer(StarGraphGraphSONSerializer.DirectionalStarGraph.class, new StarGraphGraphSONSerializer(normalize));
 
             // java.util

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c01f1970/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
----------------------------------------------------------------------
diff --git a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
index e21bb0e..0712cd1 100644
--- a/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
+++ b/gremlin-core/src/main/java/org/apache/tinkerpop/gremlin/structure/io/graphson/GraphSONSerializers.java
@@ -48,7 +48,10 @@ import java.util.HashMap;
 import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 import java.util.concurrent.TimeUnit;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
+import org.apache.tinkerpop.gremlin.structure.T;
 
 /**
  * GraphSON serializers for graph-based objects such as vertices, edges, properties, and
paths. These serializers
@@ -257,6 +260,41 @@ final class GraphSONSerializers {
         }
 
     }
+    
+    final static class TreeJacksonSerializer extends StdSerializer<Tree> {
+
+        public TreeJacksonSerializer() {
+            super(Tree.class);
+        }
+
+        @Override
+        public void serialize(final Tree tree, final JsonGenerator jsonGenerator, final SerializerProvider
serializerProvider) throws IOException, JsonGenerationException {
+            ser(tree, jsonGenerator, null);
+        }
+        
+        @Override
+        public void serializeWithType(final Tree tree, final JsonGenerator jsonGenerator,
+                                      final SerializerProvider serializerProvider, final
TypeSerializer typeSerializer)
+                throws IOException, JsonProcessingException {
+            ser(tree, jsonGenerator, typeSerializer);
+        }
+        
+        private static void ser(final Tree tree, final JsonGenerator jsonGenerator, final
TypeSerializer typeSerializer)
+                throws IOException {
+            jsonGenerator.writeStartObject(); // {
+            
+            Set<Map.Entry<Element, Tree<T>>> set = tree.entrySet();
+            for(Map.Entry<Element, Tree<T>> entry : set)
+            {
+                jsonGenerator.writeObjectFieldStart(entry.getKey().id().toString()); 
+                if (typeSerializer != null) jsonGenerator.writeStringField(GraphSONTokens.CLASS,
HashMap.class.getName()); 
+                jsonGenerator.writeObjectField(GraphSONTokens.KEY, entry.getKey()); 
+                jsonGenerator.writeObjectField(GraphSONTokens.VALUE, entry.getValue());
+                jsonGenerator.writeEndObject();
+            }
+            jsonGenerator.writeEndObject();
+        }
+    } 
 
     /**
      * Maps in the JVM can have {@link Object} as a key, but in JSON they must be a {@link
String}.

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c01f1970/gremlin-shaded/pom.xml
----------------------------------------------------------------------
diff --git a/gremlin-shaded/pom.xml b/gremlin-shaded/pom.xml
index a8d1d9b..56f7dd4 100644
--- a/gremlin-shaded/pom.xml
+++ b/gremlin-shaded/pom.xml
@@ -50,7 +50,7 @@ limitations under the License.
             <groupId>com.fasterxml.jackson.core</groupId>
             <artifactId>jackson-databind</artifactId>
             <!-- Update the shade plugin config whenever you change this version -->
-            <version>2.5.3</version>
+            <version>2.7.0</version>
             <optional>true</optional>
         </dependency>
     </dependencies>

http://git-wip-us.apache.org/repos/asf/incubator-tinkerpop/blob/c01f1970/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/SerializationTest.java
----------------------------------------------------------------------
diff --git a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/SerializationTest.java
b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/SerializationTest.java
index c7bf8b4..5fb8fb0 100644
--- a/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/SerializationTest.java
+++ b/gremlin-test/src/main/java/org/apache/tinkerpop/gremlin/structure/SerializationTest.java
@@ -46,6 +46,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.TimeUnit;
+import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
 
 import static org.junit.Assert.*;
 
@@ -349,5 +350,57 @@ public class SerializationTest {
             assertTrue(metrics0.containsKey(GraphSONTokens.COUNTS));
             assertTrue(metrics0.containsKey(GraphSONTokens.DURATION));
         }
+        
+        @Test
+        @LoadGraphWith(LoadGraphWith.GraphData.MODERN)
+        public void shouldSerializeTree() throws Exception {
+            final ObjectMapper mapper = graph.io(GraphSONIo.build()).mapper().create().createMapper();
+            final Tree t = g.V(1).out().properties("name").tree().next();
+            final String json = mapper.writeValueAsString(t);
+            
+            final HashMap<String, Object> m = (HashMap<String, Object>) mapper.readValue(json,
mapTypeReference);
+            
+            // Check Structure
+            assertEquals(1, m.size());
+            assertTrue(m.containsKey("1"));
+            
+            // Check Structure n+1
+            final HashMap<String, Object> branch = (HashMap<String, Object>)
m.get("1");
+            assertEquals(2, branch.size());
+            assertTrue(branch.containsKey(GraphSONTokens.KEY));
+            assertTrue(branch.containsKey(GraphSONTokens.VALUE));
+            
+            //Check n+1 key (traversed element)
+            final HashMap<String, Object> branchKey = (HashMap<String, Object>)
branch.get(GraphSONTokens.KEY);
+            assertTrue(branchKey.containsKey(GraphSONTokens.ID));
+            assertTrue(branchKey.containsKey(GraphSONTokens.LABEL));
+            assertTrue(branchKey.containsKey(GraphSONTokens.TYPE));
+            assertTrue(branchKey.containsKey(GraphSONTokens.PROPERTIES));
+            assertEquals(1, branchKey.get(GraphSONTokens.ID));
+            assertEquals("person", branchKey.get(GraphSONTokens.LABEL));
+            assertEquals("vertex", branchKey.get(GraphSONTokens.TYPE));
+            final HashMap<String, List<HashMap<String, Object>>> branchKeyProps
= (HashMap<String, List<HashMap<String, Object>>>) branchKey.get(GraphSONTokens.PROPERTIES);
+            assertEquals("marko", branchKeyProps.get("name").get(0).get("value"));
+            assertEquals(29, branchKeyProps.get("age").get(0).get("value"));
+            
+            //Check n+1 value (traversed element)
+            final HashMap<String, Object> branchValue = (HashMap<String, Object>)
branch.get(GraphSONTokens.VALUE);
+            assertEquals(3, branchValue.size());
+            assertTrue(branchValue.containsKey("2"));
+            assertTrue(branchValue.containsKey("3"));
+            assertTrue(branchValue.containsKey("4"));
+            
+            // Check that vp[] functioned properly
+            final HashMap<String, HashMap<String, Object>> branch2 = (HashMap<String,
HashMap<String, Object>>) branchValue.get("2");
+            assertTrue(branch2.containsKey(GraphSONTokens.KEY));
+            assertTrue(branch2.containsKey(GraphSONTokens.VALUE));
+            final HashMap<String, HashMap<String, Object>> branch2Prop = (HashMap<String,
HashMap<String, Object>>) branch2.get(GraphSONTokens.VALUE).get("2");
+            assertTrue(branch2Prop.get(GraphSONTokens.KEY).containsKey(GraphSONTokens.ID));
+            assertTrue(branch2Prop.get(GraphSONTokens.KEY).containsKey(GraphSONTokens.VALUE));
+            assertTrue(branch2Prop.get(GraphSONTokens.KEY).containsKey(GraphSONTokens.LABEL));
+            assertEquals("name", branch2Prop.get(GraphSONTokens.KEY).get(GraphSONTokens.LABEL));
+            assertEquals("vadas", branch2Prop.get(GraphSONTokens.KEY).get(GraphSONTokens.VALUE));
+            assertEquals(2, branch2Prop.get(GraphSONTokens.KEY).get(GraphSONTokens.ID));
+        }
     }
 }


Mime
View raw message