使用 JSON.net,如何防止在基类上下文中使用派生类的属性?

Using JSON.net, how do I prevent serializing properties of a derived class, when used in a base class context?(使用 JSON.net,如何防止在基类上下文中使用派生类的属性?)

本文介绍了使用 JSON.net,如何防止在基类上下文中使用派生类的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个数据模型:

[DataContract]
public class Parent
{
    [DataMember]
    public IEnumerable<ChildId> Children { get; set; }
}

[DataContract]
public class ChildId
{
    [DataMember]
    public string Id { get; set; }
}

[DataContract]
public class ChildDetail : ChildId
{
    [DataMember]
    public string Name { get; set; }
}

出于实现方便的原因,有时 Parent 上的 ChildId 对象实际上是 ChildDetail 对象.当我使用 JSON.net 序列化 Parent 时,它们会与所有 ChildDetail 属性一起写出.

For implementation convenience reasons, there are times when the ChildId objects on the Parent are in fact ChildDetail objects. When I use JSON.net to serialize the Parent, they are written out with all of the ChildDetail properties.

是否有任何方法可以指示 JSON.net(或任何其他 JSON 序列化程序,我对项目的投入还不够远)在序列化为基类时忽略派生类属性?

Is there any way to instruct JSON.net (or any other JSON serializer, I'm not far enough into the project to be committed to one) to ignore derived class properties when serializing as a base class?

重要的是,当我直接序列化派生类时,我能够生成所有属性.我只想抑制 Parent 对象中的多态性.

It is important that when I serialize the derived class directly that I'm able to produce all the properties. I only want to inhibit the polymorphism in the Parent object.

推荐答案

我使用自定义 Contract Resolver 来限制我的哪些属性要序列化.这可能会为您指明正确的方向.

I use a custom Contract Resolver to limit which of my properties to serialize. This might point you in the right direction.

例如

/// <summary>
/// json.net serializes ALL properties of a class by default
/// this class will tell json.net to only serialize properties if they MATCH 
/// the list of valid columns passed through the querystring to criteria object
/// </summary>
public class CriteriaContractResolver<T> : DefaultContractResolver
{
    List<string> _properties;

    public CriteriaContractResolver(List<string> properties)
    {
        _properties = properties
    }

    protected override IList<JsonProperty> CreateProperties(
        JsonObjectContract contract)
    {
        IList<JsonProperty> filtered = new List<JsonProperty>();

        foreach (JsonProperty p in base.CreateProperties(contract))
            if(_properties.Contains(p.PropertyName)) 
                filtered.Add(p);

        return filtered;
    }
}

在覆盖 IList 函数中,您可以使用反射来仅使用父属性填充列表.

In the override IList function, you could use reflection to populate the list with only the parent properties perhaps.

合同解析器应用于您的 json.net 序列化程序.这个例子来自一个 asp.net mvc 应用程序.

Contract resolver is applied to your json.net serializer. This example is from an asp.net mvc app.

JsonNetResult result = new JsonNetResult();
result.Formatting = Formatting.Indented;
result.SerializerSettings.ContractResolver = 
    new CriteriaContractResolver<T>(Criteria);

这篇关于使用 JSON.net,如何防止在基类上下文中使用派生类的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 JSON.net,如何防止在基类上下文中使用派生类的属性?

基础教程推荐