1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207 |
<br> using
System; using
System.Collections.Generic; using
System.Linq; using
System.Text; using
System.IO; using
System.Runtime.Serialization; using
System.Runtime.Serialization.Json; using
System.Runtime.Serialization.Formatters.Binary; using
System.Runtime.Serialization.Formatters.Soap; using
System.Xml.Serialization; namespace
SerializerSample { /// <summary> /// 序列化帮助类 /// </summary> public
sealed class SerializeHelper { #region DataContract序列化 /// <summary> /// DataContract序列化 /// </summary> /// <param name="value"></param> /// <param name="knownTypes"></param> /// <returns></returns> public
static string SerializeDataContract( object
value, List<Type> knownTypes = null ) { DataContractSerializer dataContractSerializer = new
DataContractSerializer(value.GetType(), knownTypes); using
(MemoryStream ms = new
MemoryStream()) { dataContractSerializer.WriteObject(ms, value); ms.Seek(0, SeekOrigin.Begin); using
(StreamReader sr = new
StreamReader(ms)) { return
sr.ReadToEnd(); } } } /// <summary> /// DataContract反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="xml"></param> /// <returns></returns> public
static T DeserializeDataContract<T>( string
xml) { using
(MemoryStream ms = new
MemoryStream(Encoding.UTF8.GetBytes(xml))) { DataContractSerializer serializer = new
DataContractSerializer( typeof (T)); return
(T)serializer.ReadObject(ms); } } #endregion #region DataContractJson序列化 /// <summary> /// DataContractJson序列化 /// </summary> /// <param name="value"></param> /// <returns></returns> public
static string SerializeDataContractJson( object
value) { DataContractJsonSerializer dataContractSerializer = new
DataContractJsonSerializer(value.GetType()); using
(MemoryStream ms = new
MemoryStream()) { dataContractSerializer.WriteObject(ms, value); return
Encoding.UTF8.GetString(ms.ToArray()); } } /// <summary> /// DataContractJson反序列化 /// </summary> /// <param name="type"></param> /// <param name="str"></param> /// <returns></returns> public
static object DeserializeDataContractJson(Type type, string
str) { DataContractJsonSerializer dataContractSerializer = new
DataContractJsonSerializer(type); using
(MemoryStream ms = new
MemoryStream(System.Text.Encoding.UTF8.GetBytes(str))) { return
dataContractSerializer.ReadObject(ms); } } /// <summary> /// DataContractJson反序列化 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="json"></param> /// <returns></returns> public
T DeserializeDataContractJson<T>( string
json) { DataContractJsonSerializer dataContractSerializer = new
DataContractJsonSerializer( typeof (T)); using
(MemoryStream ms = new
MemoryStream(Encoding.UTF8.GetBytes(json))) { return
(T)dataContractSerializer.ReadObject(ms); } } #endregion #region XmlSerializer序列化 /// <summary> /// 将对象序列化到 XML 文档中和从 XML 文档中反序列化对象。XmlSerializer 使您得以控制如何将对象编码到 XML 中。 /// </summary> /// <param name="value"></param> /// <returns></returns> public
static string SerializeXml( object
value) { XmlSerializer serializer = new
XmlSerializer(value.GetType()); using
(MemoryStream ms = new
MemoryStream()) { serializer.Serialize(ms, value); ms.Seek(0, SeekOrigin.Begin); using
(StreamReader sr = new
StreamReader(ms)) { return
sr.ReadToEnd(); } } } /// <summary> /// XmlSerializer反序列化 /// </summary> /// <param name="type"></param> /// <param name="str"></param> /// <returns></returns> public
static object DeserializeXml(Type type, string
str) { XmlSerializer serializer = new
XmlSerializer(type); byte [] bytes = System.Text.Encoding.UTF8.GetBytes(str); using
(MemoryStream ms = new
MemoryStream(bytes)) { return
serializer.Deserialize(ms); } } #endregion #region BinaryFormatter序列化 /// <summary> /// BinaryFormatter序列化 /// 必须类型必须标记为Serializable /// </summary> /// <param name="obj"></param> /// <returns></returns> public
static string SerializeBinaryFormatter( object
obj) { BinaryFormatter formatter = new
BinaryFormatter(); using
(MemoryStream ms = new
MemoryStream()) { formatter.Serialize(ms,obj); byte [] bytes = ms.ToArray(); obj = formatter.Deserialize( new
MemoryStream(bytes)); //如果是UTF8格式,则反序列化报错。可以用Default格式,不过,建议还是传参为byte数组比较好 return
Encoding.Default.GetString(bytes); } } /// <summary> /// BinaryFormatter反序列化 /// 必须类型必须标记为Serializable /// </summary> /// <param name="serializedStr"></param> /// <returns></returns> public
static T DeserializeBinaryFormatter<T>( string
serializedStr) { BinaryFormatter formatter = new
BinaryFormatter(); byte [] bytes = Encoding.Default.GetBytes(serializedStr); using
(MemoryStream ms = new
MemoryStream(bytes)) { return
(T)formatter.Deserialize(ms); } } #endregion #region SoapFormatter序列化 /// <summary> /// SoapFormatter序列化 /// 必须类型必须标记为Serializable /// </summary> /// <param name="obj"></param> /// <returns></returns> public
static string SerializeSoapFormatter( object
obj) { SoapFormatter formatter = new
SoapFormatter(); using
(MemoryStream ms = new
MemoryStream()) { formatter.Serialize(ms, obj); byte [] bytes = ms.ToArray(); return
Encoding.UTF8.GetString(bytes); } } /// <summary> /// SoapFormatter反序列化 /// 必须类型必须标记为Serializable /// </summary> /// <param name="serializedStr"></param> /// <returns></returns> public
static T DeserializeSoapFormatter<T>( string
serializedStr) { SoapFormatter formatter = new
SoapFormatter(); using
(MemoryStream ms = new
MemoryStream(Encoding.UTF8.GetBytes(serializedStr))) { return
(T)formatter.Deserialize(ms); } } #endregion } } |
原文:http://www.cnblogs.com/chenqingpeng/p/3546213.html