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);
        }

 

posted on 2016-11-22 17:33  菜鸟Only  阅读(137)  评论(0编辑  收藏  举报