Skip to content

Codec

type Codec<'t> =
    {
        Encoder: Encoder<'t>
        Decoder: Decoder<'t>
    }
AssemblyRanvier.Wire
Attributes[<NoComparison>] [<NoEquality>]

An encoder and a decoder for the same type.

Building both from one description keeps them in step. See objectCodec and variantCodec.

Declared inside

Enum

Fields

Encoder: Encoder<'t>
Decoder: Decoder<'t>

Functions and values

array<'t> (x: Codec<'t>) : Codec<'t array>

A codec for an array, as a JSON array.

at<'t> (fieldNames: string list) (x: Codec<'t>) : Codec<'t>

Move a codec under a path of property names. Decoding reads the value there, encoding wraps it back in nested objects.

bigint: Codec<bigint>

A codec for bigint, using the same representation as Encode.bigint and Decode.bigint.

bool: Codec<bool>

A codec for bool, using the same representation as Encode.bool and Decode.bool.

byte: Codec<byte>

A codec for byte, using the same representation as Encode.byte and Decode.byte.

char: Codec<char>

A codec for char, using the same representation as Encode.char and Decode.char.

create<'t> (encoder: Encoder<'t>) (decoder: Decoder<'t>) : Codec<'t>

Pair an encoder and a decoder into a codec.

datetimeOffset: Codec<DateTimeOffset>

A codec for DateTimeOffset, using the same representation as Encode.datetimeOffset and Decode.datetimeOffset.

decimal: Codec<decimal>

A codec for decimal, using the same representation as Encode.decimal and Decode.decimal.

decoder<'t> (c: Codec<'t>) : Decoder<'t>

The decoder half of a codec.

dict<'t> (x: Codec<'t>) : Codec<Map<string,'t>>

A codec for a map keyed by string, as a JSON object.

encoder<'t> (c: Codec<'t>) : Encoder<'t>

The encoder half of a codec.

fix<'t> (make: Codec<'t> -> Codec<'t>) : Codec<'t>

Build a codec which can refer to itself, for a recursive type such as a tree.

type Tree = Tree of value: int * children: Tree list

let codec : Codec<Tree> =
Codec.fix (fun self ->
objectCodec {
let! value =
Codec.field "value" (fun (Tree(v, _)) -> v) Codec.int

and! children =
Codec.field
"children"
(fun (Tree(_, c)) -> c)
(Codec.list self)

return Tree(value, children)
}
)
float: Codec<float>

A codec for float, using the same representation as Encode.float and Decode.float.

float32: Codec<float32>

A codec for float32, using the same representation as Encode.float32 and Decode.float32.

guid: Codec<Guid>

A codec for Guid, using the same representation as Encode.guid and Decode.guid.

int: Codec<int>

A codec for int, using the same representation as Encode.int and Decode.int.

int16: Codec<int16>

A codec for int16, using the same representation as Encode.int16 and Decode.int16.

int64: Codec<int64>

A codec for int64, using the same representation as Encode.int64 and Decode.int64.

keyValuePairs<'t> (x: Codec<'t>) : Codec<(string * 't) list>

A codec for a list of properties, as a JSON object.

lazily<'t> (codec: Lazy<Codec<'t>>) : Codec<'t>

Defer the construction of a codec until it is first used, for mutually recursive types.

list<'t> (x: Codec<'t>) : Codec<'t list>

A codec for a list, as a JSON array.

losslessOption<'t> (x: Codec<'t>) : Codec<'t option>

A codec for an option, writing an object which carries the case.

Round-trips at any nesting depth, at the cost of a heavier representation.

lossyOption<'t> (x: Codec<'t>) : Codec<'t option>

A codec for an option, writing Some x as x and None as null.

Lossy: a nested option does not round-trip. Use losslessOption when you need the distinction.

map<'t, 'u> (decoder: 't -> 'u) (encoder: 'u -> 't) (codec: Codec<'t>) : Codec<'u>

Turn a codec into one for another type, given a conversion for each direction.

map'<'key, 'value when 'key: comparison> (key: Codec<'key>) (value: Codec<'value>) : Codec<Map<'key,'value>>

A codec for a map, as an array of [ key, value ] pairs. Use it when the key is not a string.

nil<'a> (output: 'a) : Codec<'a>

Encode any value as null, and decode null back to the given constant.

Every input encodes to the same null, so only the given value round-trips. Use it as a branch of oneOf to represent an absent value.

oneOf<'t> (codecs: Codec<'t> list) : Codec<'t>

Decode with the first codec of the list that succeeds, and always encode with the first one.

A value read through a fallback codec is written back with the first one, so it is not a round-trip. Use it to accept an old representation while writing the new one.

resizeArray<'t> (x: Codec<'t>) : Codec<ResizeArray<'t>>

A codec for a ResizeArray, as a JSON array.

sbyte: Codec<sbyte>

A codec for sbyte, using the same representation as Encode.sbyte and Decode.sbyte.

seq<'t> (x: Codec<'t>) : Codec<'t seq>

A codec for a sequence, as a JSON array.

string: Codec<string>

A codec for string, using the same representation as Encode.string and Decode.string.

timespan: Codec<TimeSpan>

A codec for TimeSpan, using the same representation as Encode.timespan and Decode.timespan.

tuple2<'a, 'b> (a: Codec<'a>) (b: Codec<'b>) : Codec<'a * 'b>

A codec for a tuple of 2 elements, as a JSON array.

tuple3<'a, 'b, 'c> (a: Codec<'a>) (b: Codec<'b>) (c: Codec<'c>) : Codec<'a * 'b * 'c>

A codec for a tuple of 3 elements, as a JSON array.

tuple4<'a, 'b, 'c, 'd> (a: Codec<'a>) (b: Codec<'b>) (c: Codec<'c>) (d: Codec<'d>) : Codec<'a * 'b * 'c * 'd>

A codec for a tuple of 4 elements, as a JSON array.

tuple5<'a, 'b, 'c, 'd, 'e> (a: Codec<'a>) (b: Codec<'b>) (c: Codec<'c>) (d: Codec<'d>) (e: Codec<'e>) : Codec<'a * 'b * 'c * 'd * 'e>

A codec for a tuple of 5 elements, as a JSON array.

tuple6<'a, 'b, 'c, 'd, 'e, 'f> (a: Codec<'a>) (b: Codec<'b>) (c: Codec<'c>) (d: Codec<'d>) (e: Codec<'e>) (f: Codec<'f>) : Codec<'a * 'b * 'c * 'd * 'e * 'f>

A codec for a tuple of 6 elements, as a JSON array.

tuple7<'a, 'b, 'c, 'd, 'e, 'f, 'g> (a: Codec<'a>) (b: Codec<'b>) (c: Codec<'c>) (d: Codec<'d>) (e: Codec<'e>) (f: Codec<'f>) (g: Codec<'g>) : Codec<'a * 'b * 'c * 'd * 'e * 'f * 'g>

A codec for a tuple of 7 elements, as a JSON array.

tuple8<'a, 'b, 'c, 'd, 'e, 'f, 'g, 'h> (a: Codec<'a>) (b: Codec<'b>) (c: Codec<'c>) (d: Codec<'d>) (e: Codec<'e>) (f: Codec<'f>) (g: Codec<'g>) (h: Codec<'h>) : Codec<'a * 'b * 'c * 'd * 'e * 'f * 'g * 'h>

A codec for a tuple of 8 elements, as a JSON array.

uint16: Codec<uint16>

A codec for uint16, using the same representation as Encode.uint16 and Decode.uint16.

uint32: Codec<uint32>

A codec for uint32, using the same representation as Encode.uint32 and Decode.uint32.

uint64: Codec<uint64>

A codec for uint64, using the same representation as Encode.uint64 and Decode.uint64.

unit: Codec<unit>

A codec for unit, using the same representation as Encode.unit and Decode.unit.

uri: Codec<Uri>

A codec for Uri, using the same representation as Encode.uri and Decode.uri.

value: Codec<Json>

A codec for the JSON tree itself, passing values through untouched.