Class CompositeValueObjectJsonConverter<T>
- Namespace
- Trellis.Primitives
- Assembly
- Trellis.Primitives.dll
Convention-based JsonConverter<T> for composite value objects.
public sealed class CompositeValueObjectJsonConverter<T> : JsonConverter<T> where T : ValueObject
Type Parameters
TA composite value object (derives from ValueObject) with a public static
TryCreatefactory.
- Inheritance
-
CompositeValueObjectJsonConverter<T>
- Inherited Members
- Extension Methods
Remarks
Apply via [JsonConverter(typeof(CompositeValueObjectJsonConverter<TSelf>))] on the
value object class itself. The attribute is required: without it, ASP.NET Core model binding falls
back to default construction and silently bypasses TryCreate — inner-field validation
never runs and an invalid payload propagates into the domain layer.
For the full Domain + API JSON binding + EF Core ownership walkthrough on a multi-field VO
(ShippingAddress-style), see Cookbook Recipe 13 — Composite value object end-to-end
in docs/docfx_project/api_reference/trellis-api-cookbook.md.
Discovery convention: each public read-only instance property declared on T
becomes a JSON field (camelCase of the property name). The property's "primitive type" is the
underlying primitive of an IScalarValue<TSelf, TPrimitive> property, or the property's
own type when it is already a primitive. T must expose
public static Result<T> TryCreate(p1, ..., pN[, string? fieldName]) where the parameters
are the primitive types in the order the properties are declared.
On read, the converter populates a value array by JSON property name (case-insensitive), invokes
TryCreate, and throws TrellisJsonValidationException with the result's display
message on failure.
This converter uses reflection at first use (results are cached). Native AOT apps must root the closed converter type through JsonConverterAttribute or a source-generated context.
Methods
Read(ref Utf8JsonReader, Type, JsonSerializerOptions)
Reads and converts the JSON to type T.
public override 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
- T
The converted value.
Write(Utf8JsonWriter, T, JsonSerializerOptions)
Writes a specified value as JSON.
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
Parameters
writerUtf8JsonWriterThe writer to write to.
valueTThe value to convert to JSON.
optionsJsonSerializerOptionsAn object that specifies serialization options to use.