Skip to main content
Version: 0.3.x (0.3.0)

Class ValueObjectFormatter<TSelf, TValue>

Namespace: AdCodicem.ValueObjects.MessagePack
Assembly: AdCodicem.ValueObjects.MessagePack.dll

Writes a value object as the bare value the options' formatter of its underlying type writes, and reads it back through the value object's rules.

public sealed class ValueObjectFormatter<TSelf, TValue> : IMessagePackFormatter<TSelf> where TSelf : struct, IValueObject<TSelf, TValue>

Type Parameters​

TSelf

Value object type.

TValue

Underlying value type.

Inheritance​

object ← ValueObjectFormatter<TSelf, TValue>

Implements​

IMessagePackFormatter<TSelf>

Inherited Members​

object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

The value goes through the formatter the options' resolver holds for TValue, so a value object writes exactly what the primitive it replaces writes under the same options: a as text under StandardResolver and as 16 bytes under NativeGuidResolver, a or a as the Native* resolvers placed in the chain write them. A formatting hook of the value object never reaches the wire.

A read is strict unless the formatter is trusted: the value read goes through TryCreate, which normalizes it, and one the value object refuses throws a naming the type and the rule, never the value, with the rule's code in under , where finds it through the exception MessagePack wraps it in. A trusted formatter reads through CreateUnchecked, for bytes the application alone wrote, a cache among them. Either way, a nil is refused with , since a value object cannot hold one: a member that can be missing is a TSelf?, which MessagePack's own NullableFormatter reads as null without asking this formatter; and a value the formatter of TValue cannot read, a value of another MessagePack type, one beyond the range of TValue, text it cannot parse, truncated bytes, with .

A write refuses, with a carrying the rule's code the same way, an uninitialized instance, equal to the default, whose value the value object rejects: validation tells a valid zero from a refused one. Any other instance went through Create, or was read by a trusted formatter, and is written as it is, without being validated again.

Constructors​

ValueObjectFormatter(bool)​

Initializes the formatter.

public ValueObjectFormatter(bool trusted = false)

Parameters​

trusted bool

Whether a read skips validation, for bytes the application alone wrote; false, the default, reads through the value object's rules.

Methods​

Deserialize(ref MessagePackReader, MessagePackSerializerOptions)​

Deserializes a value.

public TSelf Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)

Parameters​

reader MessagePackReader

The reader to deserialize from.

options MessagePackSerializerOptions

The serialization settings to use, including the resolver to use to obtain formatters for types that make up the composite type T.

Returns​

TSelf

The deserialized value.

Exceptions​

ArgumentNullException

options is null.

MessagePackSerializationException

The value read is a nil, which a value object cannot hold, a value the formatter of the underlying value cannot read, or, unless the formatter is trusted, a value the value object refuses.

FormatterNotRegisteredException

The options' resolver holds no formatter for TValue.

Serialize(ref MessagePackWriter, TSelf, MessagePackSerializerOptions)​

Serializes a value.

public void Serialize(ref MessagePackWriter writer, TSelf value, MessagePackSerializerOptions options)

Parameters​

writer MessagePackWriter

The writer to use when serializing the value.

value TSelf

The value to be serialized.

options MessagePackSerializerOptions

The serialization settings to use, including the resolver to use to obtain formatters for types that make up the composite type T.

Exceptions​

ArgumentNullException

options is null.

MessagePackSerializationException

value equals default(TSelf) and holds a value the value object rejects.

FormatterNotRegisteredException

The options' resolver holds no formatter for TValue.