Table of Contents

Class MessagePackConverter<T>

Namespace
Nerdbank.MessagePack
Assembly
Nerdbank.MessagePack.dll

An interface for all message pack converters.

public abstract class MessagePackConverter<T>

Type Parameters

T

The data type that can be converted by this object.

Inheritance
MessagePackConverter<T>
Inherited Members

Properties

PreferAsyncSerialization

Gets a value indicating whether callers should prefer the async methods on this object.

public virtual bool PreferAsyncSerialization { get; }

Property Value

bool

Unless overridden in a derived converter, this value is always false.

Remarks

Derived types that override the WriteAsync(MessagePackAsyncWriter, T?, SerializationContext, CancellationToken) and/or ReadAsync(MessagePackAsyncReader, SerializationContext, CancellationToken) methods should also override this property and have it return true.

Methods

Read(ref MessagePackReader, SerializationContext)

Deserializes an instance of T.

public abstract T? Read(ref MessagePackReader reader, SerializationContext context)

Parameters

reader MessagePackReader

The reader to use.

context SerializationContext

Context for the deserialization.

Returns

T

The deserialized value.

ReadAsync(MessagePackAsyncReader, SerializationContext, CancellationToken)

Deserializes an instance of T.

public virtual ValueTask<T?> ReadAsync(MessagePackAsyncReader reader, SerializationContext context, CancellationToken cancellationToken)

Parameters

reader MessagePackAsyncReader

The reader to use.

context SerializationContext

Context for the deserialization.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<T>

The deserialized value.

Remarks

The default implementation delegates to Read(ref MessagePackReader, SerializationContext) after ensuring there is sufficient buffer to read the next structure.

Derived classes should only override this method if they may read a lot of data. They should do so with the intent to be able to read some data then asynchronously wait for data before reading more in order to reduce the amount of memory required to buffer.

Write(ref MessagePackWriter, in T?, SerializationContext)

Serializes an instance of T.

public abstract void Write(ref MessagePackWriter writer, in T? value, SerializationContext context)

Parameters

writer MessagePackWriter

The writer to use.

value T

The value to serialize.

context SerializationContext

Context for the serialization.

WriteAsync(MessagePackAsyncWriter, T?, SerializationContext, CancellationToken)

Serializes an instance of T.

public virtual ValueTask WriteAsync(MessagePackAsyncWriter writer, T? value, SerializationContext context, CancellationToken cancellationToken)

Parameters

writer MessagePackAsyncWriter

The writer to use.

value T

The value to serialize.

context SerializationContext

Context for the serialization.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask

A task that tracks the async serialization.

Remarks

The default implementation delegates to Write(ref MessagePackWriter, in T?, SerializationContext) and then flushes the data to the pipe if the buffers are getting relatively full.

Derived classes should only override this method if they may write a lot of data. They should do so with the intent of writing fragments of data at a time and periodically call FlushIfAppropriateAsync(SerializationContext, CancellationToken) in order to keep the size of memory buffers from growing too much.