问题描述
当我从DocumentDB中序列化JSON对象时,我的 Control
不会通过 Options
属性反序列化为 OptionsControl
.
When I serialize my JSON object out of DocumentDB, my Control
is not deserializing into the OptionsControl
with the Options
property.
我有以下课程, 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; }
}
我还有 OptionsControl
,它继承自 Control
:
public class OptionsControl : Control
{
public IDictionary<string, string> Options;
}
我还有一个 ClickableControl
:
public class ClickableControl : Control
{
public string Url { get; set; }
public string UrlTarget { get; set; }
}
我在Azure中使用文档浏览器将此JSON放在DocumentDB 集合
的 document
中:
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"
}
],
}
]
}
]
当我从DocumentDB中提取数据时,我尝试将其序列化到我的 Row
类中:
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; }
}
我需要能够将任何类型的控件"放入DocDB的控件列表中,并让C#将列表反序列化为正确的 Control
类(因为基本的 Control
类,或类似 OptionsControl
或 ClickableControl
的派生之一).
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
).
问题是因为我要反序列化为 Control
,所以我获得了Control上除 Options
以外的所有属性.或者,如果我尝试反序列化具有 Url
和 UrlTarget
的代码,那么我只会获得基本的Control属性,而不是URL属性.我以为C#会处理将反序列化的对象转换为OptionsControl或ClickableControl,但是我想那是不正确的?我该怎么做才能使DocumentDB中的JSON对象正确序列化并变成 OptionsControl
(具有Options属性),而不仅仅是基本Control?
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?
推荐答案
您可以尝试使用Json.NET自己序列化对象,然后将序列化的内容发布到DocumentDb中.然后,当您需要数据时,将其作为json字符串读回,并再次使用Json.NET进行反序列化.Json.NET可以处理继承,因此您只需配置它即可了解您的类型层次结构.使用TypeNameHandling设置:
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
};
这篇关于将JSON反序列化为多个继承的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!