C# and .NET Basics

Working with JSON

Spencer Schneidenbach

Spencer Schneidenbach

Aviron Software, Microsoft MVP
C# and .NET Basics

Check out a free preview of the full C# and .NET Basics course

The "Working with JSON" Lesson is part of the full, C# and .NET Basics course featured in this preview video. Here's what you'd learn in this lesson:

Spencer discusses the importance of JSON in data transformation for the web and compares two major JSON libraries in .NET: Newtonsoft.Json and System.Text.Json. He explains the process of serializing and deserializing objects to and from JSON and provides advice on which library to use based on the system being used.

Preview
Close

Transcript from the "Working with JSON" Lesson

[00:00:00]
>> Spencer Schneidenbach: Talking a little bit about Json and .NET, Json is a very important data transform language for the web. It is basically the way that most data is transferred t to and from servers. So coming from XML, which was a mess, I don't need to talk about too much about it.

[00:00:21]
But in addition to having properties inside of the brackets or inside of attributes, there's all sorts of reasons that Json proliferated, and it was all the mistakes of XML. So if you're not familiar with Json, you've probably seen it sometime before, especially if you've done JavaScript. This is just a structure, a very neat, tidy and very restricted structure grammar for describing data, structured data specifically.

[00:00:47]
And I just wanna touch on the two major libraries that are used for for Json in .NET. So there's Newtonsoft.Json, which has been around for a very long time and has been around since the .NET framework days and is around in .NET core as well. Typically, for new projects, you wanna avoid using it and then SystemText.Json, which is the replacement.

[00:01:12]
This is the Microsoft backed one where they said, okay, we need to really extract as much performance and security and strictness as we can out of this framework. We can't do it outta Newtonsoft.Json without introducing breaking changes, so we'll bring in SystemTextJson. So typically speaking, there's gonna be two things that you're gonna do with Json.

[00:01:33]
In ASP net core, it will do this mostly for you, but sometimes you're explicitly taking an object and serializing into Json, so converting something like this to its Json equivalent, or you're deserializing it. You're taking an employee object that is in Json form, and you're typically reading it from from Json and then materializing or creating that .NET object.

[00:01:56]
I'm not gonna spend too much time talking about the differences between the two other than I I will say two pieces of advice. If you're on a new system, Use Systemtext.Json, I it's the Json. It's the standard way of reading and writing Json these days, it's the one that ASP.Net core uses.

[00:02:13]
But if you're on an older system and you're using Newtonsoft, I would just ask that you don't mix the two, I don't advise that you mix the two. Lastly, I guess this is a be a bonus, a lot of these Json serializers, they tend to, by default, serialize as the ENUMS default value.

[00:02:28]
So ENUMS are used everywhere. And for some reason, they made the choice to serialize ENUMS as if the underlying value is a 1, sorry, an integer. They're gonna serialize it as 0 and 1. I tend to and our front end people thank me when I say that all ENUMS will be sent as strings and not their underlying type.

[00:02:49]
It seems like a minor point, but it is something that I have seen people do over and over again. So you're just a couple of lines of code from configuring that in most ASPNETCore solutions, which we'll go over.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now