Skip to content

ArrayConverter<TElement,TConverter> Class

JSON converter for nullable-element arrays of TElement. Each non-null element is delegated to an instance of TConverter; each null element is preserved as default() on read and emitted as a JSON null on write, independently of how the inner converter would handle null. A whole-array JSON null deserializes to a null array. Read accepts any JSON value form the inner converter can consume (string, number, boolean, object, nested array), keeping it symmetric with whatever Write can produce.

C#
public class ArrayConverter<TElement,TConverter> : System.Text.Json.Serialization.JsonConverter<TElement?[]?>
    where TConverter : System.Text.Json.Serialization.JsonConverter<TElement>, new()

Type parameters

TElement

The element type of the array.

TConverter

The per-element converter; instantiated once per ArrayConverter<TElement,TConverter>.

Inheritance System.ObjectSystem.Text.Json.Serialization.JsonConverterSystem.Text.Json.Serialization.JsonConverter<TElement[]> → ArrayConverter<TElement,TConverter>

Methods

ArrayConverter<TElement,TConverter>.Read(Utf8JsonReader, Type, JsonSerializerOptions) Method

Deserializes a JSON array into TElement?[]?, or returns a null array when the whole value is JSON null. Inside the array, a JSON null element becomes default() without invoking the inner converter; every other value-start token (string, number, boolean, object, nested array) is forwarded to the inner converter.

C#
public override TElement?[]? Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options);

Parameters

reader System.Text.Json.Utf8JsonReader

The reader positioned at the value to deserialize.

typeToConvert System.Type

The CLR type the framework asked to deserialize into.

options System.Text.Json.JsonSerializerOptions

Serializer options forwarded to the inner converter.

Returns

TElement[]
The deserialized array, or null.

ArrayConverter<TElement,TConverter>.Write(Utf8JsonWriter, TElement[], JsonSerializerOptions) Method

Writes an array of TElement to JSON.

C#
public override void Write(System.Text.Json.Utf8JsonWriter writer, TElement?[]? value, System.Text.Json.JsonSerializerOptions options);

Parameters

writer System.Text.Json.Utf8JsonWriter

The writer to write JSON to.

value TElement[]

The array of TElement to write.

options System.Text.Json.JsonSerializerOptions

Options for the serializer.