Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


JSON Serializer in .NET
///
/// Helper JSON Serialization tools
///

public static class JSONSerializationUtility
{
///
/// Serialize to JSON
///

///
///
public static string Serialize(object @object)
{
string xml = null;
using (MemoryStream stream1 = new MemoryStream())
{
var ser = new DataContractJsonSerializer(@object.GetType());
ser.WriteObject(stream1, @object);

stream1.Position = 0;
using (StreamReader sr = new StreamReader(stream1))
{
xml = sr.ReadToEnd();
sr.Close();
}
stream1.Close();
}
return xml;
}

///
/// Deserialize from JSON
///

///
///
///
public static T Deserialize (string xml)
{
object result = null;
using (MemoryStream ms = new MemoryStream(Encoding.Unicode.GetBytes(xml)))
{
var ser = new DataContractJsonSerializer(typeof(T));
result = ser.ReadObject(ms);
ms.Close();
}

return (T)result;
}
}

Created By: amos 4/7/2015 11:32:08 AM