How to use default serialization in a custom JsonConverter

In your custom JsonConverter, override CanWrite and return false:

public override bool CanWrite { get { return false; } }

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
    throw new NotImplementedException();
}

Then you can just throw an exception from WriteJson, since it won’t get called.

(Similarly, to get default behavior during deserialization, override CanRead and return false.)

Note that the same approach can be used for JsonConverter<T> (introduced in Json.NET 11.0.1) since it is just a subclass of JsonConverter that introduces type-safe versions of ReadJson() and WriteJson().

Leave a Comment