Table of Contents

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

T

A composite value object (derives from ValueObject) with a public static TryCreate factory.

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

reader Utf8JsonReader

The reader.

typeToConvert Type

The type to convert.

options JsonSerializerOptions

An 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

writer Utf8JsonWriter

The writer to write to.

value T

The value to convert to JSON.

options JsonSerializerOptions

An object that specifies serialization options to use.