Json.NET & C# – How to Seralize an Object ?

Published on February 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 3201 times

One of the ways to serialize an object to JSON string in C# 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 Seralize an Object in C# 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 static string SerializeObject(object value);
public static string SerializeObject(object value, params JsonConverter[] converters);
public static string SerializeObject(object value, JsonSerializerSettings settings);
public static string SerializeObject(object value, Formatting formatting);
public static string SerializeObject(object value, Formatting formatting, params JsonConverter[] converters);
public static string SerializeObject(object value, Type type, JsonSerializerSettings settings);
public static string SerializeObject(object value, Formatting formatting, JsonSerializerSettings settings);
public static string SerializeObject(object value, Type type, Formatting formatting, JsonSerializerSettings settings);

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

using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace ACConsoleCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee
            {
                Name = "Abundantcode",
                IsPermanent = true,
                Departments = new List<string> {"Technology", "Product Engineering"}
            };
            // Serializing the employee object to Json string
            string jsonTxt = JsonConvert.SerializeObject(emp, Formatting.Indented);
            Console.WriteLine(jsonTxt);
            Console.ReadLine();
        }
    }

    public class Employee
    {
        public string Name { get; set; }
        public bool IsPermanent { get; set; }
        // Employee can belong to multiple departments
        public List<string> Departments { get; set; }
    }
}

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.

2 Comments

  1. Json.NET & Oxygene – How to Serialize an Object ? | Abundant Code
    March 13, 2016 - 4:59 pm

  2. Json.NET & Oxygene – How to Deserialize an Object ? | Abundant Code
    March 13, 2016 - 5:18 pm

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...

C++ Program to display "Hello, World!"

Problem Write a program in C++ to display "Hello, World!" on the screen. C++ Program to...

C# and Lambda - Finding all the elements of an integer array less than 35

Here's a sample code snippet demonstrating how to find all the elements of an integer array that is ...