This project has moved. For the latest updates, please go here.


JsonConverter not applied for all types of Json data (e.g. strings) when reading (but correctly applied when writing)


The attached program is the same as my example on issue 23112 but with one change: instead of ComplicatedThing generating an array, it now generates a string.
This reveals another problem. On serialization, my JsonConverter is properly applied, so the resulting Json is:
{ "Thing": "42" }
But on deserialization the JsonConverter is not applied, so the round-trip fails.
By debugging I found a spot where arrays and objects get special treatment. In JsonSerializerInternalReader.CalculatePropertyDetails, if the current token is neither an array nor an object, and the property is not writable, deserialization does not happen for that property at all. This is despite the fact that the property may contain an existing object (so does not need to be directly written) of a type that has a converter that wants to interpret a primitive value.
i.e. converters do not give complete control over the JSON format during reading, only during writing. For reading, they only apply if the JSON format is an array or an object. Otherwise a round-trip will fail.
This is more of a serious problem as I have a variety of existing types that sometimes simplify their representation to primitive values depending on their state (e.g. an object that can be represented either as a URI string or as an object containing the embedded data).
So (with regret!) abandoning my attempt to use JSON.NET serialization for this.

file attachments

Closed Aug 2, 2012 at 8:23 AM by JamesNK
Make the Thing property set not private or put a JsonProperty on it.