Class MaybePrimitiveJsonConverter<T>
- Namespace
- Trellis.Asp.Validation
- Assembly
- Trellis.Asp.dll
JSON converter for Maybe<T> where T is an STJ-native
primitive in the allowed list enforced by MaybePrimitiveJsonConverterFactory.
public sealed class MaybePrimitiveJsonConverter<T> : JsonConverter<Maybe<T>> where T : notnull
Type Parameters
TOne of: string, decimal, int, long, short, byte, double, float, bool, Guid, DateTime, DateTimeOffset.
- Inheritance
-
JsonConverter<Maybe<T>>MaybePrimitiveJsonConverter<T>
- Inherited Members
- Extension Methods
Remarks
- JSON
null→Maybe<T>.None(no error; absent or null are both "not provided"). - Primitive JSON value →
Maybe.From(T)using STJ's typed primitive reader. - Wrong JSON shape (e.g. number for a string field) throws the standard JsonException.
- None writes as JSON
null.
Reads and writes dispatch on the closed primitive allowed list via typed Utf8JsonReader / Utf8JsonWriter methods — no reflection, no JsonSerializer round-trip, no AOT/trim warnings.
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type Trellis.Maybe.
public override Maybe<T> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
Parameters
readerUtf8JsonReaderThe reader.
typeToConvertTypeThe type to convert.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.
Returns
- Maybe<T>
The converted value.
Write(Utf8JsonWriter, Maybe<T>, JsonSerializerOptions)
Writes a specified value as JSON.
public override void Write(Utf8JsonWriter writer, Maybe<T> value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valueMaybe<T>The value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.