You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tinkerpop.apache.org by jo...@apache.org on 2017/07/14 16:47:29 UTC
[1/3] tinkerpop git commit: Gremlin .NET: GraphSON3 Deserialization
Repository: tinkerpop
Updated Branches:
refs/heads/TINKERPOP-1552-master 5c3cc5acd -> 8fd4c8cdc
Gremlin .NET: GraphSON3 Deserialization
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/6b3fdcb8
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/6b3fdcb8
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/6b3fdcb8
Branch: refs/heads/TINKERPOP-1552-master
Commit: 6b3fdcb8dc076f2d4c8e00f0058ff4aa0c665ce5
Parents: 5c3cc5a
Author: Jorge Bay Gondra <jo...@gmail.com>
Authored: Fri Jul 14 16:53:19 2017 +0200
Committer: Jorge Bay Gondra <jo...@gmail.com>
Committed: Fri Jul 14 16:53:19 2017 +0200
----------------------------------------------------------------------
.../src/Gremlin.Net/Driver/ConnectionFactory.cs | 5 +-
.../src/Gremlin.Net/Driver/GremlinClient.cs | 2 +-
.../Structure/IO/GraphSON/GraphSON2Reader.cs | 50 ++++++
.../Structure/IO/GraphSON/GraphSON3Reader.cs | 66 +++++++
.../Structure/IO/GraphSON/GraphSON3Writer.cs | 57 ++++++
.../Structure/IO/GraphSON/GraphSONReader.cs | 19 +-
.../Structure/IO/GraphSON/GraphSONWriter.cs | 17 +-
.../Structure/IO/GraphSON/ListSerializer.cs | 54 ++++++
.../Structure/IO/GraphSON/MapSerializer.cs | 52 ++++++
.../Structure/IO/GraphSON/SetSerializer.cs | 48 +++++
.../IO/GraphSON/GraphSONReaderTests.cs | 173 +++++++++++++------
11 files changed, 473 insertions(+), 70 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
index 0041a67..e3fd068 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
@@ -21,6 +21,7 @@
#endregion
+using System;
using Gremlin.Net.Structure.IO.GraphSON;
namespace Gremlin.Net.Driver
@@ -35,8 +36,8 @@ namespace Gremlin.Net.Driver
GraphSONWriter graphSONWriter)
{
_gremlinServer = gremlinServer;
- _graphSONReader = graphSONReader;
- _graphSONWriter = graphSONWriter;
+ _graphSONReader = graphSONReader ?? throw new ArgumentNullException(nameof(graphSONReader));
+ _graphSONWriter = graphSONWriter ?? throw new ArgumentNullException(nameof(graphSONWriter));
}
public Connection CreateConnection()
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
index 46dd8a6..a81c17e 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
@@ -45,7 +45,7 @@ namespace Gremlin.Net.Driver
public GremlinClient(GremlinServer gremlinServer, GraphSONReader graphSONReader = null,
GraphSONWriter graphSONWriter = null)
{
- var reader = graphSONReader ?? new GraphSONReader();
+ var reader = graphSONReader ?? new GraphSON3Reader();
var writer = graphSONWriter ?? new GraphSONWriter();
var connectionFactory = new ConnectionFactory(gremlinServer, reader, writer);
_connectionPool = new ConnectionPool(connectionFactory);
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON2Reader.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON2Reader.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON2Reader.cs
new file mode 100644
index 0000000..90c9e4d
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON2Reader.cs
@@ -0,0 +1,50 @@
+#region License
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#endregion
+
+using System.Collections.Generic;
+
+namespace Gremlin.Net.Structure.IO.GraphSON
+{
+ /// <summary>
+ /// Handles deserialization of GraphSON3 data.
+ /// </summary>
+ public class GraphSON2Reader : GraphSONReader
+ {
+ /// <summary>
+ /// Creates a new instance of <see cref="GraphSON2Reader"/>.
+ /// </summary>
+ public GraphSON2Reader()
+ {
+
+ }
+
+ /// <summary>
+ /// Creates a new instance of <see cref="GraphSON2Reader"/>.
+ /// </summary>
+ public GraphSON2Reader(IReadOnlyDictionary<string, IGraphSONDeserializer> deserializerByGraphSONType) :
+ base(deserializerByGraphSONType)
+ {
+
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Reader.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Reader.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Reader.cs
new file mode 100644
index 0000000..4bc7878
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Reader.cs
@@ -0,0 +1,66 @@
+#region License
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#endregion
+
+using System.Collections.Generic;
+
+namespace Gremlin.Net.Structure.IO.GraphSON
+{
+ /// <summary>
+ /// Handles deserialization of GraphSON3 data.
+ /// </summary>
+ public class GraphSON3Reader : GraphSONReader
+ {
+ private static readonly IDictionary<string, IGraphSONDeserializer> GraphSON3SpecificDeserializers =
+ new Dictionary<string, IGraphSONDeserializer>
+ {
+ { "g:List", new ListSerializer() },
+ { "g:Set", new SetSerializer() },
+ { "g:Map", new MapSerializer() }
+ };
+
+ /// <summary>
+ /// Creates a new instance of <see cref="GraphSON3Reader"/>.
+ /// </summary>
+ public GraphSON3Reader()
+ {
+ foreach (var kv in GraphSON3SpecificDeserializers)
+ {
+ Deserializers[kv.Key] = kv.Value;
+ }
+ }
+
+ /// <summary>
+ /// Creates a new instance of <see cref="GraphSON3Reader"/>.
+ /// </summary>
+ /// <param name="deserializerByGraphSONType">
+ /// Overrides <see cref="IGraphSONDeserializer" /> instances by their type identifier.
+ /// </param>
+ public GraphSON3Reader(IReadOnlyDictionary<string, IGraphSONDeserializer> deserializerByGraphSONType) : this()
+ {
+ foreach (var deserializerAndGraphSONType in deserializerByGraphSONType)
+ {
+ Deserializers[deserializerAndGraphSONType.Key] = deserializerAndGraphSONType.Value;
+ }
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs
new file mode 100644
index 0000000..96c303c
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs
@@ -0,0 +1,57 @@
+#region License
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+namespace Gremlin.Net.Structure.IO.GraphSON
+{
+ /// <summary>
+ /// Handles deserialization of GraphSON3 data.
+ /// </summary>
+ public class GraphSON3Writer : GraphSONWriter
+ {
+ /// <summary>
+ /// Creates a new instance of <see cref="GraphSON3Writer"/>.
+ /// </summary>
+ public GraphSON3Writer()
+ {
+ // TODO: Include GraphSON3 specific serializers
+ }
+
+ /// <summary>
+ /// Creates a new instance of <see cref="GraphSON3Writer"/>.
+ /// </summary>
+ /// <param name="customSerializerByType">
+ /// <see cref="IGraphSONSerializer" /> serializers identified by their
+ /// <see cref="Type" />.
+ /// </param>
+ public GraphSON3Writer(IReadOnlyDictionary<Type, IGraphSONSerializer> customSerializerByType) : this()
+ {
+ foreach (var serializerAndType in customSerializerByType)
+ {
+ Serializers[serializerAndType.Key] = serializerAndType.Value;
+ }
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs
index aa1fc48..ee54a9b 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONReader.cs
@@ -31,9 +31,12 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// <summary>
/// Allows to deserialize GraphSON to objects.
/// </summary>
- public class GraphSONReader
+ public abstract class GraphSONReader
{
- private readonly Dictionary<string, IGraphSONDeserializer> _deserializerByGraphSONType = new Dictionary
+ /// <summary>
+ /// Contains the <see cref="IGraphSONDeserializer" /> instances by their type identifier.
+ /// </summary>
+ protected readonly Dictionary<string, IGraphSONDeserializer> Deserializers = new Dictionary
<string, IGraphSONDeserializer>
{
{"g:Traverser", new TraverserReader()},
@@ -54,7 +57,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// <summary>
/// Initializes a new instance of the <see cref="GraphSONReader" /> class.
/// </summary>
- public GraphSONReader()
+ protected GraphSONReader()
{
}
@@ -65,10 +68,10 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// <see cref="IGraphSONDeserializer" /> deserializers identified by their
/// GraphSON type.
/// </param>
- public GraphSONReader(IReadOnlyDictionary<string, IGraphSONDeserializer> deserializerByGraphSONType)
+ protected GraphSONReader(IReadOnlyDictionary<string, IGraphSONDeserializer> deserializerByGraphSONType)
{
foreach (var deserializerAndGraphSONType in deserializerByGraphSONType)
- _deserializerByGraphSONType[deserializerAndGraphSONType.Key] = deserializerAndGraphSONType.Value;
+ Deserializers[deserializerAndGraphSONType.Key] = deserializerAndGraphSONType.Value;
}
/// <summary>
@@ -76,7 +79,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// </summary>
/// <param name="graphSonData">The GraphSON collection to deserialize.</param>
/// <returns>The deserialized object.</returns>
- public dynamic ToObject(IEnumerable<JToken> graphSonData)
+ public virtual dynamic ToObject(IEnumerable<JToken> graphSonData)
{
return graphSonData.Select(graphson => ToObject(graphson));
}
@@ -86,7 +89,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// </summary>
/// <param name="jToken">The GraphSON to deserialize.</param>
/// <returns>The deserialized object.</returns>
- public dynamic ToObject(JToken jToken)
+ public virtual dynamic ToObject(JToken jToken)
{
if (jToken is JArray)
return jToken.Select(t => ToObject(t));
@@ -104,7 +107,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
private dynamic ReadTypedValue(JToken typedValue)
{
var graphSONType = (string) typedValue[GraphSONTokens.TypeKey];
- return _deserializerByGraphSONType[graphSONType].Objectify(typedValue[GraphSONTokens.ValueKey], this);
+ return Deserializers[graphSONType].Objectify(typedValue[GraphSONTokens.ValueKey], this);
}
private dynamic ReadDictionary(JToken jtokenDict)
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
index ba632b1..db0ae7d 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
@@ -38,7 +38,10 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// </summary>
public class GraphSONWriter
{
- private readonly Dictionary<Type, IGraphSONSerializer> _serializerByType = new Dictionary
+ /// <summary>
+ /// Contains the information of serializers by type.
+ /// </summary>
+ protected readonly Dictionary<Type, IGraphSONSerializer> Serializers = new Dictionary
<Type, IGraphSONSerializer>
{
{typeof(ITraversal), new TraversalSerializer()},
@@ -77,7 +80,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
public GraphSONWriter(IReadOnlyDictionary<Type, IGraphSONSerializer> customSerializerByType)
{
foreach (var serializerAndType in customSerializerByType)
- _serializerByType[serializerAndType.Key] = serializerAndType.Value;
+ Serializers[serializerAndType.Key] = serializerAndType.Value;
}
/// <summary>
@@ -85,7 +88,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// </summary>
/// <param name="objectData">The object to serialize.</param>
/// <returns>The serialized GraphSON.</returns>
- public string WriteObject(dynamic objectData)
+ public virtual string WriteObject(dynamic objectData)
{
return JsonConvert.SerializeObject(ToDict(objectData));
}
@@ -104,15 +107,15 @@ namespace Gremlin.Net.Structure.IO.GraphSON
private bool TryGetSerializerFor(out IGraphSONSerializer serializer, Type type)
{
- if (_serializerByType.ContainsKey(type))
+ if (Serializers.ContainsKey(type))
{
- serializer = _serializerByType[type];
+ serializer = Serializers[type];
return true;
}
- foreach (var supportedType in _serializerByType.Keys)
+ foreach (var supportedType in Serializers.Keys)
if (supportedType.IsAssignableFrom(type))
{
- serializer = _serializerByType[supportedType];
+ serializer = Serializers[supportedType];
return true;
}
serializer = null;
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs
new file mode 100644
index 0000000..f738ac8
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs
@@ -0,0 +1,54 @@
+#region License
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#endregion
+
+using System.Collections.Generic;
+using Newtonsoft.Json.Linq;
+
+namespace Gremlin.Net.Structure.IO.GraphSON
+{
+ internal class ListSerializer : IGraphSONDeserializer, IGraphSONSerializer
+ {
+ private static readonly IReadOnlyList<object> EmptyList = new object[0];
+
+ public dynamic Objectify(JToken graphsonObject, GraphSONReader reader)
+ {
+ var jArray = graphsonObject as JArray;
+ if (jArray == null)
+ {
+ return EmptyList;
+ }
+ var result = new object[jArray.Count];
+ for (var i = 0; i < result.Length; i++)
+ {
+ result[i] = reader.ToObject(jArray[i]);
+ }
+ // object[] implements IList<object>
+ return result;
+ }
+
+ public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter writer)
+ {
+ throw new System.NotImplementedException();
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs
new file mode 100644
index 0000000..5f9c326
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs
@@ -0,0 +1,52 @@
+#region License
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#endregion
+
+using System.Collections.Generic;
+using Newtonsoft.Json.Linq;
+
+namespace Gremlin.Net.Structure.IO.GraphSON
+{
+ internal class MapSerializer : IGraphSONDeserializer, IGraphSONSerializer
+ {
+ public dynamic Objectify(JToken graphsonObject, GraphSONReader reader)
+ {
+ var jArray = graphsonObject as JArray;
+ if (jArray == null)
+ {
+ return new Dictionary<object, object>(0);
+ }
+ var result = new Dictionary<object, object>(jArray.Count / 2);
+ for (var i = 0; i < jArray.Count; i += 2)
+ {
+ result[reader.ToObject(jArray[i])] = reader.ToObject(jArray[i + 1]);
+ }
+ // IDictionary<object, object>
+ return result;
+ }
+
+ public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter writer)
+ {
+ throw new System.NotImplementedException();
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs
new file mode 100644
index 0000000..ed2a973
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs
@@ -0,0 +1,48 @@
+#region License
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#endregion
+
+using System.Collections.Generic;
+using System.Linq;
+using Newtonsoft.Json.Linq;
+
+namespace Gremlin.Net.Structure.IO.GraphSON
+{
+ internal class SetSerializer : IGraphSONDeserializer, IGraphSONSerializer
+ {
+ public dynamic Objectify(JToken graphsonObject, GraphSONReader reader)
+ {
+ var jArray = graphsonObject as JArray;
+ if (jArray == null)
+ {
+ return new HashSet<object>();
+ }
+ // ISet<object>
+ return new HashSet<object>(jArray.Select(reader.ToObject));
+ }
+
+ public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter writer)
+ {
+ throw new System.NotImplementedException();
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/6b3fdcb8/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
index 747a94e..e2c6dc9 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
@@ -33,9 +33,30 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
{
public class GraphSONReaderTests
{
- private GraphSONReader CreateStandardGraphSONReader()
+ /// <summary>
+ /// Parameters for each test supporting multiple versions of GraphSON
+ /// </summary>
+ public static IEnumerable<object[]> Versions => new []
{
- return new GraphSONReader();
+ new object[] { 2 },
+ new object[] { 3 }
+ };
+
+ /// <summary>
+ /// Parameters for each collections test supporting multiple versions of GraphSON
+ /// </summary>
+ public static IEnumerable<object[]> VersionsSupportingCollections => new []
+ {
+ new object[] { 3 }
+ };
+
+ private GraphSONReader CreateStandardGraphSONReader(int version)
+ {
+ if (version == 3)
+ {
+ return new GraphSON3Reader();
+ }
+ return new GraphSON2Reader();
}
[Fact]
@@ -45,7 +66,7 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
{
{"NS:TestClass", new TestGraphSONDeserializer()}
};
- var reader = new GraphSONReader(deserializerByGraphSONType);
+ var reader = new GraphSON2Reader(deserializerByGraphSONType);
var graphSON = "{\"@type\":\"NS:TestClass\",\"@value\":\"test\"}";
var jObject = JObject.Parse(graphSON);
@@ -63,7 +84,7 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
{
{overrideTypeString, customSerializerMock.Object}
};
- var reader = new GraphSONReader(customSerializerByType);
+ var reader = new GraphSON2Reader(customSerializerByType);
reader.ToObject(JObject.Parse($"{{\"@type\":\"{overrideTypeString}\",\"@value\":12}}"));
@@ -71,11 +92,11 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
customSerializerMock.Verify(m => m.Objectify(It.IsAny<JToken>(), It.IsAny<GraphSONReader>()));
}
- [Fact]
- public void ShouldDeserializeDateToDateTime()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeDateToDateTime(int version)
{
var graphSon = "{\"@type\":\"g:Date\",\"@value\":1475583442552}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
DateTime readDateTime = reader.ToObject(JObject.Parse(graphSon));
@@ -83,11 +104,12 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedDateTime, readDateTime);
}
- [Fact]
- public void ShouldDeserializeDictionary()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeDictionary(int version)
{
+ Console.WriteLine("Starting");
var serializedDict = "{\"age\":[{\"@type\":\"g:Int32\",\"@value\":29}],\"name\":[\"marko\"]}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
var jObject = JObject.Parse(serializedDict);
var deserializedDict = reader.ToObject(jObject);
@@ -100,12 +122,12 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedDict, deserializedDict);
}
- [Fact]
- public void ShouldDeserializeEdge()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeEdge(int version)
{
var graphSon =
"{\"@type\":\"g:Edge\", \"@value\":{\"id\":{\"@type\":\"g:Int64\",\"@value\":17},\"label\":\"knows\",\"inV\":\"x\",\"outV\":\"y\",\"inVLabel\":\"xLab\",\"properties\":{\"aKey\":\"aValue\",\"bKey\":true}}}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
Edge readEdge = reader.ToObject(JObject.Parse(graphSon));
@@ -115,11 +137,11 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(new Vertex("y"), readEdge.OutV);
}
- [Fact]
- public void ShouldDeserializeInt()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeInt(int version)
{
var serializedValue = "{\"@type\":\"g:Int32\",\"@value\":5}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
var jObject = JObject.Parse(serializedValue);
var deserializedValue = reader.ToObject(jObject);
@@ -127,11 +149,11 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(5, deserializedValue);
}
- [Fact]
- public void ShouldDeserializeLong()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeLong(int version)
{
var serializedValue = "{\"@type\":\"g:Int64\",\"@value\":5}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
var jObject = JObject.Parse(serializedValue);
var deserializedValue = reader.ToObject(jObject);
@@ -139,11 +161,11 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal((long) 5, deserializedValue);
}
- [Fact]
- public void ShouldDeserializeFloat()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeFloat(int version)
{
var serializedValue = "{\"@type\":\"g:Float\",\"@value\":31.3}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
var jObject = JObject.Parse(serializedValue);
var deserializedValue = reader.ToObject(jObject);
@@ -151,11 +173,11 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal((float) 31.3, deserializedValue);
}
- [Fact]
- public void ShouldDeserializeDouble()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeDouble(int version)
{
var serializedValue = "{\"@type\":\"g:Double\",\"@value\":31.2}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
var jObject = JObject.Parse(serializedValue);
var deserializedValue = reader.ToObject(jObject);
@@ -163,11 +185,11 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(31.2, deserializedValue);
}
- [Fact]
- public void ShouldDeserializeList()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeList(int version)
{
var serializedValue = "[{\"@type\":\"g:Int32\",\"@value\":5},{\"@type\":\"g:Int32\",\"@value\":6}]";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
var jObject = JArray.Parse(serializedValue);
var deserializedValue = reader.ToObject(jObject);
@@ -175,12 +197,12 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(new List<object> {5, 6}, deserializedValue);
}
- [Fact]
- public void ShouldDeserializePath()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializePath(int version)
{
var graphSon =
"{\"@type\":\"g:Path\",\"@value\":{\"labels\":[[\"a\"],[\"b\",\"c\"],[]],\"objects\":[{\"@type\":\"g:Vertex\",\"@value\":{\"id\":{\"@type\":\"g:Int32\",\"@value\":1},\"label\":\"person\",\"properties\":{\"name\":[{\"@type\":\"g:VertexProperty\",\"@value\":{\"id\":{\"@type\":\"g:Int64\",\"@value\":0},\"value\":\"marko\",\"label\":\"name\"}}],\"age\":[{\"@type\":\"g:VertexProperty\",\"@value\":{\"id\":{\"@type\":\"g:Int64\",\"@value\":1},\"value\":{\"@type\":\"g:Int32\",\"@value\":29},\"label\":\"age\"}}]}}},{\"@type\":\"g:Vertex\",\"@value\":{\"id\":{\"@type\":\"g:Int32\",\"@value\":3},\"label\":\"software\",\"properties\":{\"name\":[{\"@type\":\"g:VertexProperty\",\"@value\":{\"id\":{\"@type\":\"g:Int64\",\"@value\":4},\"value\":\"lop\",\"label\":\"name\"}}],\"lang\":[{\"@type\":\"g:VertexProperty\",\"@value\":{\"id\":{\"@type\":\"g:Int64\",\"@value\":5},\"value\":\"java\",\"label\":\"lang\"}}]}}},\"lop\"]}}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
Path readPath = reader.ToObject(JObject.Parse(graphSon));
@@ -191,12 +213,12 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(3, readPath.Count);
}
- [Fact]
- public void ShouldDeserializePropertyWithEdgeElement()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializePropertyWithEdgeElement(int version)
{
var graphSon =
"{\"@type\":\"g:Property\",\"@value\":{\"key\":\"aKey\",\"value\":{\"@type\":\"g:Int64\",\"@value\":17},\"element\":{\"@type\":\"g:Edge\",\"@value\":{\"id\":{\"@type\":\"g:Int64\",\"@value\":122},\"label\":\"knows\",\"inV\":\"x\",\"outV\":\"y\",\"inVLabel\":\"xLab\"}}}}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
Property readProperty = reader.ToObject(JObject.Parse(graphSon));
@@ -210,11 +232,11 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal("y", edge.OutV.Id);
}
- [Fact]
- public void ShouldDeserializeTimestampToDateTime()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeTimestampToDateTime(int version)
{
var graphSon = "{\"@type\":\"g:Timestamp\",\"@value\":1475583442558}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
DateTime readDateTime = reader.ToObject(JObject.Parse(graphSon));
@@ -222,23 +244,23 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedDateTime, readDateTime);
}
- [Fact]
- public void ShouldDeserializeGuid()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeGuid(int version)
{
var graphSon = "{\"@type\":\"g:UUID\",\"@value\":\"41d2e28a-20a4-4ab0-b379-d810dede3786\"}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
Guid readGuid = reader.ToObject(JObject.Parse(graphSon));
Assert.Equal(Guid.Parse("41d2e28a-20a4-4ab0-b379-d810dede3786"), readGuid);
}
- [Fact]
- public void ShouldDeserializeVertexProperty()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeVertexProperty(int version)
{
var graphSon =
"{\"@type\":\"g:VertexProperty\",\"@value\":{\"id\":\"anId\",\"label\":\"aKey\",\"value\":true,\"vertex\":{\"@type\":\"g:Int32\",\"@value\":9}}}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
VertexProperty readVertexProperty = reader.ToObject(JObject.Parse(graphSon));
@@ -248,12 +270,12 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.NotNull(readVertexProperty.Vertex);
}
- [Fact]
- public void ShouldDeserializeVertexPropertyWithLabel()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeVertexPropertyWithLabel(int version)
{
var graphSon =
"{\"@type\":\"g:VertexProperty\", \"@value\":{\"id\":{\"@type\":\"g:Int32\",\"@value\":1},\"label\":\"name\",\"value\":\"marko\"}}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
VertexProperty readVertexProperty = reader.ToObject(JObject.Parse(graphSon));
@@ -263,23 +285,23 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Null(readVertexProperty.Vertex);
}
- [Fact]
- public void ShouldDeserializeVertex()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeVertex(int version)
{
var graphSon = "{\"@type\":\"g:Vertex\", \"@value\":{\"id\":{\"@type\":\"g:Float\",\"@value\":45.23}}}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
var readVertex = reader.ToObject(JObject.Parse(graphSon));
Assert.Equal(new Vertex(45.23f), readVertex);
}
- [Fact]
- public void ShouldDeserializeVertexWithEdges()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeVertexWithEdges(int version)
{
var graphSon =
"{\"@type\":\"g:Vertex\", \"@value\":{\"id\":{\"@type\":\"g:Int32\",\"@value\":1},\"label\":\"person\",\"outE\":{\"created\":[{\"id\":{\"@type\":\"g:Int32\",\"@value\":9},\"inV\":{\"@type\":\"g:Int32\",\"@value\":3},\"properties\":{\"weight\":{\"@type\":\"g:Double\",\"@value\":0.4}}}],\"knows\":[{\"id\":{\"@type\":\"g:Int32\",\"@value\":7},\"inV\":{\"@type\":\"g:Int32\",\"@value\":2},\"properties\":{\"weight\":{\"@type\":\"g:Double\",\"@value\":0.5}}},{\"id\":{\"@type\":\"g:Int32\",\"@value\":8},\"inV\":{\"@type\":\"g:Int32\",\"@value\":4},\"properties\":{\"weight\":{\"@type\":\"g:Double\",\"@value\":1.0}}}]},\"properties\":{\"name\":[{\"id\":{\"@type\":\"g:Int64\",\"@value\":0},\"value\":\"marko\"}],\"age\":[{\"id\":{\"@type\":\"g:Int64\",\"@value\":1},\"value\":{\"@type\":\"g:Int32\",\"@value\":29}}]}}}";
- var reader = CreateStandardGraphSONReader();
+ var reader = CreateStandardGraphSONReader(version);
var readVertex = reader.ToObject(JObject.Parse(graphSon));
@@ -288,6 +310,53 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(typeof(int), readVertex.Id.GetType());
}
+ [Theory, MemberData(nameof(VersionsSupportingCollections))]
+ public void ShouldDeserializeEmptyGList(int version)
+ {
+ var graphSon =
+ "{\"@type\":\"g:List\", \"@value\": []}";
+ var reader = CreateStandardGraphSONReader(version);
+
+ var deserializedValue = reader.ToObject(JObject.Parse(graphSon));
+ Assert.Equal(new object[0], deserializedValue);
+ }
+
+ [Theory, MemberData(nameof(VersionsSupportingCollections))]
+ public void ShouldDeserializeGList(int version)
+ {
+ const string json = "{\"@type\":\"g:List\", \"@value\": [{\"@type\": \"g:Int32\", \"@value\": 1}," +
+ "{\"@type\": \"g:Int32\", \"@value\": 2}, {\"@type\": \"g:Int32\", \"@value\": 3}]}";
+ var reader = CreateStandardGraphSONReader(version);
+
+ var deserializedValue = reader.ToObject(JObject.Parse(json));
+
+ Assert.Equal((IList<object>)new object[] { 1, 2, 3}, deserializedValue);
+ }
+
+ [Theory, MemberData(nameof(VersionsSupportingCollections))]
+ public void ShouldDeserializeGSet(int version)
+ {
+ const string json = "{\"@type\":\"g:Set\", \"@value\": [{\"@type\": \"g:Int32\", \"@value\": 1}," +
+ "{\"@type\": \"g:Int32\", \"@value\": 2}, {\"@type\": \"g:Int32\", \"@value\": 3}]}";
+ var reader = CreateStandardGraphSONReader(version);
+
+ var deserializedValue = reader.ToObject(JObject.Parse(json));
+
+ Assert.Equal((ISet<object>)new HashSet<object>{ 1, 2, 3}, deserializedValue);
+ }
+
+ [Theory, MemberData(nameof(VersionsSupportingCollections))]
+ public void ShouldDeserializeGMap(int version)
+ {
+ const string json = "{\"@type\":\"g:Map\", \"@value\": [\"a\",{\"@type\": \"g:Int32\", \"@value\": 1}, " +
+ "\"b\", {\"@type\": \"g:Int32\", \"@value\": 2}]}";
+ var reader = CreateStandardGraphSONReader(version);
+
+ var deserializedValue = reader.ToObject(JObject.Parse(json));
+
+ Assert.Equal(new Dictionary<object, object>{ { "a", 1 }, { "b", 2 }}, deserializedValue);
+ }
+
[Fact]
public void ShouldDeserializeTraverser()
{
[3/3] tinkerpop git commit: Gremlin .NET: Use
application/vnd.gremlin-v3.0+json by default
Posted by jo...@apache.org.
Gremlin .NET: Use application/vnd.gremlin-v3.0+json by default
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/8fd4c8cd
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/8fd4c8cd
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/8fd4c8cd
Branch: refs/heads/TINKERPOP-1552-master
Commit: 8fd4c8cdc7a8299af47e592389b3183a9c64c1ee
Parents: 8cdc941
Author: Jorge Bay Gondra <jo...@gmail.com>
Authored: Fri Jul 14 18:44:43 2017 +0200
Committer: Jorge Bay Gondra <jo...@gmail.com>
Committed: Fri Jul 14 18:44:43 2017 +0200
----------------------------------------------------------------------
gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs | 5 +++--
.../src/Gremlin.Net/Driver/ConnectionFactory.cs | 6 ++++--
gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs | 10 ++++++++--
.../src/Gremlin.Net/Driver/JsonMessageSerializer.cs | 9 +++++++--
4 files changed, 22 insertions(+), 8 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8fd4c8cd/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
index 126b461..3d3e902 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/Connection.cs
@@ -36,20 +36,21 @@ namespace Gremlin.Net.Driver
{
private readonly GraphSONReader _graphSONReader;
private readonly GraphSONWriter _graphSONWriter;
- private readonly JsonMessageSerializer _messageSerializer = new JsonMessageSerializer();
+ private readonly JsonMessageSerializer _messageSerializer;
private readonly Uri _uri;
private readonly WebSocketConnection _webSocketConnection = new WebSocketConnection();
private readonly string _username;
private readonly string _password;
public Connection(Uri uri, string username, string password, GraphSONReader graphSONReader,
- GraphSONWriter graphSONWriter)
+ GraphSONWriter graphSONWriter, string mimeType)
{
_uri = uri;
_username = username;
_password = password;
_graphSONReader = graphSONReader;
_graphSONWriter = graphSONWriter;
+ _messageSerializer = new JsonMessageSerializer(mimeType);
}
public async Task<IReadOnlyCollection<T>> SubmitAsync<T>(RequestMessage requestMessage)
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8fd4c8cd/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
index e3fd068..e2ff5b7 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/ConnectionFactory.cs
@@ -31,11 +31,13 @@ namespace Gremlin.Net.Driver
private readonly GraphSONReader _graphSONReader;
private readonly GraphSONWriter _graphSONWriter;
private readonly GremlinServer _gremlinServer;
+ private readonly string _mimeType;
public ConnectionFactory(GremlinServer gremlinServer, GraphSONReader graphSONReader,
- GraphSONWriter graphSONWriter)
+ GraphSONWriter graphSONWriter, string mimeType)
{
_gremlinServer = gremlinServer;
+ _mimeType = mimeType;
_graphSONReader = graphSONReader ?? throw new ArgumentNullException(nameof(graphSONReader));
_graphSONWriter = graphSONWriter ?? throw new ArgumentNullException(nameof(graphSONWriter));
}
@@ -43,7 +45,7 @@ namespace Gremlin.Net.Driver
public Connection CreateConnection()
{
return new Connection(_gremlinServer.Uri, _gremlinServer.Username, _gremlinServer.Password, _graphSONReader,
- _graphSONWriter);
+ _graphSONWriter, _mimeType);
}
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8fd4c8cd/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
index 9781a7c..a251ab7 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
@@ -34,6 +34,11 @@ namespace Gremlin.Net.Driver
/// </summary>
public class GremlinClient : IGremlinClient
{
+ /// <summary>
+ /// Defines the default mime type to use.
+ /// </summary>
+ public const string DefaultMimeType = "application/vnd.gremlin-v3.0+json";
+
private readonly ConnectionPool _connectionPool;
/// <summary>
@@ -42,12 +47,13 @@ namespace Gremlin.Net.Driver
/// <param name="gremlinServer">The <see cref="GremlinServer" /> the requests should be sent to.</param>
/// <param name="graphSONReader">A <see cref="GraphSONReader" /> instance to read received GraphSON data.</param>
/// <param name="graphSONWriter">a <see cref="GraphSONWriter" /> instance to write GraphSON data.</param>
+ /// <param name="mimeType">The GraphSON version mime type, defaults to latest supported by the server.</param>
public GremlinClient(GremlinServer gremlinServer, GraphSONReader graphSONReader = null,
- GraphSONWriter graphSONWriter = null)
+ GraphSONWriter graphSONWriter = null, string mimeType = null)
{
var reader = graphSONReader ?? new GraphSON3Reader();
var writer = graphSONWriter ?? new GraphSON3Writer();
- var connectionFactory = new ConnectionFactory(gremlinServer, reader, writer);
+ var connectionFactory = new ConnectionFactory(gremlinServer, reader, writer, mimeType ?? DefaultMimeType);
_connectionPool = new ConnectionPool(connectionFactory);
}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8fd4c8cd/gremlin-dotnet/src/Gremlin.Net/Driver/JsonMessageSerializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/JsonMessageSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/JsonMessageSerializer.cs
index c3270bf..53a546c 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/JsonMessageSerializer.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/JsonMessageSerializer.cs
@@ -28,7 +28,12 @@ namespace Gremlin.Net.Driver
{
internal class JsonMessageSerializer
{
- private const string MimeType = "application/vnd.gremlin-v2.0+json";
+ private readonly string _mimeType;
+
+ public JsonMessageSerializer(string mimeType)
+ {
+ _mimeType = mimeType;
+ }
public byte[] SerializeMessage(string msg)
{
@@ -37,7 +42,7 @@ namespace Gremlin.Net.Driver
private string MessageWithHeader(string messageContent)
{
- return $"{(char) MimeType.Length}{MimeType}{messageContent}";
+ return $"{(char) _mimeType.Length}{_mimeType}{messageContent}";
}
public TMessage DeserializeMessage<TMessage>(byte[] message)
[2/3] tinkerpop git commit: Gremlin .NET: GraphSON3 Serialization
Posted by jo...@apache.org.
Gremlin .NET: GraphSON3 Serialization
Project: http://git-wip-us.apache.org/repos/asf/tinkerpop/repo
Commit: http://git-wip-us.apache.org/repos/asf/tinkerpop/commit/8cdc941f
Tree: http://git-wip-us.apache.org/repos/asf/tinkerpop/tree/8cdc941f
Diff: http://git-wip-us.apache.org/repos/asf/tinkerpop/diff/8cdc941f
Branch: refs/heads/TINKERPOP-1552-master
Commit: 8cdc941f084ffc0a8d1013d09cd9a44993b44a40
Parents: 6b3fdcb
Author: Jorge Bay Gondra <jo...@gmail.com>
Authored: Fri Jul 14 17:56:41 2017 +0200
Committer: Jorge Bay Gondra <jo...@gmail.com>
Committed: Fri Jul 14 17:56:41 2017 +0200
----------------------------------------------------------------------
.../src/Gremlin.Net/Driver/GremlinClient.cs | 2 +-
.../Structure/IO/GraphSON/GraphSON2Writer.cs | 51 +++++
.../Structure/IO/GraphSON/GraphSON3Writer.cs | 15 +-
.../Structure/IO/GraphSON/GraphSONUtil.cs | 21 +++
.../Structure/IO/GraphSON/GraphSONWriter.cs | 6 +-
.../Structure/IO/GraphSON/ListSerializer.cs | 6 +-
.../Structure/IO/GraphSON/MapSerializer.cs | 16 +-
.../Structure/IO/GraphSON/SetSerializer.cs | 3 +-
.../Process/Traversal/GraphSONWriterTests.cs | 24 ++-
.../GraphSON/BytecodeGraphSONSerializerTests.cs | 60 +++---
.../IO/GraphSON/GraphSONReaderTests.cs | 1 -
.../IO/GraphSON/GraphSONWriterTests.cs | 188 ++++++++++++-------
.../IO/GraphSON/StrategyWriterTests.cs | 30 ++-
13 files changed, 313 insertions(+), 110 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
index a81c17e..9781a7c 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Driver/GremlinClient.cs
@@ -46,7 +46,7 @@ namespace Gremlin.Net.Driver
GraphSONWriter graphSONWriter = null)
{
var reader = graphSONReader ?? new GraphSON3Reader();
- var writer = graphSONWriter ?? new GraphSONWriter();
+ var writer = graphSONWriter ?? new GraphSON3Writer();
var connectionFactory = new ConnectionFactory(gremlinServer, reader, writer);
_connectionPool = new ConnectionPool(connectionFactory);
}
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON2Writer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON2Writer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON2Writer.cs
new file mode 100644
index 0000000..b1083da
--- /dev/null
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON2Writer.cs
@@ -0,0 +1,51 @@
+#region License
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#endregion
+
+using System;
+using System.Collections.Generic;
+
+namespace Gremlin.Net.Structure.IO.GraphSON
+{
+ /// <summary>
+ /// Handles serialization of GraphSON2 data.
+ /// </summary>
+ public class GraphSON2Writer : GraphSONWriter
+ {
+ /// <summary>
+ /// Creates a new instance of <see cref="GraphSON2Writer"/>.
+ /// </summary>
+ public GraphSON2Writer()
+ {
+
+ }
+
+ /// <summary>
+ /// Creates a new instance of <see cref="GraphSON2Writer"/>.
+ /// </summary>
+ public GraphSON2Writer(IReadOnlyDictionary<Type, IGraphSONSerializer> customSerializerByType) :
+ base(customSerializerByType)
+ {
+
+ }
+ }
+}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs
index 96c303c..0469bbe 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSON3Writer.cs
@@ -27,16 +27,27 @@ using System.Collections.Generic;
namespace Gremlin.Net.Structure.IO.GraphSON
{
/// <summary>
- /// Handles deserialization of GraphSON3 data.
+ /// Handles serialization of GraphSON3 data.
/// </summary>
public class GraphSON3Writer : GraphSONWriter
{
+ private static readonly IDictionary<Type, IGraphSONSerializer> GraphSON3SpecificSerializers =
+ new Dictionary<Type, IGraphSONSerializer>
+ {
+ { typeof(IList<object>), new ListSerializer() },
+ { typeof(ISet<object>), new SetSerializer() },
+ { typeof(IDictionary<object, object>), new MapSerializer() }
+ };
+
/// <summary>
/// Creates a new instance of <see cref="GraphSON3Writer"/>.
/// </summary>
public GraphSON3Writer()
{
- // TODO: Include GraphSON3 specific serializers
+ foreach (var kv in GraphSON3SpecificSerializers)
+ {
+ Serializers[kv.Key] = kv.Value;
+ }
}
/// <summary>
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONUtil.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONUtil.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONUtil.cs
index 037839b..ccdd47a 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONUtil.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONUtil.cs
@@ -21,6 +21,8 @@
#endregion
+using System;
+using System.Collections;
using System.Collections.Generic;
namespace Gremlin.Net.Structure.IO.GraphSON
@@ -58,5 +60,24 @@ namespace Gremlin.Net.Structure.IO.GraphSON
{
return $"{namespacePrefix}:{typeName}";
}
+
+ /// <summary>
+ /// Converts a Collection to a representation of g:List or g:Set
+ /// </summary>
+ internal static Dictionary<string, dynamic> ToCollection(dynamic objectData, GraphSONWriter writer,
+ string typename)
+ {
+ var collection = objectData as IEnumerable;
+ if (collection == null)
+ {
+ throw new InvalidOperationException("Object must implement IEnumerable");
+ }
+ var result = new List<object>();
+ foreach (var item in collection)
+ {
+ result.Add(writer.ToDict(item));
+ }
+ return ToTypedValue(typename, result);
+ }
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
index db0ae7d..55d24d6 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/GraphSONWriter.cs
@@ -36,7 +36,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// <summary>
/// Allows to serialize objects to GraphSON.
/// </summary>
- public class GraphSONWriter
+ public abstract class GraphSONWriter
{
/// <summary>
/// Contains the information of serializers by type.
@@ -66,7 +66,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// <summary>
/// Initializes a new instance of the <see cref="GraphSONWriter" /> class.
/// </summary>
- public GraphSONWriter()
+ protected GraphSONWriter()
{
}
@@ -77,7 +77,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
/// <see cref="IGraphSONSerializer" /> serializers identified by their
/// <see cref="Type" />.
/// </param>
- public GraphSONWriter(IReadOnlyDictionary<Type, IGraphSONSerializer> customSerializerByType)
+ protected GraphSONWriter(IReadOnlyDictionary<Type, IGraphSONSerializer> customSerializerByType)
{
foreach (var serializerAndType in customSerializerByType)
Serializers[serializerAndType.Key] = serializerAndType.Value;
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs
index f738ac8..f432c7e 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/ListSerializer.cs
@@ -21,7 +21,11 @@
#endregion
+using System;
+using System.Collections;
using System.Collections.Generic;
+using System.Linq;
+using Microsoft.Win32.SafeHandles;
using Newtonsoft.Json.Linq;
namespace Gremlin.Net.Structure.IO.GraphSON
@@ -48,7 +52,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter writer)
{
- throw new System.NotImplementedException();
+ return GraphSONUtil.ToCollection(objectData, writer, "List");
}
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs
index 5f9c326..a096e3e 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/MapSerializer.cs
@@ -21,6 +21,8 @@
#endregion
+using System;
+using System.Collections;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
@@ -46,7 +48,19 @@ namespace Gremlin.Net.Structure.IO.GraphSON
public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter writer)
{
- throw new System.NotImplementedException();
+ var map = objectData as IDictionary;
+ if (map == null)
+ {
+ throw new InvalidOperationException("Object must implement IDictionary");
+ }
+ var result = new object[map.Count * 2];
+ var index = 0;
+ foreach (var key in map.Keys)
+ {
+ result[index++] = writer.ToDict(key);
+ result[index++] = writer.ToDict(map[key]);
+ }
+ return GraphSONUtil.ToTypedValue("Map", result);
}
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs
index ed2a973..e657bd8 100644
--- a/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs
+++ b/gremlin-dotnet/src/Gremlin.Net/Structure/IO/GraphSON/SetSerializer.cs
@@ -21,6 +21,7 @@
#endregion
+using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json.Linq;
@@ -42,7 +43,7 @@ namespace Gremlin.Net.Structure.IO.GraphSON
public Dictionary<string, dynamic> Dictify(dynamic objectData, GraphSONWriter writer)
{
- throw new System.NotImplementedException();
+ return GraphSONUtil.ToCollection(objectData, writer, "Set");
}
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Process/Traversal/GraphSONWriterTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Process/Traversal/GraphSONWriterTests.cs b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Process/Traversal/GraphSONWriterTests.cs
index d8ccabe..a522a22 100644
--- a/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Process/Traversal/GraphSONWriterTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.IntegrationTest/Process/Traversal/GraphSONWriterTests.cs
@@ -21,6 +21,7 @@
#endregion
+using System.Collections.Generic;
using Gremlin.Net.Process.Traversal;
using Gremlin.Net.Structure.IO.GraphSON;
using Xunit;
@@ -29,10 +30,19 @@ namespace Gremlin.Net.IntegrationTest.Process.Traversal
{
public class GraphSONWriterTests
{
- [Fact]
- public void ShouldSerializeLongPredicateCorrectly()
+ /// <summary>
+ /// Parameters for each test supporting multiple versions of GraphSON
+ /// </summary>
+ public static IEnumerable<object[]> Versions => new []
{
- var writer = CreateStandardGraphSONWriter();
+ new object[] { 2 },
+ new object[] { 3 }
+ };
+
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeLongPredicateCorrectly(int version)
+ {
+ var writer = CreateGraphSONWriter(version);
var predicate = P.Lt("b").Or(P.Gt("c")).And(P.Neq("d"));
var graphSon = writer.WriteObject(predicate);
@@ -42,9 +52,13 @@ namespace Gremlin.Net.IntegrationTest.Process.Traversal
Assert.Equal(expected, graphSon);
}
- private GraphSONWriter CreateStandardGraphSONWriter()
+ private GraphSONWriter CreateGraphSONWriter(int version)
{
- return new GraphSONWriter();
+ if (version == 3)
+ {
+ return new GraphSON3Writer();
+ }
+ return new GraphSON2Writer();
}
}
}
\ No newline at end of file
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/BytecodeGraphSONSerializerTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/BytecodeGraphSONSerializerTests.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/BytecodeGraphSONSerializerTests.cs
index 8ed7a3d..8b87df6 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/BytecodeGraphSONSerializerTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/BytecodeGraphSONSerializerTests.cs
@@ -22,6 +22,7 @@
#endregion
using System.Collections.Generic;
+using System.Numerics;
using Gremlin.Net.Process.Traversal;
using Gremlin.Net.Structure.IO.GraphSON;
using Xunit;
@@ -30,13 +31,26 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
{
public class BytecodeGraphSONSerializerTests
{
- private GraphSONWriter CreateGraphSONWriter()
+ /// <summary>
+ /// Parameters for each test supporting multiple versions of GraphSON
+ /// </summary>
+ public static IEnumerable<object[]> Versions => new []
{
- return new GraphSONWriter();
+ new object[] { 2 },
+ new object[] { 3 }
+ };
+
+ private GraphSONWriter CreateGraphSONWriter(int version)
+ {
+ if (version == 3)
+ {
+ return new GraphSON3Writer();
+ }
+ return new GraphSON2Writer();
}
- [Fact]
- public void ShouldSerializeByteCodeWithNestedTraversal()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeByteCodeWithNestedTraversal(int version)
{
var bytecode = new Bytecode();
bytecode.AddStep("V");
@@ -44,7 +58,7 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
var nestedTraversal = new TestTraversal(nestedBytecode);
nestedBytecode.AddStep("out");
bytecode.AddStep("repeat", nestedTraversal);
- var graphsonWriter = CreateGraphSONWriter();
+ var graphsonWriter = CreateGraphSONWriter(version);
var graphSON = graphsonWriter.WriteObject(bytecode);
@@ -53,14 +67,14 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSon, graphSON);
}
- [Fact]
- public void ShouldSerializeBytecodeWithNumbers()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeBytecodeWithNumbers(int version)
{
var bytecode = new Bytecode();
bytecode.AddStep("V", (long) 1);
bytecode.AddStep("has", "age", 20);
bytecode.AddStep("has", "height", 6.5);
- var graphsonWriter = CreateGraphSONWriter();
+ var graphsonWriter = CreateGraphSONWriter(version);
var graphSON = graphsonWriter.WriteObject(bytecode);
@@ -69,25 +83,25 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSon, graphSON);
}
- [Fact]
- public void ShouldSerialize_g_V()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerialize_g_V(int version)
{
var bytecode = new Bytecode();
bytecode.AddStep("V");
- var graphsonWriter = CreateGraphSONWriter();
+ var graphsonWriter = CreateGraphSONWriter(version);
var graphSON = graphsonWriter.WriteObject(bytecode);
Assert.Equal("{\"@type\":\"g:Bytecode\",\"@value\":{\"step\":[[\"V\"]]}}", graphSON);
}
- [Fact]
- public void ShouldSerialize_g_V_Count()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerialize_g_V_Count(int version)
{
var bytecode = new Bytecode();
bytecode.AddStep("V");
bytecode.AddStep("count");
- var graphsonWriter = CreateGraphSONWriter();
+ var graphsonWriter = CreateGraphSONWriter(version);
var graphSON = graphsonWriter.WriteObject(bytecode);
@@ -95,14 +109,14 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSon, graphSON);
}
- [Fact]
- public void ShouldSerialize_g_V_HasXPerson_Name_GremlinX_Count()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerialize_g_V_HasXPerson_Name_GremlinX_Count(int version)
{
var bytecode = new Bytecode();
bytecode.AddStep("V");
bytecode.AddStep("has", "Person", "Name", "Gremlin");
bytecode.AddStep("count");
- var graphsonWriter = CreateGraphSONWriter();
+ var graphsonWriter = CreateGraphSONWriter(version);
var graphSON = graphsonWriter.WriteObject(bytecode);
@@ -111,15 +125,15 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSon, graphSON);
}
- [Fact]
- public void ShouldSerializeBytecodeWithSourcesStep()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeBytecodeWithSourcesStep(int version)
{
var bytecode = new Bytecode();
bytecode.AddSource("withSideEffect", "a", new List<string> {"josh", "peter"});
bytecode.AddStep("V", 1);
bytecode.AddStep("values", "name");
bytecode.AddStep("where", new TraversalPredicate("within", "a"));
- var graphsonWriter = CreateGraphSONWriter();
+ var graphsonWriter = CreateGraphSONWriter(version);
var graphSON = graphsonWriter.WriteObject(bytecode);
@@ -128,12 +142,12 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSon, graphSON);
}
- [Fact]
- public void ShouldSerializeBytecodeWithBindings()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeBytecodeWithBindings(int version)
{
var bytecode = new Bytecode();
bytecode.AddStep("V", new Binding("id", 123));
- var graphsonWriter = CreateGraphSONWriter();
+ var graphsonWriter = CreateGraphSONWriter(version);
var graphSon = graphsonWriter.WriteObject(bytecode);
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
index e2c6dc9..3fca7f7 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONReaderTests.cs
@@ -107,7 +107,6 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
[Theory, MemberData(nameof(Versions))]
public void ShouldDeserializeDictionary(int version)
{
- Console.WriteLine("Starting");
var serializedDict = "{\"age\":[{\"@type\":\"g:Int32\",\"@value\":29}],\"name\":[\"marko\"]}";
var reader = CreateStandardGraphSONReader(version);
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
index 77af255..a352a3e 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/GraphSONWriterTests.cs
@@ -33,65 +33,86 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
{
public class GraphSONWriterTests
{
- private GraphSONWriter CreateStandardGraphSONWriter()
+ /// <summary>
+ /// Parameters for each test supporting multiple versions of GraphSON
+ /// </summary>
+ public static IEnumerable<object[]> Versions => new []
{
- return new GraphSONWriter();
+ new object[] { 2 },
+ new object[] { 3 }
+ };
+
+ /// <summary>
+ /// Parameters for each collections test supporting multiple versions of GraphSON
+ /// </summary>
+ public static IEnumerable<object[]> VersionsSupportingCollections => new []
+ {
+ new object[] { 3 }
+ };
+
+ private GraphSONWriter CreateGraphSONWriter(int version)
+ {
+ if (version == 3)
+ {
+ return new GraphSON3Writer();
+ }
+ return new GraphSON2Writer();
}
- [Fact]
- public void ShouldSerializeInt()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeInt(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var graphSon = writer.WriteObject(1);
Assert.Equal("{\"@type\":\"g:Int32\",\"@value\":1}", graphSon);
}
- [Fact]
- public void ShouldSerializeLong()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeLong(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var graphSon = writer.WriteObject((long) 2);
Assert.Equal("{\"@type\":\"g:Int64\",\"@value\":2}", graphSon);
}
- [Fact]
- public void ShouldSerializeFloat()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeFloat(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var graphSon = writer.WriteObject((float) 3.2);
Assert.Equal("{\"@type\":\"g:Float\",\"@value\":3.2}", graphSon);
}
- [Fact]
- public void ShouldSerializeDouble()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeDouble(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var graphSon = writer.WriteObject(3.2);
Assert.Equal("{\"@type\":\"g:Double\",\"@value\":3.2}", graphSon);
}
- [Fact]
- public void ShouldSerializeBoolean()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeBoolean(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var graphSon = writer.WriteObject(true);
Assert.Equal("true", graphSon);
}
- [Fact]
- public void ShouldSerializeArray()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeArray(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var array = new[] {5, 6};
var serializedGraphSON = writer.WriteObject(array);
@@ -100,10 +121,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSON, serializedGraphSON);
}
- [Fact]
- public void ShouldSerializeBinding()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeBinding(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var binding = new Binding("theKey", 123);
var graphSon = writer.WriteObject(binding);
@@ -120,7 +141,7 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
{
{typeof(TestClass), new TestGraphSONSerializer {TestNamespace = "NS"}}
};
- var writer = new GraphSONWriter(customSerializerByType);
+ var writer = new GraphSON2Writer(customSerializerByType);
var testObj = new TestClass {Value = "test"};
var serialized = writer.WriteObject(testObj);
@@ -136,17 +157,17 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
{
{typeof(int), customSerializerMock.Object}
};
- var writer = new GraphSONWriter(customSerializerByType);
+ var writer = new GraphSON2Writer(customSerializerByType);
writer.WriteObject(12);
customSerializerMock.Verify(m => m.Dictify(It.Is<int>(v => v == 12), It.IsAny<GraphSONWriter>()));
}
- [Fact]
- public void ShouldSerializeDateTime()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeDateTime(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var dateTime = TestUtils.FromJavaTime(1475583442552);
var graphSon = writer.WriteObject(dateTime);
@@ -155,10 +176,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expected, graphSon);
}
- [Fact]
- public void ShouldSerializeDictionary()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeDictionary(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var dictionary = new Dictionary<string, dynamic>
{
{"age", new List<int> {29}},
@@ -171,10 +192,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSON, serializedDict);
}
- [Fact]
- public void ShouldSerializeEdge()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeEdge(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var edge = new Edge(7, new Vertex(0, "person"), "knows", new Vertex(1, "dog"));
var graphSON = writer.WriteObject(edge);
@@ -184,10 +205,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expected, graphSON);
}
- [Fact]
- public void ShouldSerializeEnum()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeEnum(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var serializedEnum = writer.WriteObject(Direction.Both);
@@ -195,10 +216,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSON, serializedEnum);
}
- [Fact]
- public void ShouldSerializeList()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeList(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var list = new List<int> {5, 6};
var serializedGraphSON = writer.WriteObject(list.ToArray());
@@ -207,10 +228,49 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSON, serializedGraphSON);
}
- [Fact]
- public void ShouldSerializePredicateWithTwoValues()
+ [Theory, MemberData(nameof(VersionsSupportingCollections))]
+ public void ShouldSerializeGList(int version)
+ {
+ var writer = CreateGraphSONWriter(version);
+ var list = new List<object> {5, 6};
+
+ var serializedGraphSON = writer.WriteObject(list);
+
+ var expectedGraphSON = "{\"@type\":\"g:List\",\"@value\":[{\"@type\":\"g:Int32\",\"@value\":5}," +
+ "{\"@type\":\"g:Int32\",\"@value\":6}]}";
+ Assert.Equal(expectedGraphSON, serializedGraphSON);
+ }
+
+ [Theory, MemberData(nameof(VersionsSupportingCollections))]
+ public void ShouldSerializeGSet(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
+ ISet<object> set = new HashSet<object> {600L, 700L};
+
+ var serializedGraphSON = writer.WriteObject(set);
+
+ var expectedGraphSON = "{\"@type\":\"g:Set\",\"@value\":[{\"@type\":\"g:Int64\",\"@value\":600}," +
+ "{\"@type\":\"g:Int64\",\"@value\":700}]}";
+ Assert.Equal(expectedGraphSON, serializedGraphSON);
+ }
+
+ [Theory, MemberData(nameof(VersionsSupportingCollections))]
+ public void ShouldSerializeGMap(int version)
+ {
+ var writer = CreateGraphSONWriter(version);
+ IDictionary<object, object> map = new Dictionary<object, object> { { 1L, "a"}, { 200L, "b"}};
+
+ var serializedGraphSON = writer.WriteObject(map);
+
+ var expectedGraphSON = "{\"@type\":\"g:Map\",\"@value\":[{\"@type\":\"g:Int64\",\"@value\":1},\"a\"," +
+ "{\"@type\":\"g:Int64\",\"@value\":200},\"b\"]}";
+ Assert.Equal(expectedGraphSON, serializedGraphSON);
+ }
+
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializePredicateWithTwoValues(int version)
+ {
+ var writer = CreateGraphSONWriter(version);
var predicate = new TraversalPredicate("within", new List<int> {1, 2});
var serializedPredicate = writer.WriteObject(predicate);
@@ -220,10 +280,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSON, serializedPredicate);
}
- [Fact]
- public void ShouldSerializePredicateWithSingleValue()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializePredicateWithSingleValue(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var predicate = new TraversalPredicate("lt", 5);
var serializedPredicate = writer.WriteObject(predicate);
@@ -233,10 +293,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expectedGraphSON, serializedPredicate);
}
- [Fact]
- public void ShouldSerializePropertyWithEdgeElement()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializePropertyWithEdgeElement(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var property = new Property("aKey", "aValue", new Edge("anId", new Vertex(1), "edgeLabel", new Vertex(2)));
var graphSON = writer.WriteObject(property);
@@ -246,10 +306,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expected, graphSON);
}
- [Fact]
- public void ShouldSerializePropertyWithVertexPropertyElement()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializePropertyWithVertexPropertyElement(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var property = new Property("name", "marko",
new VertexProperty("anId", "aKey", 21345, new Vertex("vertexId")));
@@ -260,10 +320,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expected, graphSON);
}
- [Fact]
- public void ShouldSerializeVertexProperty()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeVertexProperty(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var vertexProperty = new VertexProperty("blah", "keyA", true, new Vertex("stephen"));
var graphSON = writer.WriteObject(vertexProperty);
@@ -273,10 +333,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expected, graphSON);
}
- [Fact]
- public void ShouldSerializeGuid()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeGuid(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var guid = Guid.Parse("41d2e28a-20a4-4ab0-b379-d810dede3786");
var graphSon = writer.WriteObject(guid);
@@ -285,10 +345,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expected, graphSon);
}
- [Fact]
- public void ShouldSerializeVertex()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeVertex(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var vertex = new Vertex(45.23f);
var graphSON = writer.WriteObject(vertex);
@@ -298,10 +358,10 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expected, graphSON);
}
- [Fact]
- public void ShouldSerializeVertexWithLabel()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeVertexWithLabel(int version)
{
- var writer = CreateStandardGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var vertex = new Vertex((long) 123, "project");
var graphSON = writer.WriteObject(vertex);
http://git-wip-us.apache.org/repos/asf/tinkerpop/blob/8cdc941f/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/StrategyWriterTests.cs
----------------------------------------------------------------------
diff --git a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/StrategyWriterTests.cs b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/StrategyWriterTests.cs
index 4bdb141..5a04b54 100644
--- a/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/StrategyWriterTests.cs
+++ b/gremlin-dotnet/test/Gremlin.Net.UnitTest/Structure/IO/GraphSON/StrategyWriterTests.cs
@@ -21,6 +21,7 @@
#endregion
+using System.Collections.Generic;
using Gremlin.Net.Process.Traversal;
using Gremlin.Net.Process.Traversal.Strategy.Decoration;
using Gremlin.Net.Structure.IO.GraphSON;
@@ -30,16 +31,29 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
{
public class StrategyWriterTests
{
- private GraphSONWriter CreateGraphSONWriter()
+ /// <summary>
+ /// Parameters for each test supporting multiple versions of GraphSON
+ /// </summary>
+ public static IEnumerable<object[]> Versions => new []
{
- return new GraphSONWriter();
+ new object[] { 2 },
+ new object[] { 3 }
+ };
+
+ private GraphSONWriter CreateGraphSONWriter(int version)
+ {
+ if (version == 3)
+ {
+ return new GraphSON3Writer();
+ }
+ return new GraphSON2Writer();
}
- [Fact]
- public void ShouldSerializeSubgraphStrategyWithoutValues()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldSerializeSubgraphStrategyWithoutValues(int version)
{
var subgraphStrategy = new SubgraphStrategy();
- var writer = CreateGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var graphSon = writer.WriteObject(subgraphStrategy);
@@ -47,14 +61,14 @@ namespace Gremlin.Net.UnitTest.Structure.IO.GraphSON
Assert.Equal(expected, graphSon);
}
- [Fact]
- public void ShouldDeserializeSubgraphStrategyWithVertexCriterion()
+ [Theory, MemberData(nameof(Versions))]
+ public void ShouldDeserializeSubgraphStrategyWithVertexCriterion(int version)
{
var vertexCriterionBytecode = new Bytecode();
vertexCriterionBytecode.AddStep("has", "name", "marko");
var vertexCriterion = new TestTraversal(vertexCriterionBytecode);
var subgraphStrategy = new SubgraphStrategy(vertexCriterion);
- var writer = CreateGraphSONWriter();
+ var writer = CreateGraphSONWriter(version);
var graphSon = writer.WriteObject(subgraphStrategy);