https://dotnetfiddle.net/ka6XVw - Fiddle with example type structure
Suppose I have a class that implements IDictionary<string, T>
. Json.Net can deserialize such types out of the box, creating an instance of the type and using its indexer to populate the dictionary. The issue is that this class also inherits a string Error
property marked with JsonProperty
attribute from its base class, and I'd like this property to be populated whenever the input json contains an error
field. However, when deserializing an IDictionary
Json.Net considers all fields to be dictionary entries and tries to add the value with the error
key to the dictionary.
What is the simplest and cleanest way to deserialize the json into a dictionary and the error
field into the Error
property? Please note that the class is generic, so JsonExtensionData
is not an option (without casting its values to the provided type).
Sample valid dictionary json: { 'foo': '1', 'bar': '2' }
Sample error json { 'error': 'blah' }