Class ReadModelJsonSerialization

Namespace
Cratis.Chronicle.ReadModels
Assembly
Cratis.Chronicle.dll

Serialization semantics a read model is read back with.

public static class ReadModelJsonSerialization
Inheritance
ReadModelJsonSerialization
Inherited Members

Remarks

The read-model sink omits an empty child collection rather than writing [], deliberately: a parallel replay races sibling partitions through their own events, and an unconstrained Children=[] from the root's own event would erase whatever a sibling's ChildAdded already pushed. Absence is the encoding, and it is not going to change.

What must change is what a reader does with that absence. A read model declares its child collection as a non-nullable IEnumerable<T>, so nullable-reference analysis never warns and nothing prompts a guard - and the property materializes as null anyway, on the state every one of these read models is in immediately after creation. The declared type is a promise the deserializer was not keeping.

Methods

DeclaredCollectionsAreNeverNull(JsonTypeInfo)

A JsonTypeInfo modifier that materializes a property declared as a non-nullable collection as an empty collection when the document carries no value for it.

public static void DeclaredCollectionsAreNeverNull(JsonTypeInfo typeInfo)

Parameters

typeInfo JsonTypeInfo

The JsonTypeInfo to modify.

Remarks

Only non-nullable declarations are touched. A read model that genuinely needs to tell "no collection" from "an empty one" says so by declaring the property nullable, and then gets to keep the distinction - the same carve-out the TypeScript reader made when it was fixed for this in Fundamentals 7.16.8.

WithDeclaredCollectionsNeverNull(JsonSerializerOptions)

Add the read-model collection semantics to a set of JsonSerializerOptions.

public static JsonSerializerOptions WithDeclaredCollectionsNeverNull(this JsonSerializerOptions options)

Parameters

options JsonSerializerOptions

The JsonSerializerOptions to add to.

Returns

JsonSerializerOptions

The options for continuation.

Remarks

Applied to the client's own options and to the ones the in-process spec harness materializes through, so that a spec and a running system answer "what is an empty child collection" with the same code rather than with two constants that happen to match.