ReplaceAllDateInstacesWithNeoDates() public static method

public static ReplaceAllDateInstacesWithNeoDates ( string content ) : string
content string
return string
Ejemplo n.º 1
0
        public PartialDeserializationContext CheckForErrorsInTransactionResponse(string content)
        {
            if (!inTransaction)
            {
                throw new InvalidOperationException("Deserialization of this type must be done inside of a transaction scope.");
            }

            var context = new DeserializationContext
            {
                Culture        = culture,
                JsonConverters = Enumerable.Reverse(client.JsonConverters ?? new List <JsonConverter>(0)).ToArray()
            };

            content = CommonDeserializerMethods.ReplaceAllDateInstacesWithNeoDates(content);

            var reader = new JsonTextReader(new StringReader(content))
            {
                DateParseHandling = DateParseHandling.None// DateParseHandling.DateTimeOffset
            };

            var root = JToken.ReadFrom(reader).Root as JObject;

            return(new PartialDeserializationContext
            {
                RootResult = GetRootResultInTransaction(root),
                DeserializationContext = context
            });
        }
Ejemplo n.º 2
0
        public IEnumerable <TResult> Deserialize(string content)
        {
            try
            {
                var context = new DeserializationContext
                {
                    Culture              = culture,
                    JsonConverters       = Enumerable.Reverse(client.JsonConverters ?? new List <JsonConverter>(0)).ToArray(),
                    JsonContractResolver = client.JsonContractResolver
                };
                content = CommonDeserializerMethods.ReplaceAllDateInstacesWithNeoDates(content);

                var reader = new JsonTextReader(new StringReader(content))
                {
                    DateParseHandling = DateParseHandling.DateTimeOffset
                };

                // Force the deserialization to happen now, not later, as there's
                // not much value to deferred execution here and we'd like to know
                // about any errors now
                return(inTransaction
                    ? FullDeserializationFromTransactionResponse(reader, context).ToArray()
                    : DeserializeFromRoot(content, reader, context).ToArray());
            }
            catch (Exception ex)
            {
                // we want the NeoException to be thrown
                if (ex is NeoException)
                {
                    throw;
                }

                const string messageTemplate =
                    @"Neo4j returned a valid response, however Neo4jClient was unable to deserialize into the object structure you supplied.

First, try and review the exception below to work out what broke.

If it's not obvious, you can ask for help at http://stackoverflow.com/questions/tagged/neo4jclient

Include the full text of this exception, including this message, the stack trace, and all of the inner exception details.

Include the full type definition of {0}.

Include this raw JSON, with any sensitive values replaced with non-sensitive equivalents:

{1}";
                var message = string.Format(messageTemplate, typeof(TResult).FullName, content);

                // If it's a specifc scenario that we're blowing up about, put this front and centre in the message
                var deserializationException = ex as DeserializationException;
                if (deserializationException != null)
                {
                    message = deserializationException.Message + "\r\n\r\n----\r\n\r\n" + message;
                }

                throw new ArgumentException(message, "content", ex);
            }
        }
Ejemplo n.º 3
0
        public T Deserialize <T>(string content) where T : new()
        {
            var context = new DeserializationContext
            {
                Culture              = culture,
                JsonConverters       = (jsonConverters ?? new List <JsonConverter>(0)).Reverse().ToArray(),
                JsonContractResolver = jsonResolver
            };

            content = CommonDeserializerMethods.ReplaceAllDateInstacesWithNeoDates(content);

            var reader = new JsonTextReader(new StringReader(content))
            {
                DateParseHandling = DateParseHandling.None
            };

            var target = new T();

            if (target is IList)
            {
                var objType = target.GetType();
                var json    = JToken.ReadFrom(reader);
                target = (T)CommonDeserializerMethods.BuildList(context, objType, json.Root.Children(), new TypeMapping[0], 0);
            }
            else if (target is IDictionary)
            {
                var root = JToken.ReadFrom(reader).Root;
                target = (T)CommonDeserializerMethods.BuildDictionary(context, target.GetType(), root.Children(), new TypeMapping[0], 0);
            }
            else
            {
                var root = JToken.ReadFrom(reader).Root;
                CommonDeserializerMethods.Map(context, target, root, new TypeMapping[0], 0);
            }

            return(target);
        }
Ejemplo n.º 4
0
        IEnumerable <TResult> DeserializeInternal(string content)
        {
            var context = new DeserializationContext
            {
                Culture              = culture,
                JsonConverters       = Enumerable.Reverse(client.JsonConverters ?? new List <JsonConverter>(0)).ToArray(),
                JsonContractResolver = client.JsonContractResolver
            };

            content = CommonDeserializerMethods.ReplaceAllDateInstacesWithNeoDates(content);

            var reader = new JsonTextReader(new StringReader(content))
            {
                DateParseHandling = DateParseHandling.None
            };
            var root = JToken.ReadFrom(reader).Root;

            var columnsArray = (JArray)root["columns"];
            var columnNames  = columnsArray
                               .Children()
                               .Select(c => c.AsString())
                               .ToArray();

            var jsonTypeMappings = new List <TypeMapping>
            {
                new TypeMapping
                {
                    ShouldTriggerForPropertyType = (nestingLevel, type) =>
                                                   type.GetTypeInfo().IsGenericType&&
                                                   type.GetGenericTypeDefinition() == typeof(Node <>),
                    DetermineTypeToParseJsonIntoBasedOnPropertyType = t =>
                    {
                        var nodeType = t.GetGenericArguments();
                        return(typeof(NodeApiResponse <>).MakeGenericType(nodeType));
                    },
                    MutationCallback = n => n.GetType().GetMethod("ToNode").Invoke(n, new object[] { client })
                },
                new TypeMapping
                {
                    ShouldTriggerForPropertyType = (nestingLevel, type) =>
                                                   type.GetTypeInfo().IsGenericType&&
                                                   type.GetGenericTypeDefinition() == typeof(RelationshipInstance <>),
                    DetermineTypeToParseJsonIntoBasedOnPropertyType = t =>
                    {
                        var relationshipType = t.GetGenericArguments();
                        return(typeof(RelationshipApiResponse <>).MakeGenericType(relationshipType));
                    },
                    MutationCallback = n => n.GetType().GetMethod("ToRelationshipInstance").Invoke(n, new object[] { client })
                }
            };

            switch (resultMode)
            {
            case CypherResultMode.Set:
                return(ParseInSingleColumnMode(context, root, columnNames, jsonTypeMappings.ToArray()));

            case CypherResultMode.Projection:
                jsonTypeMappings.Add(new TypeMapping
                {
                    ShouldTriggerForPropertyType = (nestingLevel, type) =>
                                                   nestingLevel == 0 && type.GetTypeInfo().IsClass,
                    DetermineTypeToParseJsonIntoBasedOnPropertyType = t =>
                                                                      typeof(NodeOrRelationshipApiResponse <>).MakeGenericType(new[] { t }),
                    MutationCallback = n =>
                                       n.GetType().GetProperty("Data").GetGetMethod().Invoke(n, new object[0])
                });
                return(ParseInProjectionMode(context, root, columnNames, jsonTypeMappings.ToArray()));

            default:
                throw new NotSupportedException(string.Format("Unrecognised result mode of {0}.", resultMode));
            }
        }