.

JSON serialization

//This is just used to serialize the results to the file as JSON and read it back
private static string ToJson(T obj)
{
MemoryStream stream = new MemoryStream();

try
{
DataContractJsonSerializer jsSerializer = new DataContractJsonSerializer(typeof (T));
jsSerializer.WriteObject(stream, obj);

return Encoding.UTF8.GetString(stream.ToArray());
}
finally
{
stream.Close();
stream.Dispose();
}
}

private static T FromJson(string input)
{
MemoryStream stream = new MemoryStream();

try
{
DataContractJsonSerializer jsSerializer = new DataContractJsonSerializer(typeof (T));
stream = new MemoryStream(Encoding.UTF8.GetBytes(input));
T obj = (T) jsSerializer.ReadObject(stream);

return obj;
}
finally
{
stream.Close();
stream.Dispose();
}
}

What's your thoughts on this?

*

Protected by WP Anti Spam