JsonModule

PackageReified.Schema

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

NameSignatureSynopsis
Operations
compileJson.compile schemaCompiles a completed schema into a reusable JSON codec.
serializeJson.serialize codec valueSerializes a trusted model to a JSON string through a compiled codec.
serializeBytesJson.serializeBytes codec valueSerializes a trusted model to UTF-8 JSON bytes through a compiled codec.
defaultsJson.defaults The unmodified JSON writer options: compact, minimal escaping — exactly what serialize`, serializeBytes, and serializeToStream produce.
indentedJson.indented optionsConfigures the writer to indent nested levels by two spaces.
serializeWithJson.serializeWith configure codec valueSerializes a trusted model to a JSON string, configuring the writer through JsonWriteOptions.
serializeBytesWithJson.serializeBytesWith configure codec valueSerializes a trusted model to UTF-8 JSON bytes, configuring the writer through JsonWriteOptions.
serializeIndentedJson.serializeIndented codec valueSerializes a trusted model to an indented JSON string with two-space nesting.
reindentJson.reindent jsonReformats a JSON string with two-space indentation.
parseDataJson.parseData jsonParses one JSON value into source-neutral structured data.
tryParseDataJson.tryParseData jsonParses a JSON string into Data, returning malformed input as a rendered message.
deserializeBytesJson.deserializeBytes codec bytesDeserializes UTF-8 JSON bytes to a trusted model through a compiled codec.
deserializeJson.deserialize codec jsonDeserializes a JSON string to a trusted model through a compiled codec.
tryDeserializeJson.tryDeserialize codec jsonDeserializes a JSON string, returning decode failures as a rendered message instead of raising.
serializeToStreamJson.serializeToStream codec stream valueSerializes a trusted model as UTF-8 JSON directly to a stream through a compiled codec, flushing once when complete.
serializeToStreamWithJson.serializeToStreamWith configure codec stream valueSerializes a trusted model as UTF-8 JSON directly to a stream, configuring the writer through JsonWriteOptions.
deserializeStreamAsyncJson.deserializeStreamAsync codec streamReads a stream to end into a pooled buffer, then deserializes it as UTF-8 JSON through a compiled codec.

Operations

kind:member

compile

Json.compile schema
Member
Compiles a completed schema into a reusable JSON codec.

Parameters

NameTypeDescription
schemaSchema<'model>

Returns

JsonCodec<'model>

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 json
kind:member

serialize

Json.serialize codec value
Member
Serializes a trusted model to a JSON string through a compiled codec.

Parameters

NameTypeDescription
codecJsonCodec<'model>
value'model

Returns

string
kind:member

serializeBytes

Json.serializeBytes codec value
Member
Serializes a trusted model to UTF-8 JSON bytes through a compiled codec.

Parameters

NameTypeDescription
codecJsonCodec<'model>
value'model

Returns

byte[]
kind:member

defaults

Json.defaults
Member
The unmodified JSON writer options: compact, minimal escaping — exactly what serialize``1, serializeBytes, and serializeToStream produce.

Returns

JsonWriteOptions
kind:member

indented

Json.indented options
Member
Configures the writer to indent nested levels by two spaces. Compose it with serializeWith``1: Json.serializeWith Json.indented codec value.

Parameters

NameTypeDescription
optionsJsonWriteOptions

Returns

JsonWriteOptions
kind:member

serializeWith

Json.serializeWith configure codec value
Member
Serializes a trusted model to a JSON string, configuring the writer through JsonWriteOptions.

Parameters

NameTypeDescription
configureJsonWriteOptions -> JsonWriteOptions
codecJsonCodec<'model>
value'model

Returns

string
kind:member

serializeBytesWith

Json.serializeBytesWith configure codec value
Member
Serializes a trusted model to UTF-8 JSON bytes, configuring the writer through JsonWriteOptions.

Parameters

NameTypeDescription
configureJsonWriteOptions -> JsonWriteOptions
codecJsonCodec<'model>
value'model

Returns

byte[]
kind:member

serializeIndented

Json.serializeIndented codec value
Member
Serializes a trusted model to an indented JSON string with two-space nesting.

Parameters

NameTypeDescription
codecJsonCodec<'model>
value'model

Returns

string
kind:member

reindent

Json.reindent json
Member
Reformats a JSON string with two-space indentation.

Parameters

NameTypeDescription
jsonstring

Returns

string
kind:member

parseData

Json.parseData json
Member
Parses one JSON value into source-neutral structured data.

Parameters

NameTypeDescription
jsonstring

Returns

Data

Verification Examples

Json.parseData "{\"name\":\"Ada\"}"
 // Data.Object [ "name", Data.Text "Ada" ]
kind:member

tryParseData

Json.tryParseData json
Member
Parses a JSON string into Data, returning malformed input as a rendered message.

Parameters

NameTypeDescription
jsonstring

Returns

Result<Data, string>

Verification Examples

Json.tryParseData "{\"name\":\"Ada\"}" // Ok (Data.Object [ "name", Data.Text "Ada" ])
kind:member

deserializeBytes

Json.deserializeBytes codec bytes
Member
Deserializes UTF-8 JSON bytes to a trusted model through a compiled codec.

Parameters

NameTypeDescription
codecJsonCodec<'model>
bytesbyte[]

Returns

'model
kind:member

deserialize

Json.deserialize codec json
Member
Deserializes a JSON string to a trusted model through a compiled codec.

Parameters

NameTypeDescription
codecJsonCodec<'model>
jsonstring

Returns

'model
kind:member

tryDeserialize

Json.tryDeserialize codec json
Member
Deserializes a JSON string, returning decode failures as a rendered message instead of raising.

Parameters

NameTypeDescription
codecJsonCodec<'model>
jsonstring

Returns

Result<'model, string>
kind:member

serializeToStream

Json.serializeToStream codec stream value
Member
Serializes a trusted model as UTF-8 JSON directly to a stream through a compiled codec, flushing once when complete.

Parameters

NameTypeDescription
codecJsonCodec<'model>
streamStream
value'model

Returns

unit
kind:member

serializeToStreamWith

Json.serializeToStreamWith configure codec stream value
Member
Serializes a trusted model as UTF-8 JSON directly to a stream, configuring the writer through JsonWriteOptions. Not available on Fable.

Parameters

NameTypeDescription
configureJsonWriteOptions -> JsonWriteOptions
codecJsonCodec<'model>
streamStream
value'model

Returns

unit
kind:member

deserializeStreamAsync

Json.deserializeStreamAsync codec stream
Member
Reads a stream to end into a pooled buffer, then deserializes it as UTF-8 JSON through a compiled codec.

Parameters

NameTypeDescription
codecJsonCodec<'model>
streamStream

Returns

Task<'model>