1

I want to assign a JsonConverter to a class without using the [JsonConverter(typeof(...))] attribute (because I'm doing some complex stuff with Web API that requires me to assign the JsonConverter in a different assembly than where the class is defined).

The answer to Globally use a JsonConverter on a class without the attribute almost answers my question. However my question is in regards to Web API so I'm unclear about:

  • What base class should I use for my contract resolver? I suspect it should be JsonContractResolver but not sure what MediaTypeFormatter object to pass to its constructor.
  • What is the proper way to declare a custom contract resolver in Web API?

The code below is what I've tried but it does not work. My custom contract resolver does not get used and breakpoints in CreateObjectContract() do not get hit.

Global.asax.cs

protected void Application_Start()
{
    GlobalConfiguration.Configuration.Formatters.JsonFormatter
        .SerializerSettings.ContractResolver =
            new CustomResolver(GlobalConfiguration.Configuration.Formatters.JsonFormatter);
    ...
}

CustomResolver.cs

public class CustomResolver : JsonContractResolver
{
    public CustomResolver(MediaTypeFormatter formatter) : base(formatter) { }

    protected override JsonObjectContract CreateObjectContract(Type objectType)
    {
        JsonObjectContract contract = base.CreateObjectContract(objectType);
        if (typeof(MyBaseClass).IsAssignableFrom(objectType))
            contract.Converter = new MyBaseClassJsonConverter();

        return contract;
    }
}
Community
  • 1
  • 1
Keith
  • 20,636
  • 11
  • 84
  • 125
  • You don't need to use a custom contract resolver, you could your converter to [`JsonSerializerSettings.Converters`](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonSerializerSettings_Converters.htm) in Global.asax.cs. See http://stackoverflow.com/questions/7427909/how-to-tell-json-net-globally-to-apply-the-stringenumconverter-to-all-enums/18152853#18152853. – dbc Oct 27 '15 at 18:58
  • @dbc Thanks very much, I was able to solve it that way. If you add it as an answer I will accept it. – Keith Oct 27 '15 at 19:47
  • My answer would just be a duplicate though, so I'm going to suggest closing it as such. – dbc Oct 27 '15 at 20:01

1 Answers1

1

I solved this without using a custom contract resolver simply by adding my JsonConverter class to the JsonSerializerSettings.Converters collection in WebApiConfig.cs:

config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new MyBaseClassJsonConverter());

You can also register it in Global.asax.cs:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new MyBaseClassJsonConverter());

(This question is flagged as a duplicate of another similar question. However I am still posting an answer because the other question is in regards to just one specific JsonConverter and the accepted answer does not make it clear how to solve the problem for Web API.)

Keith
  • 20,636
  • 11
  • 84
  • 125
  • While the accepted answer in the other question doesn't specifically address WebAPI, [the second answer below it](http://stackoverflow.com/a/18152853/10263) does. Just FYI. – Brian Rogers Oct 30 '15 at 15:24