http://www.clarkrabbit.net/2008/08/course-serialize-and-deserialize-your.html
http://www.dotblogs.com.tw/jeff377/archive/2008/08/19/4953.aspx
大家有沒有遇過一個問題,就是當你想要將一個 .net 裡自行開發的一個物件存到資料庫裡或是想要保存資料到網頁上某個 hidden field 要怎麼將物件變成可以儲存的格式?
序列化(Serialize)和反序列化(Deserialize)就是在解決大家所遇到的這件事情,並且提供了許多種格式的序列化類別來幫大家節省開發 的時間。事情當然不是簡單到直接把東西丟到序列化類別中就可以處理掉完成,因為只有你自已才知道這個物件哪些要序列化、哪些不序列化、哪些屬性要在物件建 立時預先設定,所以必需要在想要被序列化的物件實作必要的method,來完全這一項序列化的工作。
以下以一個 UserInfo 物件的例子來看看怎麼開始來做序列化和反序列化這事情。一開始我們會先有一個 UserInfo 的 class 。
view plaincopy to clipboardprint?
再來,我們需要替 UserInfo 加上一個 Interface ,並實作 Interface 裡的項目,來提供序列化類別使用,結果會像這樣。
view plaincopy to clipboardprint?
好了,經過上面兩個動作以後,這個UserInfo可以被多種序列化類別使用(相關文章看這裡),為了驗證是否正確,以下使用一種類別來作序列化工作。
好了,我們終於完成序列化的準備工作,開始來測試囉!
得到結果如下:
可喜可賀,我們完成了序列化和反序列化的偉大工作,UserInfo 物件從此將可以無所不在!
====序列化各種存儲格式======================================================================
序列化是將物件狀態轉換為可保存或可傳輸格式的處理序。序列化的反面是還原序列化,它可以將資料流轉換成物件。在 .NET 程式中常用的序列化方式如下表所示。
我們撰寫一個 TBSerializerUtil 類別,來處理上述幾種序列化,TBSerializerUtil 類別包含的序列化方法如下表所示。
TBSerializerUtil 類別的完整程式碼如下
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
Imports
System.Xml.Serialization
Imports
System.IO
Imports
System.Runtime.Serialization.Formatters.Soap
Imports
System.Runtime.Serialization.Formatters.Binary
Imports
System.Runtime.Serialization.Json
Imports
System.Runtime.Serialization
Namespace
[Lib]
''' <summary>
''' 物件序列化與反序化共用函式。
''' </summary>
Public
Class
TBSerializerUtil
''' <summary>
''' 將物件序列化為 XML 檔案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Sub
ObjectToXmlFile(ByVal
Value As
Object, ByVal
FileName As
String)
Dim
oSerializer As
XmlSerializer
Dim
oStream As
StreamWriter
Dim
sPath As
String
sPath = System.IO.Path.GetDirectoryName(FileName)
If
Not
System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End
If
oSerializer = New
XmlSerializer(Value.GetType())
oStream = New
StreamWriter(FileName)
oSerializer.Serialize(oStream, Value)
oStream.Close()
End
Sub
''' <summary>
''' 將 XML 檔案反序列化為物件。
''' </summary>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Function
XmlFileToObject(ByVal
FileName As
String, ByVal
Type As
System.Type) As
Object
Dim
oSerializer As
XmlSerializer
Dim
oFileStream As
FileStream
Dim
oValue As
Object
'若指定的檔案不存在則離開
If
Not
System.IO.File.Exists(FileName) Then
Return
Nothing
oSerializer = New
XmlSerializer(Type)
oFileStream = New
FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.Deserialize(oFileStream)
oFileStream.Close()
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 XML 字串。
''' </summary>
''' <param name="Value">物件。</param>
Public
Shared
Function
ObjectToXml(ByVal
Value As
Object) As
String
Dim
olSerializer As
XmlSerializer
Dim
oMemoryStream As
MemoryStream
Dim
oReader As
StreamReader
Dim
sText As
String
olSerializer = New
XmlSerializer(Value.GetType())
oMemoryStream = New
MemoryStream()
olSerializer.Serialize(oMemoryStream, Value)
oMemoryStream.Position = 0
oReader = New
StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return
sText
End
Function
''' <summary>
''' 將 XML 字串反序列化為物件。
''' </summary>
''' <param name="Text">XML 字串。</param>
Public
Shared
Function
XmlToObject(ByVal
Text As
String, ByVal
Type As
System.Type) As
Object
Dim
oSerializer As
XmlSerializer
Dim
oMemoryStream As
MemoryStream
Dim
oBuffer As
Byte()
Dim
oValue As
Object
oSerializer = New
XmlSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New
MemoryStream(oBuffer)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.Deserialize(oMemoryStream)
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 Soap 檔案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Sub
ObjectToSoapFile(ByVal
Value As
Object, ByVal
FileName As
String)
Dim
oFormatter As
SoapFormatter
Dim
oFileStream As
FileStream
Dim
sPath As
String
sPath = System.IO.Path.GetDirectoryName(FileName)
If
Not
System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End
If
oFormatter = New
SoapFormatter()
oFileStream = New
FileStream(FileName, FileMode.Create)
oFormatter.Serialize(oFileStream, Value)
oFileStream.Close()
End
Sub
''' <summary>
''' 將 Soap 檔案反序列化為物件。
''' </summary>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Function
SoapFileToObject(ByVal
FileName As
String, ByVal
Type As
System.Type) As
Object
Dim
oFormatter As
SoapFormatter
Dim
oFileStream As
FileStream
Dim
oValue As
Object
'若指定的檔案不存在則離開
If
Not
System.IO.File.Exists(FileName) Then
Return
Nothing
oFormatter = New
SoapFormatter()
oFileStream = New
FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oFileStream)
oFileStream.Close()
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 Soap 字串。
''' </summary>
''' <param name="Value">物件。</param>
Public
Shared
Function
ObjectToSoap(ByVal
Value As
Object) As
String
Dim
oFormatter As
SoapFormatter
Dim
oMemoryStream As
MemoryStream
Dim
oReader As
StreamReader
Dim
sText As
String
oFormatter = New
SoapFormatter()
oMemoryStream = New
MemoryStream()
oFormatter.Serialize(oMemoryStream, Value)
oMemoryStream.Position = 0
oReader = New
StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return
sText
End
Function
''' <summary>
''' 將 Soap 字串反序列化為物件。
''' </summary>
''' <param name="Text">Soap 字串。</param>
Public
Shared
Function
SoapToObject(ByVal
Text As
String, ByVal
Type As
System.Type) As
Object
Dim
oFormatter As
SoapFormatter
Dim
oMemoryStream As
MemoryStream
Dim
oBuffer As
Byte()
Dim
oValue As
Object
oFormatter = New
SoapFormatter()
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New
MemoryStream(oBuffer)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oMemoryStream)
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 Binary 檔案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Sub
ObjectToBinaryFile(ByVal
Value As
Object, ByVal
FileName As
String)
Dim
oFormatter As
BinaryFormatter
Dim
oFileStream As
FileStream
Dim
sPath As
String
sPath = System.IO.Path.GetDirectoryName(FileName)
If
Not
System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End
If
oFormatter = New
BinaryFormatter()
oFileStream = New
FileStream(FileName, FileMode.Create)
oFormatter.Serialize(oFileStream, Value)
oFileStream.Close()
End
Sub
''' <summary>
''' 將 Binary 檔案反序列化為物件。
''' </summary>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Function
BinaryFileToObject(ByVal
FileName As
String, ByVal
Type As
System.Type) As
Object
Dim
oFormatter As
BinaryFormatter
Dim
oFileStream As
FileStream
Dim
oValue As
Object
'若指定的檔案不存在則離開
If
Not
System.IO.File.Exists(FileName) Then
Return
Nothing
oFormatter = New
BinaryFormatter()
oFileStream = New
FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oFileStream)
oFileStream.Close()
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 Binary 資料。
''' </summary>
''' <param name="Value">物件。</param>
Public
Shared
Function
ObjectToBinary(ByVal
Value As
Object) As
Byte()
Dim
oFormatter As
SoapFormatter
Dim
oMemoryStream As
MemoryStream
oFormatter = New
SoapFormatter()
oMemoryStream = New
MemoryStream()
oFormatter.Serialize(oMemoryStream, Value)
oMemoryStream.Position = 0
Return
oMemoryStream.GetBuffer()
End
Function
''' <summary>
''' 將 Binary 資料反序列化為物件。
''' </summary>
''' <param name="Buffer">Binary 資料。</param>
Public
Shared
Function
BinaryToObject(ByVal
Buffer As
Byte(), ByVal
Type As
System.Type) As
Object
Dim
oFormatter As
SoapFormatter
Dim
oMemoryStream As
MemoryStream
Dim
oValue As
Object
oFormatter = New
SoapFormatter()
oMemoryStream = New
MemoryStream(Buffer)
'Call the Deserialize method and cast to the object type.
oValue = oFormatter.Deserialize(oMemoryStream)
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 Json 檔案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Sub
ObjectToJsonFile(ByVal
Value As
Object, ByVal
FileName As
String)
Dim
oSerializer As
DataContractJsonSerializer
Dim
oFileStream As
FileStream
Dim
sPath As
String
sPath = System.IO.Path.GetDirectoryName(FileName)
If
Not
System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End
If
oSerializer = New
DataContractJsonSerializer(Value.GetType())
oFileStream = New
FileStream(FileName, FileMode.Create)
oSerializer.WriteObject(oFileStream, Value)
oFileStream.Close()
End
Sub
''' <summary>
''' 將 Json 檔案反序列化為物件。
''' </summary>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Function
JsonFileToObject(ByVal
FileName As
String, ByVal
Type As
System.Type) As
Object
Dim
oSerializer As
DataContractJsonSerializer
Dim
oFileStream As
FileStream
Dim
oValue As
Object
'若指定的檔案不存在則離開
If
Not
System.IO.File.Exists(FileName) Then
Return
Nothing
oSerializer = New
DataContractJsonSerializer(Type)
oFileStream = New
FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oFileStream)
oFileStream.Close()
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 Json 字串。
''' </summary>
''' <param name="Value">物件。</param>
Public
Shared
Function
ObjectToJson(ByVal
Value As
Object) As
String
Dim
oSerializer As
DataContractJsonSerializer
Dim
oMemoryStream As
MemoryStream
Dim
oReader As
StreamReader
Dim
sText As
String
oSerializer = New
DataContractJsonSerializer(Value.GetType)
oMemoryStream = New
MemoryStream()
oSerializer.WriteObject(oMemoryStream, Value)
oMemoryStream.Position = 0
oReader = New
StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return
sText
End
Function
''' <summary>
''' 將 Json 字串反序列化為物件。
''' </summary>
''' <param name="Text">Json 字串。</param>
Public
Shared
Function
JsonToObject(ByVal
Text As
String, ByVal
Type As
System.Type) As
Object
Dim
oSerializer As
DataContractJsonSerializer
Dim
oMemoryStream As
MemoryStream
Dim
oBuffer As
Byte()
Dim
oValue As
Object
oSerializer = New
DataContractJsonSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New
MemoryStream(oBuffer)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oMemoryStream)
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 DataContract 檔案。
''' </summary>
''' <param name="Value">物件。</param>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Sub
ObjectToDataContractFile(ByVal
Value As
Object, ByVal
FileName As
String)
Dim
oSerializer As
DataContractSerializer
Dim
oFileStream As
FileStream
Dim
sPath As
String
sPath = System.IO.Path.GetDirectoryName(FileName)
If
Not
System.IO.Directory.Exists(sPath) Then
System.IO.Directory.CreateDirectory(sPath)
End
If
oSerializer = New
DataContractSerializer(Value.GetType())
oFileStream = New
FileStream(FileName, FileMode.Create)
oSerializer.WriteObject(oFileStream, Value)
oFileStream.Close()
End
Sub
''' <summary>
''' 將 DataContract 檔案反序列化為物件。
''' </summary>
''' <param name="FileName">檔案名稱。</param>
Public
Shared
Function
DataContractFileToObject(ByVal
FileName As
String, ByVal
Type As
System.Type) As
Object
Dim
oSerializer As
DataContractSerializer
Dim
oFileStream As
FileStream
Dim
oValue As
Object
'若指定的檔案不存在則離開
If
Not
System.IO.File.Exists(FileName) Then
Return
Nothing
oSerializer = New
DataContractSerializer(Type)
oFileStream = New
FileStream(FileName, FileMode.Open)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oFileStream)
oFileStream.Close()
Return
oValue
End
Function
''' <summary>
''' 將物件序列化為 DataContract 字串。
''' </summary>
''' <param name="Value">物件。</param>
Public
Shared
Function
ObjectToDataContract(ByVal
Value As
Object) As
String
Dim
oSerializer As
DataContractSerializer
Dim
oMemoryStream As
MemoryStream
Dim
oReader As
StreamReader
Dim
sText As
String
oSerializer = New
DataContractSerializer(Value.GetType)
oMemoryStream = New
MemoryStream()
oSerializer.WriteObject(oMemoryStream, Value)
oMemoryStream.Position = 0
oReader = New
StreamReader(oMemoryStream)
sText = oReader.ReadToEnd()
Return
sText
End
Function
''' <summary>
''' 將 DataContract 字串反序列化為物件。
''' </summary>
''' <param name="Text">Json 字串。</param>
Public
Shared
Function
DataContractToObject(ByVal
Text As
String, ByVal
Type As
System.Type) As
Object
Dim
oSerializer As
DataContractSerializer
Dim
oMemoryStream As
MemoryStream
Dim
oBuffer As
Byte()
Dim
oValue As
Object
oSerializer = New
DataContractSerializer(Type)
oBuffer = System.Text.Encoding.UTF8.GetBytes(Text)
oMemoryStream = New
MemoryStream(oBuffer)
'Call the Deserialize method and cast to the object type.
oValue = oSerializer.ReadObject(oMemoryStream)
Return
oValue
End
Function
End
Class
End
Namespace