JsonModule

PackageReified.Schema.Json

Json

Json.compile interprets a Schema<'model> as a reusable JsonCodec<'model>. Compile once, then use the codec to serialize trusted models or deserialize JSON into the same field plan used by Schema.parse.

The codec is built from the explicit schema rather than runtime type inspection. It supports NativeAOT, trimming, and Fable, and it preserves schema paths in decoding failures.

See JSON Codecs for streams, buffers, diagnostics, and the boundary between trusted codec decoding and untrusted Data parsing.

Summary

NameSignatureSynopsis
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.
parseDataJson.parseData jsonParses one JSON value into source-neutral structured data.
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.
deserializeStreamAsyncJson.deserializeStreamAsync codec streamReads a stream to end into a pooled buffer, then deserializes it as UTF-8 JSON through a compiled codec.

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

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

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[]

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" ]

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

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

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>

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

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>