Skip to main content
Version: 0.2.x (0.2.1)

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.

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.

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: those are served from the registry, statically. A hand-written value object combined with trimming or native AOT has to supply its own converter.")]
[UnconditionalSuppressMessage("Trimming", "IL2055", Justification = "Only reached for a value object that registered no converter, which never happens for a generated one: those are served from the registry, statically. A hand-written value object combined with trimming or native AOT has to supply its own converter.")]
[UnconditionalSuppressMessage("Trimming", "IL2071", Justification = "Only reached for a value object that registered no converter, which never happens for a generated one: those are served from the registry, statically. A hand-written value object combined with trimming or native AOT has to supply its own converter.")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "Only reached for a value object that registered no converter, which never happens for a generated one: those are served from the registry, statically. A hand-written value object combined with trimming or native AOT has to supply its own converter.")]
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.