Encoding Guide
An encoder is defined as:
type Encoder<'T> = 'T -> MessagePackValue
Encode provides encoders for the same primitive set as the decoders:
string, boolean, char, guid, binary, numeric widths, decimal,
dateTime, dateTimeOffset, and timeSpan.
Maps are the default record form. Encode.entryMap builds a map from string
keys. Encode.array builds one positional array. Encode.sequence encodes
each element of a sequence with one encoder.
type Coord = { X: int; Y: int } with
static member Encoder: Encoder<Coord> =
fun coord -> Encode.array [| Encode.int coord.X; Encode.int coord.Y |]
type Person = { name: string; age: int } with
static member Encoder: Encoder<Person> =
fun person ->
Encode.entryMap [
"name", Encode.string person.name
"age", Encode.int person.age
]
let encodedPerson = Person.Encoder { name = "Ada"; age = 36 }
printfn $"Kind: %A{encodedPerson.Kind}"
let encodedCoord = Coord.Encoder { X = 3; Y = 4 }
printfn $"Coord kind: %A{encodedCoord.Kind}"
Wire Forms
The library uses the same native forms as the automatic serializer, so manual and automatic code agree on one choice per type:
charuses one unsigned 16-bit integer.guiduses extension type 2 with 16 big-endian bytes.decimaluses extension type 4 with 16 little-endianDECIMALbytes.dateTimeuses the timestamp extension (type code -1, 4, 8, or 12 bytes).-
dateTimeOffsetuses a two-item array of the timestamp extension of the UTC instant and the offset in whole minutes. timeSpanuses integer ticks.unitusesNil.
Decoders stay lenient: guid, decimal, dateTime, and dateTimeOffset
also read their old invariant string forms, and decimal also reads a
16-byte binary value with the same DECIMAL layout.
For default serializers this costs nothing and needs no action. One
exception matters: guid and decimal use serializer-reserved codes.
Remapping LibraryExtensionTypeCodes on your serializer changes those
codes, and manual payloads then stop matching that serializer. Keep the
default codes when you mix manual and automatic paths.
The value then travels through MessagePackSerializer as one MessagePackValue.
Inspecting Payloads
MessagePackSerializer.ConvertToJson prints real bytes as JSON, which
shows the native forms above without guessing at raw bytes:
let inspectSerializer = MessagePackSerializer()
let inspectShape =
ReflectionTypeShapeProvider.Default.GetTypeShape<MessagePackValue>()
let mutable inspectOutgoing = Encode.decimal 1234.56m
let inspectBytes: byte array = inspectSerializer.Serialize(&inspectOutgoing, inspectShape)
printfn "%s" (inspectSerializer.ConvertToJson(System.ReadOnlyMemory(inspectBytes)))
F# Values to Bytes
Serializing is the byte-level counterpart of Decoding.
Three paths cover the F# serialization cases:
Serializing.autowrites one F# value with the reflection shape.-
Serializing.autoWithShapewrites one value with an explicit shape. AOT apps use this path with a source-generated shape. -
Serializing.toBytesruns one manualEncoder<'T>first. It then writes the resultingMessagePackValue.
Every path returns byte array.
Serializing.toStream and Serializing.autoToStream write to a stream.
Pass a serializer as the first argument when codecs or custom settings apply.
type Reading = { Name: string; Value: int }
let autoBytes = Serializing.auto { Name = "temp"; Value = 21 }
printfn $"Auto bytes: %d{autoBytes.Length}"
let manualBytes =
Serializing.toBytes(
{ name = "Ada"; age = 36 },
fun person ->
Encode.entryMap [
"name", Encode.string person.name
"age", Encode.int person.age
]
)
printfn $"Manual bytes: %d{manualBytes.Length}"
let manualBack = Decoding.fromBytes (manualBytes, fun value -> decode {
let! name = Required.Map.get ("name", Required.string) value
and! age = Required.Map.get ("age", Required.int) value
return name, age
})
printfn $"Manual round-trip: %A{manualBack}"
type MessagePackValue = new: value: byte -> unit + 16 overloads member Equals: other: MessagePackValue -> bool + 1 overload member GetHashCode: unit -> int member StructuralEquals: other: MessagePackValue -> bool member ToString: unit -> string static member op_Explicit: token: MessagePackValue -> byte + 17 overloads static member op_Implicit: value: byte -> MessagePackValue + 17 overloads static val Nil: MessagePackValue member Kind: MessagePackValueKind member ValueAsArray: ReadOnlyMemory<MessagePackValue> ...
<summary> A variant type that can represent any of the MessagePack primitives. </summary>
--------------------
MessagePackValue ()
(+0 other overloads)
MessagePackValue(value: byte) : MessagePackValue
(+0 other overloads)
MessagePackValue(value: sbyte) : MessagePackValue
(+0 other overloads)
MessagePackValue(value: uint16) : MessagePackValue
(+0 other overloads)
MessagePackValue(value: int16) : MessagePackValue
(+0 other overloads)
MessagePackValue(value: uint32) : MessagePackValue
(+0 other overloads)
MessagePackValue(value: int) : MessagePackValue
(+0 other overloads)
MessagePackValue(value: uint64) : MessagePackValue
(+0 other overloads)
MessagePackValue(value: int64) : MessagePackValue
(+0 other overloads)
MessagePackValue(value: bool) : MessagePackValue
(+0 other overloads)
val int: value: 'T -> int (requires member op_Explicit)
--------------------
type int = int32
--------------------
type int<'Measure> = int
val string: value: 'T -> string
--------------------
type string = String
<summary> Gets the type of the MessagePack token. </summary>
--------------------
MessagePackSerializer() : MessagePackSerializer
<summary> Provides a <see cref="T:PolyType.ITypeShapeProvider" /> implementation that uses reflection. </summary>
<summary> Gets the default provider instance using configuration supported by the current platform. </summary>
ReflectionTypeShapeProvider.GetTypeShape(``type`` : Type) : PolyType.ITypeShape
val byte: value: 'T -> byte (requires member op_Explicit)
--------------------
type byte = Byte
--------------------
type byte<'Measure> = byte
(+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T,'TProvider>(value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : byte array
(+0 other overloads)
MessagePackSerializer.Serialize<'T,'TProvider (requires 'TProvider :> PolyType.IShapeable<'T>)>(value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : byte array
(+0 other overloads)
MessagePackSerializer.Serialize<'T (requires 'T :> PolyType.IShapeable<'T>)>(value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : byte array
(+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T>(writer: byref<MessagePackWriter>, value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : unit
(+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T>(writer: Buffers.IBufferWriter<byte>, value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : unit
(+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T>(stream: IO.Stream, value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : unit
(+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T,'TProvider>(writer: byref<MessagePackWriter>, value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : unit
(+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T,'TProvider>(writer: Buffers.IBufferWriter<byte>, value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : unit
(+0 other overloads)
(extension) MessagePackSerializer.Serialize<'T,'TProvider>(stream: IO.Stream, value: inref<'T>, ?cancellationToken: Threading.CancellationToken) : unit
(+0 other overloads)
MessagePackSerializer.ConvertToJson(msgpack: ReadOnlyMemory<byte>, ?options: Nullable<MessagePackSerializer.JsonOptions>) : string
MessagePackSerializer.ConvertToJson(reader: byref<MessagePackReader>, jsonWriter: IO.TextWriter, ?options: Nullable<MessagePackSerializer.JsonOptions>) : unit
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>
--------------------
ReadOnlyMemory ()
ReadOnlyMemory(array: 'T array) : ReadOnlyMemory<'T>
ReadOnlyMemory(array: 'T array, start: int, length: int) : ReadOnlyMemory<'T>
<summary> Serializes F# values and manual encoder output to MessagePack bytes. Automatic members use a PolyType shape. Manual members run one encoder to a MessagePackValue, then write that value with the serializer. </summary>
static member Serializing.auto: serializer: MessagePackSerializer * value: 'T -> byte array
<summary>Gets the total number of elements in all the dimensions of the <see cref="T:System.Array" />.</summary>
<exception cref="T:System.OverflowException">The array is multidimensional and contains more than <see cref="F:System.Int32.MaxValue">Int32.MaxValue</see> elements.</exception>
<returns>The total number of elements in all the dimensions of the <see cref="T:System.Array" />; zero if there are no elements in the array.</returns>
static member Serializing.toBytes: serializer: MessagePackSerializer * value: 'T * encoder: Encoder<'T> -> byte array
<summary> Decodes MessagePack payloads with JDeck decoders or automatic shapes. There is no fromString: MessagePack is a binary format. </summary>
<summary> Required decoders fail when the value is missing or has the wrong kind. </summary>
<summary> Reads entries of a Map value by string key. </summary>
static member Required.Map.get: key: string * decoder: Decoder<'TResult> -> Decoder<'TResult>
JDeck