Overview
These notes are for System.Text.Json.
There are two approaches for serializing and deserializing with System.Text.Json:
- Using
JsonSerializer- See notes.
- Using the JSON DOM
- Use the DOM when you receive JSON that doesn’t have a fixed schema and must be inspected to know what it contains.
- Two JSON DOM approaches:
JsonDocument(see notes)
- A read-only (immutable) DOM. Cannot be changed after creation. Faster.
- Uses
JsonElements.JsonElementhas JSON Array and Object enumerators.
JsonNode(see notes)
- A mutable DOM. Can be changed after creation. Slower.
- Uses
JsonNode,JsonObject,JsonArray,JsonValue, andJsonElement.
Convenience vs. Control
Reference: https://devblogs.microsoft.com/dotnet/the-convenience-of-system-text-json/
The APIs in the System.Text.Json namespace can be placed on a spectrum of convenience vs control.
These APIs are listed from low level (most control) to high level (most convenient):
System.Text.Json.Utf8JsonReader/Writer— for reading and writing JSON documents one node at a timeSystem.Text.Json.JsonDocument— for providing a view of the entire JSON document with patterns for reading and writingSystem.Text.Json.JsonSerializer— for automatically serializing and deserializing JSON data

security
System.Text.Json has a threat model. See:
https://github.com/dotnet/runtime/blob/main/src/libraries/System.Text.Json/docs/ThreatModel.md
thread safety
JsonDocument is not thread safe. All other aspects of System.Text.Json are thread safe.
Reflection vs. Source Generation [ Documentation]
System.Text.Json can gather metadata needed to access properties of objects for serialization/deserialization two ways:
- Using reflection at run time (default)
- Using source generation
Source generation has two modes: metadata collection mode and serialization optimization mode:
| Mode | Support for public properties | Support for private accessors | Support forrequired and init members |
|---|---|---|---|
| Reflection | Yes | Yes | Yes |
| Source generation Metadata collection | Yes | Yes | Yes (.NET 8) |
| Source generation Serialization optimization | Yes | No | No |
enabling source generation
Follow instructions here to enable source generation.
Deserialize with HttpClient and HttpContent Extension Methods
This technique still requires that you create a response class.