Deserialize JSON into multiple inherited classes

孤人 提交于 2021-02-11 09:22:49

问题


When I serialize my JSON object out of DocumentDB, my Control is not deserializing into the OptionsControl with the Options property.

I have the following class, Control:

public class Control : IControl
{
    public Guid Id { get; set; }

    public virtual Enums.ControlType Type { get; set; }

    public string PropertyName { get; set; }

    public string ControlCssClass { get; set; }

    public string Description { get; set; }
}

I also have OptionsControl, which inherits from Control:

public class OptionsControl : Control
{
    public IDictionary<string, string> Options;
}

I also have a ClickableControl:

public class ClickableControl : Control
{
    public string Url { get; set; }
    public string UrlTarget { get; set; }
}

I used the Document Explorer in Azure to put this JSON in document in a DocumentDB collection:

Rows:
[
    {
        Controls:
        [
            {
              "PropertyName": "Relationship",
              "ControlCssClass": "",
              "Description": "",
              "Type": 3,
              "Options": [
                  {
                    "Key": "Spouse",
                    "Value": "Spouse"
                  },
                  {
                    "Key": "Child",
                    "Value": "Child"
                  },
                  {
                    "Key": "Step-child",
                    "Value": "Step-child"
                  }
              ],
           }
        ]
    }
]

When I pull the data out of DocumentDB, I attempt to serialize it into my Row class:

public class Row
{
    public IList<Control> Controls { get; set; }
}

I need to be able to put any type of "Control" in my Control list in DocDB and have C# deserialize that list back into the proper Control class (be that the base Control class, or one of the derivatives like OptionsControl or ClickableControl).

The problem is since I'm deserializing into Control, I get all of the properties on the Control except for Options. Or if I try to deserialize one that has Url and UrlTarget, I just get the base Control properties and not the URL properties. I thought C# would handle turning the deserialized object into an OptionsControl or a ClickableControl, but I guess that is incorrect? What do I need to do so that the JSON object from DocumentDB serializes properly and turns into an OptionsControl (has the Options property) instead of just the base Control?


回答1:


You could try serializing your objects yourself with Json.NET and then posting the serialized content into DocumentDb. Then, when you need the data, read it back as a json string and use Json.NET again to deserialize. Json.NET can handle inheritance, so you just have to configure it to be aware of your type hierarchy. Use the TypeNameHandling setting:

http://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm

JsonConvert.DefaultSettings = () => new JsonSerializerSettings
{
    TypeNameHandling = TypeNameHandling.All
};


来源:https://stackoverflow.com/questions/42858166/deserialize-json-into-multiple-inherited-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!