Scott Hanselman

System.Text.Json and new built-in JSON support in .NET Core

July 23, '19 Comments [10] Posted in ASP.NET | DotNetCore | Javascript
Sponsored By

In a world where JSON (JavaScript Object Notation) is everywhere it's long been somewhat frustrating that .NET didn't have built-in JSON support. JSON.NET is great and has served us well but it's remained a 3rd party dependency for basic stuff like an ASP.NET web site or a simple console app.

Back in 2018 plans were announced to move JSON into .NET Core 3.0 as an intrinsic supported feature, and while they're at it, get double the performance or more with Span<T> support and no memory allocations. ASP.NET in .NET Core 3.0 removes the JSON.NET dependency but still allows you to add it back in a single line if you'd like.

NOTE: This is all automatic and built in with .NET Core 3.0, but if you’re targeting .NET Standard or .NET Framework. Install the System.Text.Json NuGet package (make sure to include previews and install version 4.6.0-preview6.19303.8 or higher). In order to get the integration with ASP.NET Core, you must target .NET Core 3.0.

It's very clean as well. Here's a simple example.

using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace verysmall
class WeatherForecast
public DateTimeOffset Date { get; set; }
public int TemperatureC { get; set; }
public string Summary { get; set; }

class Program
static void Main(string[] args)
var w = new WeatherForecast() { Date = DateTime.Now, TemperatureC = 30, Summary = "Hot" };

The default options result in minified JSON as well.


Of course, when you're returning JSON from a Controller in ASP.NET it's all automatic and with .NET Core 3.0 it'll automatically use the new System.Text.Json unless you override it.

Here's an example where we pull out some fake Weather data (5 randomly created reports) and return the array.

public IEnumerable<WeatherForecast> Get()
var rng = new Random();
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
Date = DateTime.Now.AddDays(index),
TemperatureC = rng.Next(-20, 55),
Summary = Summaries[rng.Next(Summaries.Length)]

The application/json is used and JSON is returned by default. If the return type was just string, we'd get text/plain. Check out this YouTube video to learn more details about System.Text.Json works and how it was designed. I'm looking forward to working with it more!

Sponsor: Get the latest JetBrains Rider with WinForms designer, Edit & Continue, and an IL (Intermediate Language) viewer. Preliminary C# 8.0 support, rename refactoring for F#-defined symbols across your entire solution, and Custom Themes are all included.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Saturday, July 27, 2019 9:35:04 AM UTC
You are a beautiful man
Saturday, July 27, 2019 3:47:18 PM UTC
Is James Newton-King involved in this new library or is he working on other projects?
Jesse Mandel
Sunday, July 28, 2019 6:23:08 AM UTC
Jesse - Yes, James is helping guide development on this as well.
Scott Hanselman
Monday, July 29, 2019 7:29:05 AM UTC
Looking forward to using this once they allow deserializing to types that use non-default constructors...
Monday, July 29, 2019 10:34:59 AM UTC
"it's long been somewhat frustrating that .NET didn't have built-in JSON support.."

You forgot the good old DataContractJsonSerializer :)
Monday, July 29, 2019 2:09:35 PM UTC
When they make a movie about JSON, James Newton Howard should compose the score.
Monday, July 29, 2019 10:13:29 PM UTC
so good...
Tuesday, July 30, 2019 10:26:41 AM UTC
Looks good.
ToArray() needed :)
Friday, August 02, 2019 2:55:49 PM UTC
Amazingly well explained, thank you very much !
Friday, August 02, 2019 5:52:37 PM UTC
It is great news! JSON is used so often. It totaly makes sense to support it as intrinsic feature of platform.
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.