liveBook · Manning

System.Text.Json

As explained in F# in Action: A Revised Edition of Get Programming with F#

System.Text.Json is the .NET library namespace providing APIs for JSON serialization and deserialization.

System.Text.Json is the built-in .NET library for converting JSON to and from F# values. It works best at the boundary of an application: deserialize into simple raw records, then validate and map those records into richer domain types. section 10.2.1 section 10.7

A practical workflow separates transport-shaped data from domain invariants:

  • Validate required fields and convert raw values into discriminated unions and other domain types. section 10.2.1
  • Serialize domain values by first mapping them into a JSON-shaped record; anonymous records can avoid output-only type declarations. section 10.2.3

Guitar inventory data

A guitar domain model can use Brand, Strings, Pickup, and Kind unions, while incoming JSON uses ordinary strings and arrays. Deserialize the JSON into RawGuitar, run tryAsFullGuitar, and obtain a validated Guitar or an error. For reporting, count guitars by brand, project anonymous records such as {| Brand = brand; Guitars = count |}, and pipe them to JsonSerializer.Serialize.

How F# in Action: A Revised Edition of Get Programming with F# covers it

How deep it goes
Explains API usage, raw-type mapping, validation, anonymous records, and discriminated-union limitations, but not converter implementation.
Examples
Uses guitar inventory JSON, RawGuitar validation, discriminated-union domain values, and anonymous-record reporting output.
What it assumes
Assumes basic F# records and functions while teaching JSON mapping, validation, and discriminated-union interoperability concerns.
What you can do after
Apply it via JsonSerializer to exchange JSON, validate raw records, and produce or consume richer F# domain values.
How it connects
Connects JSON serialization, F# records, discriminated unions, domain modeling, validation, and type providers.

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 F# in Action: A Revised Edition of Get Programming with F#. The section links point to the parts of the book it draws on.