Json.Net has support for controlling custom serialization using derived versions of JsonConverter
. These enable a developer to control how serialization for a particular type occurs. There are a few ways a JsonConverter can be associated with a type or field including such as using a [JsonConverter]
attribute on the member or type.
However these are only called if using JsonConvert.DeserializeObject
or JsonConvert.SerializeObject
for deserializing to new instances of objects or serializing existing objects. If you need to patch or update an existing object, JsonConverter.Populate
is what is referred to to use, but does not use converters. Instead, it relies solely on reflection and naive object creation which blows up if you implement polymorphic serialization and/or use abstract classes or interfaces.
if(_value == null)
{
_value = JsonConvert.DeserializeObject<T>(jsonContent, WebConstants.JSON_SERIALIZER_SETTINGS);
}
else
{
JsonConvert.PopulateObject(jsonContent, _value, WebConstants.JSON_SERIALIZER_SETTINGS);
}
My question is; what can you do in this case to control custom serialization when patching existing objects in Json.Net? Ideally I would like to reuse the logic from the custom JsonConverter
implementations so that I am not implementing this logic twice.