0

I'm using NodaTime for all of our various time stamping on our raven documents. I'm injecting the IClock interface using :

IClock clock = SystemClock.Instance;`

The problem is, I'm having a weird issue when I'm serializing a LocalDateTime into Raven. What's strange is that it's being used elsewhere, but the formatting is coming out fine. I'm running a series of patch commands to update only the SavedDistricts portion of the document :

new PatchCommandData
{
     Key = String.Format("Users/{0}", i),
     Patches = (from s in x 
               select new PatchRequest {Type = PatchCommandType.Add,
                                        Name = "SavedDistricts",
                                        Value = RavenJObject.FromObject(s)
                                       }).ToArray()
} 

My object properties :

public class User
{
     public string Email { get; set; }
     public string Name { get; set; }
     public LocalDateTime LastModified { get; set; }
     public LocalDateTime CreateDate { get; set; },
     public List<District> SavedDistricts { get; set; }
}    

public class District
{   
    public string Id {get; set; }
    public LocalDateTime? LastVisited { get; set; }
    public LocalDateTime LastModified { get; set; }
}

The way I'm setting the values :

LastModified = _clock.Now.InUtc().LocalDateTime,
LastVisited = LocalDateTime.FromDateTime(userSourceTableEntity.LastVisited.ToLocalTime()),

My document ends up looking like :

{
    "Email": "xxxxxx@gmail.com",
    "Name": "xxxxxxxxx",
    "LastModified": "2013-08-25T08:30:11.0330000",
    "CreateDate": "2013-08-25T08:28:25.6130000",
    "SavedDistricts": [
         {
             "Id": "athos",
             "LastVisited": {
                  "ticks": 13774338000000000,
                  "calendar": "ISO"
             },
             "LastModified": {
                 "ticks": 14116967307765180,
                 "calendar": "ISO"
             }
         }
     ]
}

What am I doing wrong here? I have to be missing something in the serialization of these...

X3074861X
  • 3,709
  • 5
  • 32
  • 45
  • Please clarify: Are you using my [RavenDB-NodaTime integration library](https://github.com/mj1856/RavenDB-NodaTime) and just having trouble with patching? – Matt Johnson-Pint Sep 29 '14 at 15:53
  • I'd like to help resolve this. Is this an issue with [RavenDB-NodaTime](https://github.com/mj1856/RavenDB-NodaTime), or are you rolling your own custom serialization? Or trying to use it directly without customizing the serializers, or something else? – Matt Johnson-Pint Oct 07 '14 at 19:45
  • @MattJohnson I was trying to use it directly without customizing the serializers... which I can't expect a patch command to know what to do with a `LocalDateTime` without giving it a serializer to use. I believe we are using the RavenDB-NodaTime integration library elsewhere in the project, but this more raven specific. Thank you for following up! – X3074861X Oct 08 '14 at 17:45

2 Answers2

1

I guess that in this code

new PatchCommandData
{
    Key = String.Format("Users/{0}", i),
    Patches = (from s in x 
              select new PatchRequest {Type = PatchCommandType.Add,
                                       Name = "SavedDistricts",
                                       Value = RavenJObject.FromObject(s)
                                      }).ToArray()
} 

the serialization goes wrong and RavenJObject.FromObject tries to serialize the properties of NodaTime. Maybe explicitly converting to string helps by calling

                                       Value = RavenJObject.FromObject(s.ToString())

helps.

Thomas Freudenberg
  • 5,048
  • 1
  • 35
  • 44
1

This was actually a simple fix, but it was difficult to narrow down.

I wasn't giving the RavenJObject.FromObject constructor a proper json serializer, so by default NodaTime was being represented as a two part object of Ticks and Calendar. All I needed to do was give it _docStore.Conventions.CreateSerializer().

Thus, my patch command ended up looking like this :

new PatchCommandData
{
    Key = String.Format("Users/{0}", i),
    Patches = (from s in x 
              select new PatchRequest {Type = PatchCommandType.Add,
                                       Name = "SavedDistricts",
                                       Value = RavenJObject.FromObject(s, _docStore.Conventions.CreateSerializer())
                                      }).ToArray()
}

Worked perfectly, my LocalDateTime's are now represented as strings.

X3074861X
  • 3,709
  • 5
  • 32
  • 45