示例#1
0
        /// <summary>
        ///     Transforms an object into its GraphSON representation including type information.
        /// </summary>
        /// <param name="objectData">The object to transform.</param>
        /// <returns>A GraphSON representation of the object ready to be serialized.</returns>
        public virtual dynamic ToDict(dynamic objectData)
        {
            var type = objectData.GetType();

            IGraphSONSerializer serializer = TryGetSerializerFor(type);

            if (serializer != null)
            {
                return(serializer.Dictify(objectData, this));
            }
            if (IsDictionaryType(type))
            {
                return(DictToGraphSONDict(objectData));
            }
            if (IsCollectionType(type))
            {
                return(CollectionToGraphSONCollection(objectData));
            }
            return(objectData);
        }
        /// <summary>
        ///     Transforms an object into its GraphSON representation including type information.
        /// </summary>
        /// <param name="objectData">The object to transform.</param>
        /// <param name="result">Output parameter with the GraphSON representation of the object ready to be serialized.</param>
        /// <returns>True if serialiation is successful and the output parameter was populated.</returns>
        public bool TryToDict(dynamic objectData, out dynamic result)
        {
            if (objectData == null)
            {
                result = null;
                return(true);
            }

            if (objectData is IGraphNode graphNode)
            {
                if (!(graphNode is GraphNode concreteGraphNode))
                {
                    throw new InvalidOperationException("Serialization of custom IGraphNode implementations is not supported.");
                }

                result = concreteGraphNode.GetRaw();
                return(true);
            }

            var type = objectData.GetType();

            if (_customSerializers != null)
            {
                var customSerializer = (IGraphSONSerializer)TryGetSerializerFor(_customSerializers, type);

                if (customSerializer != null)
                {
                    result = customSerializer.Dictify(objectData, Writer);
                    return(true);
                }
            }

            IGraphSONSerializer serializer = TryGetSerializerFor(Serializers, type);

            if (serializer != null)
            {
                result = serializer.Dictify(objectData, Writer);
            }
            else if (type == typeof(string))
            {
                result = objectData;
            }
            else if (IsSet(type))
            {
                result = SetToGraphSONSet(objectData);
            }
            else if (IsDictionary(objectData))
            {
                result = DictToGraphSONDict(objectData);
            }
            else if (IsEnumerable(objectData))
            {
                result = ListToGraphSONList(objectData);
            }
            else
            {
                return(TryHandleNotSupportedType(type, objectData, out result));
            }

            return(true);
        }