Example #1
0
        /// <inheritdoc />
        public IGraphTypeSerializer CreateSerializer(
            IInternalSession session,
            IReadOnlyDictionary <GraphProtocol, IReadOnlyDictionary <string, IGraphSONDeserializer> > customDeserializers,
            IReadOnlyDictionary <GraphProtocol, IReadOnlyDictionary <Type, IGraphSONSerializer> > customSerializers,
            GraphProtocol graphProtocol,
            bool deserializeGraphNodes)
        {
            switch (graphProtocol)
            {
            case GraphProtocol.GraphSON1:
                return(_graphSon1TypeSerializer);

            case GraphProtocol.GraphSON2:
            case GraphProtocol.GraphSON3:
                IReadOnlyDictionary <Type, IGraphSONSerializer>     serializers   = null;
                IReadOnlyDictionary <string, IGraphSONDeserializer> deserializers = null;
                customDeserializers?.TryGetValue(graphProtocol, out deserializers);
                customSerializers?.TryGetValue(graphProtocol, out serializers);
                var cacheKey = new CacheKey(graphProtocol, serializers, deserializers, deserializeGraphNodes);
                return(_graphTypeSerializers.GetOrAdd(cacheKey, k => new GraphTypeSerializer(
                                                          session,
                                                          graphProtocol,
                                                          deserializers,
                                                          serializers,
                                                          deserializeGraphNodes)));

            default:
                throw new DriverInternalError($"Invalid graph protocol: {graphProtocol.GetInternalRepresentation()}");
            }
        }
        public GraphTypeSerializer(
            IInternalSession session,
            GraphProtocol protocol,
            IReadOnlyDictionary <string, IGraphSONDeserializer> customDeserializers,
            IReadOnlyDictionary <Type, IGraphSONSerializer> customSerializers,
            bool deserializeGraphNodes)
        {
            _session       = session;
            _typeConverter = GraphTypeSerializer.DefaultTypeConverter;
            DefaultDeserializeGraphNodes = deserializeGraphNodes;
            GraphProtocol = protocol;

            customDeserializers = customDeserializers ?? GraphTypeSerializer.EmptyDeserializersDict;
            customSerializers   = customSerializers ?? GraphTypeSerializer.EmptySerializersDict;

            switch (protocol)
            {
            case GraphProtocol.GraphSON2:
                _reader = new CustomGraphSON2Reader(token => new GraphNode(new GraphSONNode(this, token)), customDeserializers, this);
                _writer = new CustomGraphSON2Writer(customSerializers, this);
                break;

            case GraphProtocol.GraphSON3:
                _reader = new CustomGraphSON3Reader(token => new GraphNode(new GraphSONNode(this, token)), customDeserializers, this);
                _writer = new CustomGraphSON3Writer(customSerializers, this);
                break;

            default:
                throw new ArgumentException($"Can not create graph type serializer for {protocol.GetInternalRepresentation()}");
            }

            _rowParser = row => new GraphNode(new GraphSONNode(this, row.GetValue <string>("gremlin")));
        }