我正在使用NodaTime对我们的乌鸦文件进行各种时间戳。我正在使用:
注入IClock
接口
IClock clock = SystemClock.Instance;`
问题是,当我将LocalDateTime
序列化为Raven时,我遇到了一个奇怪的问题。奇怪的是,它正在其他地方使用,但格式化很好。我正在运行一系列补丁命令来仅更新文档的SavedDistricts
部分:
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()
}
我的对象属性:
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; }
}
我设置值的方式:
LastModified = _clock.Now.InUtc().LocalDateTime,
LastVisited = LocalDateTime.FromDateTime(userSourceTableEntity.LastVisited.ToLocalTime()),
我的文件最终看起来像:
{
"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"
}
}
]
}
我在这里做错了什么?我必须在这些序列化中遗漏一些东西...
答案 0 :(得分:1)
我猜这段代码
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()
}
序列化出错,RavenJObject.FromObject
尝试序列化NodaTime的属性。也许通过调用
string
帮助
Value = RavenJObject.FromObject(s.ToString())
帮助。
答案 1 :(得分:1)
这实际上是一个简单的修复,但很难缩小范围。
我没有给RavenJObject.FromObject
构造函数一个正确的json序列化程序,因此默认情况下NodaTime被表示为Ticks
和Calendar
的两部分对象。我需要做的就是给它_docStore.Conventions.CreateSerializer()
。
因此,我的补丁命令最终看起来像这样:
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()
}
完美地工作,我的LocalDateTime
现在被表示为字符串。