Trying to hide base class member during serialization with Json.NET

╄→гoц情女王★ 提交于 2019-12-24 17:43:56

问题


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;}
}

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

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.

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.


回答1:


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.



来源:https://stackoverflow.com/questions/9304096/trying-to-hide-base-class-member-during-serialization-with-json-net

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