JsonModule
Json
Json.compile interprets a Schema<'model> as a reusable JsonCodec<'model>. Compile once, then use the codec to
serialize trusted models or decode JSON using the schema's field structure. Unlike Schema.parse, trusted codec
decoding does not check value constraints; use Json.parseData followed by Schema.parse for untrusted input.
The codec is built from the explicit schema rather than runtime type inspection. It supports NativeAOT, trimming, and Fable.
Choosing a read operation
Json exposes three ways to turn JSON text into a model, with different input, output, and failure behavior:
| Operation | Input | Success | Failure |
|---|---|---|---|
deserialize / deserializeBytes |
JSON string / UTF-8 bytes | 'model |
raises JsonCodecException whose Path identifies the first failing schema field |
tryDeserialize |
JSON string | Result<'model, string> |
Error with the exception's rendered message, including the path — a string, not structured SchemaErrors |
parseData |
JSON string | Result<Data, string> |
Error string; produces a Data tree for Schema.parse, which then applies constraints and accumulates path-aware SchemaErrors |
deserialize and deserializeBytes stop at the first structural failure. tryDeserialize is the result-returning
form of the same behavior. parseData does not run schema rules at all; it is the lossless JSON-to-Data step that
feeds untrusted boundary parsing.
See JSON Codecs for streams, buffers, diagnostics, and how trusted codec decoding differs
from untrusted Data parsing.
Summary
| Name | Signature | Synopsis |
|---|---|---|
| Operations | ||
| compile | Json.compile schema | Compiles a completed schema into a reusable JSON codec. |
| serialize | Json.serialize codec value | Serializes a trusted model to a JSON string through a compiled codec. |
| serializeBytes | Json.serializeBytes codec value | Serializes a trusted model to UTF-8 JSON bytes through a compiled codec. |
| defaults | Json.defaults | The unmodified JSON writer options: compact, minimal escaping — exactly what serialize`, serializeBytes, and serializeToStream produce. |
| indented | Json.indented options | Configures the writer to indent nested levels by two spaces. |
| serializeWith | Json.serializeWith configure codec value | Serializes a trusted model to a JSON string, configuring the writer through JsonWriteOptions. |
| serializeBytesWith | Json.serializeBytesWith configure codec value | Serializes a trusted model to UTF-8 JSON bytes, configuring the writer through JsonWriteOptions. |
| serializeIndented | Json.serializeIndented codec value | Serializes a trusted model to an indented JSON string with two-space nesting. |
| reindent | Json.reindent json | Reformats a JSON string with two-space indentation. |
| parseData | Json.parseData json | Parses one JSON value into source-neutral structured data. |
| tryParseData | Json.tryParseData json | Parses a JSON string into Data, returning malformed input as a rendered message. |
| deserializeBytes | Json.deserializeBytes codec bytes | Deserializes UTF-8 JSON bytes to a trusted model through a compiled codec. |
| deserialize | Json.deserialize codec json | Deserializes a JSON string to a trusted model through a compiled codec. |
| tryDeserialize | Json.tryDeserialize codec json | Deserializes a JSON string, returning decode failures as a rendered message instead of raising. |
| serializeToStream | Json.serializeToStream codec stream value | Serializes a trusted model as UTF-8 JSON directly to a stream through a compiled codec, flushing once when complete. |
| serializeToStreamWith | Json.serializeToStreamWith configure codec stream value | Serializes a trusted model as UTF-8 JSON directly to a stream, configuring the writer through JsonWriteOptions. |
| deserializeStreamAsync | Json.deserializeStreamAsync codec stream | Reads a stream to end into a pooled buffer, then deserializes it as UTF-8 JSON through a compiled codec. |
Operations
Parameters
| Name | Type | Description |
|---|---|---|
| schema | Schema<'model> |
Returns
Verification Examples
open Reified.SchemaDSL
open Reified.ConstraintDSL
type Customer = { Name: string; Email: string }
let customerSchema =
schema<Customer> {
field _.Name { constrain present }
field _.Email { constraints [ present; email ] }
construct (fun name email -> { Name = name; Email = email })
}
let customer = { Name = "Ada"; Email = "ada@example.org" }
let codec = Json.compile customerSchema
let json = Json.serialize codec customer
let roundTripped = Json.deserialize codec jsonParameters
| Name | Type | Description |
|---|---|---|
| configure | JsonWriteOptions -> JsonWriteOptions | |
| codec | JsonCodec<'model> | |
| value | 'model |
Returns
Parameters
| Name | Type | Description |
|---|---|---|
| configure | JsonWriteOptions -> JsonWriteOptions | |
| codec | JsonCodec<'model> | |
| value | 'model |
Returns
Parameters
| Name | Type | Description |
|---|---|---|
| configure | JsonWriteOptions -> JsonWriteOptions | |
| codec | JsonCodec<'model> | |
| stream | Stream | |
| value | 'model |