using System.Web.Script.Serialization;
/// <summary>
/// 序列化器
/// </summary>
private static readonly JavaScriptSerializer serializer;
/// <summary>
/// 构造函数
/// </summary>
static SlJson()
{
serializer = new JavaScriptSerializer();
serializer.MaxJsonLength = int.MaxValue;
serializer.RecursionLimit = int.MaxValue;
}
/// <summary>
/// 将对象序列化为Json格式的字符串
/// </summary>
/// <param name="source">源类型对象</param>
/// <returns>Json格式的字符串</returns>
public static string ToJson(object source)
{
return serializer.Serialize(source);
}
/// <summary>
/// 将Json格式的数据转换为对象
/// </summary>
/// <typeparam name="T">源类型</typeparam>
/// <param name="json">json格式的字符串</param>
/// <returns>序列化之后的格式</returns>
public static T FromJson<T>(string json)
{
return serializer.Deserialize<T>(json);
}