Updating my ASP.NET podcast site to System.Text.Json from Newtonsoft.Json

JSON Logo

JSON LogoNow that .NET Core 3.1 is LTS (Long Term Support) and will be supported for 3 years, it’s the right time for me to update all my .NET Core 2.x sites to 3.1. It hasn’t take long at all and the piece of mind is worth it. It’s nice to get all these sites (in the Hanselman ecosystem LOL) onto the .NET Core 3.1 mainline.

While most of my sites working and running just fine – the upgrade was easy – there was an opportunity with the podcast site to move off the venerable Newtonsoft.Json library and move (upgrade?) to System.Text.Json. It’s blessed by (and worked on by) James Newton-King so I don’t feel bad. It’s only a good thing. Json.NET has a lot of history and existed before .NET Standard, Spanand existed in a world where .NET thought more about XML than JSON.

Now that JSON is essential, it was time that JSON be built into .NET itself and System.Text.Json also allows ASP.NET Core to existed without any compatibility issues given its historical dependency on Json.NET. (Although for back-compat reasons you can add Json.NET back with one like using AddJsonOptions if you like).

Everyone’s usage of JSON is different so your mileage will depend on how much of Json.NET you used, how much custom code you wrote, and how deep your solution goes. My podcast site uses it to access a number of JSON files I have stored in Azure Storage, as well as to access 3rd party RESTful APIs that return JSON. My podcast site’s “in memory database” is effectively a de-serialized JSON file.

I start by bringing in two namespaces, and removing Json.NET’s reference and seeing if it compiles! Just rip that Band-Aid off fast and see if it hurts.

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

I use Json Serialization in Newtonsoft.Json and have talked before about how much I like C# Type Aliases. Since I used J as an alias for all my Attributes, that made this code easy to convert, and easy to read. Fortunately things like JsonIgnore didn’t have their names changed so the namespace was all that was needed there.

NOTE: The commented out part in these snippets is the Newtonsoft bit so you can see Before and After

//using J = Newtonsoft.Json.JsonPropertyAttribute;
using J = System.Text.Json.Serialization.JsonPropertyNameAttribute;

/* SNIP */

public partial class Sponsor
{
[J("id")]
public int Id { get; set; }

[J("name")]
public string Name { get; set; }

[J("url")]
public Uri Url { get; set; }

[J("image")]
public Uri Image { get; set; }
}

I was using Newtonsoft’s JsonConvert, so I changed that DeserializeObject call like this:

//public static v2ShowsAPIResult FromJson(string json) => JsonConvert.DeserializeObject(json, Converter.Settings);
public static v2ShowsAPIResult FromJson(string json) => JsonSerializer.Deserialize(json);

In other classes some of the changes weren’t stylistically the way I’d like them (as an SDK designer) but these things are all arguable either way.

For example, ReadAsAsync is a super useful extension method that has hung off of HttpContent for many years, and it’s gone in .NET 3.x. It was an extension that came along for the write inside Microsoft.AspNet.WebApi.Client, but it would bring Newtonsoft.Json back along for the ride.

In short, this Before becomes this After which isn’t super pretty.

return await JsonSerializer.DeserializeAsync>(await res.Content.ReadAsStreamAsync());
//return await res.Content.ReadAsAsync>();

But one way to fix this (if this kind of use of ReadAsAsync is spread all over your app) is to make your own extension class:

public static class HttpContentExtensions
{
public static async Task ReadAsAsync(this HttpContent content) =>
await JsonSerializer.DeserializeAsync(await content.ReadAsStreamAsync());
}

My calls to JsonConvert.Serialize turned into JsonSerializer.Serialize:

//public static string ToJson(this List self) => JsonConvert.SerializeObject(self);
public static string ToJson(this List self) => JsonSerializer.Serialize(self);

And the reverse of course with JsonSerializer.Deserialize:

//public static Dictionary FromJson(string json) => JsonConvert.DeserializeObject>(json);
public static Dictionary FromJson(string json) => JsonSerializer.Deserialize>(json);

All in all, far easier than I thought. How have YOU found System.Text.Json to work in your apps?


Sponsor: When DevOps teams focus on fixing new flaws first, they can add to mounting security debt. Veracode’s 2019 SOSS X report spotlights how developers can reduce fix rate times by 72% with frequent scans.

.


READ MORE – CLICK HERE

www.Red360.com.ve


Leave a Reply

Your email address will not be published. Required fields are marked *