Logo JDeck

Codecs and Serialization

The Codec module registers manual encoders and decoders as one MessagePackConverter inside a MessagePackSerializer. Serializers are immutable, so each Codec function returns a new serializer. The input serializer keeps its other settings.

A Manual Codec

let coordEncoder: Encoder<Coord> =
  fun coord -> Encode.array [| Encode.int coord.X; Encode.int coord.Y |]

let coordDecoder: Decoder<Coord> =
  fun value -> decode {
    let! x = Decode.decodeAt Required.int 0 value
    and! y = Decode.decodeAt Required.int 1 value
    return { X = x; Y = y }
  }

let serializer =
  Codec.useCodec (MessagePackSerializer()) (coordEncoder, coordDecoder)

let shape = ReflectionTypeShapeProvider.Default.GetTypeShape<Coord>()
let mutable outgoing = { X = 3; Y = 4 }
let bytes: byte array = serializer.Serialize(&outgoing, shape)
let back: Coord = serializer.Deserialize(System.ReadOnlyMemory(bytes), shape)

printfn $"Coord: %A{back}"

Use Codec.useEncoder or Codec.useDecoder when only one direction needs help. Use Codec.useCodecWithContext when a codec must read the active context.

Decoder failures raise DecodingException inside the converter. MessagePackSerializer then wraps that error in MessagePackSerializationException.

Manual and Automatic Together

Decoding.auto delegates a whole payload to MessagePackSerializer. Decoding.fromBytes runs one manual decoder on the decoded value. Both return Result<'T, DecodeError>, so call sites handle both the same way.

One upstream limit applies: runtime converters do not target FSharp unions. Register codecs on records and other concrete types instead.

namespace Nerdbank
namespace Nerdbank.MessagePack
namespace PolyType
namespace PolyType.ReflectionProvider
namespace JDeck
namespace JDeck.MessagePack
type Coord = { X: int Y: int }
Coord.X: int
Multiple items
val int: value: 'T -> int (requires member op_Explicit)

--------------------
type int = int32

--------------------
type int<'Measure> = int
Coord.Y: int
val coordEncoder: coord: Coord -> MessagePackValue
type Encoder<'T> = 'T -> MessagePackValue
<summary> Encodes a value into a MessagePackValue. </summary>
val coord: Coord
module Encode from JDeck.MessagePack.Encoding
val array: values: MessagePackValue array -> MessagePackValue
val int: value: int -> MessagePackValue
val coordDecoder: value: MessagePackValue -> Result<Coord,DecodeError>
type Decoder<'TResult> = MessagePackValue -> Result<'TResult,DecodeError>
<summary> Decodes a MessagePackValue into a value of type 'TResult. </summary>
val value: MessagePackValue
val decode: DecodeBuilder
val x: int
Multiple items
module Decode from JDeck.MessagePack.Decode

--------------------
module Decode from JDeck.MessagePack
val decodeAt: decoder: Decoder<'TResult> -> index: int -> value: MessagePackValue -> Result<'TResult,DecodeError>
module Required from JDeck.MessagePack.Decode
<summary> Required decoders fail when the value is missing or has the wrong kind. </summary>
val int: Decoder<int>
val y: int
val serializer: MessagePackSerializer
module Codec from JDeck.MessagePack
val useCodec: serializer: MessagePackSerializer -> encoder: Encoder<'T> * decoder: Decoder<'T> -> MessagePackSerializer
<summary> Registers an encoder and a decoder and returns a new serializer. The input serializer keeps its other settings: only the converter collection changes, with the new converter first. Decoder failures raise DecodingException, which MessagePackSerializer wraps in MessagePackSerializationException. </summary>
Multiple items

--------------------
MessagePackSerializer() : MessagePackSerializer
val shape: PolyType.ITypeShape<Coord>
type ReflectionTypeShapeProvider = interface ITypeShapeProvider member GetTypeShape<'T> : unit -> ITypeShape<'T> + 1 overload static member Create: options: ReflectionTypeShapeProviderOptions -> ReflectionTypeShapeProvider member Options: ReflectionTypeShapeProviderOptions static member Default: ReflectionTypeShapeProvider
<summary> Provides a <see cref="T:PolyType.ITypeShapeProvider" /> implementation that uses reflection. </summary>
property ReflectionTypeShapeProvider.Default: ReflectionTypeShapeProvider with get
<summary> Gets the default provider instance using configuration supported by the current platform. </summary>
ReflectionTypeShapeProvider.GetTypeShape<'T>() : PolyType.ITypeShape<'T>
ReflectionTypeShapeProvider.GetTypeShape(``type`` : System.Type) : PolyType.ITypeShape
val mutable outgoing: Coord
val bytes: byte array
Multiple items
val byte: value: 'T -> byte (requires member op_Explicit)

--------------------
type byte = System.Byte

--------------------
type byte<'Measure> = byte
type 'T array = 'T array
(extension) MessagePackSerializer.Serialize<'T>(value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : byte array
   (+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T,'TProvider>(value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : byte array
   (+0 other overloads)
MessagePackSerializer.Serialize<'T,'TProvider (requires 'TProvider :> PolyType.IShapeable<'T>)>(value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : byte array
   (+0 other overloads)
MessagePackSerializer.Serialize<'T (requires 'T :> PolyType.IShapeable<'T>)>(value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : byte array
   (+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T>(writer: byref<MessagePackWriter>, value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : unit
   (+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T>(writer: System.Buffers.IBufferWriter<byte>, value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : unit
   (+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T>(stream: System.IO.Stream, value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : unit
   (+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T,'TProvider>(writer: byref<MessagePackWriter>, value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : unit
   (+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T,'TProvider>(writer: System.Buffers.IBufferWriter<byte>, value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : unit
   (+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T,'TProvider>(stream: System.IO.Stream, value: inref<'T>, ?cancellationToken: System.Threading.CancellationToken) : unit
   (+0 other overloads)
val back: Coord
(extension) MessagePackSerializer.Deserialize<'T>(reader: byref<MessagePackReader>, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
(extension) MessagePackSerializer.Deserialize<'T>(bytes: System.ReadOnlyMemory<byte>, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
(extension) MessagePackSerializer.Deserialize<'T>(bytes: inref<System.Buffers.ReadOnlySequence<byte>>, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
(extension) MessagePackSerializer.Deserialize<'T>(stream: System.IO.Stream, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
(extension) MessagePackSerializer.Deserialize<'T,'TProvider>(reader: byref<MessagePackReader>, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
(extension) MessagePackSerializer.Deserialize<'T,'TProvider>(bytes: System.ReadOnlyMemory<byte>, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
(extension) MessagePackSerializer.Deserialize<'T,'TProvider>(bytes: inref<System.Buffers.ReadOnlySequence<byte>>, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
(extension) MessagePackSerializer.Deserialize<'T,'TProvider>(stream: System.IO.Stream, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
MessagePackSerializer.Deserialize<'T,'TProvider (requires 'TProvider :> PolyType.IShapeable<'T>)>(stream: System.IO.Stream, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
MessagePackSerializer.Deserialize<'T,'TProvider (requires 'TProvider :> PolyType.IShapeable<'T>)>(bytes: inref<System.Buffers.ReadOnlySequence<byte>>, ?cancellationToken: System.Threading.CancellationToken) : 'T
   (+0 other overloads)
namespace System
Multiple items
type ReadOnlyMemory<'T> = new: array: 'T array -> unit + 1 overload member CopyTo: destination: Memory<'T> -> unit member Equals: obj: obj -> bool + 1 overload member GetHashCode: unit -> int member Pin: unit -> MemoryHandle member Slice: start: int -> ReadOnlyMemory<'T> + 1 overload member ToArray: unit -> 'T array member ToString: unit -> string member TryCopyTo: destination: Memory<'T> -> bool static member op_Implicit: segment: ArraySegment<'T> -> ReadOnlyMemory<'T> + 1 overload ...
<summary>Represents a contiguous region of memory, similar to <see cref="T:System.ReadOnlySpan`1" />. Unlike <see cref="T:System.ReadOnlySpan`1" />, it is not a byref-like type.</summary>
<typeparam name="T">The object type from which the contiguous region of memory will be read.</typeparam>


--------------------
System.ReadOnlyMemory ()
System.ReadOnlyMemory(array: 'T array) : System.ReadOnlyMemory<'T>
System.ReadOnlyMemory(array: 'T array, start: int, length: int) : System.ReadOnlyMemory<'T>
val printfn: format: Printf.TextWriterFormat<'T> -> 'T

Type something to start searching.