How to use JsonSerializer in Newtonsoft.Json?

I’ve recently publish few posts that explains how to work with Newtonsoft.Json framework. Make sure you check them out if you would like to learn more. In this post, I’ll tell you how to use JsonSerializer in NewtonSoft.Json.

There will be time when your requirement may not be covered by JsonConvert class available in Newtonsoft.Json framework.

JsonSerializer

JsonSerializer is the quickest method of converting between JSON text and a .NET object. It converts .NET objects into their JSON equivalent and back again by mapping the .NET object property names to the JSON property names and copies the values. It also gives you more control and performance benefits to process JSON data.

I’m using a console application in VS 2015 as my IDE.

Let’s look at the simplest code that we can write to use JsonSerializer to convert an object to JSON and write that JSON data to a file on system.

The code above is quite simple. I’m creating an object of type Author and a serializer object of type JsonSerializer. Next, as such we would like to write something to a file on system, I’m using StreamWriter class.

The important class here is JsonTextWriter which is a writer that provides a fast, non-cached, forward-only way of generating JSON data. This class works with the StreamWriter object and is then used when calling Serialize method on serializer that actually serializes the author object to a file.

The output of this program looks like this:

Indented JSON

As you can see the output above, it is all in one line and can be hard to read if the object has many members. Let us now add indentation formatting to the serializer by using Formatting.Indented so that the output in the file is easy to read.

The output of this program is nicely formatted and is easy to read:

Null values

As you can see in the outputs above, the members with null value are also present in the JSON. If you are working on a cross-platform application then you should think about ignoring null values on serialization. As for complex objects, the JSON data will be more in size and that will then need to be sent to your clients over wire.

Let us see how we can ignore null values by simply configuring the serializer setting with NullValueHandling.Ignore:

The output of this program is really small in size and looks like this:

This framework is really great to deal with JSON data and .NET objects. Checkout my other posts for Newtonsoft.Json framework. I hope this post explains how to use JsonSerializer in Newtonsoft.Json framework properly.

Siddharth Pandey

Siddharth Pandey is a Software Engineer with thorough hands-on commercial experience & exposure to building enterprise applications using Agile methodologies. Siddharth specializes in building, managing on-premise, cloud based real-time standard, single page web applications (SPAs). He has successfully delivered applications in health-care, finance, insurance, e-commerce sectors for major brands in the UK. Other than programming, he also has experience of managing teams, trainer, actively contributing to the IT community by sharing his knowledge using Stack Overflow, personal website & video tutorials.

You may also like...

Advertisment ad adsense adlogger