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



我正在使用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被反序列化时,任何分配给公司的课程都为null。

问题变体2-如果我通过将开始data["CompanyCourses"] =的行放在data["VideoCourses"] =之后来更改字典创建的顺序,那么当执行List<CompanyVideoCourse> companyCourses = ...时,VideoCourse属性为null(这是一个包含额外数据的联接表)。

问题变体1和2取决于字典的顺序。如果其中一个发生了,另一个就不会。

我已经检查了生成的json,所有的$id和$ref值看起来都很好。如上所述,只有在使用PreserveReferences Handling的情况下,这才是一个问题。如果不使用它,那么无论词典的创建顺序如何,一切都会按预期进行。

所以问题是:如何重新创建所有的对象?

更新

下面是一个显示问题的生成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>rn                             <p>Topics:</p>rn                             <ul>rn                             <li>Risk recognition</li>rn                             <li>Piracy and hijacking prevention</li>rn                             <li>The threat of unauthorized access</li>rn                             <li>Security and team-effort approach</li>rn                             <li>Controlling shipboard access in port</li>rn                             </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")位于顶部。

解决方案是先序列化字典的每个部分,然后再序列化整个字典。这样,当每个字典值被反序列化时,它就拥有了独立所需的所有信息。例如:

 data["Location"] = location;

成为

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

相关内容

  • 没有找到相关文章

最新更新