[ 
https://issues.apache.org/jira/browse/TINKERPOP3-978?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15032440#comment-15032440
 ] 

ASF GitHub Bot commented on TINKERPOP3-978:
-------------------------------------------

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

    https://github.com/apache/incubator-tinkerpop/pull/147#discussion_r46202284
  
    --- 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 --
    
    oops - yes @pluradj - good find - will fix before merge.


> Native TinkerGraph Serializers for GraphSON
> -------------------------------------------
>
>                 Key: TINKERPOP3-978
>                 URL: https://issues.apache.org/jira/browse/TINKERPOP3-978
>             Project: TinkerPop 3
>          Issue Type: Improvement
>          Components: io, tinkergraph
>    Affects Versions: 3.0.2-incubating
>            Reporter: stephen mallette
>            Assignee: stephen mallette
>            Priority: Minor
>             Fix For: 3.1.1-incubating
>
>
> TinkerGraph supports native serialization to Gryo, meaning that you can pass 
> a TinkerGraph directly to a Kryo object and have it serialize it - which 
> means that it can be treated as a return object from Gremlin Server (which is 
> nice for subgraphs).  
> Add the same capability for GraphSON so that non-jvm languages can take 
> advantage of that.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to