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

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

How to decode a base64 string in C#?

Here's a sample code snippet demonstrating how you can easily decode a base64 encoded string in C#. ...

How to disable editing of items in a combo box in c#?

When working in Win forms using C# and especially when you use the combo box , you would have notice...

C# Program to find the ASCII Value of a Character

Problem Write a program in Visual C# to find the ASCII value of the input character. How to find t...