I am trying to deserialize a class using Json.NET and a custom JsonConverter object. The class currently defines a converter for default serialization using the JsonConverterAttribute. I need to do a custom deserialization by passing in a custom converter. However, the deserialization still seems to be using the default converter. How can I get Json.NET to prefer my custom converter?
Here's a bit of sample code that demonstrates the issue. I'm using NewtonSoft.Json 4.5.11:
void Main() { JsonConvert.DeserializeObject<Foo>("{}"); // throws "in the default converter" var settings = new JsonSerializerSettings { Converters = new[] { new CustomConverter() } }; JsonConvert.DeserializeObject<Foo>("{}", settings); // still throws "in the default converter" :-/ } [JsonConverter(typeof(DefaultConverter))] public class Foo { } public class DefaultConverter : JsonConverter { public override bool CanConvert(Type objectType) { return typeof(Foo).IsAssignableFrom(objectType); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new Exception("in the default converter!"); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new Exception("in the default converter!"); } } public class CustomConverter : JsonConverter { public override bool CanConvert(Type objectType) { return typeof(Foo).IsAssignableFrom(objectType); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { throw new Exception("in the custom converter!"); } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { throw new Exception("in the custom converter!"); } }