Skip to content

Json.NET vs .NET DataContractJsonSerializer

August 22, 2010

In keeping with my previous post comparing .NET compression against an open source alternative, this time I compare the Json.NET serializer to the built-in .NET DataContractJsonSerializer class, and find that .NET performs better for my usage.

I was recommended to Json.NET based on a report by its author that it outperforms .NET.  I was already happily using .NET, so wanted a quick test to verify the claim and determine if switching is worthwhile.

For this test, I used JSON objects sent by Facebook’s real-time API.  They contain an array of nested objects, which in turn store arrays of other objects.  I echoed this structure with a C# class, so that the JSON object parses directly into my code object. Then I timed the parsing of a million of the objects using the different deserializers.  I also serialized them back to text.  And for kicks, I threw the XML-based DataContractSerializer into the mix.

Here’s the timings (averaged over several runs):

ActionLibrarySpeed (objects/ms)
ReadJson.NET (3.5 R8)9.28
DataContractJsonSerializer (4.0)15.8

The table shows the number of objects parsed or written per millisecond, so higher is better.  Here, we see that the .NET JSON parser is a factor of 1.7x faster than Json.NET.  .NET is also 1.4x faster at writing objects to JSON strings.  I also found that using the XML-based DataContractSerializer is even speedier at both reading and writing.  I expected JSON to win, simply because it’s represented in shorter and simpler strings, but that isn’t the case (I still prefer JSON for most kind of objects due to its ease of working with).

My test contradicts the claim that Json.NET performs better.  So who’s right?  Well I trust his findings were reported accurately, so it might just come down to the particular data set we are working with.  Maybe .NET is better suited to mine?  Regardless, I think it means that you should always check benchmark claims, because what applies to some tests may not match your situation.

By the way, if you ever need the DataContractJsonSerializer class, it’s useful to know that even though the documentation says it is in System.Runtime.Serialization.dll, you need to include a reference to System.ServiceModel.Web.dll to use it.

No comments yet

Leave a Reply

Your email address will not be published. Required fields are marked *