Class PrimitivesAsObjectConverter
- Namespace
- Nerdbank.MessagePack.Converters
- Assembly
- Nerdbank.MessagePack.dll
A converter that can write msgpack primitives or other convertible values typed as object and reads everything into primitives, dictionaries and arrays.
public class PrimitivesAsObjectConverter : MessagePackConverter<object?>
- Inheritance
-
PrimitivesAsObjectConverter
- Inherited Members
Remarks
This converter is not included by default because untyped serialization is not generally desirable. But it is offered as a converter that may be added to Converters in order to enable limited untyped serialization.
Fields
Instance
Gets the default instance of the converter.
public static readonly PrimitivesAsObjectConverter Instance
Field Value
Methods
GetJsonSchema(JsonSchemaContext, ITypeShape)
Gets the JSON schema that resembles the data structure that this converter can serialize and deserialize.
public override JsonObject? GetJsonSchema(JsonSchemaContext context, ITypeShape typeShape)
Parameters
context
JsonSchemaContextA means to obtain schema fragments for inclusion when your converter delegates to other converters.
typeShape
ITypeShapeThe shape of the type object, in case it provides useful metadata for constructing the schema.
Returns
- JsonObject
The fragment of JSON schema that describes the value written by this converter, or null if this method has not been overridden.
Remarks
Implementations should return a new instance of JsonObject that represents the JSON schema fragment for every caller. A shared instance may be used to call DeepClone() and the result returned.
The type
property should not include "null" on account of the converted type being a reference type.
It is the consumer of the object that will determine whether null is an acceptable value of the object.
Custom converters that do not override this method will lead to a JSON schema that does not describe the written data, and allows any data as input.
If the converter delegates to other converters, the schemas for those sub-values can be obtained for inclusion in the returned schema
by calling GetJsonSchema(ITypeShape) on the context
.
- See Also
Read(ref MessagePackReader, SerializationContext)
Reads any one msgpack structure.
public override object? Read(ref MessagePackReader reader, SerializationContext context)
Parameters
reader
MessagePackReaderThe msgpack reader.
context
SerializationContextThe serialization context.
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.
Remarks
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. Maps also 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.
Exceptions
- NotSupportedException
Thrown if a msgpack map includes a null key.
Write(ref MessagePackWriter, in object?, SerializationContext)
Serializes an instance of object.
public override void Write(ref MessagePackWriter writer, in object? value, SerializationContext context)
Parameters
writer
MessagePackWriterThe writer to use.
value
objectThe value to serialize.
context
SerializationContextContext for the serialization.
Remarks
Implementations of this method should not flush the writer.
Exceptions
- NotSupportedException
Always thrown.