我有这个类需要序列化为json:
[DataContract]
public class InfoRequest
{
[DataMember]
public string folder_id { get; set; }
[DataMember]
public string file_type_id { get; set; }
[DataMember]
public string prefix { get; set; }
[DataMember]
public fileInfo[] files;
[DataMember]
public termInfo[] terms;
}
[DataContract]
public class fileInfo
{
[DataMember]
public string name { get; set; }
[DataMember]
public string size { get; set; }
}
[DataContract]
public class termInfo
{
[DataMember]
public string term_id { get; set; }
[DataMember]
public string value { get; set; }
}
我尝试过ServiceStack.Text,NewtonSoft.Json和DataContractJsonSerializer。我从每个库得到不同的结果,并且没有一个保留DataMembers的顺序。 (ServiceStack甚至不会序列化所有DataMembers。)
将相同的InfoRequest对象传递给每个库,我得到这些字符串:
JsonConvert:
{
"files":[
{
"name":"Blip.txt",
"size":"448"
},
{
"name":"blip.jpg",
"size":"71535"
}
],
"terms":[
{
"term_id":"1000",
"value":"Copyright"
},
{
"term_id":"1000",
"value":"Copyright"
}
],
"folder_id":"11245",
"file_type_id":"234",
"prefix":"",
"ServiceStack":{
"folder_id":"11245",
"file_type_id":"234",
"prefix":""
},
"MS":{
"file_type_id":"234",
"files":[
{
"name":"Blip.txt",
"size":"448"
},
{
"name":"blip.jpg",
"size":"71535"
}
],
"folder_id":"11245",
"prefix":"",
"terms":[
{
"term_id":"1000",
"value":"Copyright"
},
{
"term_id":"1000",
"value":"Copyright"
}
]
}
}
我怀疑DataMembers的顺序应该不重要,但我从服务器收到错误,并认为这可能是问题。
任何见解将不胜感激......
答案 0 :(得分:0)
默认情况下,请注意ServiceStack's JSON Serializer仅限序列化工具公共属性,如果您还要包含公共字段(例如,还要包含序列化程序的“文件”和“条款”),则需要包括:
ServiceStack.Text.JsConfig.IncludePublicFields = true;
DataMembers does affect serialization in ServiceStack使所有字段选择加入,如果您想退出,请使用IgnoreDataMember
。