Json.net反序列化创建了null对象

时间:2014-04-14 16:52:36

标签: c# serialization json.net

我正在使用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 - 系统具有视频课程。每个公司都可以分配多个课程。在这个测试中,有一半的可用课程被分配给公司。对视频课程进行反序列化时,任何分配给公司的课程都为空。

问题变量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”属性为null,即使$ id $ ref指向(“3”)位于顶部。

1 个答案:

答案 0 :(得分:0)

在序列化整个字典之前,解决方案结果是序列化字典的每个部分。这样,当每个字典值被反序列化时,它具有它自己需要的所有信息。所以,例如,这个:

 data["Location"] = location;

变为

data["Location"] = JsonConvert.SerializeObject(location, new JsonSerializerSettings { 
             PreserveReferencesHandling = PreserveReferencesHandling.Objects
         });