Table of Contents

Struct ShapeShiftSequenceReader<T, TEncoder, TDecoder>

Namespace
ShapeShift
Assembly
ShapeShift.dll

Enables incremental, low-allocation enumeration of the elements of a vector (a JSON array, or a MessagePack array), whether that vector is the root of a document, or is reached by first seeking into an enclosing document (for example with ShapeShiftPath).

public struct ShapeShiftSequenceReader<T, TEncoder, TDecoder> : IDisposable where TEncoder : IEncoder, allows ref struct where TDecoder : IDecoder, allows ref struct

Type Parameters

T

The type of each element in the vector.

TEncoder

The type of encoder used by the serializer that created this reader.

TDecoder

The type of decoder that supplies the serialized data.

Implements
Inherited Members

Remarks

Create an instance with CreateSequenceReader<T>(ITypeShape<T>, CancellationToken) or a format-specific convenience overload (e.g. JsonSerializer.CreateSequenceReader).

This type deliberately does not store the TDecoder that supplies its data: because decoders are typically ref structs, a type that stored one as a field could never itself be anything but a ref struct, which in turn would rule out patterns like await between calls. Instead, the same decoder value must be passed ref to every call to MoveNext(ref TDecoder), much like an old-fashioned while-based iteration over an IEnumerator.

Always call Dispose() (or use a using statement) when finished with a reader, so that pooled resources it may hold can be released.

Properties

Current

Gets the element most recently read by MoveNext(ref TDecoder).

public readonly T? Current { get; }

Property Value

T

Remarks

This property's value is undefined before the first call to MoveNext(ref TDecoder), and after any call to it that returns false.

Methods

Dispose()

Releases any pooled resources held by this reader.

public void Dispose()

MoveNext(ref TDecoder)

Advances to the next element in the vector, reading it into Current.

public bool MoveNext(ref TDecoder decoder)

Parameters

decoder TDecoder

The decoder that supplies the data. On the first call, this must be positioned at the start of the vector (i.e. its NextTokenType must be StartVector). On every call, the same value (by ref) that was supplied to the previous call must be given, so that this reader observes the decoder's position as it was left after the last element (or the vector header) was read.

Returns

bool

true if another element was read into Current; false if the vector has been fully enumerated, in which case its closing token has also been consumed.

Exceptions

InvalidOperationException

Thrown if this method is called again after it has already returned false once.