3

I'm trying to get strongly typed objects back out of Neo4j using the C# client. This all works until I add a DateTime property.

I've successfully inserted data into the Neo4j database and I can see it using the console. I can also query the data, but I can't return any strongly typed objects because the deserialization seems to fail.

I'm using parameters to insert the data:

_graphClient.Cypher
.WithParams(new
{
    id = node.Id,
    createdAt = node.CreatedAt,
    lastModified = node.LastModified              
})
.Create("(c { " +
    "Id: {id}, " +
    "CreatedAt: {createdAt}, " +
    "LastModified: {lastModified} } )")

My query to get the data is pretty basic:

nodes = _graphClient.Cypher
.Match("(n)")
.Return((n) => n.As<NeoObject>()).Results.ToList();

But then I receive an error...

The log file states the following:

Parameter name: content ---> Newtonsoft.Json.JsonReaderException: Could not convert string to DateTime: 17-9-2015 21:57:14 +00:00. Path 'a', line 1, position 32.

The data looks like this (entry from log):

"data" : {
  "Id" : 31,
  "LastModified" : "2015-09-17T21:57:14Z",
  "CreatedAt" : "2015-09-17T21:57:14Z",
}

My c# type definitions:

public class NeoObject
{
    public int Id { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime LastModified { get; set; }
}

or

public class NeoObject2
{
    public int Id { get; set; }
    public DateTime? CreatedAt { get; set; }
    public DateTime? LastModified { get; set; }
}
Ropstah
  • 17,538
  • 24
  • 120
  • 194
  • 1
    The date format in your error message is European format. Take a look at this question. It is not a duplicate, but it may help. http://stackoverflow.com/questions/21256132/deserializing-dates-with-dd-mm-yyyy-format-using-json-net – Olivier De Meulder Oct 29 '15 at 20:22

1 Answers1

3

If I recall correctly you need to use a DateTimeOffset type as your property.

public class NeoObject
{
    public int Id { get; set; }
    public DateTimeOffset CreatedAt { get; set; }
    public DateTimeOffset LastModified { get; set; }
}

Edit This used to be the case but it appears that a recent update added support for DateTime object types. what version of Neo4jClient are you using? Have you tried DateTimeOffset?

ceej
  • 1,863
  • 1
  • 15
  • 24
  • Great. The change my edit refers to can be seen in a pull request here https://github.com/Readify/Neo4jClient/pull/100/files. Be interesting to know if the client version you are using is meant to allow DateTime. I am sure @ChrisSkardon would be interested. – ceej Oct 29 '15 at 22:55
  • I tried updating to the latest version (1.1.0.11) but that still didn't allow me to use the DateTime object... - EDIT: It does work with `DateTime`, I had another reference in another project, rebuilding and updating did the trick! Thanks for the lead. – Ropstah Oct 29 '15 at 23:50