- 1 using Newtonsoft.Json;
- 2 using System;
- 3 using System.IO;
- 4 using System.Runtime.Serialization.Json;
- 5 using System.Text;
- 6 /************************************************
- 7 ◇作者: LowKeyC
- 8 ◇说明: 通用管理里面的JSON的序列化和反序列化
- 9 ◇版本号:V1.0
- 10 ◇创建日期:2017年6月22日 星期四
- 11 *****************************************************/
- 12 namespace RapidDevelopmentFramework.Common
- 13 {
- 14 /// <summary>
- 15 /// JSON辅助类
- 16 /// </summary>
- 17 public static class JSONHelper
- 18 {
- 19
- 20 /// <summary>
- 21 /// JSON 序列化的普通版本
- 22 /// </summary>
- 23 /// <typeparam name="T"></typeparam>
- 24 /// <param name="_Object"></param>
- 25 /// <returns></returns>
- 26 public static
- String ObjectToJSON(
- this
- T _Object)
- where
- T :
- class
- 27 {
- 28 return JsonConvert.SerializeObject(_Object);
- 29 }
- 30
- 31 /// <summary>
- 32 /// JSON 序列化的标准版本
- 33 /// </summary>
- 34 /// <param name="_Object"></param>
- 35 /// <returns></returns>
- 36 public static
- String ObjectToJSON(
- this Object _Object)
- 37 {
- 38 return JsonConvert.SerializeObject(_Object);
- 39 }
- 40
- 41
- 42 /// <summary>
- 43 /// JSON 序列化的官方版本
- 44 /// </summary>
- 45 /// <typeparam name="T"></typeparam>
- 46 /// <param name="_Object"></param>
- 47 /// <returns></returns>
- 48 public static
- String ObjectToJSONOfficial(
- this
- T _Object)
- where
- T :
- class
- 49 {
- 50 using
- (
- var
- TempMemoryStream =
- new MemoryStream())
- 51 {
- 52 var
- MyDataContractJsonSerializer =
- new
- DataContractJsonSerializer(
- typeof(T));
- 53 MyDataContractJsonSerializer.WriteObject(TempMemoryStream, _Object);
- 54 var
- MySerializationString =
- Encoding.UTF8.GetString(TempMemoryStream.ToArray());
- 55 return MySerializationString;
- 56 }
- 57 }
- 58
- 59
- 60 /// <summary>
- 61 ///JSON 序列化的Jil 版本
- 62 /// </summary>
- 63 /// <typeparam name="T"></typeparam>
- 64 /// <param name="_Object"></param>
- 65 /// <returns></returns>
- 66 public static
- String ObjectToJSONJil(
- this
- T _Object)
- where
- T :
- class
- 67 {
- 68 return Jil.JSON.Serialize(_Object, Jil.Options.ExcludeNullsIncludeInherited);
- 69 }
- 70
- 71
- 72 /// <summary>
- 73 /// JSON 序列化的Jil版本 可以包含null
- 74 /// </summary>
- 75 /// <typeparam name="T"></typeparam>
- 76 /// <param name="_Object"></param>
- 77 /// <returns></returns>
- 78 public static
- String ObjectToJSONJilIncludeNulls(
- this
- T _Object)
- where
- T:
- class
- 79 {
- 80 return Jil.JSON.Serialize(_Object, Jil.Options.IncludeInherited);
- 81 }
- 82
- 83
- 84 /// <summary>
- 85 /// JSON 的反序列化
- 86 /// </summary>
- 87 /// <typeparam name="T"></typeparam>
- 88 /// <param name="_JSONString"></param>
- 89 /// <returns></returns>
- 90 public static
- T JSONToObjectJSON(
- this
- String _JSONString)
- where
- T:
- class
- 91 {
- 92 return
- JsonConvert.DeserializeObject
- (_JSONString);
- 93 }
- 94
- 95
- 96 /// <summary>
- 97 /// JSON的反序列化的官方版本
- 98 /// </summary>
- 99 /// <typeparam name="T"></typeparam>
- 100 /// <param name="_JSONString"></param>
- 101 /// <returns></returns>
- 102 public static
- T JSONToObjectOfficial(
- this
- String _JSONString)
- where
- T :
- class
- 103 {
- 104 using
- (
- var
- TempMemoryStream =
- new MemoryStream(Encoding.UTF8.GetBytes(_JSONString)))
- 105 {
- 106 var
- MyDataContractJsonSerializer =
- new
- DataContractJsonSerializer(
- typeof(T));
- 107 return (T)MyDataContractJsonSerializer.ReadObject(TempMemoryStream);
- 108 }
- 109 }
- 110
- 111
- 112 /// <summary>
- 113 /// JSON 的反序列化的官方版本
- 114 /// </summary>
- 115 /// <typeparam name="T"></typeparam>
- 116 /// <param name="_JSONString"></param>
- 117 /// <returns></returns>
- 118 public static
- T JSONToObjectJil(
- this
- String _JSONString)
- where
- T :
- class
- 119 {
- 120 using
- (
- var
- InputString =
- new StringReader(_JSONString))
- 121 {
- 122 var
- ObjectResult = Jil.JSON.Deserialize
- (InputString);
- 123 return ObjectResult;
- 124 }
- 125 }
- 126
- 127
- 128 /// <summary>
- 129 /// JSON 的反序列化的Jil 版本 其中可以包含null
- 130 /// </summary>
- 131 /// <typeparam name="T"></typeparam>
- 132 /// <param name="_JSONString"></param>
- 133 /// <returns></returns>
- 134 public static
- T JSONToObjectJilIncludeNulls(
- this
- String _JSONString)
- where
- T :
- class
- 135 {
- 136 using
- (
- var
- InputString =
- new StringReader(_JSONString))
- 137 {
- 138 var
- ObjectResult = Jil.JSON.Deserialize
- (InputString, Jil.Options.IncludeInherited);
- 139 return ObjectResult;
- 140 }
- 141 }
- 142
- 143 }
- 144
- }
来源: http://www.cnblogs.com/LowKeyCXY/p/7146563.html