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

Published on April 13, 2016 by abundantcode

Filed under Json.NET

Last modified February 13, 2016

Print this page

rate 1 star rate 2 star rate 3 star rate 4 star rate 5 star
Your rating: none, Average: 0 (0 votes)

This article have been viewed 3544 times

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"
  ]
}
If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Comment


x

You might be Interested in these Posts

C Program to display Inverted half-pyramid using *

Problem Write a program in C to print Inverted half-pyramid using * as shown below.The program shou...

VB.NET and Lambda – Filter elements from object collection with Logical Operators

Here’s a sample code snippet demonstrating how to filter elements from a list of objects using the w...

VB.NET and LINQ – Filter elements from object collection with Logical Operators

Here’s a sample code snippet demonstrating how to filter elements from a list of objects using the w...