Thread: C# and JSON
View Single Post
  #2 (permalink)  
Old March 11th, 2009, 01:10 PM
jmcpeak's Avatar
jmcpeak jmcpeak is offline
Wrox Author
Points: 467, Level: 7
Points: 467, Level: 7 Points: 467, Level: 7 Points: 467, Level: 7
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2005
Location: , Texas, USA.
Posts: 87
Thanks: 0
Thanked 18 Times in 17 Posts
Default

Howdy, Throp.

We used the Json.NET library throughout the book, so if want to follow the code I recommend the version of Json.NET supplied in the code download. The DLL is in the bin folders wherever we used it.

If your inquiry is for your own projects, I’ll disclaim this post by saying I haven’t used many JSON libraries for .NET. We used one library (Nii.JSON.dll) for the first edition of Professional Ajax, and we switched to Json.NET for the second edition. The only other JSON serializer/deserializer I’ve used is what Microsoft added to the CLI for .NET 3.0. My personal recommendations are as follows:

.NET 1.1 – Nii.JSON found at http://www.json.org/cs.zip

.NET 2.0 – Eearly versions of Json.NET http://www.codeplex.com/json

.NET 3.5 (what I’ll focus on since it’s the latest version of .NET) – Either Json.NET or the classes in the System.Runtime.Serialization.Json namespace. I prefer Json.NET. It’s nice when Microsoft adds support for any technology to .NET, but their additions are typically and unfortunately basic. Anything more advanced requires you to either find a third party library or roll your own library. I find more value in Json.NET than I do in the System.Runtime.Serialization.Json namespace. It’s also worth mentioning that I use LINQ like it’s going out of style, and I find the LINQ to JSON features very handy.

Features aside, serializing objects and deserializing JSON text is much simpler with Json.NET.
Code:
// Json.NET Serializing
string json = JavaScriptConvert.SerializeObject(myObj);

// Json.NET Deserializing
MyObj deserializedObj = JavaScriptConvert.DeserializeObject<MyObj>(json);

// System.Runtime.Serialization.Json.DataContractJsonSerializer Serializing
DataContractJsonSerializer serializer = new DataContractJsonSerializer(myObj.GetType());
using (MemoryStream ms = new MemoryStream())
{
    serializer.WriteObject(ms, myObj);
    string json = Encoding.Default.GetString(ms.ToArray());
}

// System.Runtime.Serialization.Json.DataContractJsonSerializer Deserializing
MyObj myObj = new MyObj();
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)))
{
    DataContractJsonSerializer serializer = new DataContractJsonSerializer(myObj.GetType());
    myObj = serializer.ReadObject(ms) as MyObj;
}
So to answer your question, it’s ultimately up to you. It’s always good to know how to use the tools Microsoft provides you; use it if their implementation is sufficient for your needs. If it is, you can always write a helper class leveraging the power of generics to make serializing and deserializing less painful and more like Json.NET’s API.

I hope this helps you determine what route you want to take.

Last edited by jmcpeak; March 11th, 2009 at 01:13 PM.. Reason: Added link to Json.NET at CodePlex
The Following User Says Thank You to jmcpeak For This Useful Post:
throp (March 12th, 2009)