我有一个 Web API 项目,它可以从 JSON 中对定义为以下内容的对象进行水合。我正在尝试将此对象插入 RavenDB 数据库,但发现动态属性“内容”未正确序列化(注意空数组)。
我尝试了几种序列化程序来生成 json 字符串:System.Helpers.Json.Encode()、System.Web.Script.Serialization.JavaScriptSerializer。两者都面临同样的问题。
RavenJObject.fromObject(obj) 也有同样的问题。
尽管 CLR 反射有明显的限制,有没有办法完成我的目标?
public class SampleType
{
public Guid? Id { get; private set; }
public dynamic Content { get; set; }
public string Message { get; set; }
public string Actor { get; set; }
public LogEntry()
{
Id = Guid.NewGuid();
}
}
JSON submitted to API:
{
"Content": {
"SomeNumber": 5,
"ADate": "/Date(1360640329155)/",
"MaybeABoolean": true,
"EmptyGUID": "00000000-0000-0000-0000-000000000000"
},
"Message": "Hey there",
"Actor": "John Dow"
}
Hydrated object:
ID: {b75d9134-2fd9-4c89-90f7-a814fa2f244d}
Content: {
"SomeNumber": 5,
"ADate": "2013-02-12T04:37:44.029Z",
"MaybeABoolean": true,
"EmptyGUID": "00000000-0000-0000-0000-000000000000"
}
Message: "Hey there",
Actor: "John Dow"
JSON from all three methods:
{
"Id": "b75d9134-2fd9-4c89-90f7-a814fa2f244d",
"Content": [
[
[]
],
[
[]
],
[
[]
],
[
[]
]
],
"Message": "Hey there",
"Actor": "John Dow"
}