CrystalJsonStreamWriter

Namespace: SnowBank.Data.Json · class

Implements: IDisposable, IAsyncDisposable

Writes JSON document fragments into a destination stream

Constructors

CrystalJsonStreamWriter

CrystalJsonStreamWriter(Stream output, CrystalJsonSettings settings, CrystalJsonTypeResolver resolver = null, bool ownStream = false)

Properties

CurrentNode

NodeType CurrentNode { get; }

NewLine

string NewLine { get; }

PositionHint

long? PositionHint { get; }

Returns the estimated position in the source stream

CAUTION: this value is only accurate right after a Flush!

Methods

BeginArrayFragment

ArrayStream BeginArrayFragment(CancellationToken cancellationToken = null)

Starts manual serialization of an array

Returns: Sub-stream that can be used to write the array fragment, and which should Disposed once the array is complete.

BeginObjectFragment

ObjectStream BeginObjectFragment(CancellationToken cancellationToken = null)

Starts manual serialization of an object

Returns: Sub-stream that can be used to write the object fragment, and which should Disposed once the object is complete.

Create

static CrystalJsonStreamWriter Create(string path, CrystalJsonSettings settings = null, CrystalJsonTypeResolver resolver = null)

static CrystalJsonStreamWriter Create(Stream stream, CrystalJsonSettings settings = null, CrystalJsonTypeResolver resolver = null, bool ownStream = false)

Dispose

void Dispose()

DisposeAsync

ValueTask DisposeAsync()

Flush

void Flush()

FlushAsync

Task FlushAsync(CancellationToken ct)

WriteArrayFragment

void WriteArrayFragment(Action<ArrayStream> handler, CancellationToken cancellationToken)

Writes a top-level array, and flushes the stream

  • handler — Handler that is responsible for writing the array content into the stream
  • cancellationToken — Token used to cancel the operation

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginArrayFragment.

void WriteArrayFragment<T>(IEnumerable<T> items, CancellationToken cancellationToken)

Writes a top-level array, and flushes the stream

  • items — Sequence that contains the items that will be written to the stream
  • cancellationToken — Token used to cancel the operation

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginArrayFragment.

void WriteArrayFragment(IEnumerable<JsonValue> items, CancellationToken cancellationToken)

Writes a top-level array, and flushes the stream

  • items — Sequence that contains the items that will be written to the stream
  • cancellationToken — Token used to cancel the operation

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginArrayFragment.

void WriteArrayFragment<TState>(TState state, Action<TState, ArrayStream> handler, CancellationToken cancellationToken)

Writes a top-level array, and flushes the stream

  • state — Value that will be passed as the first parameter to handler
  • handler — Handler that is responsible for writing the array content into the stream
  • cancellationToken — Token used to cancel the operation

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginArrayFragment.

WriteArrayFragmentAsync

Task WriteArrayFragmentAsync(Func<ArrayStream, Task> handler, CancellationToken cancellationToken)

Writes a top-level array, and flushes the stream

  • handler — Handler that is responsible for writing the array content into the stream
  • cancellationToken — Token used to cancel the operation

Returns: Task that completes once the array has been completely written, and the buffer has been flushed into the stream

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginArrayFragment.

Task WriteArrayFragmentAsync<T>(IEnumerable<T> items, CancellationToken cancellationToken)

Writes a top-level array, and flushes the stream

  • items — Sequence that contains the items that will be written to the stream
  • cancellationToken — Token used to cancel the operation

Returns: Task that completes once the array has been completely written, and the buffer has been flushed into the stream

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginArrayFragment.

Task WriteArrayFragmentAsync(IEnumerable<JsonValue> items, CancellationToken cancellationToken)

Writes a top-level array, and flushes the stream

  • items — Sequence that contains the items that will be written to the stream
  • cancellationToken — Token used to cancel the operation

Returns: Task that completes once the array has been completely written, and the buffer has been flushed into the stream

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginArrayFragment.

WriteFragment

void WriteFragment<T>(T item, CancellationToken cancellationToken)

Writes a document fragment, and flushes the stream

WriteFragmentAsync

Task WriteFragmentAsync<T>(T item, CancellationToken cancellationToken)

Writes a document fragment, and flushes the stream

WriteObjectFragment

void WriteObjectFragment(Action<ObjectStream> handler, CancellationToken cancellationToken)

Writes a top-level object, and flushes the stream

  • handler — Handler that is responsible for writing the object content into the stream
  • cancellationToken — Token used to cancel the operation

Returns: Task that completes once the object has been completely written, and the buffer has been flushed into the stream

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginObjectFragment.

WriteObjectFragmentAsync

Task WriteObjectFragmentAsync(Func<ObjectStream, Task> handler, CancellationToken cancellationToken)

Writes a top-level object, and flushes the stream

  • handler — Handler that is responsible for writing the object content into the stream
  • cancellationToken — Token used to cancel the operation

Returns: Task that completes once the object has been completely written, and the buffer has been flushed into the stream

The stream will be flushed after this call

This method should only be called for top-level object. For sub-objects, please use BeginObjectFragment.