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
AllowTrailingCommas
Gets a value indicating whether trailing commas are accepted while reading.
public bool AllowTrailingCommas { get; init; }
Property Value
CommentHandling
Gets the handling applied to JSON comments while reading.
public JsonCommentHandling CommentHandling { get; init; }
Property Value
Indented
Gets a value indicating whether JSON output is indented.
public bool Indented { get; init; }
Property Value
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
cancellationTokenCancellationTokenA 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
TThe 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
cancellationTokenCancellationTokenA 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
TThe type of each top-level value.
TProviderThe 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
cancellationTokenCancellationTokenA 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
TThe 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
cancellationTokenCancellationTokenA 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
TThe type of each element in the array.
TProviderThe 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
readerPipeReaderThe source reader. This method does not complete it; the caller retains ownership.
maxBufferedSizelongThe 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).
cancellationTokenCancellationTokenA 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
TThe 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
readerPipeReaderThe source reader. This method does not complete it; the caller retains ownership.
maxBufferedSizelongThe 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).
cancellationTokenCancellationTokenA 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
TThe type of each top-level value.
TProviderThe 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
readerPipeReaderThe source reader. This method does not complete it; the caller retains ownership.
maxBufferedSizelongThe 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).
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe 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
streamStreamThe source stream. It is not closed or disposed by this method.
maxBufferedSizelongThe 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).
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe 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
readerPipeReaderThe source reader. This method does not complete it; the caller retains ownership.
maxBufferedSizelongThe 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).
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe value type.
TProviderThe 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
streamStreamThe source stream. It is not closed or disposed by this method.
maxBufferedSizelongThe 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).
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
The deserialized value.
Type Parameters
TThe value type.
TProviderThe 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
jsonReadOnlySpan<byte>The UTF-8 JSON document.
pathShapeShiftPathThe location of the value to deserialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe value type.
Exceptions
- ShapeShiftSerializationException
Thrown when
pathcould 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
jsonstringThe JSON document.
pathShapeShiftPathThe location of the value to deserialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe value type.
Exceptions
- ShapeShiftSerializationException
Thrown when
pathcould 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
jsonReadOnlySpan<byte>The UTF-8 JSON document.
pathShapeShiftPathThe location of the value to deserialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe value type.
TProviderThe type shape provider.
Exceptions
- ShapeShiftSerializationException
Thrown when
pathcould 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
jsonstringThe JSON document.
pathShapeShiftPathThe location of the value to deserialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe value type.
TProviderThe type shape provider.
Exceptions
- ShapeShiftSerializationException
Thrown when
pathcould 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
jsonReadOnlySequence<byte>The UTF-8 JSON document.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe 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
jsonReadOnlySpan<byte>The UTF-8 JSON document.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe 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
jsonstringThe JSON document.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe 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
jsonReadOnlySequence<byte>The UTF-8 JSON document.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe value type.
TProviderThe 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
jsonReadOnlySpan<byte>The UTF-8 JSON document.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe value type.
TProviderThe 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
jsonstringThe JSON document.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe value type.
TProviderThe 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
typeShapeITypeShapeThe shape of the type to describe.
optionsJsonSchemaOptionsOptions 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
optionsJsonSchemaOptionsOptions 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
TThe 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
optionsJsonSchemaOptionsOptions 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
TThe type to describe.
TProviderThe 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
writerPipeWriterThe destination writer. This method flushes it but does not complete it.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that represents the operation.
Type Parameters
TThe 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
streamStreamThe destination stream. It is not closed or disposed by this method.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that represents the operation.
Type Parameters
TThe 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
writerPipeWriterThe destination writer. This method flushes it but does not complete it.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that represents the operation.
Type Parameters
TThe value type.
TProviderThe 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
streamStreamThe destination stream. It is not closed or disposed by this method.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that represents the operation.
Type Parameters
TThe value type.
TProviderThe 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
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- byte[]
The UTF-8 JSON document.
Type Parameters
TThe 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
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- byte[]
The UTF-8 JSON document.
Type Parameters
TThe value type.
TProviderThe 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
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- string
The JSON document.
Type Parameters
TThe 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
destinationIBufferWriter<byte>The destination buffer.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe value type.
TProviderThe 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
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- string
The JSON document.
Type Parameters
TThe value type.
TProviderThe 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
jsonReadOnlySpan<byte>The UTF-8 JSON document.
pathShapeShiftPathThe location of the value to deserialize.
valueTReceives the deserialized value if this method returns true; otherwise default.
cancellationTokenCancellationTokenA cancellation token.
Returns
Type Parameters
TThe 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
jsonstringThe JSON document.
pathShapeShiftPathThe location of the value to deserialize.
valueTReceives the deserialized value if this method returns true; otherwise default.
cancellationTokenCancellationTokenA cancellation token.
Returns
Type Parameters
TThe 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
jsonReadOnlySpan<byte>The UTF-8 JSON document.
pathShapeShiftPathThe location of the value to deserialize.
valueTReceives the deserialized value if this method returns true; otherwise default.
cancellationTokenCancellationTokenA cancellation token.
Returns
Type Parameters
TThe value type.
TProviderThe 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
jsonstringThe JSON document.
pathShapeShiftPathThe location of the value to deserialize.
valueTReceives the deserialized value if this method returns true; otherwise default.
cancellationTokenCancellationTokenA cancellation token.
Returns
Type Parameters
TThe value type.
TProviderThe type shape provider.