Class MaybePrimitiveJsonConverterFactory
- Namespace
- Trellis.Asp.Validation
- Assembly
- Trellis.Asp.dll
Factory for MaybePrimitiveJsonConverter<T> covering Maybe<T>
where T is an STJ-native primitive (string, decimal, int,
long, short, byte, double, float, bool,
Guid, DateTime, DateTimeOffset).
public sealed class MaybePrimitiveJsonConverterFactory : JsonConverterFactory
- Inheritance
-
MaybePrimitiveJsonConverterFactory
- Inherited Members
- Extension Methods
Remarks
Closes the asymmetry where MaybeScalarValueJsonConverterFactory already
supports Maybe<TScalar> (typed value objects) but raw Maybe<long>
/ Maybe<string> / etc. fell through to STJ's default object handling, which
silently serializes the Maybe<T> struct's public members (HasValue,
HasNoValue, Value) — producing JSON the converter cannot itself parse back.
The supported primitive set deliberately mirrors the
CompositeValueObjectJsonConverter<T> allowed list (in Trellis.Primitives):
the rule is "Maybe<T> works wherever T is a primitive Trellis already supports directly".
Wire shape: JSON null or absent property reads as None;
a primitive value reads as From<T>(T?). None
writes as JSON null. STJ-native primitive parsing rules apply for the underlying
value (formatting errors throw the standard JsonException with a field path).
Registered by AddScalarValueValidation() (or by the convenience helper
AddTrellisAspWithScalarValidation()) alongside the existing scalar factory.
Methods
CanConvert(Type)
True when typeToConvert is Maybe<T> and T is in
the closed primitive allowed list defined by SupportedPrimitives.
public override bool CanConvert(Type typeToConvert)
Parameters
typeToConvertType
Returns
CreateConverter(Type, JsonSerializerOptions)
Creates a MaybePrimitiveJsonConverter<T> for the supplied Maybe<T>.
[UnconditionalSuppressMessage("Trimming", "IL2055", Justification = "T is a closed-set primitive (string/decimal/int/long/short/byte/double/float/bool/Guid/DateTime/DateTimeOffset); the generic instantiation is trim-safe by construction.")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "MakeGenericType over a closed primitive set. Trellis registers this factory only when JsonSerializer.IsReflectionEnabledByDefault is true (same gate as the existing scalar factory).")]
public override JsonConverter? CreateConverter(Type typeToConvert, JsonSerializerOptions options)
Parameters
typeToConvertTypeoptionsJsonSerializerOptions