在使用Json.NET进行序列化期间尝试隐藏基类成员 [英] Trying to hide base class member during serialization with Json.NET

查看:187
本文介绍了在使用Json.NET进行序列化期间尝试隐藏基类成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,其中具体的类Model<T>隐藏了基类的Items属性.

I have two classes, where the concrete class Model<T> hides the base class' Items property.

class Model
{
    List<ListItem> Items {get;set;}
}

class Model<T> : Model
{
    new List<ListItem<T>> Items {get;set;}
}

使用Json.NET序列化Model<T>的实例后,出现错误:

Upon serializing an instance of Model<T> with Json.NET I get the error:

Newtonsoft.Json.JsonSerializationException:名称为"Items"的成员已经存在于"Model<T>"上.使用JsonPropertyAttribute指定其他名称.

Newtonsoft.Json.JsonSerializationException: A member with the name 'Items' already exists on 'Model<T>'. Use the JsonPropertyAttribute to specify another name.

我理解为什么会收到此错误,但是,我不想更改具体类的属性名称;我希望能够告诉序列化程序忽略基类属性.

I understand why I'm receiving this error, however, I don't want to change the property name on the concrete class; I want to be able to tell the serializer to ignore the base class property.

我尝试使用XmlSerializer支持的ShouldSerialize{PropertyName}()约定和

I tried using the ShouldSerialize{PropertyName}() convention that XmlSerializer supports, and Json.NET claims to support too, however this doesn't seem to work for my scenario.

推荐答案

感谢

Thanks to rsbarro for pointing out that this was fixed in a later version of Json.NET (v4.0.6.0). I've upgraded and confirmed that the correct property is now serialized.

这篇关于在使用Json.NET进行序列化期间尝试隐藏基类成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆