Utilice esta clase genérica para serializar/deserializar JSON. Puede serializar fácil estructura de datos compleja como esta:
Dictionary<string, Tuple<int, int[], bool, string>>
a cadena JSON y luego guardarlo en configuración de la aplicación o de lo contrario
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;
}
}
JSON.Net está bien apoyado y parece que Microsoft tiene la intención de adoptarlo ellos mismos * "Nosotros en el equipo web incluiremos JSON.NET como el serializador JSON predeterminado en la API web cuando se lance, por lo que será agradable". * de http://www.hanselman.com/blog/ OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx – Liam
¡Tenga en cuenta la biblioteca incorporada para el ** rendimiento ** de serialización de JSon en .Net! – Babak
@Babak ¿a qué te refieres, cuidado? Por favor elabora. –