Getting Started with JSON in C# Using Json.NET

JSON is a lightweight, human-readable, efficient, and easy to understand data interchange format. Learn how to serialize, deserialize, use LINQ to JSON, improve serialization performance, BSON, create schemas, validate JSON, and, in general, work with Json.NET.
Course info
Rating
(211)
Level
Intermediate
Updated
Nov 5, 2015
Duration
3h 14m
Table of contents
The Case for JSON & Json.NET
Serialization Fundamentals
Settings & Attributes
Custom Serialization
Performance Tips
LINQ to JSON
JSON & XML
Binary JSON (BSON)
Json.NET Schema
Wrap Up
Description
Course info
Rating
(211)
Level
Intermediate
Updated
Nov 5, 2015
Duration
3h 14m
Description

The rising popularity of the web, mainly around JavaScript related technologies, has given JSON a great deal of importance over other data interchange formats, like XML. JSON is a lightweight, human-readable, efficient, and easy to understand data interchange format. JSON stands for JavaScript Object Notation. If you are a C# or .NET developer who needs to work with JSON, or even understand what JSON is, then this course is for you. In this course, we will learn how to serialize, deserialize, use LINQ To JSON, improve serialization performance, BSON, create schemas, validate JSON, and, in general, work with Json.NET, a popular, high performance, free, and open source JSON framework for .NET.

About the author
About the author

Xavier is very passionate about teaching, helping others understand search and Big Data. He is also an entrepreneur, project manager, technical author, trainer, and holds a few certifications with Cloudera, Microsoft, and the Scrum Alliance, along with being a Microsoft MVP.

More from the author
Importing Data: Python Data Playbook
Beginner
1h 35m
Nov 17, 2018
More courses by Xavier Morera
Section Introduction Transcripts
Section Introduction Transcripts

Serialization Fundamentals
Hello and welcome to the next module of this Json. NET course, Serialization Fundamentals. Serialization and deserialization is the main functionality of Json. NET. Let's get into the details and understand why it is so important and how it can help you. Serialization and deserialization involves taking a data structure or object and converting it back and forth between JSON text and. NET objects. A. NET object when serialized can be stored as a stream of bytes of file or in memory and later it can be used to recreate the original object. You have to be careful though. In some cases there are private implementation details that are not available when deserializing or serializing, so you need to review the recreated object to determine if there is any information missing. In the serialization and deserialization process, you map property names and copy their values using the main JSON serializer class with the support of JsonReader and JsonWriter. I will show you important considerations to take like working with dates, collections, error handling, along with a few useful tips and tricks. In this module we'll work closely with the next module where I will show you available settings and attributes that help you control the serialization process.

Settings & Attributes
Hello, and welcome to this next module of JSON and Json. NET course. In this module we will learn how to control and customize the serialization process via settings and attributes. So what is a setting? A setting is a user preference that is supplied during the conversion process. It can be specified as a property on the JsonSerializer class or using the JsonSerializer settings on JsonConvert. And what is an attribute? An attribute is a declarative tag that is supplied on classes, properties, and more, that is taking into account during the serialization and deserialization process.

Custom Serialization
Hello, and welcome to the next module of this Json. NET course, Custom Serialization. Json. NET is very powerful with some key features that include conditional serialization, serialization callbacks, debugging, and more. In this module I will teach you how to customize your serialization process with Json. NET along with a few more pieces of information that will prove invaluable and will save you time while coding. Given that we already covered serialization and deserialization, the next step involves taking it a notch further and explaining the more advanced topics around handling JSON. I will be covering conditional serialization. It may be the case that you want to control the serialization process based on specific conditions. I will show you how. Custom JsonConverter. It may be possible that you want to extend or customize the serialization and deserialization process. Let's learn how we can create our own custom JsonConverter to get exactly the results that we need. Serialization callbacks in general are used to raise events before and after the serialization and deserialization process. I will teach you what events are available and when during the process they occur. ITraceWriter. Debugging the serialization is not a common scenario, but being able to do it using ITraceWriter for logging and debugging could be important. Learn the process in this demo to be prepared.

Performance Tips
Hello, and welcome to this next module of this Json. NET course, Performance Tips. In a lot of cases, performance is one of the most critical features of an application. A badly performed application can affect sales, delivery, and in general it can be problematic. So what if your application works with large amounts of JSON or requires quick responses on code that rely on JSON serialization? Json. NET is the way to go. When you compare Json. NET with other serializers, Json. NET is faster. In this module I will demonstrate tips on how to improve performance when using Json. NET, but more than comparing with other serializers, I will teach you multiple tips to make Json. NET serialization even faster including reading and writing JSON directly instead of serializing, working with fragments, populate objects, control what get serialized, and lowering your memory utilization. We will get started with manual serialization. What's this? When you use the JsonSerializer class, serialization uses reflection which is slower than writing and reading JSON directly. We will then go to fragments where I will show you how to work only with a subset of the JSON objects to make reading much faster. With populate objects I will show you how to populate specific properties of a large JSON object instead of working with the full JSON object, which is similar to merge array handling where I will show you how to control when two JSON objects are merged. Then we learn how to improve serialization speed by using attributes to control what is serialized and finally, we will learn how to optimize memory usage when working with JSON. And most importantly, in several of the demos we will be using a stopwatch to capture timings as proof. It is very easy to use. The code is only a few lines long.

LINQ to JSON
Hello, and welcome to the next module of this Json. NET course, LINQ to JSON. LINQ to JSON is an API used to work with JSON objects. LINQ in general has been available for many years, but just in case, I will take a step back and start by talking about LINQ. So what is LINQ? LINQ stands for language integrated query. It extends powerful query capabilities to C# and VB. NET and also it includes functions. Its standard and easily learned patterns for querying and updating data potentially to any store. Among those we have LINQ to SQL, LINQ to XML, LINQ to ADO. NET, LINQ to objects, and of course, LINQ to JSON. It started around Visual Studio 2008 and I'll show you how we can use it with Json. NET. LINQ to JSON can be found under the Newtonsoft. JSON. Linq namespace. You can use it with JTokenReader or JTokenWriter for fast, non-cached, forward only reading and writing of JSON in a LINQ style. It is also possible to parse JSON using JObject. Parse. The parse method is not only available in the JObject class; it is available under many of the LINQ to JSON classes. If you want to create JSON there are many ways. You can do it in an imperative, in a declarative, or in a from object way. Also you can query JSON in a very simple notation or you can use JSON path with SelectToken. Let's learn what can be done with this namespace and how it can help you improve your code.

JSON & XML
Hello, and welcome to the next module of this Json. NET course, JSON and XML. JSON is a data interchange format which is easy for humans to read and write. It is easy for machines to parse and generate. On the other hand, XML is a markup language that defines a set of rules for encoding documents in a format which is also both supported by humans and machines. They both can serve the same purpose; however, converting between them can be a little bit tricky and there are several different considerations to take. In this module I will teach you how to convert between JSON and XML using JSON convert with an understanding of possible scenarios. Let's talk about JSON and XML. Xml is a markup language. It can be used as a data interchange format and it is both easy for humans to understand and machines to process and JSON is a text based data interchange format. It's made up of key value pairs, arrays, and objects. By now you should be very familiar with JSON. It's also easy for humans to understand and machines to process. XML can be used in multiple different ways, but JSON is specifically used as a data interchange format which means that you don't have to guess about the structure of the data that you're receiving. It is very straightforward, so when you're converting data from XML and JSON, there is no standard way of converting it. In many cases the conversion can be simple, but in others, that round trip conversion can be hard.