liveBook · Manning

System.Text.Json

As explained in .NET in Action, Second Edition

System.Text.Json is the .NET library that provides JSON serialization, deserialization, and document-processing APIs.

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

How .NET in Action, Second Edition covers it

How deep it goes
Explains typed deserialization and practical benchmarking, but does not descend into serializer internals or configuration details.
Examples
Uses a jokes.json file, Joke records, command-line file processing, and comparative deserialization benchmarks.
What you can do after
Apply it via JsonSerializer.Deserialize<T> to convert JSON strings into typed C# objects and arrays.
How it connects
Connects JSON processing with Newtonsoft.Json, object serialization, deserialization, source generators, and BenchmarkDotNet.

Also explained in

Compare how 3 books explain System.Text.Json →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of .NET in Action, Second Edition. The section links point to the parts of the book it draws on.