我有一個Web API項目,它將JSON中定義爲以下內容的對象進行水合。我試圖將這個對象插入到RavenDB數據庫中,但是我發現動態屬性'Content'沒有正確序列化(注意空數組)。將動態屬性序列化爲JSON
我已經嘗試了幾個序列化器來生成json strins: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"
}
這是關鍵。我結束了使用ExpandoObject類型。 – ebpa 2013-02-15 23:53:12