Skip to main content
Version: Preview (0.3.0-preview.205)

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.

[RequiresUnreferencedCode("Delegating the underlying value to the serializer needs its metadata, which trimming may remove. Generated value objects carry their own converter and do not go through this one.")]
[RequiresDynamicCode("Delegating the underlying value to the serializer may need run-time code generation. Generated value objects carry their own converter and do not go through this one.")]
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.

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.

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.

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 .