I'm writing a custom JsonConverter, but only really want to change the read logic. The write logic should perform normally. Any way I try it, I'm getting stack overflows, since Json.NET just goes straight back to the converter. Is there a way to specify that I don't want to customize the writer, or that I want to defer back to the normal Json.NET logic?
It turns out that you can override a CanWrite property on the JsonConverter that will prevent Json.NET from calling the converter.
public class MyConverter: JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException(); //This will never be called since CanWrite is false
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//Do your thing
return new object();
}
public override bool CanWrite { get { return false; } }
public override bool CanConvert(Type objectType)
{
//Do your thing
return true;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With