How do I take an object and convert it to a JSON string and then back into that object from a string, specifically, in WinRT for my Windows 8 Metro application?
Like this:
using System.IO; using System.Runtime.Serialization.Json; using System.Text; public static T Deserialize<T>(string json) { var _Bytes = Encoding.Unicode.GetBytes(json); using (MemoryStream _Stream = new MemoryStream(_Bytes)) { var _Serializer = new DataContractJsonSerializer(typeof(T)); return (T)_Serializer.ReadObject(_Stream); } } public static string Serialize(object instance) { using (MemoryStream _Stream = new MemoryStream()) { var _Serializer = new DataContractJsonSerializer(instance.GetType()); _Serializer.WriteObject(_Stream, instance); _Stream.Position = 0; using (StreamReader _Reader = new StreamReader(_Stream)) { return _Reader.ReadToEnd(); } } }
1.4m articles
1.4m replys
5 comments
57.0k users