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
| Name | Signature | Synopsis |
|---|---|---|
| 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. |
| parseData | Json.parseData json | Parses one JSON value into source-neutral structured data. |
| 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. |
| 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. |
Compiles a completed schema into a reusable JSON codec.
Parameters
| Name | Type | Description |
|---|---|---|
| schema | Schema<'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 jsonSerializes a trusted model to a JSON string through a compiled codec.
Parameters
| Name | Type | Description |
|---|---|---|
| codec | JsonCodec<'model> | |
| value | 'model |
Returns
string
Serializes a trusted model to UTF-8 JSON bytes through a compiled codec.
Parameters
| Name | Type | Description |
|---|---|---|
| codec | JsonCodec<'model> | |
| value | 'model |
Returns
byte[]
Parses one JSON value into source-neutral structured data.
Parameters
| Name | Type | Description |
|---|---|---|
| json | string |
Returns
Data
Verification Examples
Json.parseData "{\"name\":\"Ada\"}"
// Data.Object [ "name", Data.Text "Ada" ]Deserializes UTF-8 JSON bytes to a trusted model through a compiled codec.
Parameters
| Name | Type | Description |
|---|---|---|
| codec | JsonCodec<'model> | |
| bytes | byte[] |
Returns
'model
Deserializes a JSON string to a trusted model through a compiled codec.
Parameters
| Name | Type | Description |
|---|---|---|
| codec | JsonCodec<'model> | |
| json | string |
Returns
'model
Deserializes a JSON string, returning decode failures as a rendered message instead of raising.
Parameters
| Name | Type | Description |
|---|---|---|
| codec | JsonCodec<'model> | |
| json | string |
Returns
Result<'model, string>
Serializes a trusted model as UTF-8 JSON directly to a stream through a compiled codec, flushing once when complete.
Parameters
| Name | Type | Description |
|---|---|---|
| codec | JsonCodec<'model> | |
| stream | Stream | |
| value | 'model |
Returns
unit