Replace JSON.NET with ServiceStack.Text in ASP.NET Web API

Strath

January 6th, 2013

Replace JSON.NET with ServiceStack.Text in ASP.NET Web API

Because ServiceStack.Text performs much better

I recently stumbled across a comparison of JSON serialization libraries. which shows that ServiceStack.Text by far outperforms any of the competitors. Indeed, the folks down at ServiceStack have been building a lot of great stuff for the past few (4?) years to facilitate their framework.
ServiceStack.Text is available on Nuget and can be used outside of ServiceStack, within any .NET project, so why not use it with Web API, replacing the default serializer, JSON.NET?

Let’s do that.

Creating a ServiceStack.Text MediaTypeFormatter

Typically, whenever you want to introduce a new serialziation mechanism to ASP.NET Web API, you’d create a new MediaTypeFormatter. This case is no different. Let’s grab ServiceStack.Text from Nuget:

Once you have it refrenced in your solution, the formatter is pretty straight forward:

We tell the formatter a few things:
– we will support application/json media type
– we support UtF-8 and Unicode encoding
– Read and Write is available for all types of objects
– we tell ServiceStack to handle dates as ISO8601, to avoid JSON dates with Unix Epoch milliseconds (read more here)
– in the read/write methods we simply asynchronously call the respective methods of the ServiceStack.Text.JsonSerializer

Replacing JSON.NET

Now, in order to wire this up, we need to remove the default JSON formatter (JSON.NET) and inject our new formatter into the GlobalConfiguration.Formatters collection.

And that’s it!

From now your ASP.NET Web API application will be using ServiceStack.Text, a serializer which benchmarks show is almost 2x faster than JSON.NET. In all fairness, that’s one of the micro optimizations, but still, if you can improve something, why not do that?

Be Sociable, Share!

  • Pingback: The Morning Brew - Chris Alcock » The Morning Brew #1267

  • Pingback: Dew Drop – January 7, 2013 (#1,476) | Alvin Ashcraft's Morning Dew

  • Allen

    Hi Filip,

    Do you have any experience to build Roslyn over ServiceStack.net ? I like the concept of ServiceStack and I do believe Roslyn will add more value to make a dynamic version of ServiceStack to suit my specific purpose of application, I saw you nice blog about Roslyn but all the samples seem to build over ASP.NET Web API, it will be grateful if you could shed some lights of Roslyn over ServiecStack.net

  • Puvana

    Hi, Will this replace the JSON serialization of MVC controller?

  • http://twitter.com/GFoley83 Gavin Foley

    This just saved my ass; JSON.NET was falling over when tring to serialize some Castle Proxy classes in my Web API controller.
    Switched to ServiceStack.Text, disabled XmlFormatter and I was sorted.

  • AquaBirdConsult

    So when do you replace WebAPI with ServiceStack? I feel like you just took the engine out of a Ferrari and put it into a Ford Focus : ).

    Great post, I love reading the stuff you write. Keep it up.

  • Aref Karimi

    I am trying to replace Json.NET with StackService.Text but I am facing a difficulty which I posted about it on StackOverflow. Can you please see if you can help me?

    http://stackoverflow.com/questions/21475505/json-deserialization-and-controlling-the-instantiaion