JSON Serializers on C# / .NET – Late 2011

JSON has been around for a while now, and it’s appeal is still growing in the interest of things like REST services and the NoSQL (A.K.A Document) database movement (FYI – I’m much more fond of the NoSQL movement than the Occupy movement). But, yet, it still seems that a good JSON serializer is hard to come by.

I’m in the habit of making my apps simply aware of an ISerializer so that I can choose to implement the JSON-serializer-of the-day. I have toyed with a few JSON serializers:

Json.NET
JayRock
DataContractJsonSerializer
JSONSharp
LitJSON
JavaScriptSerializer

They’re all sort of <OK>, and each one presents various performance and other anomalies  to work around. However, with my new found excitement with CouchDB, (hey just stinkin’ relax) I really wanted to find a good JSON serializer. To my pleasure I found some promising new ones:

fastJSON
ServiceStack.Text

The performance was great! But the maturity was lacking. (Sound familiar ladies?) Despite fastJSON showing some impressive results, it didn’t actually work on many of my not-so-exotic POCO’s. ServiceStack turned out much better. My only suggestion was better recognition of the [DataMember] attributes (ie; name=) so that it provided more Deserialization flexibilty when the JSON string didn’t exactly match the POCO properties. So here is how my latest ISerializer impl looks:

————————————————————————————

using System;
using ServiceStack.Text;

namespace MyProject.Serializers
{
    /// <summary>
    /// This is an implementaion ISerializer that uses the ServiceStackText JSON
    /// </summary>
    public class ServiceStackJsonSerializer : ISerializer
    {

        public string Serialize<T>(T obj)
        {
            string jsonText = ServiceStack.Text.JsonSerializer.SerializeToString<T>(obj);
            return jsonText;
        }

        public T Deserialize<T>(string json)
        {
            T obj;
            obj = (T)ServiceStack.Text.JsonSerializer.DeserializeFromString<T>(json);
            return obj;
        }

        public Object Deserialize(string json, Type type)
        {
            Object obj;
            obj = ServiceStack.Text.JsonSerializer.DeserializeFromString(json, type);
            return obj;
        }
    }
}

————————————————————————————

Does anyone have other recommendations? Reading through Stackoverflow, it looks like JSON.Net is very popular, and — in the end I like it too. However, I still hope to find a JSON serializer that’s faster.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s