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
TThe .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
schemaJsonObjectThe 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
Returns
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
undocumentingConverterTypeThe 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
readerJsonReaderThe reader to consume the JSON value from.
contextSerializationContextContext 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
readerJsonAsyncReaderThe asynchronous reader.
contextSerializationContextContext 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
readerJsonReaderThe source reader.
contextSerializationContextThe 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
readerJsonAsyncReaderThe source reader.
contextSerializationContextThe operation context.
Returns
Write(ref JsonWriter, in T?, SerializationContext)
Writes a value as JSON.
public abstract void Write(ref JsonWriter writer, in T? value, SerializationContext context)
Parameters
writerJsonWriterThe writer to receive the JSON value.
valueTThe value to write.
contextSerializationContextContext 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
writerJsonAsyncWriterThe asynchronous writer.
valueTThe value to write.
contextSerializationContextContext 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
writerJsonWriterThe destination writer.
valueobjectThe value to write.
contextSerializationContextThe operation context.
WriteObjectAsync(JsonAsyncWriter, object?, SerializationContext)
Writes an untyped value asynchronously.
public override sealed ValueTask WriteObjectAsync(JsonAsyncWriter writer, object? value, SerializationContext context)
Parameters
writerJsonAsyncWriterThe destination writer.
valueobjectThe value to write.
contextSerializationContextThe operation context.
Returns
- ValueTask
A task tracking the write.