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

Class ValueObjectJsonConverter<TSelf, TValue>

Namespace: AdCodicem.ValueObjects.Json
Assembly: AdCodicem.ValueObjects.Json.dll

Serializes a value object as its bare underlying value, delegating the underlying value to System.Text.Json itself.

public sealed class ValueObjectJsonConverter<TSelf, TValue> : JsonConverter<TSelf> where TSelf : struct, IValueObject<TSelf, TValue>

Type Parameters​

TSelf

Value object type.

TValue

Underlying value type.

Inheritance​

object ← JsonConverter ← JsonConverter<TSelf> ← ValueObjectJsonConverter<TSelf, TValue>

Inherited Members​

JsonConverter<TSelf>.CanConvert(Type), JsonConverter<TSelf>.Read(ref Utf8JsonReader, Type, JsonSerializerOptions), JsonConverter<TSelf>.ReadAsPropertyName(ref Utf8JsonReader, Type, JsonSerializerOptions), JsonConverter<TSelf>.Write(Utf8JsonWriter, TSelf, JsonSerializerOptions), JsonConverter<TSelf>.WriteAsPropertyName(Utf8JsonWriter, TSelf, JsonSerializerOptions), JsonConverter<TSelf>.HandleNull, JsonConverter<TSelf>.Type, JsonConverter.CanConvert(Type), JsonConverter.Type, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

This is the general-purpose converter, used for value objects written by hand. A generated value object carries its own converter, which writes the value directly instead of going back through the serializer, and is the one the factory hands out whenever it is available.

The underlying value is read and written through the contract the options hold for TValue (), so the converter needs neither reflection nor dynamic code of its own: a source-generated context listing the value object holds that contract too, since the value object exposes its value as a property. Called directly, it takes options, never null, which it refuses with an ; options no serializer has used and that set no resolver are given the serializer's default one and locked, as serializing through them would lock them, so that setting one of their properties afterwards throws an , whichever member was called.

A value or a key the value object rejects, and a value to write that it rejects, are refused with a carrying the code of the rule, as the generated converter refuses them. The underlying value itself is read by System.Text.Json, whose own exception, for a token that is not one of TValue, carries no code.

Methods​

Read(ref Utf8JsonReader, Type, JsonSerializerOptions)​

Reads and converts the JSON to type T.

public override TSelf Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

Parameters​

reader Utf8JsonReader

The reader.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

An object that specifies serialization options to use.

Returns​

TSelf

The converted value.

ReadAsPropertyName(ref Utf8JsonReader, Type, JsonSerializerOptions)​

Reads a dictionary key from a JSON property name.

public override TSelf ReadAsPropertyName(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)

Parameters​

reader Utf8JsonReader

The to read from.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

The options to use when reading the value.

Returns​

TSelf

The value that was converted.

Remarks​

The key is read as System.Text.Json reads a key of TValue, then validated through TryCreate, as a value is: the reverse of , whatever text the value object's own parser reads. A key that is not one of TValue is refused as it is in a dictionary of its own, and a key the value object rejects with a naming the rule and carrying its code.

Write(Utf8JsonWriter, TSelf, JsonSerializerOptions)​

Writes a specified value as JSON.

public override void Write(Utf8JsonWriter writer, TSelf value, JsonSerializerOptions options)

Parameters​

writer Utf8JsonWriter

The writer to write to.

value TSelf

The value to convert to JSON.

options JsonSerializerOptions

An object that specifies serialization options to use.

Remarks​

An instance equal to the default whose value the value object rejects is refused with a naming the rule and carrying its code, as the converter the generator emits refuses it, rather than written for a reader to refuse.

WriteAsPropertyName(Utf8JsonWriter, TSelf, JsonSerializerOptions)​

Writes a dictionary key as a JSON property name.

public override void WriteAsPropertyName(Utf8JsonWriter writer, TSelf value, JsonSerializerOptions options)

Parameters​

writer Utf8JsonWriter

The to write to.

value TSelf

The value to convert. The value of determines if the converter handles null values.

options JsonSerializerOptions

The options to use when writing the value.

Remarks​

The key is the underlying value, written as System.Text.Json writes a key of TValue: the form the value itself travels in. The value object's own formatting, which may print something its parser does not read, never reaches the wire. An underlying type System.Text.Json cannot write as a key is refused as it is in a dictionary of its own, with a , and a key the value object rejects, as refuses a value, with a naming the rule.