Table of Contents

Class OptionalConverters

Namespace
Nerdbank.MessagePack
Assembly
Nerdbank.MessagePack.dll

Contains extension methods to add optional converters.

public static class OptionalConverters
Inheritance
OptionalConverters
Inherited Members

Remarks

The library comes with many converters. Some are not enabled by default to avoid unnecessary dependencies and to keep a trimmed application size small when it doesn't require them. The extension methods in this class can be used to turn these optional converters on.

Methods

WithAssumedDateTimeKind(MessagePackSerializer, DateTimeKind)

Configures the built-in DateTime converter to assume a specific DateTimeKind when serializing values that have a Unspecified kind.

public static MessagePackSerializer WithAssumedDateTimeKind(this MessagePackSerializer serializer, DateTimeKind kind)

Parameters

serializer MessagePackSerializer

The serializer to add converters to.

kind DateTimeKind

Either Utc or Local.

Returns

MessagePackSerializer

The modified serializer.

Remarks

By default, serializing a DateTime with an Unspecified kind throws an exception because the MessagePack format does not permit that kind of ambiguity. While an explicit DateTimeKind is always preferred, this method allows you to assume a specific DateTimeKind for such values. Such assumptions will be recorded in the serialized data, such that deserializing the data will produce a DateTime with the DateTimeKind specified in kind.

Exceptions

ArgumentException

Thrown if this method has already been called on the given serializer or a custom DateTime converter has already been set.

WithDynamicObjectConverter(MessagePackSerializer)

Adds a converter to the specified serializer that can write objects with a declared type of object based on their runtime type (provided a type shape is available for the runtime type), and can deserialize them based on their msgpack token types into primitives, dictionaries and arrays.

public static MessagePackSerializer WithDynamicObjectConverter(this MessagePackSerializer serializer)

Parameters

serializer MessagePackSerializer

The serializer to add converters to.

Returns

MessagePackSerializer

The modified serializer.

Remarks

This converter is very similar to the one added by WithObjectConverter(MessagePackSerializer), except that the deserialized result can be used with the C# dynamic keyword where the content of maps can also be accessed using string keys as if they were properties.

Exceptions

ArgumentException

Thrown if a converter for object has already been added.

WithExpandoObjectConverter(MessagePackSerializer)

Adds a converter for ExpandoObject to the specified serializer.

public static MessagePackSerializer WithExpandoObjectConverter(this MessagePackSerializer serializer)

Parameters

serializer MessagePackSerializer

The serializer to add converters to.

Returns

MessagePackSerializer

The modified serializer.

Remarks

This can only serialize an ExpandoObject whose properties are values or objects whose runtime type has a shape available as provided by TypeShapeProvider.

This can deserialize any msgpack map. Nested maps included in the deserialized graph will be deserialized as dictionaries that support C# dynamic access to their members, similar to ExpandoObject but with read-only access.

Exceptions

ArgumentException

Thrown if a converter for ExpandoObject has already been added.

WithGuidConverter(MessagePackSerializer, GuidFormat)

Adds a converter for Guid to the specified serializer.

public static MessagePackSerializer WithGuidConverter(this MessagePackSerializer serializer, OptionalConverters.GuidFormat format = GuidFormat.BinaryLittleEndian)

Parameters

serializer MessagePackSerializer

The serializer to add converters to.

format OptionalConverters.GuidFormat

The format in which the Guid should be written.

Returns

MessagePackSerializer

The modified serializer.

Remarks

The Guid converter is optimized to avoid allocating strings during the conversion.

Exceptions

ArgumentOutOfRangeException

Thrown if format is not one of the allowed values.

ArgumentException

Thrown if a converter for Guid has already been added.

WithObjectConverter(MessagePackSerializer)

Adds a converter to the specified serializer that can write objects with a declared type of object based on their runtime type (provided a type shape is available for the runtime type), and can deserialize them based on their msgpack token types into primitives, dictionaries and arrays.

public static MessagePackSerializer WithObjectConverter(this MessagePackSerializer serializer)

Parameters

serializer MessagePackSerializer

The serializer to add converters to.

Returns

MessagePackSerializer

The modified serializer.

Remarks

This converter is not included by default because untyped serialization is not generally desirable. It is offered as a converter that may be added to Converters in order to enable limited untyped serialization.

Maps are deserialized as objects that implement IReadOnlyDictionary<TKey, TValue> where the key is object and the value is a nullable object. This converter deserializes an entire msgpack structure using these primitives, since no type information is available to deserialize any sub-graph as a higher-level type.

Exceptions

ArgumentException

Thrown if a converter for object has already been added.

See Also

WithSystemTextJsonConverters(MessagePackSerializer)

Adds converters for common System.Text.Json types, including: JsonNode, JsonElement, and JsonDocument to the specified serializer.

public static MessagePackSerializer WithSystemTextJsonConverters(this MessagePackSerializer serializer)

Parameters

serializer MessagePackSerializer

The serializer to add converters to.

Returns

MessagePackSerializer

The modified serializer.

Exceptions

ArgumentNullException

Thrown if serializer is null.

ArgumentException

Thrown if a converter for any of these System.Text.Json types has already been added.