VB.NET || Universal Object Serializer and Deserializer Using VB.NET
The following is a module with functions which demonstrates how to create a simple universal object serializer and deserializer using VB.NET.
The following functions take in a instance of the abstract classes ‘ObjectSerializer‘, and ‘ObjectDeserializer‘. Those classes defines the functionality that is common to all the classes derived from it. They are what carries out the actual serialization and deserialization.
Breaking things up like so makes it easy to have different serializations with only calling one function.
Note: The functions in this module uses code from previous articles which explains how to serialize and deserialize objects.
For those examples of how to serialize and deserialize objects using Json and XML, see below:
1. JsonSerializer
The example below demonstrates the use of ‘Utils.Objects.JsonSerializer‘ to serialize an object to Json.
By changing the second parameter, calling the same function will change its behavior.
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 |
' JsonSerializer Public Class Part Public Property PartName As String Public Property PartId As Integer End Class ' Declare list of objects Dim parts = New List(Of Part) From { New Part With { .PartName = "crank arm", .PartId = 1234 }, New Part With { .PartName = "chain ring", .PartId = 1334 }, New Part With { .PartName = "regular seat", .PartId = 1434 }, New Part With { .PartName = "banana seat", .PartId = 1444 }, New Part With { .PartName = "cassette", .PartId = 1534 }, New Part With { .PartName = "shift lever", .PartId = 1634 }} ' Serialize to json Dim serialized = Utils.Objects.Serialize(parts, New Utils.Objects.JsonSerializer) ' Display json Debug.Print(serialized) ' expected output: ' [ ' { ' "PartName": "crank arm", ' "PartId": 1234 ' }, ' { ' "PartName": "chain ring", ' "PartId": 1334 ' }, ' { ' "PartName": "regular seat", ' "PartId": 1434 ' }, ' { ' "PartName": "banana seat", ' "PartId": 1444 ' }, ' { ' "PartName": "cassette", ' "PartId": 1534 ' }, ' { ' "PartName": "shift lever", ' "PartId": 1634 ' } ' ] |
2. JsonDeserializer
The example below demonstrates the use of ‘Utils.Objects.JsonDeserializer‘ to deserialize an object from Json.
By changing the second parameter, calling the same function will change its behavior.
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 |
' JsonDeserializer ' Declare Json array of objects Dim partsJson = " [ { ""PartName"": ""crank arm"", ""PartId"": 1234 }, { ""PartName"": ""chain ring"", ""PartId"": 1334 }, { ""PartName"": ""regular seat"", ""PartId"": 1434 }, { ""PartName"": ""banana seat"", ""PartId"": 1444 }, { ""PartName"": ""cassette"", ""PartId"": 1534 }, { ""PartName"": ""shift lever"", ""PartId"": 1634 } ] " ' Deserialize to object Dim deserialized = Utils.Objects.Deserialize(Of List(Of Part))(partsJson, New Utils.Objects.JsonDeserializer) ' Display the items For Each item In deserialized Debug.Print($"{item.PartId} - {item.PartName}") Next ' expected output: ' 1234 - crank arm ' 1334 - chain ring ' 1434 - regular seat ' 1444 - banana seat ' 1534 - cassette ' 1634 - shift lever |
3. XmlSerializer
The example below demonstrates the use of ‘Utils.Objects.XmlSerializer‘ to serialize an object to Xml.
By changing the second parameter, calling the same function will change its behavior.
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 |
' XmlSerializer Public Class Part Public Property PartName As String Public Property PartId As Integer End Class ' Declare list of objects Dim parts = New List(Of Part) From { New Part With { .PartName = "crank arm", .PartId = 1234 }, New Part With { .PartName = "chain ring", .PartId = 1334 }, New Part With { .PartName = "regular seat", .PartId = 1434 }, New Part With { .PartName = "banana seat", .PartId = 1444 }, New Part With { .PartName = "cassette", .PartId = 1534 }, New Part With { .PartName = "shift lever", .PartId = 1634 }} ' Serialize to xml Dim serialized = Utils.Objects.Serialize(parts, New Utils.Objects.XmlSerializer) ' Display xml Debug.Print(serialized) ' expected output: ' <?xml version="1.0"?> ' <ArrayOfPart xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> ' <Part> ' <PartName>crank arm</PartName> ' <PartId>1234</PartId> ' </Part> ' <Part> ' <PartName>chain ring</PartName> ' <PartId>1334</PartId> ' </Part> ' <Part> ' <PartName>regular seat</PartName> ' <PartId>1434</PartId> ' </Part> ' <Part> ' <PartName>banana seat</PartName> ' <PartId>1444</PartId> ' </Part> ' <Part> ' <PartName>cassette</PartName> ' <PartId>1534</PartId> ' </Part> ' <Part> ' <PartName>shift lever</PartName> ' <PartId>1634</PartId> ' </Part> ' </ArrayOfPart> |
4. XmlDeserializer
The example below demonstrates the use of ‘Utils.Objects.XmlDeserializer‘ to deserialize an object from xml.
By changing the second parameter, calling the same function will change its behavior.
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 |
' XmlDeserializer ' Declare Xml array of objects Dim partsXml = $"<?xml version=""1.0""?> <ArrayOfPart xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <Part> <PartName>crank arm</PartName> <PartId>1234</PartId> </Part> <Part> <PartName>chain ring</PartName> <PartId>1334</PartId> </Part> <Part> <PartName>regular seat</PartName> <PartId>1434</PartId> </Part> <Part> <PartName>banana seat</PartName> <PartId>1444</PartId> </Part> <Part> <PartName>cassette</PartName> <PartId>1534</PartId> </Part> <Part> <PartName>shift lever</PartName> <PartId>1634</PartId> </Part> </ArrayOfPart> " ' Deserialize to object Dim deserialized = Utils.Objects.Deserialize(Of List(Of Part))(partsXml, New Utils.Objects.XmlDeserializer) ' Display the items For Each item In deserialized Debug.Print($"{item.PartId} - {item.PartName}") Next ' expected output: ' 1234 - crank arm ' 1334 - chain ring ' 1434 - regular seat ' 1444 - banana seat ' 1534 - cassette ' 1634 - shift lever |
5. Utils Namespace
The following is the Utils Namespace. Include this in your project to start using!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 20, 2020 ' Taken From: http://programmingnotes.org/ ' File: Utils.vb ' Description: Handles general utility functions ' ============================================================================ Option Strict On Option Explicit On Namespace Global.Utils Namespace Objects Public Module modObjects ''' <summary> ''' Serializes the specified value according to the ObjectSerializer ''' </summary> ''' <param name="value">The value to serialize</param> ''' <param name="serializer">Determines how the value should be serialized</param> ''' <returns>The value serialized according to the ObjectSerializer</returns> Public Function Serialize(Of T)(value As T, serializer As ObjectSerializer) As String Return serializer.Serialize(value) End Function ''' <summary> ''' Deserializes the specified value according to the ObjectDeserializer ''' </summary> ''' <param name="value">The value to deserialize</param> ''' <param name="deserializer">Determines how the value should be deserialized</param> ''' <returns>The value deserialized according to the ObjectSerializer</returns> Public Function Deserialize(Of T)(value As String, deserializer As ObjectDeserializer) As T Return deserializer.Deserialize(Of T)(value) End Function End Module #Region "ObjectSerializer" ''' <summary> ''' Defines the object serializer behavior ''' </summary> Public MustInherit Class ObjectSerializer Public MustOverride Function Serialize(Of T)(value As T) As String End Class Public Class JsonSerializer Inherits ObjectSerializer Public Property settings As New Newtonsoft.Json.JsonSerializerSettings Public Sub New() settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore settings.Formatting = Newtonsoft.Json.Formatting.Indented End Sub Public Overrides Function Serialize(Of T)(value As T) As String Return Utils.Json.Serialize(value, settings) End Function End Class Public Class XmlSerializer Inherits ObjectSerializer Public Property root As New System.Xml.Serialization.XmlRootAttribute Public Overrides Function Serialize(Of T)(value As T) As String Return Utils.Xml.Serialize(value, root) End Function End Class #End Region #Region "ObjectDeserializer" ''' <summary> ''' Defines the object deserializer behavior ''' </summary> Public MustInherit Class ObjectDeserializer Public MustOverride Function Deserialize(Of T)(value As String) As T End Class Public Class JsonDeserializer Inherits ObjectDeserializer Public Property settings As New Newtonsoft.Json.JsonSerializerSettings Public Sub New() settings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore End Sub Public Overrides Function Deserialize(Of T)(value As String) As T Dim obj As T = Nothing If Not String.IsNullOrEmpty(value) Then obj = Utils.Json.Deserialize(Of T)(value, settings) End If Return obj End Function End Class Public Class XmlDeserializer Inherits ObjectDeserializer Public Property root As New System.Xml.Serialization.XmlRootAttribute Public Overrides Function Deserialize(Of T)(value As String) As T Dim obj As T = Nothing If Not String.IsNullOrEmpty(value) Then obj = Utils.Xml.Deserialize(Of T)(value, root) End If Return obj End Function End Class #End Region End Namespace End Namespace ' http://programmingnotes.org/ |
6. More Examples
Below are more examples demonstrating the use of the ‘Utils‘ Namespace. Don’t forget to include the module when running the examples!
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 |
' ============================================================================ ' Author: Kenneth Perkins ' Date: Nov 20, 2020 ' Taken From: http://programmingnotes.org/ ' File: Program.vb ' Description: The following demonstrates the use of the Utils Namespace ' ============================================================================ Option Strict On Option Explicit On Imports System Public Module Program Public Class Part Public Property PartName As String Public Property PartId As Integer End Class Sub Main(args As String()) Try ' Declare list of objects Dim parts = New List(Of Part) From { New Part With { .PartName = "crank arm", .PartId = 1234 }, New Part With { .PartName = "chain ring", .PartId = 1334 }, New Part With { .PartName = "regular seat", .PartId = 1434 }, New Part With { .PartName = "banana seat", .PartId = 1444 }, New Part With { .PartName = "cassette", .PartId = 1534 }, New Part With { .PartName = "shift lever", .PartId = 1634 }} ' Serialize to json Dim serialized = Utils.Objects.Serialize(parts, New Utils.Objects.JsonSerializer) ' Display json Display(serialized) ' Declare Json array of objects Dim partsJson = " [ { ""PartName"": ""crank arm"", ""PartId"": 1234 }, { ""PartName"": ""chain ring"", ""PartId"": 1334 }, { ""PartName"": ""regular seat"", ""PartId"": 1434 }, { ""PartName"": ""banana seat"", ""PartId"": 1444 }, { ""PartName"": ""cassette"", ""PartId"": 1534 }, { ""PartName"": ""shift lever"", ""PartId"": 1634 } ] " ' Deserialize to object Dim deserialized = Utils.Objects.Deserialize(Of List(Of Part))(partsJson, New Utils.Objects.JsonDeserializer) ' Display the items For Each item In deserialized Display($"{item.PartId} - {item.PartName}") Next ' Serialize to xml Dim serialized2 = Utils.Objects.Serialize(parts, New Utils.Objects.XmlSerializer) ' Display xml Display(serialized2) ' Declare Xml array of objects Dim partsXml = $"<?xml version=""1.0""?> <ArrayOfPart xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema""> <Part> <PartName>crank arm</PartName> <PartId>1234</PartId> </Part> <Part> <PartName>chain ring</PartName> <PartId>1334</PartId> </Part> <Part> <PartName>regular seat</PartName> <PartId>1434</PartId> </Part> <Part> <PartName>banana seat</PartName> <PartId>1444</PartId> </Part> <Part> <PartName>cassette</PartName> <PartId>1534</PartId> </Part> <Part> <PartName>shift lever</PartName> <PartId>1634</PartId> </Part> </ArrayOfPart> " ' Deserialize to object Dim deserialized2 = Utils.Objects.Deserialize(Of List(Of Part))(partsXml, New Utils.Objects.XmlDeserializer) ' Display the items For Each item In deserialized2 Display($"{item.PartId} - {item.PartName}") Next Catch ex As Exception Display(ex.ToString) Finally Console.ReadLine() End Try End Sub Public Sub Display(message As String) Console.WriteLine(message) Debug.Print(message) End Sub End Module ' http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Leave a Reply