Use this common class to serialize / deserialize JSON. You can easily serialize a complex data structure as follows:
Dictionary<string, Tuple<int, int[], bool, string>>
into a JSON string and then save it in the application settings or
public class JsonSerializer { public string Serialize<T>(T aObject) where T : new() { T serializedObj = new T(); MemoryStream ms = new MemoryStream(); DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T)); ser.WriteObject(ms, serializedObj); byte[] json = ms.ToArray(); ms.Close(); return Encoding.UTF8.GetString(json, 0, json.Length); } public T Deserialize<T>(string aJSON) where T : new() { T deserializedObj = new T(); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(aJSON)); DataContractJsonSerializer ser = new DataContractJsonSerializer(aJSON.GetType()); deserializedObj = (T)ser.ReadObject(ms); ms.Close(); return deserializedObj; } }
vinsa Nov 24 '17 at 0:51 2017-11-24 00:51
source share