Class ScalarValueJsonConverterBase<TResult, TValue, TPrimitive>
- Namespace
- Trellis.Asp.Validation
- Assembly
- Trellis.Asp.dll
Base JSON converter for scalar value objects that collects validation errors instead of throwing exceptions during deserialization.
public abstract class ScalarValueJsonConverterBase<TResult, TValue, TPrimitive> : JsonConverter<TResult> where TValue : class, IScalarValue<TValue, TPrimitive> where TPrimitive : IComparable
Type Parameters
TResultThe result type of deserialization (e.g.,
TValue?orMaybe<TValue>).TValueThe scalar value object type.
TPrimitiveThe underlying primitive type.
- Inheritance
-
JsonConverter<TResult>ScalarValueJsonConverterBase<TResult, TValue, TPrimitive>
- Derived
- Inherited Members
- Extension Methods
Properties
HandleNull
Tells System.Text.Json to call Read(ref Utf8JsonReader, Type, JsonSerializerOptions) even when the JSON
token is null. Without this, the serializer bypasses the converter for null tokens
on reference-type results, preventing OnNullToken(string) from firing.
public override bool HandleNull { get; }
Property Value
Methods
GetDefaultFieldName()
Gets the default field name derived from the value object type name.
protected static string GetDefaultFieldName()
Returns
OnNullToken(string)
Returns the result when a JSON null token is read.
protected abstract TResult OnNullToken(string fieldName)
Parameters
fieldNamestringThe resolved field name for error reporting.
Returns
- TResult
OnValidationFailure()
Returns the result when validation fails.
protected abstract TResult OnValidationFailure()
Returns
- TResult
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type T.
[UnconditionalSuppressMessage("Trimming", "IL2026", Justification = "TPrimitive type parameter is preserved by JSON serialization infrastructure")]
[UnconditionalSuppressMessage("AOT", "IL3050", Justification = "JSON deserialization of primitive types is compatible with AOT")]
public override TResult Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- TResult
The converted value.
WrapSuccess(TValue)
Wraps a successfully validated value object into the result type.
protected abstract TResult WrapSuccess(TValue value)
Parameters
valueTValue
Returns
- TResult