Json.NET & VB.NET – How to Serialize an Object ?

One of the ways to serialize an object to JSON string in VB.NET in the Json.NET is using the SerializeObject method defined in the JsonConvert method.

The JsonConvert class provides an easy to use wrapper class and method over the JsonSerializer.

How to Serialize an Object in VB.NET using Json.NET ?

Below is the sample code snippet demonstrating the usage of JsonConvert.SerializeObject method in C# for serialization. The SerializeObject method has 7 overloads option.

Public Shared Function SerializeObject(value As Object) As String
End Function
Public Shared Function SerializeObject(value As Object, ParamArray converters As JsonConverter()) As String
End Function
Public Shared Function SerializeObject(value As Object, settings As JsonSerializerSettings) As String
End Function
Public Shared Function SerializeObject(value As Object, formatting As Formatting) As String
End Function
Public Shared Function SerializeObject(value As Object, formatting As Formatting, ParamArray converters As JsonConverter()) As String
End Function
Public Shared Function SerializeObject(value As Object, type As Type, settings As JsonSerializerSettings) As String
End Function
Public Shared Function SerializeObject(value As Object, formatting As Formatting, settings As JsonSerializerSettings) As String
End Function
Public Shared Function SerializeObject(value As Object, type As Type, formatting As Formatting, settings As JsonSerializerSettings) As String
End Function

The below code uses the parameterized method which uses the Formatting as the second parameter.

Imports Newtonsoft.Json

Module Module1

    Sub Main()
        Dim emp As New Employee()
        emp.Name = "Abundantcode"
        emp.IsPermanent = True
        emp.Departments = New List(Of String)()
        emp.Departments.Add("Technology")
        emp.Departments.Add("Product Engineering")
        ' Serializing the employee object to Json string
        Dim jsonTxt As String = JsonConvert.SerializeObject(emp, Formatting.Indented)
        Console.WriteLine(jsonTxt)
        Console.ReadLine()
    End Sub
    Public Class Employee
    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set
            m_Name = Value
        End Set
    End Property
    Private m_Name As String
    Public Property IsPermanent() As Boolean
        Get
            Return m_IsPermanent
        End Get
        Set
            m_IsPermanent = Value
        End Set
    End Property
    Private m_IsPermanent As Boolean
    ' Employee can belong to multiple departments
    Public Property Departments() As List(Of String)
        Get
            Return m_Departments
        End Get
        Set
            m_Departments = Value
        End Set
    End Property
    Private m_Departments As List(Of String)
End Class

End Module

The output of the above code is

{
  "Name": "Abundantcode",
  "IsPermanent": true,
  "Departments": [
    "Technology",
    "Product Engineering"
  ]
}
%d