Github user pluradj commented on a diff in the pull request:

    https://github.com/apache/incubator-tinkerpop/pull/147#discussion_r46198196
  
    --- Diff: 
tinkergraph-gremlin/src/main/java/org/apache/tinkerpop/gremlin/tinkergraph/structure/TinkerIoRegistry.java
 ---
    @@ -86,4 +113,145 @@ public TinkerGraph read(final Kryo kryo, final Input 
input, final Class<TinkerGr
                 return graph;
             }
         }
    +
    +    /**
    +     * Provides a method to serialize an entire {@link TinkerGraph} into 
itself for GraphSON.  This is useful when
    +     * shipping small graphs around through Gremlin Server.
    +     */
    +    final static class TinkerModule extends SimpleModule {
    +        public TinkerModule() {
    +            super("tinkergraph-1.0");
    +            addSerializer(TinkerGraph.class, new 
TinkerGraphJacksonSerializer());
    +            addDeserializer(TinkerGraph.class, new 
TinkerGraphJacksonDeserializer());
    +        }
    +    }
    +
    +    /**
    +     * Serializes the graph into an edge list format.  Edge list is a 
better choices than adjacency list (which is
    +     * typically standard from the {@link GraphReader} and {@link 
GraphWriter} perspective) in this case because
    +     * the use case for this isn't around massive graphs.  The use case is 
for "small" subgraphs that are being
    +     * shipped over the wire from Gremlin Server. Edge list is format is a 
bit easier for non-JVM languages to work
    +     * with as a format and doesn't require a cache for loading (as vertex 
labels are not serialized in adjacency
    +     * list).
    +     */
    +    final static class TinkerGraphJacksonSerializer extends 
StdSerializer<TinkerGraph> {
    +
    +        public TinkerGraphJacksonSerializer() {
    +            super(TinkerGraph.class);
    +        }
    +
    +        @Override
    +        public void serialize(final TinkerGraph graph, final JsonGenerator 
jsonGenerator, final SerializerProvider serializerProvider)
    +                throws IOException {
    +            jsonGenerator.writeStartObject();
    +
    +            jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
    +            jsonGenerator.writeStartArray();
    +
    +            final Iterator<Vertex> vertices = graph.vertices();
    +            while (vertices.hasNext()) {
    +                serializerProvider.defaultSerializeValue(vertices.next(), 
jsonGenerator);
    +            }
    +
    +            jsonGenerator.writeEndArray();
    +
    +            jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
    +            jsonGenerator.writeStartArray();
    +
    +            final Iterator<Edge> edges = graph.edges();
    +            while (edges.hasNext()) {
    +                serializerProvider.defaultSerializeValue(edges.next(), 
jsonGenerator);
    +            }
    +
    +            jsonGenerator.writeEndArray();
    +
    +            jsonGenerator.writeEndObject();
    +        }
    +
    +        @Override
    +        public void serializeWithType(final TinkerGraph graph, final 
JsonGenerator jsonGenerator,
    +                                      final SerializerProvider 
serializerProvider, final TypeSerializer typeSerializer) throws IOException {
    +            jsonGenerator.writeStartObject();
    +            jsonGenerator.writeStringField(GraphSONTokens.CLASS, 
TinkerGraph.class.getName());
    +
    +            jsonGenerator.writeFieldName(GraphSONTokens.VERTICES);
    +            jsonGenerator.writeStartArray();
    +            jsonGenerator.writeString(ArrayList.class.getName());
    +            jsonGenerator.writeStartArray();
    +
    +            final Iterator<Vertex> vertices = graph.vertices();
    +            while (vertices.hasNext()) {
    +                GraphSONUtil.writeWithType(vertices.next(), jsonGenerator, 
serializerProvider, typeSerializer);
    +            }
    +
    +            jsonGenerator.writeEndArray();
    +            jsonGenerator.writeEndArray();
    +
    +            jsonGenerator.writeFieldName(GraphSONTokens.EDGES);
    +            jsonGenerator.writeStartArray();
    +            jsonGenerator.writeString(ArrayList.class.getName());
    +            jsonGenerator.writeStartArray();
    +
    +            final Iterator<Edge> edges = graph.edges();
    +            while (edges.hasNext()) {
    +                GraphSONUtil.writeWithType(edges.next(), jsonGenerator, 
serializerProvider, typeSerializer);
    +            }
    +
    +            jsonGenerator.writeEndArray();
    +            jsonGenerator.writeEndArray();
    +
    +            jsonGenerator.writeEndObject();
    +        }
    +    }
    +
    +    /**
    +     * Deserializes the edge list format.
    +     */
    +    static class TinkerGraphJacksonDeserializer extends 
StdDeserializer<TinkerGraph> {
    +        public TinkerGraphJacksonDeserializer() {
    +            super(TinkerGraph.class);
    +        }
    +
    +        @Override
    +        public TinkerGraph deserialize(JsonParser jsonParser, 
DeserializationContext deserializationContext) throws IOException, 
JsonProcessingException {
    +            final TinkerGraph graph = TinkerGraph.open();
    +
    +            final List<Map<String, Object>> edges;
    +            final List<Map<String, Object>> vertices;
    +            if (!jsonParser.getCurrentToken().isStructStart()) {
    +                if 
(!jsonParser.getCurrentName().equals(GraphSONTokens.VERTICES))
    +                    throw new IOException(String.format("Expected a '%s' 
key", GraphSONTokens.VERTICES));
    +
    +                jsonParser.nextToken();
    +                vertices = (List<Map<String, Object>>) 
deserializationContext.readValue(jsonParser, ArrayList.class);
    +                jsonParser.nextToken();
    +
    +                if 
(!jsonParser.getCurrentName().equals(GraphSONTokens.EDGES))
    +                    throw new IOException(String.format("Expected a '%s' 
key", GraphSONTokens.EDGES));
    +
    +                jsonParser.nextToken();
    +                edges = (List<Map<String, Object>>) 
deserializationContext.readValue(jsonParser, ArrayList.class);
    +            } else {
    +                final Map<String, Object> graphData = 
deserializationContext.readValue(jsonParser, HashMap.class);
    +                vertices = (List<Map<String,Object>>) 
graphData.get("vertices");
    +                edges = (List<Map<String,Object>>) graphData.get("edges");
    --- End diff --
    
    nit... `GraphSONTokens.VERTICES` and `GraphSONTokens.EDGES` here


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to