http://blogs.clariusconsulting.net/kzu

Daniel Cazzulino's Blog

Go Back to
kzu′s Latest post

Using Json.NET for text and binary Json payloads with WCF WebApi

There is an aging post by Christian on how to use the awesome Json.NET library as a default serializer for the new WCF Web Api.

Rather than having two media type formatters (in the latest bits lingo), I wanted to have a single one and have it pick the flavor of Json/Bson on the fly depending on the accept header on the request. This makes configuration simpler as you have to add only one formatter to the pipeline:

var config = HttpHostConfiguration.Create();
config.Configuration.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter());

The code above adds the formatter at the beginning of the pipeline as suggested by @gblock.

Even simpler is to use just an extension method over the configuration:

var config = HttpHostConfiguration.Create().UseJsonNet()

Given the simplicity of this class, this doesn’t deserve anything less but being a NETFx NuGet. Go get it or search for WebApi, Json.NET or MediaTypeFormatter in the package library dialog in VS (doesn’t work if you enter multiple words Confused smile):

image

 

The corresponding unit tests are available too.

 

Enjoy!

Comments

10 Comments

  1. [...] Using Json.NET for text and binary Json payloads with WCF WebApi [...]

  2. Hey @kzu,

    Any chance using JSON.NET fixes the javascript/.NET date issue, or why would I choose to use the JSON.NET over the built in Json support?

    If it doesn’t – what would you advise using to support dates in jQuery/javascript.

    And does this affect the methods that receive json as well as those that dispense it?

  3. I love it!

    …..EXCEPT for that package name, I will never remember that in a bazillion years!

    Andew, JSON.NET is much faster and has better support for cleaner JSON.

  4. [...] Using Json.NET for text and binary Json payloads with WCF WebApi [...]

  5. I tried adding this to my project but can’t seem to get it to work. I tried calling HttpHostConfiguration.Create().UseJsonNet() in the Global.asax Application_Start method, but the output of my service still seems to be getting serialized by the default json serializer and not json.net. Is that the correct usage or is there somewhere else that this should be called?

    Thanks for any help with this.

    • I am trying the same and cannot swap out the default serializer. I added the following code to Global.asax

      var serializerSettings = new JsonSerializerSettings();
      serializerSettings.Converters.Add(new IsoDateTimeConverter());

      var config = HttpHostConfiguration.Create();
      config.Configuration.OperationHandlerFactory.Formatters.Clear();
      config.Configuration.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter(serializerSettings));

      But cannot get it to working. Would really appreciate any help from you.

  6. Also, JsonNetMediaTypeFormatter has a constructor that takes JsonSerializerSettings, but the extension doesn’t come with a method that has that as a parameter, so I added

    public static IHttpHostConfigurationBuilder UseJsonNet(this IHttpHostConfigurationBuilder config, JsonSerializerSettings serializerSettings)
    {
    config.Configuration.OperationHandlerFactory.Formatters.Insert(0, new JsonNetMediaTypeFormatter(serializerSettings));
    return config;
    }

    It might be handy for other users to have this already in the package, if they want to specify extra serializer settings.

  7. It looks like the nuget package needs updating to work with preview 6. It seems to be written for preview 3.