Table of Contents

Class JsonConverter<T>

Namespace
Nerdbank.Json
Assembly
Nerdbank.Json.dll

Base type for JSON converters that handle a specific .NET type.

public abstract class JsonConverter<T> : JsonConverter

Type Parameters

T

The .NET type handled by the converter.

Inheritance
JsonConverter<T>
Inherited Members

Methods

ApplyJsonSchemaNullability(JsonObject)

Returns a schema that also accepts null.

protected static JsonObject ApplyJsonSchemaNullability(JsonObject schema)

Parameters

schema JsonObject

The non-null schema fragment.

Returns

JsonObject

The adjusted schema fragment.

CreateJsonValue(object?)

Wraps a boxed primitive as a JSON value without reflection-based serialization.

protected static JsonValue? CreateJsonValue(object? value)

Parameters

value object

The boxed primitive, or null.

Returns

JsonValue

The JSON value, or null if the input is null.

Exceptions

NotSupportedException

The value is not a supported primitive.

CreateUndocumentedSchema(Type)

Creates a permissive schema for a converter with an undocumented representation.

protected static JsonObject CreateUndocumentedSchema(Type undocumentingConverter)

Parameters

undocumentingConverter Type

The converter type.

Returns

JsonObject

A schema allowing any JSON value, annotated with the responsible converter.

Read(ref JsonReader, SerializationContext)

Reads a value from JSON.

public abstract T? Read(ref JsonReader reader, SerializationContext context)

Parameters

reader JsonReader

The reader to consume the JSON value from.

context SerializationContext

Context for the deserialization operation.

Returns

T

The deserialized value.

ReadAsync(JsonAsyncReader, SerializationContext)

Reads a value from an asynchronous, incremental reader.

public virtual ValueTask<T?> ReadAsync(JsonAsyncReader reader, SerializationContext context)

Parameters

reader JsonAsyncReader

The asynchronous reader.

context SerializationContext

Context for the deserialization operation.

Returns

ValueTask<T>

A task whose result is the deserialized value.

Remarks

The default implementation buffers the next complete JSON value and delegates to Read(ref JsonReader, SerializationContext). Override this only when a value may be very large; read fragments and periodically await more data so memory stays bounded.

ReadObject(ref JsonReader, SerializationContext)

Reads an untyped value.

public override sealed object? ReadObject(ref JsonReader reader, SerializationContext context)

Parameters

reader JsonReader

The source reader.

context SerializationContext

The operation context.

Returns

object

The deserialized value.

ReadObjectAsync(JsonAsyncReader, SerializationContext)

Reads an untyped value asynchronously.

public override sealed ValueTask<object?> ReadObjectAsync(JsonAsyncReader reader, SerializationContext context)

Parameters

reader JsonAsyncReader

The source reader.

context SerializationContext

The operation context.

Returns

ValueTask<object>

The deserialized value.

Write(ref JsonWriter, in T?, SerializationContext)

Writes a value as JSON.

public abstract void Write(ref JsonWriter writer, in T? value, SerializationContext context)

Parameters

writer JsonWriter

The writer to receive the JSON value.

value T

The value to write.

context SerializationContext

Context for the serialization operation.

WriteAsync(JsonAsyncWriter, T?, SerializationContext)

Writes a value as JSON to an asynchronous, incremental writer.

public virtual ValueTask WriteAsync(JsonAsyncWriter writer, T? value, SerializationContext context)

Parameters

writer JsonAsyncWriter

The asynchronous writer.

value T

The value to write.

context SerializationContext

Context for the serialization operation.

Returns

ValueTask

A task tracking the asynchronous write.

Remarks

The default implementation writes the whole value synchronously and then flushes the pipe if the buffer is large. Override this only when a value may be very large; write fragments and periodically call FlushIfAppropriateAsync(SerializationContext) to keep memory bounded.

WriteObject(ref JsonWriter, object?, SerializationContext)

Writes an untyped value.

public override sealed void WriteObject(ref JsonWriter writer, object? value, SerializationContext context)

Parameters

writer JsonWriter

The destination writer.

value object

The value to write.

context SerializationContext

The operation context.

WriteObjectAsync(JsonAsyncWriter, object?, SerializationContext)

Writes an untyped value asynchronously.

public override sealed ValueTask WriteObjectAsync(JsonAsyncWriter writer, object? value, SerializationContext context)

Parameters

writer JsonAsyncWriter

The destination writer.

value object

The value to write.

context SerializationContext

The operation context.

Returns

ValueTask

A task tracking the write.