我使用json.net来序列化/反序列化字典。问题是,在反序列化时,某些对象被重新创建为null。只有在使用PreserveReferenceHandling时才会发生这种情况。代码:
Dictionary<string, object> data = new Dictionary<string, object>();
data["Roles"] = (from r in Roles
select new { Id = r.Id, Name = r.Name }).ToList();
data["CompanyCourses"] = (from cvc in CompanyVideoCourses
where cvc.Company.Id == location.Company.Id
select cvc).ToList();
data["VideoCourses"] = (from vc in VideoCourses
select vc).ToList();
data["CompanyCourses"] = (from cvc in CompanyVideoCourses
where cvc.Company.Id == location.Company.Id
select cvc).ToList();
data["Location"] = location;
string output = JsonConvert.SerializeObject(data, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects});
Dictionary<string, object> newdata = new Dictionary<string, object>();
newdata = JsonConvert.DeserializeObject<Dictionary<string, object>>(output,
new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects });
JArray roles = JsonConvert.DeserializeObject<dynamic>(newdata["Roles"].ToString());
//problem 1 here
List<VideoCourse> videoCourses = JsonConvert.DeserializeObject<List<VideoCourse>>(newdata["VideoCourses"].ToString());
//problem 2 here
List<CompanyVideoCourse> companyCourses = JsonConvert.DeserializeObject<List<CompanyVideoCourse>>(newdata["CompanyCourses"].ToString());
Location location1 = JsonConvert.DeserializeObject<Location>(newdata["Location"].ToString());问题变体1-系统有视频课程。每个公司都可以被分配多个课程。在这次测试中,有一半的课程是分配给公司的。当VideoCourses被反序列化时,没有分配给公司的任何课程都是空的。
问题变体2--如果我改变字典创建的顺序,将启动data["CompanyCourses"] =的行放在行data["VideoCourses"] =之后,那么当执行List<CompanyVideoCourse> companyCourses = ...时,VideoCourse属性为null (这是一个带有附加数据的联接表)。
问题变体1和2取决于字典的排序。如果一种情况发生,另一种则不发生。
我检查了生成的json,所有的$id和$ref值看起来都很好。如前所述,如果使用PreserveReferencesHandling,这只是一个问题。如果它没有被使用,那么所有的事情都会像预期的那样工作,不管字典是按照什么顺序创建的。
所以问题是:我如何重新创建我所有的对象?
更新
下面是一个显示问题的生成的json示例。
{
"$id": "1",
"CompanyCourses": [
{
"$id": "2",
"Company": {
"$id": "3",
"Id": 1,
"Name": "Company0",
"Address": "123 e main",
"ContactName": "Joe Contact",
"ContactPhone": "2065551212",
"ContactEmail": "joec@contactcompany.com",
"Maritime_Id": "19360"
},
"VideoCourse": {
"$id": "4",
"InternetLocation": "5QyZRnYt",
"SKU": "DVD0-18",
"FileName": "Access-Control.mp4",
"CoverFileName": "Access-Control-Cover.png",
"PosterFileName": "Access-Control-Poster.png",
"VTTFileName": "Access-Control.vtt",
"RunningTime": "00:14:00",
"Id": 11,
"Name": "Access Control: Threat Awareness and Prevention",
"Description": "<p>Understand and prevent the threat of unauthorized access to your ship.</p>\r\n <p>Topics:</p>\r\n <ul>\r\n <li>Risk recognition</li>\r\n <li>Piracy and hijacking prevention</li>\r\n <li>The threat of unauthorized access</li>\r\n <li>Security and team-effort approach</li>\r\n <li>Controlling shipboard access in port</li>\r\n </ul>",
"Repeating": false,
"Interval": 0,
"Keywords": "ISPS"
},
"Id": 1,
"Corporate_Id": null
}
],
"Location": {
"$id": "14",
"Id": 1,
"Name": "ShipatSea Location0",
"Address": null,
"Company": {
"$ref": "3"
}
}
}当这被反序列化时,"Location“对象中的"Company”属性是空的,即使$id $ref指向("3")在顶部。
发布于 2014-04-14 23:24:44
结果证明,该解决方案是在序列化整个字典之前序列化字典的每个部分。这样,当每个字典值被反序列化时,它就拥有了自己需要的所有信息。因此,例如,这个:
data["Location"] = location;变成了
data["Location"] = JsonConvert.SerializeObject(location, new JsonSerializerSettings {
PreserveReferencesHandling = PreserveReferencesHandling.Objects
});https://stackoverflow.com/questions/23065792
复制相似问题