JsonModule

PackageReified.Schema
Functions for compiling and running JSON codecs over built model schemas.

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 , 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 .
serializeBytesWithJson.serializeBytesWith configure codec valueSerializes a trusted model to UTF-8 JSON bytes, configuring the writer through .
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.
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 .
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

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>