A Brief Comparison Between Newtonsoft.Json and System.Text.Json
January 14, 2020
TL;DR – System.Text.Json is a new JSON library for .NET with different design goals from its predecessor, Newtonsoft.Json. If you’re already using Newtonsoft.Json in an existing project, you likely don’t need to switch. If you absolutely need high JSON serialization/deserialization performance, go with System.Text.Json.
I think it's important to understand the reasoning behind writing a whole new JSON library when we already have Newtonsoft.Json. System.Text.Json was designed first and foremost with high performance in mind - the .NET team (which includes James Newton-King, the guy who wrote Newtonsoft.Json) found they couldn't refactor Newtonsoft.Json to meet some of these goals without making breaking changes to existing code.
In addition, System.Text.Json strictly adheres to the JSON spec, RFC 8259 - things you previously could do with Newtonsoft.Json (because it wasn't spec compliant) aren't allowed in System.Text.Json.
For example, Newtonsoft.Json will deserialize:
- Property names that have a different case
- Property names in JSON that have single/double/no quotes
- Null values for non-nullable fields properties (null -> an int property is allowed)
System.Text.Json only supports (out of the box):
- Deserializing JSON properties by names with the same case
- Property names with double quotes
- Deserializing JSON properties to their like-typed C# counterparts (int -> int only, not null -> int property)
There's a lot of differences in behavior that you can find in this article, but the question needing answered is, do you need to switch from Newtonsoft.Json to System.Text.Json?
The answer is almost certainly no. If you switch, there are a lot of subtle differences that may cause runtime errors. Newtonsoft.Json is still a nice abstraction for .NET - System.Text.Json is much closer to the JSON metal.