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

Class ValueObjectJsonConverterFactory

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

Supplies the converter of any value object, so that a value object always serializes as its bare underlying value.

public sealed class ValueObjectJsonConverterFactory : JsonConverterFactory

Inheritance​

object ← JsonConverter ← JsonConverterFactory ← ValueObjectJsonConverterFactory

Inherited Members​

JsonConverterFactory.CreateConverter(Type, JsonSerializerOptions), JsonConverterFactory.Type, JsonConverter.CanConvert(Type), JsonConverter.Type, object.Equals(object?), object.Equals(object?, object?), object.GetHashCode(), object.GetType(), object.ReferenceEquals(object?, object?), object.ToString()

Remarks​

A generated value object already carries its own [JsonConverter] and needs nothing from this factory when the serializer resolves types by reflection. The factory exists for the two cases the attribute cannot reach: a JsonSerializerContext, whose generator never sees the attribute, and value objects written by hand. For the first, it hands out the converter the generator registered with the value object's descriptor, whatever the assembly declaring the value object references. For a construction of a generic value object, whose registration registers its generic definition alone, the registry closes the generated converter over the construction, by reflection, the first time it is asked for it.

A value object written by hand carries no converter, unless it was registered with one, and gets the general-purpose , which the factory closes over it through the type arguments its descriptor hands back (), at compile time rather than by reflection. Under native AOT, register it, ValueObjectRegistry.Register<Link, Uri>(Link.Schema), and list it in the context: one nothing registered is described by reflection where the runtime supports dynamic code, and refused with a naming that registration where it does not.

Register it on the context so the System.Text.Json generator picks it up:

[JsonSourceGenerationOptions(Converters = [typeof(ValueObjectJsonConverterFactory)])]
[JsonSerializable(typeof(Payment))]
internal sealed partial class PaymentContext : JsonSerializerContext;

Methods​

CanConvert(Type)​

When overridden in a derived class, determines whether the converter instance can convert the specified object type.

public override bool CanConvert(Type typeToConvert)

Parameters​

typeToConvert Type

The type of the object to check whether it can be converted by this converter instance.

Returns​

bool

true if the instance can convert the specified object type; otherwise, false.

Remarks​

Claims a value object only, never over one: a converter in the options outranks the serializer's own handling of nullable types, which wraps the converter of the value object itself once this one declines.

CreateConverter(Type, JsonSerializerOptions)​

Creates a converter for a specified type.

[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "ValueObjectRegistry.TryResolve reflects only for a value object nothing registered: one written by hand, or a construction of a generic one. A generated value object registers itself, and its converter, statically. Without dynamic code, as under native AOT, it is not called. Trimmed, register the others through ValueObjectRegistry.Register: a construction with its generated converter, a value object written by hand with none, over which the general-purpose converter is then closed at compile time, through its descriptor.")]
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)

Parameters​

typeToConvert Type

The type handled by the converter.

options JsonSerializerOptions

The serialization options to use.

Returns​

JsonConverter?

A converter for which T is compatible with typeToConvert.

Exceptions​

NotSupportedException

The type is a value object nothing registered, and the runtime does not support the dynamic code describing it by reflection takes, as under native AOT.