I have implemented a custom JsonConverter to deserialize concrete classes when all we know about is the interface that is using them. given this, I have overridden the ReadJson method with the following:
public class MyConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return (objectType == typeof(IMyInterface));
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var jsonObject = JObject.Load(reader);
if (jsonObject["Type"].Value<string>() == "MyType")
return jsonObject.ToObject<MyConcrete>(serializer);
}
}
On the interface I have added the following decoration:
[JsonConverter(typeof(MyConverter))]
public interface IMyInterface { ... }
The issue I seem to be having is that when deserialization is attempted, I keep getting StackOverflow exception, and I think that it is going around this custom converter for each element in the JSON structure.. which there are quite a few(!)
If I remove the decoration of the interface and instead add the custom converter to the call the converter manually... it works fine!
var jsonSerializerSettings = new JsonSerializerSettings
{
Converters = { new MyConverter() }
};
Is there a way I can resolve this without calling the customer converter specifically? I am concerned about unexpected behavior on deserialization of objects that don't need the custom JsonConverter. Thoughts?