Table of Contents

Class JsonSerializer

Namespace
ShapeShift.Json
Assembly
ShapeShift.Json.dll

Serializes PolyType-described object graphs as JSON.

public sealed record JsonSerializer : ShapeShiftSerializer<JsonEncoder, JsonDecoder>, IEquatable<ShapeShiftSerializer<JsonEncoder, JsonDecoder>>, IEquatable<JsonSerializer>
Inheritance
JsonSerializer
Implements
Inherited Members

Constructors

JsonSerializer()

Initializes a new instance of the JsonSerializer class.

public JsonSerializer()

Properties

AllowNamedFloatingPointValues

Gets a value indicating whether NaN and infinity are written and accepted as named JSON strings.

public bool AllowNamedFloatingPointValues { get; init; }

Property Value

bool

AllowTrailingCommas

Gets a value indicating whether trailing commas are accepted while reading.

public bool AllowTrailingCommas { get; init; }

Property Value

bool

CommentHandling

Gets the handling applied to JSON comments while reading.

public JsonCommentHandling CommentHandling { get; init; }

Property Value

JsonCommentHandling

Indented

Gets a value indicating whether JSON output is indented.

public bool Indented { get; init; }

Property Value

bool

Methods

CreateDocumentReader<T>(CancellationToken)

Creates a reader that incrementally enumerates a sequence of whole top-level JSON values sharing one buffer, such as newline-delimited JSON (NDJSON).

public ShapeShiftDocumentReader<T, JsonEncoder, JsonDecoder> CreateDocumentReader<T>(CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

cancellationToken CancellationToken

A cancellation token that applies throughout the lifetime of the reader.

Returns

ShapeShiftDocumentReader<T, JsonEncoder, JsonDecoder>

The reader. Callers should dispose of it (or use a using statement) when done.

Type Parameters

T

The type of each top-level value.

CreateDocumentReader<T, TProvider>(CancellationToken)

Creates a reader that incrementally enumerates a sequence of whole top-level JSON values sharing one buffer using a specified shape provider, such as newline-delimited JSON (NDJSON).

public ShapeShiftDocumentReader<T, JsonEncoder, JsonDecoder> CreateDocumentReader<T, TProvider>(CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

cancellationToken CancellationToken

A cancellation token that applies throughout the lifetime of the reader.

Returns

ShapeShiftDocumentReader<T, JsonEncoder, JsonDecoder>

The reader. Callers should dispose of it (or use a using statement) when done.

Type Parameters

T

The type of each top-level value.

TProvider

The type shape provider.

CreateSequenceReader<T>(CancellationToken)

Creates a reader that incrementally enumerates the elements of a JSON array, whether that array is the root of the document or is reached by first seeking into an enclosing document (e.g. with the TrySeek decoder extension member).

public ShapeShiftSequenceReader<T, JsonEncoder, JsonDecoder> CreateSequenceReader<T>(CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

cancellationToken CancellationToken

A cancellation token that applies throughout the lifetime of the reader.

Returns

ShapeShiftSequenceReader<T, JsonEncoder, JsonDecoder>

The reader. Callers should dispose of it (or use a using statement) when done.

Type Parameters

T

The type of each element in the array.

CreateSequenceReader<T, TProvider>(CancellationToken)

Creates a reader that incrementally enumerates the elements of a JSON array using a specified shape provider, whether that array is the root of the document or is reached by first seeking into an enclosing document.

public ShapeShiftSequenceReader<T, JsonEncoder, JsonDecoder> CreateSequenceReader<T, TProvider>(CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

cancellationToken CancellationToken

A cancellation token that applies throughout the lifetime of the reader.

Returns

ShapeShiftSequenceReader<T, JsonEncoder, JsonDecoder>

The reader. Callers should dispose of it (or use a using statement) when done.

Type Parameters

T

The type of each element in the array.

TProvider

The type shape provider.

DeserializeAllAsync<T>(PipeReader, long, CancellationToken)

Asynchronously deserializes a sequence of whole top-level values sharing one PipeReader, such as newline-delimited JSON (NDJSON), buffering only as much input as each value requires.

public IAsyncEnumerable<T?> DeserializeAllAsync<T>(PipeReader reader, long maxBufferedSize = 67108864, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

reader PipeReader

The source reader. This method does not complete it; the caller retains ownership.

maxBufferedSize long

The maximum number of bytes to buffer while searching for any single value, bounding memory use against a value that never completes (e.g. one truncated by a misbehaving sender).

cancellationToken CancellationToken

A cancellation token that applies throughout the enumeration.

Returns

IAsyncEnumerable<T>

An async sequence of the values read, ending gracefully when the reader reaches its end.

Type Parameters

T

The type of each top-level value.

DeserializeAllAsync<T, TProvider>(PipeReader, long, CancellationToken)

Asynchronously deserializes a sequence of whole top-level values sharing one PipeReader using a specified shape provider, such as newline-delimited JSON (NDJSON), buffering only as much input as each value requires.

public IAsyncEnumerable<T?> DeserializeAllAsync<T, TProvider>(PipeReader reader, long maxBufferedSize = 67108864, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

reader PipeReader

The source reader. This method does not complete it; the caller retains ownership.

maxBufferedSize long

The maximum number of bytes to buffer while searching for any single value, bounding memory use against a value that never completes (e.g. one truncated by a misbehaving sender).

cancellationToken CancellationToken

A cancellation token that applies throughout the enumeration.

Returns

IAsyncEnumerable<T>

An async sequence of the values read, ending gracefully when the reader reaches its end.

Type Parameters

T

The type of each top-level value.

TProvider

The type shape provider.

DeserializeAsync<T>(PipeReader, long, CancellationToken)

Asynchronously deserializes one value from a PipeReader, buffering only as much input as that value requires.

public ValueTask<T?> DeserializeAsync<T>(PipeReader reader, long maxBufferedSize = 67108864, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

reader PipeReader

The source reader. This method does not complete it; the caller retains ownership.

maxBufferedSize long

The maximum number of bytes to buffer while searching for the value, bounding memory use against a value that never completes (e.g. one truncated by a misbehaving sender).

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<T>

The deserialized value.

Type Parameters

T

The value type.

Exceptions

DecoderException

Thrown if the reader has no more values, or ends in the middle of one.

DeserializeAsync<T>(Stream, long, CancellationToken)

Asynchronously deserializes one value from a stream, buffering only as much input as that value requires.

public ValueTask<T?> DeserializeAsync<T>(Stream stream, long maxBufferedSize = 67108864, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

stream Stream

The source stream. It is not closed or disposed by this method.

maxBufferedSize long

The maximum number of bytes to buffer while searching for the value, bounding memory use against a value that never completes (e.g. one truncated by a misbehaving sender).

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<T>

The deserialized value.

Type Parameters

T

The value type.

Exceptions

DecoderException

Thrown if the stream has no more values, or ends in the middle of one.

DeserializeAsync<T, TProvider>(PipeReader, long, CancellationToken)

Asynchronously deserializes one value from a PipeReader using a specified shape provider, buffering only as much input as that value requires.

public ValueTask<T?> DeserializeAsync<T, TProvider>(PipeReader reader, long maxBufferedSize = 67108864, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

reader PipeReader

The source reader. This method does not complete it; the caller retains ownership.

maxBufferedSize long

The maximum number of bytes to buffer while searching for the value, bounding memory use against a value that never completes (e.g. one truncated by a misbehaving sender).

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<T>

The deserialized value.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Exceptions

DecoderException

Thrown if the reader has no more values, or ends in the middle of one.

DeserializeAsync<T, TProvider>(Stream, long, CancellationToken)

Asynchronously deserializes one value from a stream using a specified shape provider, buffering only as much input as that value requires.

public ValueTask<T?> DeserializeAsync<T, TProvider>(Stream stream, long maxBufferedSize = 67108864, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

stream Stream

The source stream. It is not closed or disposed by this method.

maxBufferedSize long

The maximum number of bytes to buffer while searching for the value, bounding memory use against a value that never completes (e.g. one truncated by a misbehaving sender).

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask<T>

The deserialized value.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Exceptions

DecoderException

Thrown if the stream has no more values, or ends in the middle of one.

DeserializeFragment<T>(ReadOnlySpan<byte>, ShapeShiftPath, CancellationToken)

Deserializes the value found at a given ShapeShiftPath within a UTF-8 JSON document, skipping over everything else without fully parsing or buffering it.

public T? DeserializeFragment<T>(ReadOnlySpan<byte> json, ShapeShiftPath path, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

json ReadOnlySpan<byte>

The UTF-8 JSON document.

path ShapeShiftPath

The location of the value to deserialize.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

Exceptions

ShapeShiftSerializationException

Thrown when path could not be found.

DeserializeFragment<T>(string, ShapeShiftPath, CancellationToken)

Deserializes the value found at a given ShapeShiftPath within JSON text, skipping over everything else without fully parsing or buffering it.

public T? DeserializeFragment<T>(string json, ShapeShiftPath path, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

json string

The JSON document.

path ShapeShiftPath

The location of the value to deserialize.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

Exceptions

ShapeShiftSerializationException

Thrown when path could not be found.

DeserializeFragment<T, TProvider>(ReadOnlySpan<byte>, ShapeShiftPath, CancellationToken)

Deserializes the value found at a given ShapeShiftPath within a UTF-8 JSON document using a specified shape provider, skipping over everything else without fully parsing or buffering it.

public T? DeserializeFragment<T, TProvider>(ReadOnlySpan<byte> json, ShapeShiftPath path, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

json ReadOnlySpan<byte>

The UTF-8 JSON document.

path ShapeShiftPath

The location of the value to deserialize.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Exceptions

ShapeShiftSerializationException

Thrown when path could not be found.

DeserializeFragment<T, TProvider>(string, ShapeShiftPath, CancellationToken)

Deserializes the value found at a given ShapeShiftPath within JSON text using a specified shape provider, skipping over everything else without fully parsing or buffering it.

public T? DeserializeFragment<T, TProvider>(string json, ShapeShiftPath path, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

json string

The JSON document.

path ShapeShiftPath

The location of the value to deserialize.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Exceptions

ShapeShiftSerializationException

Thrown when path could not be found.

Deserialize<T>(in ReadOnlySequence<byte>, CancellationToken)

Deserializes a value from a potentially segmented UTF-8 JSON sequence.

public T? Deserialize<T>(in ReadOnlySequence<byte> json, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

json ReadOnlySequence<byte>

The UTF-8 JSON document.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

Deserialize<T>(ReadOnlySpan<byte>, CancellationToken)

Deserializes a value from UTF-8 JSON.

public T? Deserialize<T>(ReadOnlySpan<byte> json, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

json ReadOnlySpan<byte>

The UTF-8 JSON document.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

Deserialize<T>(string, CancellationToken)

Deserializes a value from JSON text.

public T? Deserialize<T>(string json, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

json string

The JSON document.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

Deserialize<T, TProvider>(in ReadOnlySequence<byte>, CancellationToken)

Deserializes a value from a potentially segmented UTF-8 JSON sequence using a specified shape provider.

public T? Deserialize<T, TProvider>(in ReadOnlySequence<byte> json, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

json ReadOnlySequence<byte>

The UTF-8 JSON document.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Remarks

Multi-segment input is consolidated into one buffer before decoding.

Deserialize<T, TProvider>(ReadOnlySpan<byte>, CancellationToken)

Deserializes a value from UTF-8 JSON using a specified shape provider.

public T? Deserialize<T, TProvider>(ReadOnlySpan<byte> json, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

json ReadOnlySpan<byte>

The UTF-8 JSON document.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Deserialize<T, TProvider>(string, CancellationToken)

Deserializes a value from JSON text using a specified shape provider.

public T? Deserialize<T, TProvider>(string json, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

json string

The JSON document.

cancellationToken CancellationToken

A cancellation token.

Returns

T

The deserialized value.

Type Parameters

T

The value type.

TProvider

The type shape provider.

GetJsonSchema(ITypeShape, JsonSchemaOptions?)

Creates a JSON Schema document that describes the JSON this serializer reads and writes for a type.

public JsonObject GetJsonSchema(ITypeShape typeShape, JsonSchemaOptions? options = null)

Parameters

typeShape ITypeShape

The shape of the type to describe.

options JsonSchemaOptions

Options that influence the projection. When null, defaults are used except that AllowNamedFloatingPointValues is taken from this serializer.

Returns

JsonObject

A mutable JSON Schema document conforming to the Dialect dialect.

Exceptions

NotSupportedException

Thrown when PreserveReferences is enabled.

GetJsonSchema<T>(JsonSchemaOptions?)

Creates a JSON Schema document that describes the JSON this serializer reads and writes for a type.

public JsonObject GetJsonSchema<T>(JsonSchemaOptions? options = null) where T : IShapeable<T>

Parameters

options JsonSchemaOptions

Options that influence the projection. When null, defaults are used except that AllowNamedFloatingPointValues is taken from this serializer.

Returns

JsonObject

A mutable JSON Schema document conforming to the Dialect dialect.

Type Parameters

T

The type to describe.

Exceptions

NotSupportedException

Thrown when PreserveReferences is enabled.

GetJsonSchema<T, TProvider>(JsonSchemaOptions?)

Creates a JSON Schema document that describes the JSON this serializer reads and writes for a type.

public JsonObject GetJsonSchema<T, TProvider>(JsonSchemaOptions? options = null) where TProvider : IShapeable<T>

Parameters

options JsonSchemaOptions

Options that influence the projection. When null, defaults are used except that AllowNamedFloatingPointValues is taken from this serializer.

Returns

JsonObject

A mutable JSON Schema document conforming to the Dialect dialect.

Type Parameters

T

The type to describe.

TProvider

The witness class that provides the shape for T.

Exceptions

NotSupportedException

Thrown when PreserveReferences is enabled.

SerializeAsync<T>(PipeWriter, T?, CancellationToken)

Asynchronously serializes a value to a PipeWriter.

public ValueTask SerializeAsync<T>(PipeWriter writer, T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

writer PipeWriter

The destination writer. This method flushes it but does not complete it.

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask

A task that represents the operation.

Type Parameters

T

The value type.

Remarks

The value is converted synchronously (as with any other synchronous Serialize overload); only the flush to writer is asynchronous.

SerializeAsync<T>(Stream, T?, CancellationToken)

Asynchronously serializes a value to a stream.

public ValueTask SerializeAsync<T>(Stream stream, T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

stream Stream

The destination stream. It is not closed or disposed by this method.

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask

A task that represents the operation.

Type Parameters

T

The value type.

SerializeAsync<T, TProvider>(PipeWriter, T?, CancellationToken)

Asynchronously serializes a value to a PipeWriter using a specified shape provider.

public ValueTask SerializeAsync<T, TProvider>(PipeWriter writer, T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

writer PipeWriter

The destination writer. This method flushes it but does not complete it.

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask

A task that represents the operation.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Remarks

The value is converted synchronously (as with any other synchronous Serialize overload); only the flush to writer is asynchronous.

SerializeAsync<T, TProvider>(Stream, T?, CancellationToken)

Asynchronously serializes a value to a stream using a specified shape provider.

public ValueTask SerializeAsync<T, TProvider>(Stream stream, T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

stream Stream

The destination stream. It is not closed or disposed by this method.

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Returns

ValueTask

A task that represents the operation.

Type Parameters

T

The value type.

TProvider

The type shape provider.

SerializeToUtf8Bytes<T>(in T?, CancellationToken)

Serializes a value to UTF-8 JSON.

public byte[] SerializeToUtf8Bytes<T>(in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Returns

byte[]

The UTF-8 JSON document.

Type Parameters

T

The value type.

SerializeToUtf8Bytes<T, TProvider>(in T?, CancellationToken)

Serializes a value to UTF-8 JSON using a specified shape provider.

public byte[] SerializeToUtf8Bytes<T, TProvider>(in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Returns

byte[]

The UTF-8 JSON document.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Serialize<T>(in T?, CancellationToken)

Serializes a value as JSON text.

public string Serialize<T>(in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Returns

string

The JSON document.

Type Parameters

T

The value type.

Serialize<T, TProvider>(IBufferWriter<byte>, in T?, CancellationToken)

Serializes a value into a caller-provided buffer.

public void Serialize<T, TProvider>(IBufferWriter<byte> destination, in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

destination IBufferWriter<byte>

The destination buffer.

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Type Parameters

T

The value type.

TProvider

The type shape provider.

Serialize<T, TProvider>(in T?, CancellationToken)

Serializes a value as JSON text using a specified shape provider.

public string Serialize<T, TProvider>(in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

value T

The value to serialize.

cancellationToken CancellationToken

A cancellation token.

Returns

string

The JSON document.

Type Parameters

T

The value type.

TProvider

The type shape provider.

TryDeserializeFragment<T>(ReadOnlySpan<byte>, ShapeShiftPath, out T?, CancellationToken)

Attempts to deserialize the value found at a given ShapeShiftPath within a UTF-8 JSON document, skipping over everything else without fully parsing or buffering it.

public bool TryDeserializeFragment<T>(ReadOnlySpan<byte> json, ShapeShiftPath path, out T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

json ReadOnlySpan<byte>

The UTF-8 JSON document.

path ShapeShiftPath

The location of the value to deserialize.

value T

Receives the deserialized value if this method returns true; otherwise default.

cancellationToken CancellationToken

A cancellation token.

Returns

bool

true if path was found; false otherwise.

Type Parameters

T

The value type.

TryDeserializeFragment<T>(string, ShapeShiftPath, out T?, CancellationToken)

Attempts to deserialize the value found at a given ShapeShiftPath within JSON text, skipping over everything else without fully parsing or buffering it.

public bool TryDeserializeFragment<T>(string json, ShapeShiftPath path, out T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>

Parameters

json string

The JSON document.

path ShapeShiftPath

The location of the value to deserialize.

value T

Receives the deserialized value if this method returns true; otherwise default.

cancellationToken CancellationToken

A cancellation token.

Returns

bool

true if path was found; false otherwise.

Type Parameters

T

The value type.

TryDeserializeFragment<T, TProvider>(ReadOnlySpan<byte>, ShapeShiftPath, out T?, CancellationToken)

Attempts to deserialize the value found at a given ShapeShiftPath within a UTF-8 JSON document using a specified shape provider, skipping over everything else without fully parsing or buffering it.

public bool TryDeserializeFragment<T, TProvider>(ReadOnlySpan<byte> json, ShapeShiftPath path, out T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

json ReadOnlySpan<byte>

The UTF-8 JSON document.

path ShapeShiftPath

The location of the value to deserialize.

value T

Receives the deserialized value if this method returns true; otherwise default.

cancellationToken CancellationToken

A cancellation token.

Returns

bool

true if path was found; false otherwise.

Type Parameters

T

The value type.

TProvider

The type shape provider.

TryDeserializeFragment<T, TProvider>(string, ShapeShiftPath, out T?, CancellationToken)

Attempts to deserialize the value found at a given ShapeShiftPath within JSON text using a specified shape provider, skipping over everything else without fully parsing or buffering it.

public bool TryDeserializeFragment<T, TProvider>(string json, ShapeShiftPath path, out T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>

Parameters

json string

The JSON document.

path ShapeShiftPath

The location of the value to deserialize.

value T

Receives the deserialized value if this method returns true; otherwise default.

cancellationToken CancellationToken

A cancellation token.

Returns

bool

true if path was found; false otherwise.

Type Parameters

T

The value type.

TProvider

The type shape provider.