Json.NET custom JsonConverter being ignored

Documentation says: To apply a JsonConverter to the items in a collection use either JsonArrayAttribute, JsonDictionaryAttribute or JsonPropertyAttribute and set the ItemConverterType property to the converter type you want to use.

http://james.newtonking.com/json/help/html/SerializationAttributes.htm

Maybe that will help.


What happened to me was, I added the using statement automatically as suggested by Visual Studio. And by mistake added using System.Text.Json.Serialization; instead of using Newtonsoft.Json;

So I was using System.Text.Json.Serialization.JsonConverterAttribute on the target class. Which is (correctly) ignored by Json.Net.


First of all System.Web.Mvc.Controller.Json() doesn't work with Json.NET - it uses JavaScriptSerializer that doesn't know anything about your Json.NET stuff. If you still want to use System.Web.Mvc.Controller.Json() call you should do something like this. Also change WriteJson to this:

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
    serializer.Serialize(writer, ((dynamic)value).attribute);
}

I think this should make your code work.