What libraries are available to handle JSON in .Net? I've seen this: http://james.newtonking.com/projects/json-net.aspx but would prefer a native library, if possible.
-
1What do you mean by "native" library? Method names written in some local language instead of English? – Mehrdad Afshari Feb 05 '10 at 18:51
-
@Mehrdad: He means one written entirely on the .NET framework, that does not rely on API calls. – John Gietzen Feb 05 '10 at 18:57
-
1If that's what "native" means, what's wrong with JSON.NET? – Daniel Earwicker Feb 05 '10 at 19:06
-
Nothing. I use JSON.NET myself. I think the OP is confused. Maybe he means built-in-to-.NET – John Gietzen Feb 05 '10 at 19:50
3 Answers
I have been using the JavaScriptSerializer
some to expose data structures from a WCF service to Ajax calls, and it has been working out quite well.

- 155,851
- 29
- 291
- 343
The JavaScriptSerializer
has been marked as obsolete in .NET 3.5 and but you could use the DataContractJsonSerializer
.
EDIT: See this question on SO about whether JavaScriptSerializer
is actually obsolete going forward in the .NET BCL. It looks like JavaScriptSerializer
is no longer obsolete in .NET 3.5 SP1 - so it's probably fine to use that. If in doubt, you can use the contract serializer from WCF, or JSON.NET (if you're willing to include 3rd party code).
Here's some wrapper code to make using DataContractJsonSerializer
nicer.
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
public class JSONHelper
{
public static string Serialize<T>(T obj)
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(obj.GetType());
using( MemoryStream ms = new MemoryStream() )
{
serializer.WriteObject(ms, obj);
string retVal = Encoding.Default.GetString(ms.ToArray());
return retVal;
}
}
public static T Deserialize<T>(string json)
{
using( MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(json)) )
{
DataContractJsonSerializer serializer =
new DataContractJsonSerializer(typeof(T));
T obj = (T)serializer.ReadObject(ms);
ms.Close();
return obj;
}
}
}
The code above is courtesy of: http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer.aspx,
I have altered it from it's original form to use best practices for object disposal (the using
pattern .NET supports).
-
1-1 for not wrapping streams in `using` blocks, and for creating an instance you later overwrite in the deserialize. – John Saunders Feb 05 '10 at 18:58
-
-
@John Saunders: A fair ping. I've corrected that - although I will say I am not the original author of that code. Although I did change it in my own adapted implementation. It was just easier to find the site than my code :) – LBushkin Feb 05 '10 at 19:00
-
-
@John: Thanks. @LBushkin: You could probably drop the `ms.Close();`, since it should be closed automatically as it leaves the using block. – Steven Sudit Feb 05 '10 at 19:45
-
1You are referencing `obj` before you declare it in the `Deserialize` method – Kevin DiTraglia Oct 03 '13 at 13:05
If you can require .Net 3.5, use System.Web.Script.Serialization.JavaScriptSerializer

- 24,142
- 15
- 92
- 130