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
Properties
MultiDimensionalArrayFormat
Gets the format to use when serializing multi-dimensional arrays.
public MultiDimensionalArrayFormat MultiDimensionalArrayFormat { get; init; }
Property Value
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.
StartingContext
Gets the starting context to begin (de)serializations with.
public SerializationContext StartingContext { get; init; }
Property Value
Methods
ConvertToJson(ref MessagePackReader, TextWriter)
Converts one MessagePack structure to a JSON stream.
public static void ConvertToJson(ref MessagePackReader reader, TextWriter jsonWriter)
Parameters
reader
MessagePackReaderA reader of the msgpack stream.
jsonWriter
TextWriterThe writer that will receive JSON text.
ConvertToJson(in ReadOnlySequence<byte>)
Converts a msgpack sequence into equivalent JSON.
public static string ConvertToJson(in ReadOnlySequence<byte> msgpack)
Parameters
msgpack
ReadOnlySequence<byte>The msgpack sequence.
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>)
Converts a msgpack sequence into equivalent JSON.
public static string ConvertToJson(ReadOnlyMemory<byte> msgpack)
Parameters
msgpack
ReadOnlyMemory<byte>The msgpack sequence.
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()
Creates a new serialization context that is ready to process a serialization job.
protected SerializationContext CreateSerializationContext()
Returns
- SerializationContext
The serialization context.
DeserializeAsync<T>(PipeReader, CancellationToken)
Deserializes a value from a PipeReader.
public ValueTask<T?> DeserializeAsync<T>(PipeReader reader, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
reader
PipeReaderThe reader to deserialize from.
cancellationToken
CancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
T
The type of value to deserialize.
DeserializeAsync<T, TProvider>(PipeReader, CancellationToken)
Deserializes a value from a PipeReader.
public ValueTask<T?> DeserializeAsync<T, TProvider>(PipeReader reader, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
reader
PipeReaderThe reader to deserialize from.
cancellationToken
CancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
T
The type of value to deserialize.
TProvider
The shape provider of T
. This may be the same asT
when the data type is attributed with GenerateShapeAttribute, or it may be another "witness" partial class that was annotated with GenerateShapeAttribute<T> where T for the attribute is the same as theT
used here.
Deserialize<T>(ref MessagePackReader)
Deserializes a value from a MessagePackReader.
public T? Deserialize<T>(ref MessagePackReader reader) where T : IShapeable<T>
Parameters
reader
MessagePackReaderThe msgpack reader to deserialize from.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
Deserialize<T>(ref MessagePackReader, ITypeShape<T>)
Deserializes a value from a MessagePackReader.
public T? Deserialize<T>(ref MessagePackReader reader, ITypeShape<T> shape)
Parameters
reader
MessagePackReaderThe msgpack reader to deserialize from.
shape
ITypeShape<T>The shape of
T
.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
Deserialize<T>(ReadOnlySequence<byte>)
Deserializes a value from a MessagePackReader.
public T? Deserialize<T>(ReadOnlySequence<byte> buffer) where T : IShapeable<T>
Parameters
buffer
ReadOnlySequence<byte>The msgpack to deserialize from.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
Deserialize<T>(ReadOnlySequence<byte>, ITypeShape<T>)
Deserializes a value from a MessagePackReader.
public T? Deserialize<T>(ReadOnlySequence<byte> buffer, ITypeShape<T> shape)
Parameters
buffer
ReadOnlySequence<byte>The msgpack to deserialize from.
shape
ITypeShape<T>The shape of the type to deserialize.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
Deserialize<T>(byte[])
Deserializes a value from a MessagePackReader.
public T? Deserialize<T>(byte[] buffer) where T : IShapeable<T>
Parameters
buffer
byte[]The msgpack to deserialize from.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
Deserialize<T>(byte[], ITypeShape<T>)
Deserializes a value from a MessagePackReader.
public T? Deserialize<T>(byte[] buffer, ITypeShape<T> shape)
Parameters
buffer
byte[]The msgpack to deserialize from.
shape
ITypeShape<T>The shape of the type to deserialize.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
Deserialize<T, TProvider>(ref MessagePackReader)
Deserializes a value from a MessagePackReader.
public T? Deserialize<T, TProvider>(ref MessagePackReader reader) where TProvider : IShapeable<T>
Parameters
reader
MessagePackReaderThe msgpack reader to deserialize from.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
TProvider
The shape provider of T
. This may be the same asT
when the data type is attributed with GenerateShapeAttribute, or it may be another "witness" partial class that was annotated with GenerateShapeAttribute<T> where T for the attribute is the same as theT
used here.
Deserialize<T, TProvider>(ReadOnlySequence<byte>)
Deserializes a value from a MessagePackReader.
public T? Deserialize<T, TProvider>(ReadOnlySequence<byte> buffer) where TProvider : IShapeable<T>
Parameters
buffer
ReadOnlySequence<byte>The msgpack to deserialize from.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
TProvider
The shape provider of T
. This may be the same asT
when the data type is attributed with GenerateShapeAttribute, or it may be another "witness" partial class that was annotated with GenerateShapeAttribute<T> where T for the attribute is the same as theT
used here.
Deserialize<T, TProvider>(byte[])
Deserializes a value from a MessagePackReader.
public T? Deserialize<T, TProvider>(byte[] buffer) where TProvider : IShapeable<T>
Parameters
buffer
byte[]The msgpack to deserialize from.
Returns
- T
The deserialized value.
Type Parameters
T
The type of value to deserialize.
TProvider
The shape provider of T
. This may be the same asT
when the data type is attributed with GenerateShapeAttribute, or it may be another "witness" partial class that was annotated with GenerateShapeAttribute<T> where T for the attribute is the same as theT
used here.
RegisterConverter<T>(MessagePackConverter<T>)
Registers a converter for use with this serializer.
public void RegisterConverter<T>(MessagePackConverter<T> converter)
Parameters
converter
MessagePackConverter<T>The converter.
Type Parameters
T
The convertible type.
Remarks
If a converter for the data type has already been cached, the new value takes its place. Custom converters should be registered before serializing anything on this instance of MessagePackSerializer.
SerializeAsync<T>(PipeWriter, T?, CancellationToken)
Serializes a value using the given PipeWriter.
public ValueTask SerializeAsync<T>(PipeWriter writer, T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
writer
PipeWriterThe writer to use.
value
TThe value to serialize.
cancellationToken
CancellationTokenA cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
T
The type to be serialized.
SerializeAsync<T, TProvider>(PipeWriter, T?, CancellationToken)
Serializes a value using the given PipeWriter.
public ValueTask SerializeAsync<T, TProvider>(PipeWriter writer, T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
writer
PipeWriterThe writer to use.
value
TThe value to serialize.
cancellationToken
CancellationTokenA cancellation token.
Returns
- ValueTask
A task that tracks the async serialization.
Type Parameters
T
The type to be serialized.
TProvider
The shape provider of
T
. This may be the same asT
when the data type is attributed with GenerateShapeAttribute, or it may be another "witness" partial class that was annotated with GenerateShapeAttribute<T> where T for the attribute is the same as theT
used here.
Serialize<T>(ref MessagePackWriter, in T?)
Serializes a value.
public void Serialize<T>(ref MessagePackWriter writer, in T? value) where T : IShapeable<T>
Parameters
writer
MessagePackWriterThe writer to use.
value
TThe value to serialize.
Type Parameters
T
The type to be serialized.
Serialize<T>(ref MessagePackWriter, in T?, ITypeShape<T>)
Serializes a value using the given MessagePackWriter.
public void Serialize<T>(ref MessagePackWriter writer, in T? value, ITypeShape<T> shape)
Parameters
writer
MessagePackWriterThe msgpack writer to use.
value
TThe value to serialize.
shape
ITypeShape<T>The shape of
T
.
Type Parameters
T
The type to be serialized.
Serialize<T>(IBufferWriter<byte>, in T?)
Serializes a value.
public void Serialize<T>(IBufferWriter<byte> writer, in T? value) where T : IShapeable<T>
Parameters
writer
IBufferWriter<byte>The writer to use.
value
TThe value to serialize.
Type Parameters
T
The type to be serialized.
Serialize<T>(IBufferWriter<byte>, in T?, ITypeShape<T>)
Serializes a value.
public void Serialize<T>(IBufferWriter<byte> writer, in T? value, ITypeShape<T> shape)
Parameters
writer
IBufferWriter<byte>The writer to use.
value
TThe value to serialize.
shape
ITypeShape<T>The shape of
T
.
Type Parameters
T
The type to be serialized.
Serialize<T>(in T?)
Serializes a given value to a byte array.
public byte[] Serialize<T>(in T? value) where T : IShapeable<T>
Parameters
value
TThe value to be serialized.
Returns
- byte[]
The byte array.
Type Parameters
T
The type of value to be serialized. This must be able to disclose its own shape.
Serialize<T>(in T?, ITypeShape<T>)
Serializes a given value to a byte array.
public byte[] Serialize<T>(in T? value, ITypeShape<T> shape)
Parameters
value
TThe value to be serialized.
shape
ITypeShape<T>The shape of the type.
Returns
- byte[]
The byte array.
Type Parameters
T
The type of value to be serialized.
Serialize<T, TProvider>(ref MessagePackWriter, in T?)
Serializes a value.
public void Serialize<T, TProvider>(ref MessagePackWriter writer, in T? value) where TProvider : IShapeable<T>
Parameters
writer
MessagePackWriterThe writer to use.
value
TThe value to serialize.
Type Parameters
T
The type to be serialized.
TProvider
The shape provider of
T
. This may be the same asT
when the data type is attributed with GenerateShapeAttribute, or it may be another "witness" partial class that was annotated with GenerateShapeAttribute<T> where T for the attribute is the same as theT
used here.
Serialize<T, TProvider>(IBufferWriter<byte>, in T?)
Serializes a value.
public void Serialize<T, TProvider>(IBufferWriter<byte> writer, in T? value) where TProvider : IShapeable<T>
Parameters
writer
IBufferWriter<byte>The writer to use.
value
TThe value to serialize.
Type Parameters
T
The type to be serialized.
TProvider