Skip to content

Instantly share code, notes, and snippets.

@follesoe
Created December 24, 2010 14:57
Show Gist options
  • Save follesoe/754314 to your computer and use it in GitHub Desktop.
Save follesoe/754314 to your computer and use it in GitHub Desktop.
Bridging windows and web applications using JSON
public static string ToJSON<T>(this T obj)
{
MemoryStream ms = new MemoryStream();
try
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(obj.GetType());
serializer.WriteObject(ms, obj);
return Encoding.UTF8.GetString(ms.ToArray());
}
finally
{
ms.Close();
ms.Dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment