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 6031 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"
  ]
}

Leave a Comment


x

You might be Interested in these Posts

Video - Angular Router - Brian Ford

Video - Angular Router - Brian Ford http://www.youtube.com/watch?v=sfV0ApyN5fM ...

Video - An Angular2 Todo App: First look at App Development in Angular2

In this screencast , David East , one of the core contributor of the Angular provides a glimpse of A...

Video - Getting started with Angular 2

In this screencast , Alex Eagle , developer on the Angular team talks about Angular 2 developer prev...