Skip navigation
JSON.NET: Serialization Done Right

JSON.NET: Serialization Done Right

As much as I love the .NET Framework, one area where it's always come up short in my book is serialization. Over the past 12 years I've played with several serialization options and frameworks that have been baked into the .NET Framework. I've also looked at some third-party solutions. I really hadn't found what I would have considered to be the perfect .NET serialization framework -- until I recently found JSON.NET that is.

Serialization and the .NET Framework

Truth be told, the initial XML serialization that shipped with .NET Framework 1.0 and 1.1 wasn't that bad. Or that might just be my foggy memory forgetting some of the various hurdles and headaches that stemmed from working with these earlier versions of the .NET Framework. Either way, I'm probably remembering whatever hoops I had to jump through to get .NET serialization to work were it was a lot easier and more reliable to use than what I was able to cobble together on my own with classic ASP and Visual Basic (back in the day).

But if memory serves me correctly, then around the release of.NET Framework 2.0, not only did XML serialization become more tedious and error prone, but I also seem to remember that around this time frame that Microsoft sadly became obscenely pedantic in regard to XML Schema Definitions (XSDs), namespaces, and a couple of non-trivial XML requirements that made doing anything with XML overly painful and prone to excessive amounts of troubleshooting and tweaking. In other words, the .NET Framework 2.0 serialization framework just became obscenely brittle and painful to use -- even for tasks that should be been simple such as serialization.

Fast forward to a few years later along with a couple additional .NET version releases, and I still don't think that the serialization story that ships with .NET has gotten much better. Thanks to Windows Communication Foundation (WCF), I'd argue that serialization has in many cases become much more tedious, brittle, and prone to troubleshooting. That's to say nothing of how much I despise having to couple my objects with endless [DataMember] and [DataContract] attributes just to get them to serialize using System.Runtime.Serialization infrastructure.

As a side note, a part of me still thinks that WCF was built on a bit of a dare or as an experiment to see if Microsoft could make even the nicest person (not me) curse like an irate and drunken pirate when trying to deploy WCF apps out into the wild. I've never encountered anything more heavily coupled, horribly overbuilt, and tediously demanding of error-prone configuration, reconfiguration, subconfiguration in my entire life.

JSON.NET: Simple, Easy, High-Performance Serialization

In fairness, however, the last time I really dealt with .NET serialization other than JSON outputs for client interactions was roughly two years ago. Although I haven't had time to look into some of the JSON serialization mechanisms that ship with the ASP.NET Web API framework, I knew that I'd need to look into seeing if there was some sort of sea change in the .NET serialization landscape. Consequently, I took a brief tour through Google looking for any kind of lightweight and easy-to-use serialization options that I might be able take for a spin.

What I found immediately were a few links to JSON.NET. It only took me a few minutes to be sold on the advertised features to pull down the binaries and try a few quick serialization tests. Sure enough and with just a few lines of code, I was able to seamlessly and easily serialize and then rehydrate a couple of my .NET objects. Best of all, the entire process not only was flawless in terms of working as advertised, but it also didn't require any modification to my objects. Instead, what I got was a full-blown POCO serialization without any fuss or headaches.

JSON.NET is More than just Simple Serialization

A part of what makes JSON.NET so awesome -- above and beyond its core operating principles to enable fast and easy serialization -- is the documentation. Within just a few minutes of perusing the documents and corresponding examples, I was not only able get spun up on all the core features of JSON.NET, but I was also able to spot some other very cool options and features that were also insanely easy to grok as well. For example, I initially assumed the LINQ to JSON capability was a sort of spiffy feature of JSON.NET that I probably wouldn't use. But within about 20 minutes of plugging away within my application I found a perfect place to end up using this functionality to pass around dynamic message objects and rehydrate them via receiver or router on the flip side. As with everything else in this framework, the functionality was insanely easy to use and worked just as advertised.

Some other very cool aspects of this open-source framework are that it also provides painfully simple options for debugging along with easy-to-configure settings options that have magically managed to do just about everything I need them to do in default config-mode. This means that I have yet to really even bother making any config changes. There are also a host of awesome features that you can use to address error handling, customize serialization output, and perform pretty much anything else you'd want or need a serialization framework to do. Although I still more or less despise WCF object serialization through [DataContract] attributes, one very cool thing about JSON.NET is that it fully supports opt-in member serialization either through .NET or WCF serialization attributes. In essence, JSON.NET is lightweight, easy, speedy, and fun to use. It also supports more enterprise-related scenarios in which [DataContract] serialization also makes good sense as well. Long story short: JSON.NET is totally worth a review and test drive if you have any .NET serialization needs in any of your upcoming projects.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.