Class MessagePackSerializer
- Namespace
- Nerdbank.MessagePack
- Assembly
- Nerdbank.MessagePack.dll
Serializes .NET objects using the MessagePack format.
public record MessagePackSerializer : IEquatable<MessagePackSerializer>
- Inheritance
-
MessagePackSerializer
- Implements
- Inherited Members
- Extension Methods
Remarks
This type is immutable and thread-safe.
Changes to properties on this object should be performed with the record syntax with keyword.
When targeting .NET Standard 2.0 or .NET Framework, some important methods are available only as extension methods,
so make sure to have a using Nerdbank.MessagePack; directive in your code file to see these.
Properties
ComparerProvider
Gets the provider of IEqualityComparer<T> and IComparer<T> instances to use when instantiating collections that support them.
public IComparerProvider? ComparerProvider { get; init; }
Property Value
- IComparerProvider
The default value is an instance of SecureComparerProvider, which provides hash collision resistance for improved security when deserializing untrusted data.
Remarks
This property may be cleared from its secure default for improved performance when deserializing trusted data.
- See Also
ConverterFactories
Gets an array of converter factories to consult when creating a converter for a given type.
public ImmutableArray<IMessagePackConverterFactory> ConverterFactories { get; init; }
Property Value
Remarks
Factories are the last resort for creating a custom converter, coming after Nerdbank.MessagePack.SerializerConfiguration.Converters and Nerdbank.MessagePack.SerializerConfiguration.ConverterTypes.
ConverterTypes
Gets a collection of MessagePackConverter<T> types that should be used for their designated data types.
public ConverterTypeCollection ConverterTypes { get; init; }
Property Value
Remarks
The types in this collection are searched after matching Nerdbank.MessagePack.SerializerConfiguration.Converters and before Nerdbank.MessagePack.SerializerConfiguration.ConverterFactories when creating a converter for a given type.
Converters
Gets an array of MessagePackConverter<T> objects that should be used for their designated data types.
public ConverterCollection Converters { get; init; }
Property Value
Remarks
Converters in this collection are searched first when creating a converter for a given type, before Nerdbank.MessagePack.SerializerConfiguration.ConverterTypes and Nerdbank.MessagePack.SerializerConfiguration.ConverterFactories.
DerivedTypeUnions
Gets a collection of DerivedTypeUnion objects that add runtime insight into what derived types may appear in the serialized data for a given base type.
public DerivedTypeUnionCollection DerivedTypeUnions { get; init; }
Property Value
DeserializeDefaultValues
Gets the policy concerning how to handle missing or null properties during deserialization.
public DeserializeDefaultValuesPolicy DeserializeDefaultValues { get; init; }
Property Value
- DeserializeDefaultValuesPolicy
The default value is Default.
InternStrings
Gets a value indicating whether to intern strings during deserialization.
public bool InternStrings { get; init; }
Property Value
Remarks
String interning means that a string that appears multiple times (within a single deserialization or across many) in the msgpack data will be deserialized as the same string instance, reducing GC pressure.
When enabled, all deserialized strings are retained with a weak reference, allowing them to be garbage collected while also being reusable for future deserializations as long as they are in memory.
This feature has a positive impact on memory usage but may have a negative impact on performance due to searching through previously deserialized strings to find a match. If your application is performance sensitive, you should measure the impact of this feature on your application.
This feature is orthogonal and complementary to Nerdbank.MessagePack.SerializerConfiguration.PreserveReferences. Preserving references impacts the serialized result and can hurt interoperability if the other party is not using the same feature. Preserving references also does not guarantee that equal strings will be reused because the original serialization may have had multiple string objects for the same value, so deserialization would produce the same result. Preserving references alone will never reuse strings across top-level deserialization operations either. Interning strings however, has no impact on the serialized result and is always safe to use. Interning strings will guarantee string objects are reused within and across deserialization operations so long as their values are equal. The combination of the two features will ensure the most compact msgpack, and will produce faster deserialization times than string interning alone. Combining the two features also activates special behavior to ensure that serialization only writes a string once and references that string later in that same serialization, even if the equal strings were unique objects.
LibraryExtensionTypeCodes
Gets the extension type codes to use for library-reserved extension types.
public LibraryReservedMessagePackExtensionTypeCode LibraryExtensionTypeCodes { get; init; }
Property Value
Remarks
This property may be used to reassign the extension type codes for library-provided extension types in order to avoid conflicts with other libraries the application is using.
MaxAsyncBuffer
Gets the maximum length of msgpack to buffer before beginning deserialization.
public int MaxAsyncBuffer { get; init; }
Property Value
- int
May be set to any non-negative integer. The default value is 1MB and is subject to change.
Remarks
Larger values are more likely to lead to async buffering followed by synchronous deserialization, which is significantly faster than async deserialization. Smaller values are useful for limiting memory usage since deserialization can happen while pulling in more msgpack bytes, allowing release of the buffers containing earlier bytes to make room for subsequent ones.
This value has no impact once deserialization has begun. The msgpack structure to be deserialized and converters used during deserialization may result in buffering any amount of msgpack beyond this value.
MultiDimensionalArrayFormat
Gets the format to use when serializing multi-dimensional arrays.
public MultiDimensionalArrayFormat MultiDimensionalArrayFormat { get; init; }
Property Value
PerfOverSchemaStability
Gets a value indicating whether to boost performance using methods that may compromise the stability of the serialized schema.
public bool PerfOverSchemaStability { get; init; }
Property Value
Remarks
This setting is intended for use in performance-sensitive scenarios where the serialized data will not be stored or shared with other systems, but rather is used in a single system live data such that the schema need not be stable between versions of the application.
Examples of behavioral changes that may occur when this setting is true:
- All objects are serialized with an array of their values instead of maps that include their property names.
- Polymorphic type identifiers are always integers.
In particular, the schema is liable to change when this property is true and:
- Serialized members are added, removed or reordered within their declaring type.
- A DerivedTypeShapeAttribute is removed, or inserted before the last such attribute on a given type.
Changing this property (either direction) is itself liable to alter the schema of the serialized data.
Performance and schema stability can both be achieved at once by:
- Using the KeyAttribute on all serialized properties.
- Specifying Tag explicitly for all polymorphic types.
PreserveReferences
Gets a setting that determines how references to objects are preserved during serialization and deserialization.
public ReferencePreservationMode PreserveReferences { get; init; }
Property Value
- ReferencePreservationMode
The default value is Off because it requires no msgpack extensions, is compatible with all msgpack readers, adds no security considerations and is the most performant.
Remarks
Preserving references impacts the serialized result and can hurt interoperability if the other party is not using the same feature.
PropertyNamingPolicy
Gets the transformation function to apply to property names before serializing them.
public MessagePackNamingPolicy? PropertyNamingPolicy { get; init; }
Property Value
- MessagePackNamingPolicy
The default value is null, indicating that property names should be persisted exactly as they are declared in .NET.
SerializeDefaultValues
Gets the policy concerning which properties to serialize though they are set to their default values.
public SerializeDefaultValuesPolicy SerializeDefaultValues { get; init; }
Property Value
- SerializeDefaultValuesPolicy
The default value is Required, meaning that only required properties or properties with non-default values will be serialized.
Remarks
By default, the serializer omits properties and fields that are set to their default values when serializing objects. This property can be used to override that behavior and serialize all properties and fields, regardless of their value.
Objects that are serialized as arrays (i.e. types that use KeyAttribute on their members), have a limited ability to omit default values because the order of the elements in the array is significant. See the KeyAttribute documentation for details.
Default values are assumed to be default(TPropertyType) except where overridden, as follows:
- Primary constructor default parameter values. e.g.
record Person(int Age = 18) - Properties or fields attributed with DefaultValueAttribute. e.g.
[DefaultValue(18)] internal int Age { get; set; } = 18;
When using anything besides Always,
avoid using member initializers to set default values without also using DefaultValueAttribute.
Otherwise round-trip serialization may not work as expected.
For example a property declared as public int Age = 18; may be skipped during serialization when its value is 0 (because default(int) == 0),
and upon deserialization, the value of Age will be 18 because no value for the property was provided.
But simply adding [DefaultValue(18)] to that same property declaration allows the serializer to understand what value should be considered the default value,
so that it is only skipped during serialization if the age was 18, allowing all values to be correctly round-tripped.
SerializeEnumValuesByName
Gets a value indicating whether enum values will be serialized by name rather than by their numeric value.
public bool SerializeEnumValuesByName { get; init; }
Property Value
Remarks
Serializing by name is a best effort. Most enums do not define a name for every possible value, and flags enums may have complicated string representations when multiple named enum elements are combined to form a value. When a simple string cannot be constructed for a given value, the numeric form is used.
When deserializing enums by name, name matching is case insensitive unless the enum type defines multiple values with names that are only distinguished by case.
StartingContext
Gets the starting context to begin (de)serializations with.
public SerializationContext StartingContext { get; init; }
Property Value
Methods
ConvertToJson(ref MessagePackReader, TextWriter, JsonOptions?)
Converts one MessagePack structure to a JSON stream.
public void ConvertToJson(ref MessagePackReader reader, TextWriter jsonWriter, MessagePackSerializer.JsonOptions? options = null)
Parameters
readerMessagePackReaderA reader of the msgpack stream.
jsonWriterTextWriterThe writer that will receive JSON text.
optionsMessagePackSerializer.JsonOptions?Options to customize how the JSON is written.
ConvertToJson(in ReadOnlySequence<byte>, JsonOptions?)
Converts a msgpack sequence into equivalent JSON.
public string ConvertToJson(in ReadOnlySequence<byte> msgpack, MessagePackSerializer.JsonOptions? options = null)
Parameters
msgpackReadOnlySequence<byte>The msgpack sequence.
optionsMessagePackSerializer.JsonOptions?- Options to customize how the JSON is written.
Returns
- string
The JSON.
Remarks
Not all valid msgpack can be converted to JSON. For example, msgpack maps with non-string keys cannot be represented in JSON. As such, this method is intended for debugging purposes rather than for production use.
ConvertToJson(ReadOnlyMemory<byte>, JsonOptions?)
Converts a msgpack sequence into equivalent JSON.
public string ConvertToJson(ReadOnlyMemory<byte> msgpack, MessagePackSerializer.JsonOptions? options = null)
Parameters
msgpackReadOnlyMemory<byte>The msgpack sequence.
optionsMessagePackSerializer.JsonOptions?- Options to customize how the JSON is written.
Returns
- string
The JSON.
Remarks
Not all valid msgpack can be converted to JSON. For example, msgpack maps with non-string keys cannot be represented in JSON. As such, this method is intended for debugging purposes rather than for production use.
CreateSerializationContext(ITypeShapeProvider, CancellationToken)
Creates a new serialization context that is ready to process a serialization job.
protected MessagePackSerializer.DisposableSerializationContext CreateSerializationContext(ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationTokenA cancellation token for the operation.
Returns
- MessagePackSerializer.DisposableSerializationContext
The serialization context.
Remarks
Callers should be sure to always call Dispose() when done with the context.
DeserializeAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken)
Deserializes a value from a PipeReader.
public ValueTask<T?> DeserializeAsync<T>(PipeReader reader, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe reader to deserialize from.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe type of value to deserialize.
DeserializeAsync<T>(PipeReader, ITypeShape<T>, CancellationToken)
Deserializes a value from a PipeReader.
public ValueTask<T?> DeserializeAsync<T>(PipeReader reader, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe reader to deserialize from. Complete(Exception) is not called on this at the conclusion of deserialization, and the reader is left at the position after the last msgpack byte read.
shapeITypeShape<T>The shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe type of value to deserialize.
DeserializeAsync<T>(PipeReader, CancellationToken)
Deserializes a value from a PipeReader.
[ExcludeFromCodeCoverage]
public ValueTask<T?> DeserializeAsync<T>(PipeReader reader, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
readerPipeReaderThe reader to deserialize from. Complete(Exception) is not called on this at the conclusion of deserialization, and the reader is left at the position after the last msgpack byte read.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe type of value to deserialize.
DeserializeAsync<T>(Stream, ITypeShapeProvider, CancellationToken)
Deserializes a value from a Stream.
public ValueTask<T?> DeserializeAsync<T>(Stream stream, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe type to be serialized.
DeserializeAsync<T>(Stream, ITypeShape<T>, CancellationToken)
Deserializes a value from a Stream.
public ValueTask<T?> DeserializeAsync<T>(Stream stream, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
shapeITypeShape<T>- The shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe type to be serialized.
DeserializeAsync<T>(Stream, CancellationToken)
Deserializes a value from a Stream.
[ExcludeFromCodeCoverage]
public ValueTask<T?> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe type to be serialized.
DeserializeAsync<T, TProvider>(PipeReader, CancellationToken)
Deserializes a value from a PipeReader.
[ExcludeFromCodeCoverage]
public ValueTask<T?> DeserializeAsync<T, TProvider>(PipeReader reader, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
readerPipeReaderThe reader to deserialize from. Complete(Exception) is not called on this at the conclusion of deserialization, and the reader is left at the position after the last msgpack byte read.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe type of value to deserialize.
TProvider
DeserializeAsync<T, TProvider>(Stream, CancellationToken)
Deserializes a value from a Stream.
[ExcludeFromCodeCoverage]
public ValueTask<T?> DeserializeAsync<T, TProvider>(Stream stream, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe type to be serialized. TProvider
DeserializeDynamicPrimitives(ref MessagePackReader, CancellationToken)
Deserializes msgpack into primitive values, maps and arrays,
where the maps are specially implemented to play nicely with the C# dynamic keyword.
public dynamic? DeserializeDynamicPrimitives(ref MessagePackReader reader, CancellationToken cancellationToken = default)
Parameters
readerMessagePackReaderThe source of msgpack to decode.
cancellationTokenCancellationTokenA cancellation token.
Returns
- dynamic
A null, string, long, ulong, float, double, DateTime, IReadOnlyDictionary<TKey, TValue> with object keys and values, an array of objects, or an Extension.
Examples
The following snippet demonstrates a way to use this method.
MessagePackReader reader = new(msgpack);
dynamic? deserialized = serializer.DeserializeDynamicPrimitives(ref reader);
string prop1 = deserialized!.Prop1;
int prop2 = deserialized.Prop2;
bool deeperBool = deserialized.deeper.IsAdult;
int age = deserialized.People[3].Age;
// Did we miss anything? Dump out all the keys and their values.
foreach (object key in deserialized)
{
Console.WriteLine($"{key}: {deserialized[key]}");
}
Remarks
Maps implement IReadOnlyDictionary<TKey, TValue> with object keys and values.
Keep in mind when using integers that msgpack doesn't preserve integer length information. This method deserializes all integers as ulong (or long if the value is negative). Integer length stretching is automatically applied when using integers as keys in maps so that they can match.
All arrays are instances of object?[].
Msgpack binary data is represented as a byte[] object.
Msgpack extensions are represented by Extension values.
The resulting object is designed to work conveniently with the C# dynamic type.
Maps become dynamic objects whose members are named after the keys in the maps.
When maps do not use string keys, the values are still accessible by using the indexer on the object.
Attempts to access values with keys that do not exist on the original msgpack object result in throwing a KeyNotFoundException.
All members on maps are discoverable using foreach, which enumerates the keys on the object.
DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken)
Deserializes a sequence of values such that each element is produced individually.
public IAsyncEnumerable<T?> DeserializeEnumerableAsync<T>(PipeReader reader, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe reader to deserialize from. CompleteAsync(Exception) will be called only at the conclusion of a successful enumeration.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An async enumerable, suitable for use with
await foreach.
Type Parameters
TThe type of value to be deserialized.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken) or any other overload that takes a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that is nested within a larger msgpack structure.
Exceptions
- NotSupportedException
Thrown if PreserveReferences is not Off.
DeserializeEnumerableAsync<T>(PipeReader, ITypeShape<T>, CancellationToken)
Deserializes a sequence of values such that each element is produced individually.
public IAsyncEnumerable<T?> DeserializeEnumerableAsync<T>(PipeReader reader, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe reader to deserialize from. CompleteAsync(Exception) will be called only at the conclusion of a successful enumeration.
shapeITypeShape<T>- The shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An async enumerable, suitable for use with
await foreach.
Type Parameters
TThe type of value to be deserialized.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken) or any other overload that takes a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that is nested within a larger msgpack structure.
Exceptions
- NotSupportedException
Thrown if PreserveReferences is not Off.
DeserializeEnumerableAsync<T>(PipeReader, CancellationToken)
Deserializes a sequence of values such that each element is produced individually.
[ExcludeFromCodeCoverage]
public IAsyncEnumerable<T?> DeserializeEnumerableAsync<T>(PipeReader reader, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
readerPipeReaderThe reader to deserialize from. CompleteAsync(Exception) will be called only at the conclusion of a successful enumeration.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An async enumerable, suitable for use with
await foreach.
Type Parameters
TThe type of value to be deserialized.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken) or any other overload that takes a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that is nested within a larger msgpack structure.
Exceptions
- NotSupportedException
Thrown if PreserveReferences is not Off.
DeserializeEnumerableAsync<T>(Stream, ITypeShapeProvider, CancellationToken)
public IAsyncEnumerable<T?> DeserializeEnumerableAsync<T>(Stream stream, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationToken- A cancellation token.
Returns
- IAsyncEnumerable<T>
An async enumerable, suitable for use with await foreach.
Type Parameters
TThe type of value to be deserialized.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken) or any other overload that takes a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that is nested within a larger msgpack structure.
DeserializeEnumerableAsync<T>(Stream, ITypeShape<T>, CancellationToken)
public IAsyncEnumerable<T?> DeserializeEnumerableAsync<T>(Stream stream, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
shapeITypeShape<T>- The shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationToken- A cancellation token.
Returns
- IAsyncEnumerable<T>
An async enumerable, suitable for use with await foreach.
Type Parameters
TThe type of value to be deserialized.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken) or any other overload that takes a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that is nested within a larger msgpack structure.
DeserializeEnumerableAsync<T>(Stream, CancellationToken)
[ExcludeFromCodeCoverage]
public IAsyncEnumerable<T?> DeserializeEnumerableAsync<T>(Stream stream, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
cancellationTokenCancellationToken- A cancellation token.
Returns
- IAsyncEnumerable<T>
An async enumerable, suitable for use with await foreach.
Type Parameters
TThe type of value to be deserialized.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken) or any other overload that takes a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that is nested within a larger msgpack structure.
DeserializeEnumerableAsync<T, TElement>(PipeReader, StreamingEnumerationOptions<T, TElement>, CancellationToken)
Deserializes a sequence of values in a larger msgpack structure such that each element is produced individually.
[ExcludeFromCodeCoverage]
public IAsyncEnumerable<TElement?> DeserializeEnumerableAsync<T, TElement>(PipeReader reader, MessagePackSerializer.StreamingEnumerationOptions<T, TElement> options, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
readerPipeReaderThe reader to deserialize from. CompleteAsync(Exception) will be called only at the conclusion of a successful enumeration.
optionsMessagePackSerializer.StreamingEnumerationOptions<T, TElement>Options to apply to the streaming enumeration.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An async enumerable, suitable for use with
await foreach.
Type Parameters
TThe type that describes the top-level msgpack structure.
TElementThe type of element to be enumerated within the structure.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken) or any other overload that does not take a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that are each top-level structures in the stream (with no envelope).
Exceptions
- NotSupportedException
Thrown if PreserveReferences is not Off.
DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken)
Deserializes a sequence of values in a larger msgpack structure such that each element is produced individually.
[SuppressMessage("ApiDesign", "RS0030:Do not use banned APIs", Justification = "Public API accepts an ITypeShapeProvider.")]
public IAsyncEnumerable<TElement?> DeserializeEnumerableAsync<T, TElement>(PipeReader reader, ITypeShapeProvider provider, MessagePackSerializer.StreamingEnumerationOptions<T, TElement> options, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe reader to deserialize from. CompleteAsync(Exception) will be called only at the conclusion of a successful enumeration.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. optionsMessagePackSerializer.StreamingEnumerationOptions<T, TElement>Options to apply to the streaming enumeration.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An async enumerable, suitable for use with
await foreach.
Type Parameters
TThe type that describes the top-level msgpack structure.
TElementThe type of element to be enumerated within the structure.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken) or any other overload that does not take a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that are each top-level structures in the stream (with no envelope).
Exceptions
- NotSupportedException
Thrown if PreserveReferences is not Off.
DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShape<T>, StreamingEnumerationOptions<T, TElement>, CancellationToken)
Deserializes a sequence of values in a larger msgpack structure such that each element is produced individually.
public IAsyncEnumerable<TElement?> DeserializeEnumerableAsync<T, TElement>(PipeReader reader, ITypeShape<T> shape, MessagePackSerializer.StreamingEnumerationOptions<T, TElement> options, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe reader to deserialize from. CompleteAsync(Exception) will be called only at the conclusion of a successful enumeration.
shapeITypeShape<T>- The shape of the type, as obtained from an ITypeShapeProvider.
optionsMessagePackSerializer.StreamingEnumerationOptions<T, TElement>Options to apply to the streaming enumeration.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An async enumerable, suitable for use with
await foreach.
Type Parameters
TThe type that describes the top-level msgpack structure.
TElementThe type of element to be enumerated within the structure.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken) or any other overload that does not take a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that are each top-level structures in the stream (with no envelope).
Exceptions
- NotSupportedException
Thrown if PreserveReferences is not Off.
DeserializeEnumerableAsync<T, TProvider>(PipeReader, CancellationToken)
Deserializes a sequence of values such that each element is produced individually.
[ExcludeFromCodeCoverage]
public IAsyncEnumerable<T?> DeserializeEnumerableAsync<T, TProvider>(PipeReader reader, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
readerPipeReaderThe reader to deserialize from. CompleteAsync(Exception) will be called only at the conclusion of a successful enumeration.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An async enumerable, suitable for use with
await foreach.
Type Parameters
TThe type of value to be deserialized.
TProvider
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken) or any other overload that takes a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that is nested within a larger msgpack structure.
Exceptions
- NotSupportedException
Thrown if PreserveReferences is not Off.
DeserializeEnumerableAsync<T, TElement>(Stream, StreamingEnumerationOptions<T, TElement>, CancellationToken)
[ExcludeFromCodeCoverage]
public IAsyncEnumerable<TElement?> DeserializeEnumerableAsync<T, TElement>(Stream stream, MessagePackSerializer.StreamingEnumerationOptions<T, TElement> options, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
optionsMessagePackSerializer.StreamingEnumerationOptions<T, TElement>- Options to apply to the streaming enumeration.
cancellationTokenCancellationToken- A cancellation token.
Returns
- IAsyncEnumerable<TElement>
An async enumerable, suitable for use with await foreach.
Type Parameters
TThe type that describes the top-level msgpack structure. TElementThe type of element to be enumerated within the structure.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken) or any other overload that does not take a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that are each top-level structures in the stream (with no envelope).
DeserializeEnumerableAsync<T, TElement>(Stream, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken)
[SuppressMessage("ApiDesign", "RS0030:Do not use banned APIs", Justification = "Public API accepts an ITypeShapeProvider.")]
public IAsyncEnumerable<TElement?> DeserializeEnumerableAsync<T, TElement>(Stream stream, ITypeShapeProvider provider, MessagePackSerializer.StreamingEnumerationOptions<T, TElement> options, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. optionsMessagePackSerializer.StreamingEnumerationOptions<T, TElement>- Options to apply to the streaming enumeration.
cancellationTokenCancellationToken- A cancellation token.
Returns
- IAsyncEnumerable<TElement>
An async enumerable, suitable for use with await foreach.
Type Parameters
TThe type that describes the top-level msgpack structure. TElementThe type of element to be enumerated within the structure.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken) or any other overload that does not take a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that are each top-level structures in the stream (with no envelope).
DeserializeEnumerableAsync<T, TElement>(Stream, ITypeShape<T>, StreamingEnumerationOptions<T, TElement>, CancellationToken)
public IAsyncEnumerable<TElement?> DeserializeEnumerableAsync<T, TElement>(Stream stream, ITypeShape<T> shape, MessagePackSerializer.StreamingEnumerationOptions<T, TElement> options, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
shapeITypeShape<T>- The shape of the type, as obtained from an ITypeShapeProvider.
optionsMessagePackSerializer.StreamingEnumerationOptions<T, TElement>- Options to apply to the streaming enumeration.
cancellationTokenCancellationToken- A cancellation token.
Returns
- IAsyncEnumerable<TElement>
An async enumerable, suitable for use with await foreach.
Type Parameters
TThe type that describes the top-level msgpack structure. TElementThe type of element to be enumerated within the structure.
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken) or any other overload that does not take a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that are each top-level structures in the stream (with no envelope).
DeserializeEnumerableAsync<T, TProvider>(Stream, CancellationToken)
[ExcludeFromCodeCoverage]
public IAsyncEnumerable<T?> DeserializeEnumerableAsync<T, TProvider>(Stream stream, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
cancellationTokenCancellationToken- A cancellation token.
Returns
- IAsyncEnumerable<T>
An async enumerable, suitable for use with await foreach.
Type Parameters
TThe type of value to be deserialized. TProvider
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T, TElement>(PipeReader, ITypeShapeProvider, StreamingEnumerationOptions<T, TElement>, CancellationToken) or any other overload that takes a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that is nested within a larger msgpack structure.
DeserializeEnumerableAsync<T, TElement, TProvider>(PipeReader, StreamingEnumerationOptions<T, TElement>, CancellationToken)
Deserializes a sequence of values in a larger msgpack structure such that each element is produced individually.
[ExcludeFromCodeCoverage]
public IAsyncEnumerable<TElement?> DeserializeEnumerableAsync<T, TElement, TProvider>(PipeReader reader, MessagePackSerializer.StreamingEnumerationOptions<T, TElement> options, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
readerPipeReaderThe reader to deserialize from. CompleteAsync(Exception) will be called only at the conclusion of a successful enumeration.
optionsMessagePackSerializer.StreamingEnumerationOptions<T, TElement>Options to apply to the streaming enumeration.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An async enumerable, suitable for use with
await foreach.
Type Parameters
TThe type that describes the top-level msgpack structure.
TElementThe type of element to be enumerated within the structure.
TProvider
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken) or any other overload that does not take a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that are each top-level structures in the stream (with no envelope).
Exceptions
- NotSupportedException
Thrown if PreserveReferences is not Off.
DeserializeEnumerableAsync<T, TElement, TProvider>(Stream, StreamingEnumerationOptions<T, TElement>, CancellationToken)
[ExcludeFromCodeCoverage]
public IAsyncEnumerable<TElement?> DeserializeEnumerableAsync<T, TElement, TProvider>(Stream stream, MessagePackSerializer.StreamingEnumerationOptions<T, TElement> options, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
optionsMessagePackSerializer.StreamingEnumerationOptions<T, TElement>- Options to apply to the streaming enumeration.
cancellationTokenCancellationToken- A cancellation token.
Returns
- IAsyncEnumerable<TElement>
An async enumerable, suitable for use with await foreach.
Type Parameters
TThe type that describes the top-level msgpack structure. TElementThe type of element to be enumerated within the structure. TProvider
Remarks
The content read from reader must be a sequence of msgpack-encoded values with no envelope (e.g. an array).
After the reader is exhausted, the sequence will end.
See DeserializeEnumerableAsync<T>(PipeReader, ITypeShapeProvider, CancellationToken) or any other overload that does not take a MessagePackSerializer.StreamingEnumerationOptions<T, TElement> parameter for streaming a sequence of values that are each top-level structures in the stream (with no envelope).
DeserializeObject(ref MessagePackReader, ITypeShape, CancellationToken)
Deserializes an untyped value.
public object? DeserializeObject(ref MessagePackReader reader, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
readerMessagePackReaderThe msgpack reader to deserialize from.
shapeITypeShapeThe shape of the value to deserialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- object
The deserialized value.
Examples
See the SerializeObject(ref MessagePackWriter, object?, ITypeShape, CancellationToken) method for an example of using this method.
DeserializeObject(scoped in ReadOnlySequence<byte>, ITypeShape, CancellationToken)
Deserializes an untyped value.
public object? DeserializeObject(scoped in ReadOnlySequence<byte> buffer, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlySequence<byte>The msgpack to deserialize from.
shapeITypeShapeThe shape of the value to deserialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- object
The deserialized value.
Examples
See the SerializeObject(ref MessagePackWriter, object?, ITypeShape, CancellationToken) method for an example of using this method.
DeserializeObject(Stream, ITypeShape, CancellationToken)
Deserializes an untyped value.
public object? DeserializeObject(Stream stream, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
shapeITypeShapeThe shape of the value to deserialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- object
The deserialized value.
Examples
See the SerializeObject(ref MessagePackWriter, object?, ITypeShape, CancellationToken) method for an example of using this method.
Remarks
The implementation of this method currently is to buffer the entire content of the stream into memory before deserializing.
This is for simplicity and perf reasons.
Callers should only provide streams that are known to be small enough to fit in memory and contain only msgpack content.
DeserializeObject(ReadOnlyMemory<byte>, ITypeShape, CancellationToken)
Deserializes an untyped value.
public object? DeserializeObject(ReadOnlyMemory<byte> buffer, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<byte>shapeITypeShapeThe shape of the value to deserialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- object
The deserialized value.
Examples
See the SerializeObject(ref MessagePackWriter, object?, ITypeShape, CancellationToken) method for an example of using this method.
DeserializeObjectAsync(PipeReader, ITypeShape, CancellationToken)
Deserializes a value from a PipeReader.
public ValueTask<object?> DeserializeObjectAsync(PipeReader reader, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe reader to deserialize from. Complete(Exception) is not called on this at the conclusion of deserialization, and the reader is left at the position after the last msgpack byte read.
shapeITypeShapeThe shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationTokenA cancellation token.
Returns
DeserializeObjectAsync(Stream, ITypeShape, CancellationToken)
Deserializes a value from a Stream.
public ValueTask<object?> DeserializeObjectAsync(Stream stream, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
shapeITypeShape- The shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationToken- A cancellation token.
Returns
DeserializePath<T, TElement>(ref MessagePackReader, ITypeShape<T>, in DeserializePathOptions<T, TElement>, CancellationToken)
public TElement? DeserializePath<T, TElement>(ref MessagePackReader reader, ITypeShape<T> shape, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default)
Parameters
readerMessagePackReadershapeITypeShape<T>optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElement
DeserializePath<T, TElement>(scoped in ReadOnlySequence<byte>, in DeserializePathOptions<T, TElement>, CancellationToken)
[ExcludeFromCodeCoverage]
public TElement? DeserializePath<T, TElement>(scoped in ReadOnlySequence<byte> bytes, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
bytesReadOnlySequence<byte>optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElement
DeserializePath<T, TElement>(scoped in ReadOnlySequence<byte>, ITypeShape<T>, in DeserializePathOptions<T, TElement>, CancellationToken)
public TElement? DeserializePath<T, TElement>(scoped in ReadOnlySequence<byte> buffer, ITypeShape<T> shape, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlySequence<byte>The msgpack to deserialize from.
shapeITypeShape<T>optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElement
DeserializePath<T, TElement>(Stream, in DeserializePathOptions<T, TElement>, CancellationToken)
[ExcludeFromCodeCoverage]
public TElement? DeserializePath<T, TElement>(Stream stream, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElement
Remarks
The implementation of this method currently is to buffer the entire content of the stream into memory before deserializing.
This is for simplicity and perf reasons.
Callers should only provide streams that are known to be small enough to fit in memory and contain only msgpack content.
DeserializePath<T, TElement>(Stream, ITypeShape<T>, in DeserializePathOptions<T, TElement>, CancellationToken)
public TElement? DeserializePath<T, TElement>(Stream stream, ITypeShape<T> shape, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
shapeITypeShape<T>optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElement
Remarks
The implementation of this method currently is to buffer the entire content of the stream into memory before deserializing.
This is for simplicity and perf reasons.
Callers should only provide streams that are known to be small enough to fit in memory and contain only msgpack content.
DeserializePath<T, TElement>(ReadOnlyMemory<byte>, in DeserializePathOptions<T, TElement>, CancellationToken)
[ExcludeFromCodeCoverage]
public TElement? DeserializePath<T, TElement>(ReadOnlyMemory<byte> bytes, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
bytesReadOnlyMemory<byte>optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElement
DeserializePath<T, TElement>(ReadOnlyMemory<byte>, ITypeShape<T>, in DeserializePathOptions<T, TElement>, CancellationToken)
public TElement? DeserializePath<T, TElement>(ReadOnlyMemory<byte> buffer, ITypeShape<T> shape, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<byte>The msgpack to deserialize from.
shapeITypeShape<T>optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElement
DeserializePath<T, TElement, TProvider>(scoped in ReadOnlySequence<byte>, in DeserializePathOptions<T, TElement>, CancellationToken)
[ExcludeFromCodeCoverage]
public TElement? DeserializePath<T, TElement, TProvider>(scoped in ReadOnlySequence<byte> bytes, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
bytesReadOnlySequence<byte>optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElementTProvider
DeserializePath<T, TElement, TProvider>(Stream, in DeserializePathOptions<T, TElement>, CancellationToken)
[ExcludeFromCodeCoverage]
public TElement? DeserializePath<T, TElement, TProvider>(Stream stream, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElementTProvider
Remarks
The implementation of this method currently is to buffer the entire content of the stream into memory before deserializing.
This is for simplicity and perf reasons.
Callers should only provide streams that are known to be small enough to fit in memory and contain only msgpack content.
DeserializePath<T, TElement, TProvider>(ReadOnlyMemory<byte>, in DeserializePathOptions<T, TElement>, CancellationToken)
[ExcludeFromCodeCoverage]
public TElement? DeserializePath<T, TElement, TProvider>(ReadOnlyMemory<byte> bytes, in MessagePackSerializer.DeserializePathOptions<T, TElement> options, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
bytesReadOnlyMemory<byte>optionsMessagePackSerializer.DeserializePathOptions<T, TElement>cancellationTokenCancellationToken
Returns
- TElement
Type Parameters
TTElementTProvider
DeserializePrimitives(ref MessagePackReader, CancellationToken)
Deserializes msgpack into primitive values, maps and arrays.
public object? DeserializePrimitives(ref MessagePackReader reader, CancellationToken cancellationToken = default)
Parameters
readerMessagePackReaderThe source of msgpack to decode.
cancellationTokenCancellationTokenA cancellation token.
Returns
- object
A null, string, long, ulong, float, double, DateTime, IReadOnlyDictionary<TKey, TValue> with object keys and values, an array of objects, or an Extension.
Examples
The following snippet demonstrates a way to use this method.
MessagePackReader reader = new(msgpack);
object? deserialized = serializer.DeserializePrimitives(ref reader);
var map = (IReadOnlyDictionary<object, object?>)deserialized!;
string prop1 = (string)map!["Prop1"]!;
int prop2 = (int)map!["Prop2"]!;
bool deeperBool = (bool)((IReadOnlyDictionary<object, object?>)map["deeper"]!)["IsAdult"]!;
int age = (int)((IReadOnlyDictionary<object, object?>)((IReadOnlyDictionary<object, object?>)map["People"]!)[3]!)["Age"]!;
// Did we miss anything? Dump out all the keys and their values.
foreach (KeyValuePair<object, object?> pair in map)
{
Console.WriteLine($"{pair.Key}: {pair.Value}");
}
Remarks
Maps implement IReadOnlyDictionary<TKey, TValue> with object keys and values.
Keep in mind when using integers that msgpack doesn't preserve integer length information. This method deserializes all integers as ulong (or long if the value is negative). Integer length stretching is automatically applied when using integers as keys in maps so that they can match.
All arrays are instances of object?[].
Msgpack binary data is represented as a byte[] object.
Msgpack extensions are represented by Extension values.
Deserialize<T>(ref MessagePackReader, ITypeShapeProvider, CancellationToken)
Deserializes a value.
public T? Deserialize<T>(ref MessagePackReader reader, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
readerMessagePackReaderThe msgpack reader to deserialize from.
providerITypeShapeProviderThe shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(ref MessagePackReader, ITypeShape<T>, CancellationToken)
Deserializes a value.
public T? Deserialize<T>(ref MessagePackReader reader, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
readerMessagePackReaderThe msgpack reader to deserialize from.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(scoped in ReadOnlySequence<byte>, ITypeShapeProvider, CancellationToken)
Deserializes a value.
public T? Deserialize<T>(scoped in ReadOnlySequence<byte> buffer, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlySequence<byte>The msgpack to deserialize from.
providerITypeShapeProviderThe shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(scoped in ReadOnlySequence<byte>, ITypeShape<T>, CancellationToken)
Deserializes a value.
public T? Deserialize<T>(scoped in ReadOnlySequence<byte> buffer, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlySequence<byte>The msgpack to deserialize from.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(scoped in ReadOnlySequence<byte>, CancellationToken)
Deserializes a value.
[ExcludeFromCodeCoverage]
public T? Deserialize<T>(scoped in ReadOnlySequence<byte> bytes, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
bytesReadOnlySequence<byte>cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(Stream, ITypeShapeProvider, CancellationToken)
Deserializes a value.
public T? Deserialize<T>(Stream stream, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
providerITypeShapeProviderThe shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Remarks
The implementation of this method currently is to buffer the entire content of the stream into memory before deserializing.
This is for simplicity and perf reasons.
Callers should only provide streams that are known to be small enough to fit in memory and contain only msgpack content.
Deserialize<T>(Stream, ITypeShape<T>, CancellationToken)
Deserializes a value.
public T? Deserialize<T>(Stream stream, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Remarks
The implementation of this method currently is to buffer the entire content of the stream into memory before deserializing.
This is for simplicity and perf reasons.
Callers should only provide streams that are known to be small enough to fit in memory and contain only msgpack content.
Deserialize<T>(Stream, CancellationToken)
Deserializes a value.
[ExcludeFromCodeCoverage]
public T? Deserialize<T>(Stream stream, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Remarks
The implementation of this method currently is to buffer the entire content of the stream into memory before deserializing.
This is for simplicity and perf reasons.
Callers should only provide streams that are known to be small enough to fit in memory and contain only msgpack content.
Deserialize<T>(ReadOnlyMemory<byte>, ITypeShapeProvider, CancellationToken)
Deserializes a value.
public T? Deserialize<T>(ReadOnlyMemory<byte> buffer, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<byte>The msgpack to deserialize from.
providerITypeShapeProviderThe shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(ReadOnlyMemory<byte>, ITypeShape<T>, CancellationToken)
Deserializes a value.
public T? Deserialize<T>(ReadOnlyMemory<byte> buffer, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<byte>The msgpack to deserialize from.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(ReadOnlyMemory<byte>, CancellationToken)
Deserializes a value.
[ExcludeFromCodeCoverage]
public T? Deserialize<T>(ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
bytesReadOnlyMemory<byte>cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T, TProvider>(scoped in ReadOnlySequence<byte>, CancellationToken)
Deserializes a value.
[ExcludeFromCodeCoverage]
public T? Deserialize<T, TProvider>(scoped in ReadOnlySequence<byte> bytes, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
bytesReadOnlySequence<byte>cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
TProvider
Deserialize<T, TProvider>(Stream, CancellationToken)
Deserializes a value.
[ExcludeFromCodeCoverage]
public T? Deserialize<T, TProvider>(Stream stream, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe stream to deserialize from. If this stream contains more than one top-level msgpack structure, it may be positioned beyond its end after deserialization due to buffering.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
TProvider
Remarks
The implementation of this method currently is to buffer the entire content of the stream into memory before deserializing.
This is for simplicity and perf reasons.
Callers should only provide streams that are known to be small enough to fit in memory and contain only msgpack content.
Deserialize<T, TProvider>(ReadOnlyMemory<byte>, CancellationToken)
Deserializes a value.
[ExcludeFromCodeCoverage]
public T? Deserialize<T, TProvider>(ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
bytesReadOnlyMemory<byte>cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type of value to deserialize.
TProvider
GetJsonSchema(ITypeShape)
Creates a JSON Schema that describes the msgpack serialization of the given type's shape.
public JsonObject GetJsonSchema(ITypeShape typeShape)
Parameters
typeShapeITypeShapeThe shape of the type.
Returns
- JsonObject
The JSON Schema document.
GetJsonSchema<T>()
public JsonObject GetJsonSchema<T>() where T : IShapeable<T>
Returns
- JsonObject
The JSON Schema document.
Type Parameters
TThe self-describing type whose schema should be produced.
Remarks
This overload should only be used when T is decorated with a GenerateShapeAttribute.
For non-decorated types, apply GenerateShapeForAttribute<T> to a witness type and call GetJsonSchema<T, TProvider>(MessagePackSerializer) instead,
or use GetJsonSchema<T>(ITypeShapeProvider) for an option that does not require source generation.
Exceptions
- NotSupportedException
Thrown if
Thas no type shape created via the GenerateShapeAttribute source generator.
GetJsonSchema<T>(ITypeShapeProvider)
[SuppressMessage("ApiDesign", "RS0030:Do not use banned APIs", Justification = "Public API accepts an ITypeShapeProvider.")]
public JsonObject GetJsonSchema<T>(ITypeShapeProvider provider)
Parameters
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance.
Returns
- JsonObject
The JSON Schema document.
Type Parameters
TThe type whose schema should be produced.
GetJsonSchema<T, TProvider>()
public JsonObject GetJsonSchema<T, TProvider>() where TProvider : IShapeable<T>
Returns
- JsonObject
The JSON Schema document.
Type Parameters
TThe type whose schema should be produced.
TProviderThe witness type that provides the shape for
T.
Remarks
This overload should only be used when TProvider is decorated with a GenerateShapeForAttribute<T>.
Use GetJsonSchema<T>(ITypeShapeProvider) for an option that does not require source generation.
Exceptions
- NotSupportedException
Thrown if
TProviderhas no GenerateShapeForAttribute<T> source generator attribute forT.
SerializeAsync<T>(PipeWriter, T?, ITypeShapeProvider, CancellationToken)
Serializes a value using the given PipeWriter.
public ValueTask SerializeAsync<T>(PipeWriter writer, T? value, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
writerPipeWriterThe writer to use.
valueTThe value to serialize.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
TThe type to be serialized.
SerializeAsync<T>(PipeWriter, T?, ITypeShape<T>, CancellationToken)
Serializes a value using the given PipeWriter.
public ValueTask SerializeAsync<T>(PipeWriter writer, T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
writerPipeWriterThe writer to use.
valueTThe value to serialize.
shapeITypeShape<T>The shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
TThe type to be serialized.
SerializeAsync<T>(PipeWriter, in T?, CancellationToken)
Serializes a value using the given PipeWriter.
[ExcludeFromCodeCoverage]
public ValueTask SerializeAsync<T>(PipeWriter writer, in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
writerPipeWriterThe writer to use.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
TThe type to be serialized.
SerializeAsync<T>(Stream, T?, ITypeShapeProvider, CancellationToken)
Serializes a value to a Stream.
public ValueTask SerializeAsync<T>(Stream stream, T? value, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write to.
valueT- The value to serialize.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
TThe type to be serialized.
SerializeAsync<T>(Stream, T?, ITypeShape<T>, CancellationToken)
Serializes a value to a Stream.
public ValueTask SerializeAsync<T>(Stream stream, T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write to.
valueT- The value to serialize.
shapeITypeShape<T>- The shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
TThe type to be serialized.
SerializeAsync<T>(Stream, in T?, CancellationToken)
Serializes a value to a Stream.
[ExcludeFromCodeCoverage]
public ValueTask SerializeAsync<T>(Stream stream, in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to write to.
valueT- The value to serialize.
cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
TThe type to be serialized.
SerializeAsync<T, TProvider>(PipeWriter, in T?, CancellationToken)
Serializes a value using the given PipeWriter.
[ExcludeFromCodeCoverage]
public ValueTask SerializeAsync<T, TProvider>(PipeWriter writer, in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
writerPipeWriterThe writer to use.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
TThe type to be serialized.
TProvider
SerializeAsync<T, TProvider>(Stream, in T?, CancellationToken)
Serializes a value to a Stream.
[ExcludeFromCodeCoverage]
public ValueTask SerializeAsync<T, TProvider>(Stream stream, in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe stream to write to.
valueT- The value to serialize.
cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
TThe type to be serialized. TProvider
SerializeObject(ref MessagePackWriter, object?, ITypeShape, CancellationToken)
Serializes an untyped value.
public void SerializeObject(ref MessagePackWriter writer, object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
writerMessagePackWriterThe msgpack writer to use.
valueobjectThe value to serialize.
shapeITypeShapeThe shape of the value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Examples
The following snippet demonstrates a way to use this method.
// Possible implementations of ITypeShapeProvider instances.
static readonly ITypeShapeProvider ReflectionBased = ReflectionTypeShapeProvider.Default;
static readonly ITypeShapeProvider SourceGenerated = Witness.GeneratedTypeShapeProvider;
object TypelessSerializeRoundtrip(object value, ITypeShapeProvider provider)
{
// Acquire the type's shape. For this sample, we'll use the runtime type of the object.
ITypeShape shape = provider.GetTypeShape(value.GetType()) ?? throw new ArgumentException("No shape available for runtime type", nameof(provider));
MessagePackSerializer serializer = new();
Sequence<byte> sequence = new();
// Serialize the object.
MessagePackWriter writer = new(sequence);
serializer.SerializeObject(ref writer, value, shape);
writer.Flush();
// Deserialize the object.
MessagePackReader reader = new(sequence);
return serializer.DeserializeObject(ref reader, shape)!;
}
[GenerateShapeFor<Fruit>]
partial class Witness;
[GenerateShape]
internal partial record Fruit(int Seeds);
SerializeObject(IBufferWriter<byte>, object?, ITypeShape, CancellationToken)
Serializes an untyped value.
public void SerializeObject(IBufferWriter<byte> writer, object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
writerIBufferWriter<byte>The msgpack writer to use.
valueobjectThe value to serialize.
shapeITypeShapeThe shape of the value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Examples
The following snippet demonstrates a way to use this method.
// Possible implementations of ITypeShapeProvider instances.
static readonly ITypeShapeProvider ReflectionBased = ReflectionTypeShapeProvider.Default;
static readonly ITypeShapeProvider SourceGenerated = Witness.GeneratedTypeShapeProvider;
object TypelessSerializeRoundtrip(object value, ITypeShapeProvider provider)
{
// Acquire the type's shape. For this sample, we'll use the runtime type of the object.
ITypeShape shape = provider.GetTypeShape(value.GetType()) ?? throw new ArgumentException("No shape available for runtime type", nameof(provider));
MessagePackSerializer serializer = new();
Sequence<byte> sequence = new();
// Serialize the object.
MessagePackWriter writer = new(sequence);
serializer.SerializeObject(ref writer, value, shape);
writer.Flush();
// Deserialize the object.
MessagePackReader reader = new(sequence);
return serializer.DeserializeObject(ref reader, shape)!;
}
[GenerateShapeFor<Fruit>]
partial class Witness;
[GenerateShape]
internal partial record Fruit(int Seeds);
SerializeObject(Stream, object?, ITypeShape, CancellationToken)
Serializes an untyped value.
public void SerializeObject(Stream stream, object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write to.
valueobjectThe value to serialize.
shapeITypeShapeThe shape of the value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Examples
The following snippet demonstrates a way to use this method.
// Possible implementations of ITypeShapeProvider instances.
static readonly ITypeShapeProvider ReflectionBased = ReflectionTypeShapeProvider.Default;
static readonly ITypeShapeProvider SourceGenerated = Witness.GeneratedTypeShapeProvider;
object TypelessSerializeRoundtrip(object value, ITypeShapeProvider provider)
{
// Acquire the type's shape. For this sample, we'll use the runtime type of the object.
ITypeShape shape = provider.GetTypeShape(value.GetType()) ?? throw new ArgumentException("No shape available for runtime type", nameof(provider));
MessagePackSerializer serializer = new();
Sequence<byte> sequence = new();
// Serialize the object.
MessagePackWriter writer = new(sequence);
serializer.SerializeObject(ref writer, value, shape);
writer.Flush();
// Deserialize the object.
MessagePackReader reader = new(sequence);
return serializer.DeserializeObject(ref reader, shape)!;
}
[GenerateShapeFor<Fruit>]
partial class Witness;
[GenerateShape]
internal partial record Fruit(int Seeds);
SerializeObject(object?, ITypeShape, CancellationToken)
Serializes an untyped value.
public byte[] SerializeObject(object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
valueobjectThe value to serialize.
shapeITypeShapeThe shape of the value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- byte[]
A byte array containing the serialized msgpack.
Examples
The following snippet demonstrates a way to use this method.
// Possible implementations of ITypeShapeProvider instances.
static readonly ITypeShapeProvider ReflectionBased = ReflectionTypeShapeProvider.Default;
static readonly ITypeShapeProvider SourceGenerated = Witness.GeneratedTypeShapeProvider;
object TypelessSerializeRoundtrip(object value, ITypeShapeProvider provider)
{
// Acquire the type's shape. For this sample, we'll use the runtime type of the object.
ITypeShape shape = provider.GetTypeShape(value.GetType()) ?? throw new ArgumentException("No shape available for runtime type", nameof(provider));
MessagePackSerializer serializer = new();
Sequence<byte> sequence = new();
// Serialize the object.
MessagePackWriter writer = new(sequence);
serializer.SerializeObject(ref writer, value, shape);
writer.Flush();
// Deserialize the object.
MessagePackReader reader = new(sequence);
return serializer.DeserializeObject(ref reader, shape)!;
}
[GenerateShapeFor<Fruit>]
partial class Witness;
[GenerateShape]
internal partial record Fruit(int Seeds);
SerializeObjectAsync(PipeWriter, object?, ITypeShape, CancellationToken)
Serializes a value using the given PipeWriter.
public ValueTask SerializeObjectAsync(PipeWriter writer, object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
writerPipeWriterThe writer to use.
valueobjectThe value to serialize.
shapeITypeShapeThe shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
SerializeObjectAsync(Stream, object?, ITypeShape, CancellationToken)
Serializes a value to a Stream.
public ValueTask SerializeObjectAsync(Stream stream, object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write to.
valueobject- The value to serialize.
shapeITypeShape- The shape of the type, as obtained from an ITypeShapeProvider.
cancellationTokenCancellationToken- A cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Serialize<T>(ref MessagePackWriter, in T?, ITypeShapeProvider, CancellationToken)
Serializes a value.
public void Serialize<T>(ref MessagePackWriter writer, in T? value, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
writerMessagePackWriterThe msgpack writer to use.
valueTThe value to serialize.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
Serialize<T>(ref MessagePackWriter, in T?, ITypeShape<T>, CancellationToken)
Serializes a value.
public void Serialize<T>(ref MessagePackWriter writer, in T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
writerMessagePackWriterThe msgpack writer to use.
valueTThe value to serialize.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
Serialize<T>(IBufferWriter<byte>, in T?, ITypeShapeProvider, CancellationToken)
Serializes a value.
public void Serialize<T>(IBufferWriter<byte> writer, in T? value, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
writerIBufferWriter<byte>The msgpack writer to use.
valueTThe value to serialize.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
Serialize<T>(IBufferWriter<byte>, in T?, ITypeShape<T>, CancellationToken)
Serializes a value.
public void Serialize<T>(IBufferWriter<byte> writer, in T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
writerIBufferWriter<byte>The msgpack writer to use.
valueTThe value to serialize.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
Serialize<T>(IBufferWriter<byte>, in T?, CancellationToken)
Serializes a value.
[ExcludeFromCodeCoverage]
public void Serialize<T>(IBufferWriter<byte> writer, in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
writerIBufferWriter<byte>The msgpack writer to use.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
Serialize<T>(Stream, in T?, ITypeShapeProvider, CancellationToken)
Serializes a value.
public void Serialize<T>(Stream stream, in T? value, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write to.
valueTThe value to serialize.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
Serialize<T>(Stream, in T?, ITypeShape<T>, CancellationToken)
Serializes a value.
public void Serialize<T>(Stream stream, in T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write to.
valueTThe value to serialize.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
Serialize<T>(Stream, in T?, CancellationToken)
Serializes a value.
[ExcludeFromCodeCoverage]
public void Serialize<T>(Stream stream, in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to write to.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
Serialize<T>(in T?, ITypeShapeProvider, CancellationToken)
Serializes a value.
public byte[] Serialize<T>(in T? value, ITypeShapeProvider provider, CancellationToken cancellationToken = default)
Parameters
valueTThe value to serialize.
providerITypeShapeProvider-
The shape provider of
T. This might be Default to use reflection-based shapes. It might also be the value of theGeneratedTypeShapeProviderstatic property on a witness class (a class on which GenerateShapeForAttribute<T> has been applied), although for source generated shapes, overloads that do not take an ITypeShapeProvider offer better performance. cancellationTokenCancellationTokenA cancellation token.
Returns
- byte[]
A byte array containing the serialized msgpack.
Type Parameters
TThe type to be serialized.
Serialize<T>(in T?, ITypeShape<T>, CancellationToken)
Serializes a value.
public byte[] Serialize<T>(in T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
valueTThe value to serialize.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- byte[]
A byte array containing the serialized msgpack.
Type Parameters
TThe type to be serialized.
Serialize<T>(in T?, CancellationToken)
Serializes a value.
[ExcludeFromCodeCoverage]
public byte[] Serialize<T>(in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- byte[]
A byte array containing the serialized msgpack.
Type Parameters
TThe type to be serialized.
Serialize<T, TProvider>(IBufferWriter<byte>, in T?, CancellationToken)
Serializes a value.
[ExcludeFromCodeCoverage]
public void Serialize<T, TProvider>(IBufferWriter<byte> writer, in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
writerIBufferWriter<byte>The msgpack writer to use.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
TProvider
Serialize<T, TProvider>(Stream, in T?, CancellationToken)
Serializes a value.
[ExcludeFromCodeCoverage]
public void Serialize<T, TProvider>(Stream stream, in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe stream to write to.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type to be serialized.
TProvider
Serialize<T, TProvider>(in T?, CancellationToken)
Serializes a value.
[ExcludeFromCodeCoverage]
public byte[] Serialize<T, TProvider>(in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- byte[]
A byte array containing the serialized msgpack.
Type Parameters
TThe type to be serialized.
TProvider