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

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.

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 = "Only reached for a value object that registered no converter, which never happens for a generated one that is not generic: its registration carries its converter, served from the registry, statically. A hand-written value object, or a construction of a generic one, combined with trimming or native AOT has to be registered with its converter through ValueObjectRegistry.Register.")]
[UnconditionalSuppressMessage("Trimming", "IL2055", Justification = "Only reached for a value object that registered no converter, which never happens for a generated one that is not generic: its registration carries its converter, served from the registry, statically. A hand-written value object, or a construction of a generic one, combined with trimming or native AOT has to be registered with its converter through ValueObjectRegistry.Register.")]
[UnconditionalSuppressMessage("Trimming", "IL2071", Justification = "Only reached for a value object that registered no converter, which never happens for a generated one that is not generic: its registration carries its converter, served from the registry, statically. A hand-written value object, or a construction of a generic one, combined with trimming or native AOT has to be registered with its converter through ValueObjectRegistry.Register.")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Only reached for a value object that registered no converter, which never happens for a generated one that is not generic: its registration carries its converter, served from the registry, statically. A hand-written value object, or a construction of a generic one, combined with trimming or native AOT has to be registered with its converter through ValueObjectRegistry.Register.")]
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.