A class in a third-party library I am using has the same JSON property name specified for two properties on a class. E.g. like this:
public class Foo
{
[JsonProperty("X")]
public string X1 { get; set; }
[JsonProperty("X")]
public string X2 { get; set; }
}
When I try to serialize an instance of that class to JSON, it throws an exception
var foo = new Foo { X1 = "a" };
var json = JsonConvert.SerializeObject(foo); // throws JsonSerializationException
JsonSerializationException: 'A member with the name 'X' already exists on 'Foo'. Use the JsonPropertyAttribute to specify another name.'
How can I configure the json serialization to ignore the property names specified in the JsonPropery
attribute?
Note that I have no control over the definition of Foo
and so cannot modify the attributes, etc.
I am not even using one of the conflicting properties, but the exception is still thrown when I specify NullValueHandling.Ignore
in JsonSerializerSettings
.