System.Text.Json provides .NET APIs for reading and writing JSON and for serializing and deserializing JSON data to and from objects. It offers a modern alternative to Newtonsoft.Json, with performance and source-generator integration as notable design goals. section 4.2
Common implementation tasks include:
- Deserialize a JSON string into a typed object or array with JsonSerializer.Deserialize<T>. section 10.2
- Build applications that read JSON files, search structured data, and write results to console or files. section 4.2
- Compare deserialization approaches with BenchmarkDotNet when performance differences matter. section 10.2
Benchmarking deserialization
A benchmark loads jokes.json once, then compares Newtonsoft.Json with System.Text.Json on the same Joke[] target. The recorded means are 657.7 microseconds for Newtonsoft and 285.2 microseconds for System.Text.Json. Returning each result helps prevent the compiler from optimizing away the benchmarked work. section 10.2
Listing 10.4 Benchmark comparing JSON deserialization
using BenchmarkDotNet.Attributes; #1
using Newtonsoft.Json;
using STJ = System.Text.Json; #2
namespace FirstBenchmark;
public class JsonDeserializationTest
{
private readonly string _jsonString;
public JsonDeserializationTest()
{
var reader = new StreamReader("jokes.json"); #3
_jsonString = reader.ReadToEnd();
}
[Benchmark]
public Joke[]? Newtonsoft() =>
JsonConvert.DeserializeObject<Joke[]>(_jsonString); #4
[Benchmark]
public Joke[]? SystemText() =>
STJ.JsonSerializer.Deserialize<Joke[]>(_jsonString); #5
}
View the focused C# benchmark listing, including the Newtonsoft.Json comparison, System.Text.Json alias, file loading, and typed deserialization calls. section 10.2


