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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Program { static void Main(string[] args) { Console.Clear(); Author author = new Author { name = "Sid", since = new DateTime(2015, 11, 25) }; Console.WriteLine("No settings"); var serializer = new JsonSerializer(); using (var sw = new StreamWriter(@"..\..\noparameters.json")) { using (var writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, author); } } } } |
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:
1 |
{"name":"Sid","courses":null,"since":"2015-11-25T00:00:00","happy":false,"issues":null,"car":null} |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
class Program { static void Main(string[] args) { Console.Clear(); Author author = new Author { name = "Sid", since = new DateTime(2015, 11, 25) }; Console.WriteLine("With Indentation"); var serializer = new JsonSerializer { Formatting = Formatting.Indented }; using (var sw = new StreamWriter(@"..\..\indented-data.json")) { using (var writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, author); } } } } |
The output of this program is nicely formatted and is easy to read:
1 2 3 4 5 6 7 8 |
{ "name": "Sid", "courses": null, "since": "2015-11-25T00:00:00", "happy": false, "issues": null, "car": null } |
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
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class Program { static void Main(string[] args) { Console.Clear(); Author author = new Author { name = "Sid", since = new DateTime(2015, 11, 25) }; Console.WriteLine("Ignore nulls"); var serializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore, Formatting = Formatting.Indented }; using (var sw = new StreamWriter(@"..\..\without-nulls.json")) { using (var writer = new JsonTextWriter(sw)) { serializer.Serialize(writer, author); } } } } |
The output of this program is really small in size and looks like this:
1 2 3 4 5 |
{ "name": "Sid", "since": "2015-11-25T00:00:00", "happy": false } |
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.