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.
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.

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

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

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>