Table of Contents

Class JsonConverter

Namespace
Nerdbank.Json
Assembly
Nerdbank.Json.dll

Base type for JSON converters.

public abstract class JsonConverter
Inheritance
JsonConverter
Derived
Inherited Members

Properties

PreferAsyncSerialization

Gets a value indicating whether this converter's asynchronous methods should be preferred over its synchronous methods when a graph is being (de)serialized asynchronously.

public virtual bool PreferAsyncSerialization { get; }

Property Value

bool

Remarks

The default is false, which means the default asynchronous methods buffer a whole value and delegate to the synchronous methods. Converters that may (de)serialize very large values should override the asynchronous methods, set this to true, and stream fragments so that memory use stays bounded.

Methods

GetJsonSchema(JsonSchemaContext, ITypeShape)

Produces a JSON Schema fragment that describes how this converter represents its value, or null if this converter cannot describe its representation.

public virtual JsonObject? GetJsonSchema(JsonSchemaContext context, ITypeShape typeShape)

Parameters

context JsonSchemaContext

The schema-generation context, which resolves nested schemas and shared definitions.

typeShape ITypeShape

The shape of the type this converter handles.

Returns

JsonObject

A schema fragment, or null to indicate the representation is undocumented. When null is returned, the exporter emits a permissive schema annotated with a conspicuous comment rather than pretending to know the representation.

ReadObject(ref JsonReader, SerializationContext)

Reads an untyped value.

public abstract 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 abstract ValueTask<object?> ReadObjectAsync(JsonAsyncReader reader, SerializationContext context)

Parameters

reader JsonAsyncReader

The source reader.

context SerializationContext

The operation context.

Returns

ValueTask<object>

The deserialized value.

SkipToIndexValue(ref JsonReader, object?, SerializationContext)

Skips to the value at a logical array index or dictionary key.

public virtual bool SkipToIndexValue(ref JsonReader reader, object? index, SerializationContext context)

Parameters

reader JsonReader

The reader positioned at this converter's value.

index object

The array index or dictionary key.

context SerializationContext

The serialization context.

Returns

bool

Whether the value was found, with the reader ready to read it.

SkipToIndexValueAsync(JsonAsyncReader, object?, SerializationContext)

Asynchronously skips to a logical array index or dictionary key.

public virtual ValueTask<bool> SkipToIndexValueAsync(JsonAsyncReader reader, object? index, SerializationContext context)

Parameters

reader JsonAsyncReader

The asynchronous reader.

index object

The array index or dictionary key.

context SerializationContext

The serialization context.

Returns

ValueTask<bool>

Whether the value was found, with the reader ready to read it.

Remarks

Use the reader's structural methods to enter containers so envelope draining can account for them.

SkipToPropertyValue(ref JsonReader, IPropertyShape, SerializationContext)

Skips to the value of a logical property in this converter's representation.

public virtual bool SkipToPropertyValue(ref JsonReader reader, IPropertyShape propertyShape, SerializationContext context)

Parameters

reader JsonReader

The reader positioned at this converter's value.

propertyShape IPropertyShape

The logical property to select.

context SerializationContext

The serialization context.

Returns

bool

Whether the property was found, with the reader ready to read its value.

SkipToPropertyValueAsync(JsonAsyncReader, IPropertyShape, SerializationContext)

Asynchronously skips to the value of a logical property without buffering its enclosing value.

public virtual ValueTask<bool> SkipToPropertyValueAsync(JsonAsyncReader reader, IPropertyShape propertyShape, SerializationContext context)

Parameters

reader JsonAsyncReader

The asynchronous reader.

propertyShape IPropertyShape

The logical property to select.

context SerializationContext

The serialization context.

Returns

ValueTask<bool>

Whether the property was found, with the reader ready to read its value.

Remarks

Use the reader's structural methods to enter containers so envelope draining can account for them.

WriteObject(ref JsonWriter, object?, SerializationContext)

Writes an untyped value.

public abstract 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 abstract 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.