I'm encountering an odd situation where IgnoreDataMember
isn't doing the job, but JsonIgnore
does.
In this case I'm inheriting from a class with a public {get; set;}
property, and I'm choosing to override the setter with a NotSupportedException
. I only want the serializer to be able to set the property, not the user, but I want the user to get it, so I've done the following:
[DataContract]
public class BaseObject : BaseInterface
{
[DataMember]
public virtual double information { get; set; }
}
[DataContract]
public class ServerGeneratedObject : BaseObject
{
[IgnoreDataMember]
public override double information {
get { return server_set_information; }
set { throw new NotSupportedException("Server generated property"); }
}
[DataMember(Name="information")]
private double server_set_information { get; set; }
}
Unfortunately, this raises an error "A member with the name 'information' already exists on 'ServerGeneratedObject'. Use the JsonPropertyAttribute to specify another name."
If, however, I use the [JsonIgnore] attribute, this works as expected. This appears to be due to this section of the data contract resolver (code currently lives here):
bool flag2 = JsonTypeReflector.GetAttribute<JsonIgnoreAttribute>(attributeProvider) != null ||
JsonTypeReflector.GetAttribute<JsonExtensionDataAttribute>(attributeProvider) != null ||
JsonTypeReflector.GetAttribute<NonSerializedAttribute>(attributeProvider) != null;
if (memberSerialization != MemberSerialization.OptIn)
{
bool flag3 = JsonTypeReflector.GetAttribute<IgnoreDataMemberAttribute>(attributeProvider) != null;
property.Ignored = flag2 | flag3;
}
The property isn't correctly being set to 'ignored' because it's in 'OptIn' mode, but if that's the case, I have no idea why the inherited "information" property is being "opted in" because the "DataMember" attribute is not supposed to be inheritable. I filed a bug here in case this isn't expected behaviour.
Is there something I can do here? I'm trying to avoid using any "Newtonsoft" attributes on my public data model, because I don't want people using my client library object model to necessarily have to reference the Newtonsoft assembly.