Class JsonAsyncWriter
- Namespace
- Nerdbank.Json
- Assembly
- Nerdbank.Json.dll
An incremental UTF-8 JSON writer that writes to a PipeWriter and flushes periodically so large object graphs can be serialized with bounded memory and cooperative backpressure.
public sealed class JsonAsyncWriter
- Inheritance
-
JsonAsyncWriter
- Inherited Members
Remarks
This is the asynchronous counterpart to JsonWriter and is used when implementing the asynchronous virtual methods on JsonConverter<T>. The underlying PipeWriter is never completed by this type.
Constructors
JsonAsyncWriter(PipeWriter, bool)
Initializes a new instance of the JsonAsyncWriter class.
public JsonAsyncWriter(PipeWriter pipeWriter, bool writeIndented = false)
Parameters
pipeWriterPipeWriterThe pipe to write UTF-8 JSON to. It is not completed by this writer.
writeIndentedboolWhether written JSON should be indented.
Methods
CreateWriter()
Creates a synchronous JsonWriter that writes into this writer's buffer.
public JsonWriter CreateWriter()
Returns
- JsonWriter
The synchronous writer, which must be returned via ReturnWriter(ref JsonWriter).
Flush()
Ensures everything previously written has been committed to the underlying PipeWriter.
public void Flush()
FlushAsync(CancellationToken)
Commits buffered bytes and flushes the pipe unconditionally.
public ValueTask FlushAsync(CancellationToken cancellationToken)
Parameters
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the flush.
FlushIfAppropriateAsync(SerializationContext)
Flushes the pipe if the accumulated buffer is getting large, applying backpressure from the reader.
public ValueTask FlushIfAppropriateAsync(SerializationContext context)
Parameters
contextSerializationContextThe serialization context.
Returns
- ValueTask
A task to await before writing further.
ReturnWriter(ref JsonWriter)
Applies the bytes written with a writer previously obtained from CreateWriter() back to this object.
public void ReturnWriter(ref JsonWriter writer)
Parameters
writerJsonWriterThe writer to return. It is reset to prevent reuse after this call.
WriteNullAsync(SerializationContext)
Writes a JSON null literal.
public ValueTask WriteNullAsync(SerializationContext context)
Parameters
contextSerializationContextThe serialization context.
Returns
- ValueTask
A task tracking the write.
WriteValueAsync<T>(JsonConverter<T>, T?, SerializationContext)
Writes a value using the specified converter, streaming when the converter prefers asynchronous serialization.
public ValueTask WriteValueAsync<T>(JsonConverter<T> converter, T? value, SerializationContext context)
Parameters
converterJsonConverter<T>The converter for the value.
valueTThe value to write.
contextSerializationContextThe serialization context.
Returns
- ValueTask
A task tracking the asynchronous write.
Type Parameters
TThe value type.