Class JsonSerializer
- Namespace
- Nerdbank.Json
- Assembly
- Nerdbank.Json.dll
Serializes .NET values to JSON.
public record JsonSerializer : IEquatable<JsonSerializer>
- Inheritance
-
JsonSerializer
- Implements
- Inherited Members
Remarks
This type is immutable and thread-safe.
Synchronous serialization and deserialization failures are reported as JsonSerializationException. The original exception is available as the inner exception; cancellation and argument validation errors are not wrapped.
When targeting .NET Standard 2.0 or .NET Framework, some important methods are available only as extension methods,
so make sure to have a using Nerdbank.Json; directive in your code file to see these.
Properties
AllowTrailingCommas
Gets a value indicating whether a trailing comma is permitted before the end of a JSON array or object during deserialization.
public bool AllowTrailingCommas { get; init; }
Property Value
Remarks
The default value is false.
ComparerProvider
Gets the provider of IEqualityComparer<T> and IComparer<T> instances to use when instantiating collections that support them.
public IComparerProvider? ComparerProvider { get; init; }
Property Value
- IComparerProvider
The default value is an instance of SecureComparerProvider, which provides hash collision resistance for improved security when deserializing untrusted data.
Remarks
This property may be cleared from its secure default for improved performance when deserializing trusted data.
ConverterFactories
Gets the runtime-registered converter factories that are consulted after Nerdbank.Json.JsonSerializerConfiguration.Converters and Nerdbank.Json.JsonSerializerConfiguration.ConverterTypes.
public IReadOnlyList<IJsonConverterFactory> ConverterFactories { get; init; }
Property Value
ConverterTypes
Gets the runtime-registered converter types that take precedence after Nerdbank.Json.JsonSerializerConfiguration.Converters.
public JsonConverterTypeCollection ConverterTypes { get; init; }
Property Value
Converters
Gets the runtime-registered converters that take precedence over built-in and shape-based converters.
public ConverterCollection Converters { get; init; }
Property Value
DeserializeDefaultValues
Gets the policy that determines how deserialization handles missing or null values.
public DeserializeDefaultValuesPolicy DeserializeDefaultValues { get; init; }
Property Value
Remarks
The default value is Default.
DictionaryKeyNamingPolicy
Gets the transformation applied to dictionary keys during serialization and deserialization.
public JsonNamingPolicy? DictionaryKeyNamingPolicy { get; init; }
Property Value
Remarks
The default value is null, which preserves dictionary keys as declared.
InternStrings
Gets a value indicating whether equal strings should share an instance during deserialization.
public bool InternStrings { get; init; }
Property Value
Remarks
The default value is false. When enabled, strings with equal values within one deserialization operation are represented by the same string instance.
Interned strings are retained only for the operation's lifetime, so this setting does not retain strings across deserialization operations. It can reduce memory use when input repeats string values, at the cost of looking up each deserialized string in the operation's cache.
PreserveReferences
Gets the mode that preserves reference equality during serialization and deserialization.
public ReferencePreservationMode PreserveReferences { get; init; }
Property Value
PropertyNameCaseInsensitive
Gets a value indicating whether JSON object property names are matched case-insensitively during deserialization.
public bool PropertyNameCaseInsensitive { get; init; }
Property Value
Remarks
The default value is false.
PropertyNamingPolicy
Gets the transformation applied to object property names and enum value names during serialization and deserialization.
public JsonNamingPolicy? PropertyNamingPolicy { get; init; }
Property Value
Remarks
The default value is CamelCase. Set this property to null to preserve declared property names and enum value names. Enum value names are transformed only when Nerdbank.Json.JsonSerializerConfiguration.SerializeEnumValuesByName is true.
ReadCommentHandling
Gets the policy for handling JSON comments during deserialization.
public JsonCommentHandling ReadCommentHandling { get; init; }
Property Value
Remarks
The default value is Disallow.
SerializeDefaultValues
Gets the policy that determines whether properties with default values are serialized.
public SerializeDefaultValuesPolicy SerializeDefaultValues { get; init; }
Property Value
Remarks
The default value is Always.
SerializeEnumValuesByName
Gets a value indicating whether enum values should be serialized by name rather than by numeric value when possible.
public bool SerializeEnumValuesByName { get; init; }
Property Value
Remarks
The default value is false. When enabled, Nerdbank.Json.JsonSerializerConfiguration.PropertyNamingPolicy is applied to enum value names.
StartingContext
Gets the starting context to begin serializations and deserializations with.
public SerializationContext StartingContext { get; init; }
Property Value
Unions
Gets the runtime union configuration that adds, replaces, extends, or disables shape-generated unions.
public JsonUnionConfiguration Unions { get; init; }
Property Value
- JsonUnionConfiguration
The default value is Default.
WriteIndented
Gets a value indicating whether serialized JSON should be formatted with indentation and line breaks.
public bool WriteIndented { get; init; }
Property Value
Remarks
The default value is false.
Methods
DeserializeArrayAsync<T>(PipeReader, ITypeShape<T>, CancellationToken)
Asynchronously streams the elements of a top-level JSON array, reading only enough to produce each element on demand.
public IAsyncEnumerable<T?> DeserializeArrayAsync<T>(PipeReader reader, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An asynchronous sequence of elements.
Type Parameters
TThe element type.
DeserializeArrayAsync<T>(PipeReader, CancellationToken)
Asynchronously streams the elements of a top-level JSON array, reading only enough to produce each element on demand.
public IAsyncEnumerable<T?> DeserializeArrayAsync<T>(PipeReader reader, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An asynchronous sequence of elements.
Type Parameters
TThe element type.
DeserializeArrayAsync<T>(Stream, ITypeShape<T>, CancellationToken)
Asynchronously streams the elements of a top-level JSON array from a stream.
public IAsyncEnumerable<T?> DeserializeArrayAsync<T>(Stream stream, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to read from. It is read but not disposed or closed by this method.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An asynchronous sequence of elements.
Type Parameters
TThe element type.
DeserializeArrayAsync<T>(Stream, CancellationToken)
Asynchronously streams the elements of a top-level JSON array from a stream.
public IAsyncEnumerable<T?> DeserializeArrayAsync<T>(Stream stream, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to read from. It is read but not disposed or closed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An asynchronous sequence of elements.
Type Parameters
TThe element type.
DeserializeArrayAtAsync<TElement>(PipeReader, JsonPath, ITypeShape<TElement>, MissingPathBehavior, CancellationToken)
Asynchronously streams the elements of a JSON array selected by a pre-parsed JsonPath inside an object or array envelope, parsing the preamble incrementally and draining the remainder of the envelope on completion.
public IAsyncEnumerable<TElement?> DeserializeArrayAtAsync<TElement>(PipeReader reader, JsonPath path, ITypeShape<TElement> elementShape, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
pathJsonPathThe pre-parsed path that selects the array. Names are the serialized (wire) names.
elementShapeITypeShape<TElement>The shape of
TElement.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An asynchronous sequence of elements.
Type Parameters
TElementThe element type.
DeserializeArrayAtAsync<TElement>(Stream, JsonPath, ITypeShape<TElement>, MissingPathBehavior, CancellationToken)
Asynchronously streams the elements of a JSON array selected by a pre-parsed JsonPath from a stream.
public IAsyncEnumerable<TElement?> DeserializeArrayAtAsync<TElement>(Stream stream, JsonPath path, ITypeShape<TElement> elementShape, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to read from. It is read but not disposed or closed by this method.
pathJsonPathThe pre-parsed path that selects the array.
elementShapeITypeShape<TElement>The shape of
TElement.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An asynchronous sequence of elements.
Type Parameters
TElementThe element type.
DeserializeArrayAtAsync<TRoot, TElement>(PipeReader, Expression<Func<TRoot, IEnumerable<TElement>>>, MissingPathBehavior, CancellationToken)
Asynchronously streams the elements of a JSON array selected by a member-access expression inside an object or array envelope. The expression is parsed but never compiled, so this overload is NativeAOT-safe and can traverse intermediate union or custom-converter representations.
public IAsyncEnumerable<TElement?> DeserializeArrayAtAsync<TRoot, TElement>(PipeReader reader, Expression<Func<TRoot, IEnumerable<TElement>>> path, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default) where TRoot : IShapeable<TRoot>
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
pathExpression<Func<TRoot, IEnumerable<TElement>>>An expression such as
x => x.Itemsselecting an IEnumerable<T>.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An asynchronous sequence of elements.
Type Parameters
TRootThe root document type.
TElementThe element type.
DeserializeArrayAtAsync<TRoot, TElement>(PipeReader, Expression<Func<TRoot, IEnumerable<TElement>>>, ITypeShape<TRoot>, MissingPathBehavior, CancellationToken)
Asynchronously streams the elements of a JSON array selected by a member-access expression inside an object or array envelope. The expression is parsed but never compiled, so this overload is NativeAOT-safe and can traverse intermediate union or custom-converter representations.
public IAsyncEnumerable<TElement?> DeserializeArrayAtAsync<TRoot, TElement>(PipeReader reader, Expression<Func<TRoot, IEnumerable<TElement>>> path, ITypeShape<TRoot> rootShape, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
pathExpression<Func<TRoot, IEnumerable<TElement>>>An expression such as
x => x.Itemsselecting an IEnumerable<T>.rootShapeITypeShape<TRoot>The shape of
TRoot.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An asynchronous sequence of elements.
Type Parameters
TRootThe root document type.
TElementThe element type.
DeserializeArrayAtAsync<TRoot, TElement>(Stream, Expression<Func<TRoot, IEnumerable<TElement>>>, MissingPathBehavior, CancellationToken)
Asynchronously streams the elements of a JSON array selected by a member-access expression from a stream.
public IAsyncEnumerable<TElement?> DeserializeArrayAtAsync<TRoot, TElement>(Stream stream, Expression<Func<TRoot, IEnumerable<TElement>>> path, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default) where TRoot : IShapeable<TRoot>
Parameters
streamStreamThe stream to read from. It is read but not disposed or closed by this method.
pathExpression<Func<TRoot, IEnumerable<TElement>>>An expression selecting an IEnumerable<T>.
missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An asynchronous sequence of elements.
Type Parameters
TRootThe root document type.
TElementThe element type.
DeserializeArrayAtAsync<TRoot, TElement>(Stream, Expression<Func<TRoot, IEnumerable<TElement>>>, ITypeShape<TRoot>, MissingPathBehavior, CancellationToken)
Asynchronously streams the elements of a JSON array selected by a member-access expression from a stream.
public IAsyncEnumerable<TElement?> DeserializeArrayAtAsync<TRoot, TElement>(Stream stream, Expression<Func<TRoot, IEnumerable<TElement>>> path, ITypeShape<TRoot> rootShape, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to read from. It is read but not disposed or closed by this method.
pathExpression<Func<TRoot, IEnumerable<TElement>>>An expression selecting an IEnumerable<T>.
rootShapeITypeShape<TRoot>The shape of
TRoot.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<TElement>
An asynchronous sequence of elements.
Type Parameters
TRootThe root document type.
TElementThe element type.
DeserializeAsync<T>(PipeReader, ITypeShape<T>, SerializationContext)
Deserializes a value from UTF-8 JSON read incrementally from a PipeReader using a caller-supplied starting context.
public ValueTask<T?> DeserializeAsync<T>(PipeReader reader, ITypeShape<T> shape, SerializationContext startingContext)
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
shapeITypeShape<T>The type shape describing
T.startingContextSerializationContextThe context to start this operation with, used instead of StartingContext. Its CancellationToken cancels the operation.
Returns
- ValueTask<T>
A task whose result is the deserialized value.
Type Parameters
TThe type to deserialize.
Remarks
Use this overload to supply per-call state to converters via this[object].
Exceptions
- ArgumentException
Thrown if
startingContextbelongs to an operation already in progress.- FormatException
Thrown if unexpected trailing data follows the value.
DeserializeAsync<T>(PipeReader, ITypeShape<T>, CancellationToken)
Deserializes a value from UTF-8 JSON read incrementally from a PipeReader.
public ValueTask<T?> DeserializeAsync<T>(PipeReader reader, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
A task whose result is the deserialized value.
Type Parameters
TThe type to deserialize.
Exceptions
- FormatException
Thrown if unexpected trailing data follows the value.
DeserializeAsync<T>(PipeReader, CancellationToken)
Deserializes a value from UTF-8 JSON read incrementally from a PipeReader.
public ValueTask<T?> DeserializeAsync<T>(PipeReader reader, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
A task whose result is the deserialized value.
Type Parameters
TThe type to deserialize.
Exceptions
- FormatException
Thrown if unexpected trailing data follows the value.
DeserializeAsync<T>(Stream, ITypeShape<T>, CancellationToken)
Deserializes a value from UTF-8 JSON read incrementally from a Stream.
public ValueTask<T?> DeserializeAsync<T>(Stream stream, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe source stream. It is read but not disposed or closed by this method.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
A task whose result is the deserialized value.
Type Parameters
TThe type to deserialize.
DeserializeAsync<T>(Stream, CancellationToken)
Deserializes a value from UTF-8 JSON read incrementally from a Stream.
public ValueTask<T?> DeserializeAsync<T>(Stream stream, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe source stream. It is read but not disposed or closed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
A task whose result is the deserialized value.
Type Parameters
TThe type to deserialize.
DeserializeAsync<T, TProvider>(PipeReader, CancellationToken)
Deserializes a value from UTF-8 JSON read incrementally from a PipeReader.
public ValueTask<T?> DeserializeAsync<T, TProvider>(PipeReader reader, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
A task whose result is the deserialized value.
Type Parameters
TThe type to deserialize.
TProvider
Exceptions
- FormatException
Thrown if unexpected trailing data follows the value.
DeserializeAsync<T, TProvider>(Stream, CancellationToken)
Deserializes a value from UTF-8 JSON read incrementally from a Stream.
public ValueTask<T?> DeserializeAsync<T, TProvider>(Stream stream, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe source stream. It is read but not disposed or closed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask<T>
A task whose result is the deserialized value.
Type Parameters
TThe type to deserialize.
TProvider
DeserializeAt<TValue>(scoped in ReadOnlySequence<byte>, JsonPath, ITypeShape<TValue>, MissingPathBehavior, CancellationToken)
Deserializes only the value selected by a pre-parsed JsonPath from a possibly multi-segment UTF-8 buffer.
public TValue? DeserializeAt<TValue>(scoped in ReadOnlySequence<byte> utf8Json, JsonPath path, ITypeShape<TValue> targetShape, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default)
Parameters
utf8JsonReadOnlySequence<byte>The UTF-8 JSON document, possibly spanning multiple buffer segments.
pathJsonPathThe pre-parsed path that selects the value.
targetShapeITypeShape<TValue>The shape of
TValue.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- TValue
The deserialized value, or the default value when the path is missing and default behavior is selected.
Type Parameters
TValueThe type of the selected value.
DeserializeAt<TValue>(ReadOnlyMemory<byte>, JsonPath, ITypeShape<TValue>, MissingPathBehavior, CancellationToken)
Deserializes only the value selected by a pre-parsed JsonPath from a UTF-8 buffer.
public TValue? DeserializeAt<TValue>(ReadOnlyMemory<byte> utf8Json, JsonPath path, ITypeShape<TValue> targetShape, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default)
Parameters
utf8JsonReadOnlyMemory<byte>The UTF-8 JSON document.
pathJsonPathThe pre-parsed path that selects the value.
targetShapeITypeShape<TValue>The shape of
TValue.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- TValue
The deserialized value, or the default value when the path is missing and default behavior is selected.
Type Parameters
TValueThe type of the selected value.
DeserializeAt<TValue>(string, JsonPath, MissingPathBehavior, CancellationToken)
Deserializes only the value selected by a pre-parsed JsonPath, using the type's own shape.
public TValue? DeserializeAt<TValue>(string json, JsonPath path, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default) where TValue : IShapeable<TValue>
Parameters
jsonstringThe JSON document.
pathJsonPathThe pre-parsed path that selects the value.
missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- TValue
The deserialized value.
Type Parameters
TValueThe type of the selected value.
DeserializeAt<TValue>(string, JsonPath, ITypeShape<TValue>, MissingPathBehavior, CancellationToken)
Deserializes only the value selected by a pre-parsed JsonPath, skipping unrelated JSON.
public TValue? DeserializeAt<TValue>(string json, JsonPath path, ITypeShape<TValue> targetShape, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default)
Parameters
jsonstringThe JSON document.
pathJsonPathThe pre-parsed path that selects the value.
targetShapeITypeShape<TValue>The shape of
TValue.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- TValue
The deserialized value, or the default value if the path is missing and
missingBehavioris ReturnDefault.
Type Parameters
TValueThe type of the selected value.
Remarks
The selected value is deserialized through the full converter graph, so naming policies, custom converters, and union envelopes remain correct for the target. Path segments navigate plain JSON objects and arrays; targeted deserialization is not supported when reference preservation is enabled.
DeserializeAt<TValue, TProvider>(string, JsonPath, MissingPathBehavior, CancellationToken)
Deserializes only the value selected by a pre-parsed JsonPath, using a witness type's shape.
public TValue? DeserializeAt<TValue, TProvider>(string json, JsonPath path, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default) where TProvider : IShapeable<TValue>
Parameters
jsonstringThe JSON document.
pathJsonPathThe pre-parsed path that selects the value.
missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- TValue
The deserialized value.
Type Parameters
TValueThe type of the selected value.
TProviderA witness type that provides the shape for
TValue.
DeserializeAt<TRoot, TValue>(string, Expression<Func<TRoot, TValue>>, MissingPathBehavior, CancellationToken)
Deserializes only the value selected by a member-access expression, using the root type's own shape.
public TValue? DeserializeAt<TRoot, TValue>(string json, Expression<Func<TRoot, TValue>> path, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default) where TRoot : IShapeable<TRoot>
Parameters
jsonstringThe JSON document.
pathExpression<Func<TRoot, TValue>>An expression such as
x => x.Items[3].Namethat selects the value.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- TValue
The deserialized value.
Type Parameters
TRootThe type of the root document.
TValueThe type of the selected value.
DeserializeAt<TRoot, TValue>(string, Expression<Func<TRoot, TValue>>, ITypeShape<TRoot>, MissingPathBehavior, CancellationToken)
Deserializes only the value selected by a member-access expression, skipping unrelated JSON.
public TValue? DeserializeAt<TRoot, TValue>(string json, Expression<Func<TRoot, TValue>> path, ITypeShape<TRoot> rootShape, MissingPathBehavior missingBehavior = MissingPathBehavior.Throw, CancellationToken cancellationToken = default)
Parameters
jsonstringThe JSON document.
pathExpression<Func<TRoot, TValue>>An expression such as
x => x.Items[3].Namethat selects the value.rootShapeITypeShape<TRoot>The shape of
TRoot.missingBehaviorMissingPathBehaviorControls behavior when the path is not found.
cancellationTokenCancellationTokenA cancellation token.
Returns
- TValue
The deserialized value.
Type Parameters
TRootThe type of the root document.
TValueThe type of the selected value.
Remarks
The expression is parsed but never compiled, so this overload is NativeAOT-safe. CLR member names are translated to serialized names using the configured naming policies. Array and dictionary indices must be compile-time constants; unsupported expressions are rejected before any JSON is read.
DeserializeJsonValue(scoped in ReadOnlySequence<byte>, CancellationToken)
Deserializes UTF-8 JSON, possibly spanning multiple segments, into the native JsonValue document object model.
public JsonValue? DeserializeJsonValue(scoped in ReadOnlySequence<byte> utf8Json, CancellationToken cancellationToken = default)
Parameters
utf8JsonReadOnlySequence<byte>The UTF-8 JSON bytes, possibly spanning multiple segments.
cancellationTokenCancellationTokenA cancellation token.
Returns
- JsonValue
The parsed value.
DeserializeJsonValue(Stream, CancellationToken)
Deserializes UTF-8 JSON from a stream into the native JsonValue document object model.
public JsonValue? DeserializeJsonValue(Stream stream, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream. It is read but not disposed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- JsonValue
The parsed value.
DeserializeJsonValue(ReadOnlyMemory<byte>, CancellationToken)
Deserializes UTF-8 JSON into the native JsonValue document object model.
public JsonValue? DeserializeJsonValue(ReadOnlyMemory<byte> utf8Json, CancellationToken cancellationToken = default)
Parameters
utf8JsonReadOnlyMemory<byte>The UTF-8 JSON bytes.
cancellationTokenCancellationTokenA cancellation token.
Returns
- JsonValue
The parsed value.
DeserializeJsonValue(string, CancellationToken)
Deserializes any JSON text into the native, dependency-free JsonValue document object model.
public JsonValue? DeserializeJsonValue(string json, CancellationToken cancellationToken = default)
Parameters
jsonstringThe JSON text.
cancellationTokenCancellationTokenA cancellation token.
Returns
- JsonValue
The parsed value.
Remarks
This entry point does not require a static model or a generated shape, and it neither roots
System.Text.Json nor uses reflection, so it keeps the default serializer trimming- and NativeAOT-safe.
DeserializeJsonValueAsync(PipeReader, CancellationToken)
Deserializes UTF-8 JSON from a PipeReader into the native JsonValue document object model, reading incrementally without buffering the whole document.
public ValueTask<JsonValue?> DeserializeJsonValueAsync(PipeReader reader, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe pipe. It is advanced but not completed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
DeserializeJsonValueAsync(Stream, CancellationToken)
public ValueTask<JsonValue?> DeserializeJsonValueAsync(Stream stream, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream. It is read but not disposed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
DeserializeNewlineDelimitedAsync<T>(PipeReader, ITypeShape<T>, CancellationToken)
Asynchronously streams the values of a newline-delimited JSON (NDJSON / JSON Lines) document, where each value is framed on its own line.
public IAsyncEnumerable<T?> DeserializeNewlineDelimitedAsync<T>(PipeReader reader, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An asynchronous sequence of values.
Type Parameters
TThe value type.
Exceptions
- FormatException
Thrown if two values are not separated by a line terminator.
DeserializeNewlineDelimitedAsync<T>(PipeReader, CancellationToken)
Asynchronously streams the values of a newline-delimited JSON (NDJSON / JSON Lines) document, where each value is framed on its own line.
public IAsyncEnumerable<T?> DeserializeNewlineDelimitedAsync<T>(PipeReader reader, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
readerPipeReaderThe pipe to read from. It is advanced but never completed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An asynchronous sequence of values.
Type Parameters
TThe value type.
Exceptions
- FormatException
Thrown if two values are not separated by a line terminator.
DeserializeNewlineDelimitedAsync<T>(Stream, ITypeShape<T>, CancellationToken)
Asynchronously streams the values of a newline-delimited JSON document from a stream.
public IAsyncEnumerable<T?> DeserializeNewlineDelimitedAsync<T>(Stream stream, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to read from. It is read but not disposed or closed by this method.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An asynchronous sequence of values.
Type Parameters
TThe value type.
DeserializeNewlineDelimitedAsync<T>(Stream, CancellationToken)
Asynchronously streams the values of a newline-delimited JSON document from a stream.
public IAsyncEnumerable<T?> DeserializeNewlineDelimitedAsync<T>(Stream stream, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to read from. It is read but not disposed or closed by this method.
cancellationTokenCancellationTokenA cancellation token.
Returns
- IAsyncEnumerable<T>
An asynchronous sequence of values.
Type Parameters
TThe value type.
DeserializeObject(ref JsonReader, ITypeShape, SerializationContext)
Deserializes an untyped value from JSON using the specified type shape and a caller-supplied starting context.
public object? DeserializeObject(ref JsonReader reader, ITypeShape shape, SerializationContext startingContext)
Parameters
readerJsonReaderThe reader to deserialize the value from.
shapeITypeShapeThe type shape describing the structure of the value to deserialize.
startingContextSerializationContextThe context to start this operation with, used instead of StartingContext. Its CancellationToken cancels the operation.
Returns
Exceptions
- ArgumentException
Thrown if
startingContextbelongs to an operation already in progress.
DeserializeObject(ref JsonReader, ITypeShape, CancellationToken)
Deserializes an untyped value from JSON using the specified type shape.
public object? DeserializeObject(ref JsonReader reader, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
readerJsonReaderThe reader to deserialize the value from.
shapeITypeShapeThe type shape describing the structure of the value to deserialize.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
DeserializeObject(scoped in ReadOnlySequence<byte>, ITypeShape, CancellationToken)
Deserializes an untyped value from JSON using the specified type shape.
public object? DeserializeObject(scoped in ReadOnlySequence<byte> buffer, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlySequence<byte>The JSON to deserialize from.
shapeITypeShapeThe type shape describing the structure of the value to deserialize.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
DeserializeObject(Stream, ITypeShape, CancellationToken)
Deserializes an untyped value from JSON using the specified type shape.
public object? DeserializeObject(Stream stream, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe JSON stream.
shapeITypeShapeThe type shape describing the structure of the value to deserialize.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
DeserializeObject(ReadOnlyMemory<byte>, ITypeShape, CancellationToken)
Deserializes an untyped value from JSON using the specified type shape.
public object? DeserializeObject(ReadOnlyMemory<byte> buffer, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<byte>shapeITypeShapeThe type shape describing the structure of the value to deserialize.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
DeserializeObject(string, ITypeShape, CancellationToken)
Deserializes an untyped value from JSON using the specified type shape.
public object? DeserializeObject(string json, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
jsonstringshapeITypeShapeThe type shape describing the structure of the value to deserialize.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
Deserialize<T>(ref JsonReader, ITypeShape<T>, SerializationContext)
Deserializes a value from JSON using the specified type shape and a caller-supplied starting context.
public T? Deserialize<T>(ref JsonReader reader, ITypeShape<T> shape, SerializationContext startingContext)
Parameters
readerJsonReaderThe reader to deserialize the value from.
shapeITypeShape<T>The type shape describing the structure of
T.startingContextSerializationContextThe context to start this operation with, used instead of StartingContext. Its CancellationToken cancels the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
Remarks
Use this overload to supply per-call state to converters via this[object].
Exceptions
- ArgumentException
Thrown if
startingContextbelongs to an operation already in progress.
Deserialize<T>(ref JsonReader, ITypeShape<T>, CancellationToken)
Deserializes a value from JSON using the specified type shape.
public T? Deserialize<T>(ref JsonReader reader, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
readerJsonReaderThe reader to deserialize the value from.
shapeITypeShape<T>The type shape describing the structure of
T.cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(ref JsonReader, CancellationToken)
Deserializes a value from JSON using the specified type shape.
[ExcludeFromCodeCoverage]
public T? Deserialize<T>(ref JsonReader reader, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
readerJsonReaderThe reader to deserialize the value from.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(scoped in ReadOnlySequence<byte>, ITypeShape<T>, CancellationToken)
Deserializes a value from JSON using the specified type shape.
public T? Deserialize<T>(scoped in ReadOnlySequence<byte> buffer, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlySequence<byte>The msgpack to deserialize from.
shapeITypeShape<T>The type shape describing the structure of
T.cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(scoped in ReadOnlySequence<byte>, CancellationToken)
Deserializes a value from JSON using the specified type shape.
[ExcludeFromCodeCoverage]
public T? Deserialize<T>(scoped in ReadOnlySequence<byte> bytes, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
bytesReadOnlySequence<byte>cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(Stream, ITypeShape<T>, CancellationToken)
Deserializes UTF-8 JSON from a stream.
public T? Deserialize<T>(Stream stream, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe JSON stream.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type to deserialize.
Deserialize<T>(Stream, CancellationToken)
Deserializes UTF-8 JSON from a stream.
[ExcludeFromCodeCoverage]
public T? Deserialize<T>(Stream stream, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe JSON stream.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type to deserialize.
Deserialize<T>(ReadOnlyMemory<byte>, ITypeShape<T>, CancellationToken)
Deserializes a value from JSON using the specified type shape.
public T? Deserialize<T>(ReadOnlyMemory<byte> buffer, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
bufferReadOnlyMemory<byte>The msgpack to deserialize from.
shapeITypeShape<T>The type shape describing the structure of
T.cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(ReadOnlyMemory<byte>, CancellationToken)
Deserializes a value from JSON using the specified type shape.
[ExcludeFromCodeCoverage]
public T? Deserialize<T>(ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
bytesReadOnlyMemory<byte>cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
Deserialize<T>(string, ITypeShape<T>, CancellationToken)
Deserializes JSON text.
public T? Deserialize<T>(string json, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
jsonstringThe JSON text.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type to deserialize.
Deserialize<T>(string, CancellationToken)
Deserializes JSON text.
[ExcludeFromCodeCoverage]
public T? Deserialize<T>(string json, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
jsonstringThe JSON text.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type to deserialize.
Deserialize<T, TProvider>(ref JsonReader, CancellationToken)
Deserializes a value from JSON using the specified type shape.
[ExcludeFromCodeCoverage]
public T? Deserialize<T, TProvider>(ref JsonReader reader, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
readerJsonReaderThe reader to deserialize the value from.
cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
TProvider
Deserialize<T, TProvider>(scoped in ReadOnlySequence<byte>, CancellationToken)
Deserializes a value from JSON using the specified type shape.
[ExcludeFromCodeCoverage]
public T? Deserialize<T, TProvider>(scoped in ReadOnlySequence<byte> bytes, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
bytesReadOnlySequence<byte>cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
TProvider
Deserialize<T, TProvider>(Stream, CancellationToken)
Deserializes UTF-8 JSON from a stream.
[ExcludeFromCodeCoverage]
public T? Deserialize<T, TProvider>(Stream stream, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe JSON stream.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type to deserialize.
TProvider
Deserialize<T, TProvider>(ReadOnlyMemory<byte>, CancellationToken)
Deserializes a value from JSON using the specified type shape.
[ExcludeFromCodeCoverage]
public T? Deserialize<T, TProvider>(ReadOnlyMemory<byte> bytes, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
bytesReadOnlyMemory<byte>cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- T
The deserialized value, or null if the JSON represents a null value.
Type Parameters
TThe type of value to deserialize.
TProvider
Deserialize<T, TProvider>(string, CancellationToken)
Deserializes JSON text.
[ExcludeFromCodeCoverage]
public T? Deserialize<T, TProvider>(string json, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
jsonstringThe JSON text.
cancellationTokenCancellationTokenA cancellation token.
Returns
- T
The deserialized value.
Type Parameters
TThe type to deserialize.
TProvider
GetJsonSchema(ITypeShape)
Produces a JSON Schema (draft 2020-12 by default) document that describes how this serializer represents the given type.
public string GetJsonSchema(ITypeShape typeShape)
Parameters
typeShapeITypeShapeThe shape of the type.
Returns
- string
The JSON Schema document as a JSON string.
Remarks
The schema reflects the effective configuration, including the property naming policy, enum-by-name serialization, reference preservation, and runtime union configuration. Custom converters that do not override GetJsonSchema(JsonSchemaContext, ITypeShape) produce a permissive schema annotated with a conspicuous comment rather than false precision.
GetJsonSchema(ITypeShape, JsonSchemaOptions)
Produces a JSON Schema document that describes how this serializer represents the given type.
public string GetJsonSchema(ITypeShape typeShape, JsonSchemaOptions options)
Parameters
typeShapeITypeShapeThe shape of the type.
optionsJsonSchemaOptionsThe schema generation options.
Returns
- string
The JSON Schema document as a JSON string.
Exceptions
- ArgumentOutOfRangeException
The requested dialect is not supported.
GetJsonSchema<T>()
Produces a JSON Schema (draft 2020-12 by default) document that describes how this serializer represents the given type.
public string GetJsonSchema<T>() where T : IShapeable<T>
Returns
- string
The JSON Schema document as a JSON string.
Type Parameters
TThe type to describe.
Remarks
The schema reflects the effective configuration, including the property naming policy, enum-by-name serialization, reference preservation, and runtime union configuration. Custom converters that do not override GetJsonSchema(JsonSchemaContext, ITypeShape) produce a permissive schema annotated with a conspicuous comment rather than false precision.
GetJsonSchema<T>(JsonSchemaOptions)
Produces a JSON Schema document using the specified options.
public string GetJsonSchema<T>(JsonSchemaOptions options) where T : IShapeable<T>
Parameters
optionsJsonSchemaOptionsThe schema generation options.
Returns
- string
The JSON Schema document as a JSON string.
Type Parameters
TThe type to describe.
GetJsonSchema<T>(ITypeShape<T>)
Produces a JSON Schema (draft 2020-12 by default) document that describes how this serializer represents the given type.
public string GetJsonSchema<T>(ITypeShape<T> shape)
Parameters
shapeITypeShape<T>The shape of the type.
Returns
- string
The JSON Schema document as a JSON string.
Type Parameters
TThe type to describe.
Remarks
The schema reflects the effective configuration, including the property naming policy, enum-by-name serialization, reference preservation, and runtime union configuration. Custom converters that do not override GetJsonSchema(JsonSchemaContext, ITypeShape) produce a permissive schema annotated with a conspicuous comment rather than false precision.
GetJsonSchema<T>(ITypeShape<T>, JsonSchemaOptions)
Produces a JSON Schema document that describes how this serializer represents the given type.
public string GetJsonSchema<T>(ITypeShape<T> shape, JsonSchemaOptions options)
Parameters
shapeITypeShape<T>The shape of the type.
optionsJsonSchemaOptionsThe schema generation options.
Returns
- string
The JSON Schema document as a JSON string.
Type Parameters
TThe type to describe.
Exceptions
- ArgumentOutOfRangeException
The requested dialect is not supported.
GetJsonSchema<T, TProvider>()
Produces a JSON Schema document that describes how this serializer represents T,
using a witness type for the shape.
public string GetJsonSchema<T, TProvider>() where TProvider : IShapeable<T>
Returns
- string
The JSON Schema document as a JSON string.
Type Parameters
TThe type to describe.
TProviderA witness type that provides the shape for
T.
GetJsonSchema<T, TProvider>(JsonSchemaOptions)
Produces a JSON Schema document using the specified options and witness type.
public string GetJsonSchema<T, TProvider>(JsonSchemaOptions options) where TProvider : IShapeable<T>
Parameters
optionsJsonSchemaOptionsThe schema generation options.
Returns
- string
The JSON Schema document as a JSON string.
Type Parameters
TThe type to describe.
TProviderA witness type that provides the shape for
T.
SerializeArrayAsync<T>(PipeWriter, IAsyncEnumerable<T>, ITypeShape<T>, CancellationToken)
Asynchronously serializes an IAsyncEnumerable<T> as a top-level JSON array, writing elements as they are produced with bounded memory and backpressure.
public ValueTask SerializeArrayAsync<T>(PipeWriter writer, IAsyncEnumerable<T> values, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
writerPipeWriterThe pipe to write to. It is flushed but never completed by this method.
valuesIAsyncEnumerable<T>The asynchronous source of elements.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
Type Parameters
TThe element type.
SerializeArrayAsync<T>(PipeWriter, IAsyncEnumerable<T>, CancellationToken)
Asynchronously serializes an IAsyncEnumerable<T> as a top-level JSON array, writing elements as they are produced with bounded memory and backpressure.
public ValueTask SerializeArrayAsync<T>(PipeWriter writer, IAsyncEnumerable<T> values, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
writerPipeWriterThe pipe to write to. It is flushed but never completed by this method.
valuesIAsyncEnumerable<T>The asynchronous source of elements.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
Type Parameters
TThe element type.
SerializeArrayAsync<T>(Stream, IAsyncEnumerable<T>, ITypeShape<T>, CancellationToken)
Asynchronously serializes an IAsyncEnumerable<T> as a top-level JSON array to a stream.
public ValueTask SerializeArrayAsync<T>(Stream stream, IAsyncEnumerable<T> values, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write to. It is flushed but not disposed or closed by this method.
valuesIAsyncEnumerable<T>The asynchronous source of elements.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
Type Parameters
TThe element type.
SerializeArrayAsync<T>(Stream, IAsyncEnumerable<T>, CancellationToken)
Asynchronously serializes an IAsyncEnumerable<T> as a top-level JSON array to a stream.
public ValueTask SerializeArrayAsync<T>(Stream stream, IAsyncEnumerable<T> values, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to write to. It is flushed but not disposed or closed by this method.
valuesIAsyncEnumerable<T>The asynchronous source of elements.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
Type Parameters
TThe element type.
SerializeAsync<T>(PipeWriter, T?, ITypeShape<T>, SerializationContext)
Serializes a value as UTF-8 JSON to a PipeWriter incrementally using a caller-supplied starting context, flushing periodically so large graphs are written with bounded memory and backpressure.
public ValueTask SerializeAsync<T>(PipeWriter writer, T? value, ITypeShape<T> shape, SerializationContext startingContext)
Parameters
writerPipeWriterThe pipe to write to. It is flushed but never completed by this method.
valueTThe value to serialize.
shapeITypeShape<T>The type shape describing
T.startingContextSerializationContextThe context to start this operation with, used instead of StartingContext. Its CancellationToken cancels the operation.
Returns
- ValueTask
A task tracking the asynchronous serialization.
Type Parameters
TThe type of value to serialize.
Remarks
Use this overload to supply per-call state to converters via this[object].
Exceptions
- ArgumentException
Thrown if
startingContextbelongs to an operation already in progress.
SerializeAsync<T>(PipeWriter, T?, ITypeShape<T>, CancellationToken)
Serializes a value as UTF-8 JSON to a PipeWriter incrementally, flushing periodically so large graphs are written with bounded memory and backpressure.
public ValueTask SerializeAsync<T>(PipeWriter writer, T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
writerPipeWriterThe pipe to write to. It is flushed but never completed by this method.
valueTThe value to serialize.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the asynchronous serialization.
Type Parameters
TThe type of value to serialize.
SerializeAsync<T>(PipeWriter, T?, CancellationToken)
Serializes a value as UTF-8 JSON to a PipeWriter incrementally, flushing periodically so large graphs are written with bounded memory and backpressure.
public ValueTask SerializeAsync<T>(PipeWriter writer, T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
writerPipeWriterThe pipe to write to. It is flushed but never completed by this method.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the asynchronous serialization.
Type Parameters
TThe type of value to serialize.
SerializeAsync<T>(Stream, T?, ITypeShape<T>, CancellationToken)
Serializes a value as UTF-8 JSON to a Stream incrementally.
public ValueTask SerializeAsync<T>(Stream stream, T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe destination stream. It is flushed but not disposed or closed by this method.
valueTThe value to serialize.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the asynchronous serialization.
Type Parameters
TThe type of value to serialize.
SerializeAsync<T>(Stream, T?, CancellationToken)
Serializes a value as UTF-8 JSON to a Stream incrementally.
public ValueTask SerializeAsync<T>(Stream stream, T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe destination stream. It is flushed but not disposed or closed by this method.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the asynchronous serialization.
Type Parameters
TThe type of value to serialize.
SerializeAsync<T, TProvider>(PipeWriter, T?, CancellationToken)
Serializes a value as UTF-8 JSON to a PipeWriter incrementally, flushing periodically so large graphs are written with bounded memory and backpressure.
public ValueTask SerializeAsync<T, TProvider>(PipeWriter writer, T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
writerPipeWriterThe pipe to write to. It is flushed but never completed by this method.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the asynchronous serialization.
Type Parameters
TThe type of value to serialize.
TProvider
SerializeAsync<T, TProvider>(Stream, T?, CancellationToken)
Serializes a value as UTF-8 JSON to a Stream incrementally.
public ValueTask SerializeAsync<T, TProvider>(Stream stream, T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe destination stream. It is flushed but not disposed or closed by this method.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the asynchronous serialization.
Type Parameters
TThe type of value to serialize.
TProvider
SerializeJsonValue(JsonValue?, CancellationToken)
Serializes a JsonValue to JSON text.
public string SerializeJsonValue(JsonValue? value, CancellationToken cancellationToken = default)
Parameters
valueJsonValueThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- string
The serialized JSON text.
SerializeJsonValue(IBufferWriter<byte>, JsonValue?, CancellationToken)
Serializes a JsonValue as UTF-8 JSON to a buffer.
public void SerializeJsonValue(IBufferWriter<byte> writer, JsonValue? value, CancellationToken cancellationToken = default)
Parameters
writerIBufferWriter<byte>The destination buffer.
valueJsonValueThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
SerializeJsonValue(Stream, JsonValue?, CancellationToken)
Serializes a JsonValue as UTF-8 JSON to a stream.
public void SerializeJsonValue(Stream stream, JsonValue? value, CancellationToken cancellationToken = default)
Parameters
streamStreamThe destination stream. It is flushed but not disposed by this method.
valueJsonValueThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
SerializeJsonValueAsync(PipeWriter, JsonValue?, CancellationToken)
Serializes a JsonValue as UTF-8 JSON to a PipeWriter incrementally.
public ValueTask SerializeJsonValueAsync(PipeWriter writer, JsonValue? value, CancellationToken cancellationToken = default)
Parameters
writerPipeWriterThe pipe. It is flushed but not completed by this method.
valueJsonValueThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
SerializeJsonValueAsync(Stream, JsonValue?, CancellationToken)
public ValueTask SerializeJsonValueAsync(Stream stream, JsonValue? value, CancellationToken cancellationToken = default)
Parameters
streamStreamThe destination stream. It is flushed but not disposed by this method.
valueJsonValueThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task that completes when serialization finishes.
SerializeNewlineDelimitedAsync<T>(PipeWriter, IAsyncEnumerable<T>, ITypeShape<T>, CancellationToken)
Asynchronously serializes an IAsyncEnumerable<T> as newline-delimited JSON (one value per line).
public ValueTask SerializeNewlineDelimitedAsync<T>(PipeWriter writer, IAsyncEnumerable<T> values, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
writerPipeWriterThe pipe to write to. It is flushed but never completed by this method.
valuesIAsyncEnumerable<T>The asynchronous source of values.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
Type Parameters
TThe value type.
SerializeNewlineDelimitedAsync<T>(PipeWriter, IAsyncEnumerable<T>, CancellationToken)
Asynchronously serializes an IAsyncEnumerable<T> as newline-delimited JSON (one value per line).
public ValueTask SerializeNewlineDelimitedAsync<T>(PipeWriter writer, IAsyncEnumerable<T> values, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
writerPipeWriterThe pipe to write to. It is flushed but never completed by this method.
valuesIAsyncEnumerable<T>The asynchronous source of values.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
Type Parameters
TThe value type.
SerializeNewlineDelimitedAsync<T>(Stream, IAsyncEnumerable<T>, ITypeShape<T>, CancellationToken)
Asynchronously serializes an IAsyncEnumerable<T> as newline-delimited JSON to a stream.
public ValueTask SerializeNewlineDelimitedAsync<T>(Stream stream, IAsyncEnumerable<T> values, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe stream to write to. It is flushed but not disposed or closed by this method.
valuesIAsyncEnumerable<T>The asynchronous source of values.
shapeITypeShape<T>The shape of
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
Type Parameters
TThe value type.
SerializeNewlineDelimitedAsync<T>(Stream, IAsyncEnumerable<T>, CancellationToken)
Asynchronously serializes an IAsyncEnumerable<T> as newline-delimited JSON to a stream.
public ValueTask SerializeNewlineDelimitedAsync<T>(Stream stream, IAsyncEnumerable<T> values, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe stream to write to. It is flushed but not disposed or closed by this method.
valuesIAsyncEnumerable<T>The asynchronous source of values.
cancellationTokenCancellationTokenA cancellation token.
Returns
- ValueTask
A task tracking the serialization.
Type Parameters
TThe value type.
SerializeObject(ref JsonWriter, object?, ITypeShape, SerializationContext)
Serializes an untyped value to JSON using the specified type shape and a caller-supplied starting context.
public void SerializeObject(ref JsonWriter writer, object? value, ITypeShape shape, SerializationContext startingContext)
Parameters
writerJsonWriterThe writer to serialize the value into.
valueobjectThe value to serialize.
shapeITypeShapeThe type shape describing the structure of
value.startingContextSerializationContextThe context to start this operation with, used instead of StartingContext. Its CancellationToken cancels the operation.
Exceptions
- ArgumentException
Thrown if
startingContextbelongs to an operation already in progress.
SerializeObject(ref JsonWriter, object?, ITypeShape, CancellationToken)
Serializes an untyped value to JSON using the specified type shape.
public void SerializeObject(ref JsonWriter writer, object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
writerJsonWriterThe writer to serialize the value into.
valueobjectThe value to serialize.
shapeITypeShapeThe type shape describing the structure of
value.cancellationTokenCancellationTokenA token to cancel the operation.
SerializeObject(IBufferWriter<byte>, object?, ITypeShape, CancellationToken)
Serializes an untyped value to JSON using the specified type shape.
public void SerializeObject(IBufferWriter<byte> writer, object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
writerIBufferWriter<byte>The writer to serialize the value into.
valueobjectThe value to serialize.
shapeITypeShapeThe type shape describing the structure of
value.cancellationTokenCancellationTokenA token to cancel the operation.
SerializeObject(Stream, object?, ITypeShape, CancellationToken)
Serializes an untyped value to JSON using the specified type shape.
public void SerializeObject(Stream stream, object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe destination stream.
valueobjectThe value to serialize.
shapeITypeShapeThe type shape describing the structure of
value.cancellationTokenCancellationTokenA token to cancel the operation.
SerializeObject(object?, ITypeShape, CancellationToken)
Serializes an untyped value to JSON using the specified type shape.
public string SerializeObject(object? value, ITypeShape shape, CancellationToken cancellationToken = default)
Parameters
valueobjectThe value to serialize.
shapeITypeShapeThe type shape describing the structure of
value.cancellationTokenCancellationTokenA token to cancel the operation.
Returns
- string
The serialized JSON text.
Serialize<T>(ref JsonWriter, in T?, ITypeShape<T>, SerializationContext)
Serializes a value to JSON using the specified type shape and a caller-supplied starting context.
public void Serialize<T>(ref JsonWriter writer, in T? value, ITypeShape<T> shape, SerializationContext startingContext)
Parameters
writerJsonWriterThe writer to serialize the value into.
valueTThe value to serialize.
shapeITypeShape<T>The type shape describing the structure of
T.startingContextSerializationContextThe context to start this operation with, used instead of StartingContext. Its CancellationToken cancels the operation.
Type Parameters
TThe type of value to serialize.
Remarks
Use this overload to supply per-call state to converters via this[object].
Exceptions
- ArgumentException
Thrown if
startingContextbelongs to an operation already in progress.
Serialize<T>(ref JsonWriter, in T?, ITypeShape<T>, CancellationToken)
Serializes a value to JSON using the specified type shape.
public void Serialize<T>(ref JsonWriter writer, in T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
writerJsonWriterThe writer to serialize the value into.
valueTThe value to serialize.
shapeITypeShape<T>The type shape describing the structure of
T.cancellationTokenCancellationTokenA token to cancel the operation.
Type Parameters
TThe type of value to serialize.
Serialize<T>(ref JsonWriter, in T?, CancellationToken)
Serializes a value to JSON using the specified type shape.
[ExcludeFromCodeCoverage]
public void Serialize<T>(ref JsonWriter writer, in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
writerJsonWriterThe writer to serialize the value into.
valueTThe value to serialize.
cancellationTokenCancellationTokenA token to cancel the operation.
Type Parameters
TThe type of value to serialize.
Serialize<T>(IBufferWriter<byte>, in T?, ITypeShape<T>, CancellationToken)
Serializes a value as UTF-8 JSON to a byte buffer.
public void Serialize<T>(IBufferWriter<byte> writer, in T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
writerIBufferWriter<byte>The destination buffer.
valueTThe value to serialize.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type of value to serialize.
Serialize<T>(IBufferWriter<byte>, in T?, CancellationToken)
Serializes a value as UTF-8 JSON to a byte buffer.
[ExcludeFromCodeCoverage]
public void Serialize<T>(IBufferWriter<byte> writer, in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
writerIBufferWriter<byte>The destination buffer.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type of value to serialize.
Serialize<T>(Stream, in T?, ITypeShape<T>, CancellationToken)
Serializes a value as UTF-8 JSON to a stream.
public void Serialize<T>(Stream stream, in T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
streamStreamThe destination stream.
valueTThe value to serialize.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type of value to serialize.
Serialize<T>(Stream, in T?, CancellationToken)
Serializes a value as UTF-8 JSON to a stream.
[ExcludeFromCodeCoverage]
public void Serialize<T>(Stream stream, in T? value, CancellationToken cancellationToken = default) where T : IShapeable<T>
Parameters
streamStreamThe destination stream.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type of value to serialize.
Serialize<T>(in T?, ITypeShape<T>, CancellationToken)
Serializes a value to JSON text.
public string Serialize<T>(in T? value, ITypeShape<T> shape, CancellationToken cancellationToken = default)
Parameters
valueTThe value to serialize.
shapeITypeShape<T>The type shape describing
T.cancellationTokenCancellationTokenA cancellation token.
Returns
- string
The serialized JSON text.
Type Parameters
TThe type of value to serialize.
Serialize<T>(in T?, CancellationToken)
Serializes a value to JSON text.
[ExcludeFromCodeCoverage]
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 serialized JSON text.
Type Parameters
TThe type of value to serialize.
Serialize<T, TProvider>(ref JsonWriter, in T?, CancellationToken)
Serializes a value to JSON using the specified type shape.
[ExcludeFromCodeCoverage]
public void Serialize<T, TProvider>(ref JsonWriter writer, in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
writerJsonWriterThe writer to serialize the value into.
valueTThe value to serialize.
cancellationTokenCancellationTokenA token to cancel the operation.
Type Parameters
TThe type of value to serialize.
TProvider
Serialize<T, TProvider>(IBufferWriter<byte>, in T?, CancellationToken)
Serializes a value as UTF-8 JSON to a byte buffer.
[ExcludeFromCodeCoverage]
public void Serialize<T, TProvider>(IBufferWriter<byte> writer, in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
writerIBufferWriter<byte>The destination buffer.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type of value to serialize.
TProvider
Serialize<T, TProvider>(Stream, in T?, CancellationToken)
Serializes a value as UTF-8 JSON to a stream.
[ExcludeFromCodeCoverage]
public void Serialize<T, TProvider>(Stream stream, in T? value, CancellationToken cancellationToken = default) where TProvider : IShapeable<T>
Parameters
streamStreamThe destination stream.
valueTThe value to serialize.
cancellationTokenCancellationTokenA cancellation token.
Type Parameters
TThe type of value to serialize.
TProvider
Serialize<T, TProvider>(in T?, CancellationToken)
Serializes a value to JSON text.
[ExcludeFromCodeCoverage]
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 serialized JSON text.
Type Parameters
TThe type of value to serialize.
TProvider
WithSystemTextJsonConverters()
Creates a copy of this serializer with opt-in converters registered for the JsonElement, JsonNode, and JsonDocument types.
public JsonSerializer WithSystemTextJsonConverters()
Returns
- JsonSerializer
A new JsonSerializer instance with the System.Text.Json interop converters registered.
Remarks
These converters bridge to System.Text.Json by round-tripping raw JSON text, so they add no reflection.
However, calling this method roots the System.Text.Json assembly. Because the converters are registered
only when you call this method, the default serializer keeps System.Text.Json trimmable and out of the
NativeAOT closure.
WithUntypedConverters()
Creates a copy of this serializer with opt-in converters registered for untyped JSON: the CLR object and ExpandoObject types, along with the native JsonValue document object model when it appears as a member of another shaped type.
public JsonSerializer WithUntypedConverters()
Returns
- JsonSerializer
A new JsonSerializer instance with the untyped converters registered.
Remarks
These converters use no reflection and root no additional dependencies. Because they are registered only when you call this method, the default serializer remains trimming- and NativeAOT-safe and never discovers arbitrary CLR types.
A non-null value typed as object deserializes to a boxed JsonValue, while a JSON null deserializes to C# null. Serializing an object requires a JsonValue or a boxed JSON primitive; any other CLR type throws a NotSupportedException that guides you to use a generated type shape instead.