物件序列化函式庫

物件序列化函式庫

序列化是將物件狀態轉換為可保存或可傳輸格式的處理序。序列化的反面是還原序列化,它可以將資料流轉換成物件。在 .NET 程式中常用的序列化方式如下表所示。

序列化類別說明.NET Framework 版本
XmlSerializerXML 序列化2.0
SoapFormatterSOAP 序列化2.0
BinaryFormatter二進位序列化2.0
DataContractSerializer資料合約序列化3.5
DataContractJsonSerializerJSON 序列化3.5

 

我們撰寫一個 TBSerializerUtil 類別,來處理上述幾種序列化,TBSerializerUtil 類別包含的序列化方法如下表所示。

方法序列化類別說明
ObjectToXmlFileXmlSerializer將物件序列化為 XML 檔案
XmlFileToObjectXmlSerializer將 XML 檔案反序列化為物件
ObjectToXmlXmlSerializer將物件序列化為 XML 字串
XmlToObjectXmlSerializer將 XML 字串反序列化為物件
ObjectToSoapFileSoapFormatter將物件序列化為 Soap 檔案
SoapFileToObjectSoapFormatter將 Soap 檔案反序列化為物件
ObjectToSoapSoapFormatter將物件序列化為 Soap 字串
SoapToObjectSoapFormatter將 Soap 字串反序列化為物件
ObjectToBinaryFileBinaryFormatter將物件序列化為 Binary 檔案
BinaryFileToObjectBinaryFormatter將 Binary 檔案反序列化為物件
ObjectToBinaryBinaryFormatter將物件序列化為 Binary 資料
BinaryToObjectBinaryFormatter將 Binary 資料反序列化為物件
ObjectToDataContractFileDataContractSerializer將物件序列化為 DataContract 檔案
DataContractFileToObjectDataContractSerializer將 DataContract 檔案反序列化為物件
ObjectToDataContractDataContractSerializer將物件序列化為 DataContract 字串
DataContractToObjectDataContractSerializer將 DataContract 字串反序列化為物件
ObjectToJsonFileDataContractJsonSerializer將物件序列化為 Json 檔案
JsonFileToObjectDataContractJsonSerializer將 Json 檔案反序列化為物件
ObjectToJsonDataContractJsonSerializer將物件序列化為 Json 字串
JsonToObjectDataContractJsonSerializer將 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

ASP.NET 魔法學院