You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by ok...@apache.org on 2016/10/12 21:10:00 UTC

tinkerpop git commit: updated the Gremlin-Python docs with notes on how to register serializers. Tweaked GraphSONUtil slightly as recommened by @aholmberg

Repository: tinkerpop
Updated Branches:
  refs/heads/master e70a509a3 -> e2131957e


updated the Gremlin-Python docs with notes on how to register serializers. Tweaked GraphSONUtil slightly as recommened by @aholmberg


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

Branch: refs/heads/master
Commit: e2131957e9be83987e8e2dedd6d65c32c8c7b17e
Parents: e70a509
Author: Marko A. Rodriguez <ok...@gmail.com>
Authored: Wed Oct 12 15:09:55 2016 -0600
Committer: Marko A. Rodriguez <ok...@gmail.com>
Committed: Wed Oct 12 15:09:55 2016 -0600

----------------------------------------------------------------------
 docs/src/reference/gremlin-variants.asciidoc    | 35 ++++++++++++++++++++
 .../gremlin_python/structure/io/graphson.py     |  5 ++-
 2 files changed, 39 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e2131957/docs/src/reference/gremlin-variants.asciidoc
----------------------------------------------------------------------
diff --git a/docs/src/reference/gremlin-variants.asciidoc b/docs/src/reference/gremlin-variants.asciidoc
index 4b65ab8..24c81b5 100644
--- a/docs/src/reference/gremlin-variants.asciidoc
+++ b/docs/src/reference/gremlin-variants.asciidoc
@@ -280,3 +280,38 @@ g.V().out().map(lambda: "x: len(x.get().value('name'))").sum().toList()
 <7> The default lambda language is changed back to Gremlin-Python.
 <8> If the `lambda`-prefix is not provided, then it is appended automatically in order to give a more natural look to the expression.
 
+Custom Serialization
+~~~~~~~~~~~~~~~~~~~~
+
+Gremlin-Python provides a GraphSON 2.0 serialization package with the standard Apache TinkerPop `g`-types registered
+(see link:http://tinkerpop.apache.org/docs/x.y.z/dev/io/#graphson-2d0[GraphSON 2.0]). It is possible for users to add
+new types by creating serializers and deserializers in Python and registering them with the `RemoteConnection`.
+
+[source,python]
+----
+class MyType(object):
+  GRAPHSON_PREFIX = "providerx"
+  GRAPHSON_BASE_TYPE = "MyType"
+  GRAPHSON_TYPE = GraphSONUtil.formatType(GRAPHSON_PREFIX, GRAPHSON_BASE_TYPE)
+
+  def __init__(self, x, y):
+    self.x = x
+    self.y = y
+
+  @classmethod
+  def objectify(cls, value, reader):
+    return cls(value['x'], value['y'])
+
+  @classmethod
+  def dictify(cls, value, writer):
+    return GraphSONUtil.typedValue(cls.GRAPHSON_BASE_TYPE,
+                                  {'x': value.x, 'y': value.y},
+                                  cls.GRAPHSON_PREFIX)
+
+graphson_reader = GraphSONReader({MyType.GRAPHSON_TYPE: MyType})
+graphson_writer = GraphSONWriter({MyType: MyType})
+
+connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g',
+                                     graphson_reader=graphson_reader,
+                                     graphson_writer=graphson_writer)
+----
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/e2131957/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py
----------------------------------------------------------------------
diff --git a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py
index 002e401..d516058 100644
--- a/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py
+++ b/gremlin-python/src/main/jython/gremlin_python/structure/io/graphson.py
@@ -47,11 +47,14 @@ class GraphSONUtil(object):
 
     @classmethod
     def typedValue(cls, type_name, value, prefix="g"):
-        out = {cls.TYPE_KEY: prefix + ":" + type_name}
+        out = {cls.TYPE_KEY: cls.formatType(prefix, type_name)}
         if value is not None:
             out[cls.VALUE_KEY] = value
         return out
 
+    @classmethod
+    def formatType(cls, prefix, type_name):
+        return "%s:%s" % (prefix, type_name)
 
 # Read/Write classes split to follow precedence of the Java API
 class GraphSONWriter(object):