物件序列化函式庫
序列化是將物件狀態轉換為可保存或可傳輸格式的處理序。序列化的反面是還原序列化,它可以將資料流轉換成物件。在 .NET 程式中常用的序列化方式如下表所示。
序列化類別 | 說明 | .NET Framework 版本 |
XmlSerializer | XML 序列化 | 2.0 |
SoapFormatter | SOAP 序列化 | 2.0 |
BinaryFormatter | 二進位序列化 | 2.0 |
DataContractSerializer | 資料合約序列化 | 3.5 |
DataContractJsonSerializer | JSON 序列化 | 3.5 |
我們撰寫一個 TBSerializerUtil 類別,來處理上述幾種序列化,TBSerializerUtil 類別包含的序列化方法如下表所示。
方法 | 序列化類別 | 說明 |
ObjectToXmlFile | XmlSerializer | 將物件序列化為 XML 檔案 |
XmlFileToObject | XmlSerializer | 將 XML 檔案反序列化為物件 |
ObjectToXml | XmlSerializer | 將物件序列化為 XML 字串 |
XmlToObject | XmlSerializer | 將 XML 字串反序列化為物件 |
ObjectToSoapFile | SoapFormatter | 將物件序列化為 Soap 檔案 |
SoapFileToObject | SoapFormatter | 將 Soap 檔案反序列化為物件 |
ObjectToSoap | SoapFormatter | 將物件序列化為 Soap 字串 |
SoapToObject | SoapFormatter | 將 Soap 字串反序列化為物件 |
ObjectToBinaryFile | BinaryFormatter | 將物件序列化為 Binary 檔案 |
BinaryFileToObject | BinaryFormatter | 將 Binary 檔案反序列化為物件 |
ObjectToBinary | BinaryFormatter | 將物件序列化為 Binary 資料 |
BinaryToObject | BinaryFormatter | 將 Binary 資料反序列化為物件 |
ObjectToDataContractFile | DataContractSerializer | 將物件序列化為 DataContract 檔案 |
DataContractFileToObject | DataContractSerializer | 將 DataContract 檔案反序列化為物件 |
ObjectToDataContract | DataContractSerializer | 將物件序列化為 DataContract 字串 |
DataContractToObject | DataContractSerializer | 將 DataContract 字串反序列化為物件 |
ObjectToJsonFile | DataContractJsonSerializer | 將物件序列化為 Json 檔案 |
JsonFileToObject | DataContractJsonSerializer | 將 Json 檔案反序列化為物件 |
ObjectToJson | DataContractJsonSerializer | 將物件序列化為 Json 字串 |
JsonToObject | DataContractJsonSerializer | 將 Json 字串反序列化為物件 |
TBSerializerUtil 類別的完整程式碼如下
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