Does .NET 4 come with any class that serializes/deserializes JSON data?
I know there are 3rd-party libraries, such as JSON.NET, but I am looking for somethi
Use this generic class in order to serialize / deserialize JSON. You can easy serialize complex data structure like this:
Dictionary>
to JSON string and then to save it in application setting or else
public class JsonSerializer
{
public string Serialize(T aObject) where T : new()
{
T serializedObj = new T();
MemoryStream ms = new MemoryStream();
DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(T));
ser.WriteObject(ms, aObject);
byte[] json = ms.ToArray();
ms.Close();
return Encoding.UTF8.GetString(json, 0, json.Length);
}
public T Deserialize(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;
}
}