简体   繁体   中英

Serialize/Deserialize Json DateTime in Neo4jClient

I use Neo4jClient to use Neo4j, I use cypher code for CRUD entity , Follow code :

_graphClient.Cypher.Merge("(n:Movie { Id:101 })")
            .Set("n.Key = 55,n.DateTime='" +DateTime.UtcNow.ToString()+"'").ExecuteWithoutResults();

_graphClient.Cypher
            .Match("(n:Movie)-[r:RelName]-(m:Movie)")
            .Where((EntityNode n) => n.Id == 20)
            .Return.......

public class EntityNode
{
    public int Id { get; set; }
    public string Key { get; set; }
    public DateTime DateTime { get; set; }
}

ERROR :Neo4j returned a valid response, however Neo4jClient was unable to deserialize into the object structure you supplied.Can't deserialize DateTime .

On other hand i use jsonconvertor in different ways , for example :

   _graphClient.Cypher.Merge("(n:Movie { Id:101 })")
                .Set("n.Key = 55,n.DateTime=" +JsonConvert.SerializeObject(DateTime.UtcNow)).ExecuteWithoutResults();

I still have the ERROR

Pass it as a proper parameter:

graphClient.Cypher
    .Merge("(n:Movie { Id:101 })")
    .Set("n.Key = {key}, n.DateTime = {time}")
    .WithParams(new {
        key = 55,
        time = DateTimeOffset.UtcNow
    })
    .ExecuteWithoutResults();

This way, Neo4jClient will do the serialization for you, and you don't introduce lots of security and performance issues.

This is in the doco here: https://github.com/Readify/Neo4jClient/wiki/cypher#parameters

I have faced the same issue recently its because of date time value coming from neo.

I have stored the date time in neo as epoch time but while retrieving i used long in the class. because of this its given me the above error.

Try using string for the above date time.

Hope this helps you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM