I wrote my own JSON serializer using DataContractJsonSerializer in the System.ServiceModel.Web.dll
assembly [which is a component of WCF included in .NET 3.5 as a standard assembly, and in the .NET 3.5 SP1 Client Profile] (in .NET 4.0 and Silverlight 4, it's been moved to System.Runtime.Serialization.dll
).
using System.IO;
using System.Runtime.Serialization.Json;
public class JsonObjectSerializer
{
public string Serialize<T>(T instance) where T : class
{
var serializer = new DataContractJsonSerializer(typeof(T));
using (var memoryStream = new MemoryStream())
{
serializer.WriteObject(memoryStream, instance);
memoryStream.Flush();
memoryStream.Position = 0;
using (var reader = new StreamReader(memoryStream))
{
return reader.ReadToEnd();
}
}
}
public T Deserialize<T>(string serialized) where T : class
{
var serializer = new DataContractJsonSerializer(typeof(T));
using (var memoryStream = new MemoryStream())
{
using (var writer = new StreamWriter(memoryStream))
{
writer.Write(serialized);
writer.Flush();
memoryStream.Position = 0;
return serializer.ReadObject(memoryStream) as T;
}
}
}
}