利用扩展方法重写JSON序列化和反序列化
利用.NET 3.5以后的扩展方法重写JSON序列化和反序列化,在代码可读性和可维护性上更加加强了。
首先是不使用扩展方法的写法
定义部分:
- /// <summary>
- /// JSON序列化反序列化工具
- /// </summary>
- public class JSONControl
- {
- /// <summary>
- /// JSON序列化
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="_Object"></param>
- /// <returns></returns>
- public static String JsonSerializer<T>(T _Object)
- {
- using (MemoryStream TempMemoryStream = new MemoryStream())
- {
- String MySerializationString = String.Empty;
- DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
- MyDataContractJsonSerializer.WriteObject(TempMemoryStream, _Object);
- MySerializationString = Encoding.UTF8.GetString(TempMemoryStream.ToArray());
- return MySerializationString;
- }
- }
- /// <summary>
- /// 反序列化
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="_JsonString"></param>
- /// <returns></returns>
- public static T JsonDeserializer<T>(String _JsonString)
- {
- using (MemoryStream TempMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(_JsonString)))
- {
- DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
- return (T)MyDataContractJsonSerializer.ReadObject(TempMemoryStream);
- }
- }
- }
使用部分:
- /// <summary>
- /// 场景控制对象
- /// </summary>
- public class SceneControl
- {
- /// <summary>
- /// 临时序列化对象
- /// </summary>
- [DataContract]
- public class TempClass
- {
- /// <summary>
- /// 属性1
- /// </summary>
- [DataMember(Name = "Property1")]
- public String Property1 { get; set; }
- /// <summary>
- /// 属性2
- /// </summary>
- [DataMember(Name = "Property2")]
- public String Property2 { get; set; }
- }
- /// <summary>
- /// 测试
- /// </summary>
- public void Test()
- {
- String JsonString = "{'Property1':'111','Property2':'2'}";
- TempClass MyTempClass = JSONControl.JsonDeserializer<TempClass>(JsonString);
- }
- }
然后是使用扩展方法的写法
定义部分:
- /// <summary>
- /// JSON序列化反序列化工具
- /// </summary>
- public static class JSONControl
- {
- /// <summary>
- /// JSON序列化
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="_Object"></param>
- /// <returns></returns>
- public static String ObjectToJson<T>(this T _Object)
- {
- using (MemoryStream TempMemoryStream = new MemoryStream())
- {
- String MySerializationString = String.Empty;
- DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
- MyDataContractJsonSerializer.WriteObject(TempMemoryStream, _Object);
- MySerializationString = Encoding.UTF8.GetString(TempMemoryStream.ToArray());
- return MySerializationString;
- }
- }
- /// <summary>
- /// JSON反序列化
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="_JsonString"></param>
- /// <returns></returns>
- public static T JsonToObject<T>(this String _JsonString)
- {
- using (MemoryStream TempMemoryStream = new MemoryStream(Encoding.UTF8.GetBytes(_JsonString)))
- {
- DataContractJsonSerializer MyDataContractJsonSerializer = new DataContractJsonSerializer(typeof(T));
- return (T)MyDataContractJsonSerializer.ReadObject(TempMemoryStream);
- }
- }
- }
使用部分:
- /// <summary>
- /// 场景控制对象
- /// </summary>
- public class SceneControl
- {
- /// <summary>
- /// 临时序列化对象
- /// </summary>
- [DataContract]
- public class TempClass
- {
- /// <summary>
- /// 属性1
- /// </summary>
- [DataMember(Name = "Property1")]
- public String Property1 { get; set; }
- /// <summary>
- /// 属性2
- /// </summary>
- [DataMember(Name = "Property2")]
- public String Property2 { get; set; }
- }
- /// <summary>
- /// 测试
- /// </summary>
- public void Test()
- {
- String JsonString = "{'Property1':'111','Property2':'2'}";
- TempClass MyTempClass = JsonString.JsonToObject<TempClass>();
- }
- }
原文地址:http://zhangrou.net/post/2014/06/23/利用扩展方法重写JSON序列化和反序列化.aspx