JsonSerializer.SerializeAsync Method

Definition

Overloads

SerializeAsync(Stream, Object, Type, JsonSerializerContext, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the Stream.

SerializeAsync(PipeWriter, Object, Type, JsonSerializerContext, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken)

Asynchronously converts the value of a specified type to UTF-8 encoded JSON text and writes it to the specified stream.

SerializeAsync(Stream, Object, JsonTypeInfo, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and writes it to the Stream.

SerializeAsync(PipeWriter, Object, JsonTypeInfo, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

SerializeAsync(PipeWriter, Object, Type, JsonSerializerOptions, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

SerializeAsync<TValue>(PipeWriter, TValue, JsonSerializerOptions, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

SerializeAsync<TValue>(PipeWriter, TValue, JsonTypeInfo<TValue>, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

SerializeAsync<TValue>(Stream, TValue, JsonSerializerOptions, CancellationToken)

Asynchronously converts a value of a type specified by a generic type parameter to UTF-8 encoded JSON text and writes it to a stream.

SerializeAsync<TValue>(Stream, TValue, JsonTypeInfo<TValue>, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the Stream.

SerializeAsync(Stream, Object, Type, JsonSerializerContext, CancellationToken)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Converts the provided value to UTF-8 encoded JSON text and write it to the Stream.

public static System.Threading.Tasks.Task SerializeAsync (System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Stream * obj * Type * System.Text.Json.Serialization.JsonSerializerContext * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SerializeAsync (utf8Json As Stream, value As Object, inputType As Type, context As JsonSerializerContext, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

utf8Json
Stream

The UTF-8 Stream to write to.

value
Object

The value to convert.

inputType
Type

The type of the value to convert.

context
JsonSerializerContext

A metadata provider for serializable types.

cancellationToken
CancellationToken

The CancellationToken that can be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

inputType is not compatible with value.

utf8Json, inputType, or context is null.

There is no compatible JsonConverter for inputType or its serializable members.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Serialize(Stream, Object, Type, JsonSerializerContext).

Applies to

SerializeAsync(PipeWriter, Object, Type, JsonSerializerContext, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

public static System.Threading.Tasks.Task SerializeAsync (System.IO.Pipelines.PipeWriter utf8Json, object? value, Type inputType, System.Text.Json.Serialization.JsonSerializerContext context, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Pipelines.PipeWriter * obj * Type * System.Text.Json.Serialization.JsonSerializerContext * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SerializeAsync (utf8Json As PipeWriter, value As Object, inputType As Type, context As JsonSerializerContext, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

utf8Json
PipeWriter

The UTF-8 PipeWriter to write to.

value
Object

The value to convert.

inputType
Type

The type of the value to convert.

context
JsonSerializerContext

A metadata provider for serializable types.

cancellationToken
CancellationToken

The CancellationToken that can be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

inputType is not compatible with value.

utf8Json, inputType, or context is null.

There is no compatible JsonConverter

for inputType or its serializable members.

Applies to

SerializeAsync(Stream, Object, Type, JsonSerializerOptions, CancellationToken)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Asynchronously converts the value of a specified type to UTF-8 encoded JSON text and writes it to the specified stream.

public static System.Threading.Tasks.Task SerializeAsync (System.IO.Stream utf8Json, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
public static System.Threading.Tasks.Task SerializeAsync (System.IO.Stream utf8Json, object value, Type inputType, System.Text.Json.JsonSerializerOptions options = default, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Stream * obj * Type * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function SerializeAsync (utf8Json As Stream, value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task
Public Function SerializeAsync (utf8Json As Stream, value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

utf8Json
Stream

The UTF-8 stream to write to.

value
Object

The value to convert.

inputType
Type

The type of the value to convert.

options
JsonSerializerOptions

Options to control serialization behavior.

cancellationToken
CancellationToken

A token that may be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

inputType is not compatible with value.

utf8Json or inputType is null.

There is no compatible JsonConverter for inputType or its serializable members.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For more information, see How to serialize and deserialize JSON.

This method stores in the task it returns all non-usage exceptions that the method's synchronous counterpart can throw. If an exception is stored into the returned task, that exception will be thrown when the task is awaited. Usage exceptions, such as ArgumentException, are still thrown synchronously. For the stored exceptions, see the exceptions thrown by Serialize(Stream, Object, Type, JsonSerializerOptions).

Applies to

SerializeAsync(Stream, Object, JsonTypeInfo, CancellationToken)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Converts the provided value to UTF-8 encoded JSON text and writes it to the Stream.

public static System.Threading.Tasks.Task SerializeAsync (System.IO.Stream utf8Json, object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Stream * obj * System.Text.Json.Serialization.Metadata.JsonTypeInfo * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SerializeAsync (utf8Json As Stream, value As Object, jsonTypeInfo As JsonTypeInfo, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

utf8Json
Stream

The UTF-8 Stream to write to.

value
Object

The value to convert.

jsonTypeInfo
JsonTypeInfo

Metadata about the type to convert.

cancellationToken
CancellationToken

The CancellationToken that can be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

utf8Json is null.

value does not match the type of jsonTypeInfo.

The cancellation token was canceled. This exception is stored into the returned task.

Applies to

SerializeAsync(PipeWriter, Object, JsonTypeInfo, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

public static System.Threading.Tasks.Task SerializeAsync (System.IO.Pipelines.PipeWriter utf8Json, object? value, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Pipelines.PipeWriter * obj * System.Text.Json.Serialization.Metadata.JsonTypeInfo * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SerializeAsync (utf8Json As PipeWriter, value As Object, jsonTypeInfo As JsonTypeInfo, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

utf8Json
PipeWriter

The UTF-8 PipeWriter to write to.

value
Object

The value to convert.

jsonTypeInfo
JsonTypeInfo

Metadata about the type to convert.

cancellationToken
CancellationToken

The CancellationToken that can be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

utf8Json is null.

value does not match the type of jsonTypeInfo.

Applies to

SerializeAsync(PipeWriter, Object, Type, JsonSerializerOptions, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

public static System.Threading.Tasks.Task SerializeAsync (System.IO.Pipelines.PipeWriter utf8Json, object? value, Type inputType, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Pipelines.PipeWriter * obj * Type * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SerializeAsync (utf8Json As PipeWriter, value As Object, inputType As Type, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task

Parameters

utf8Json
PipeWriter

The UTF-8 PipeWriter to write to.

value
Object

The value to convert.

inputType
Type

The type of the value to convert.

options
JsonSerializerOptions

Options to control the conversion behavior.

cancellationToken
CancellationToken

The CancellationToken that can be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

inputType is not compatible with value.

utf8Json or inputType is null.

There is no compatible JsonConverter

for inputType or its serializable members.

Applies to

SerializeAsync<TValue>(PipeWriter, TValue, JsonSerializerOptions, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

public static System.Threading.Tasks.Task SerializeAsync<TValue> (System.IO.Pipelines.PipeWriter utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Pipelines.PipeWriter * 'Value * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SerializeAsync(Of TValue) (utf8Json As PipeWriter, value As TValue, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task

Type Parameters

TValue

The type of the value to serialize.

Parameters

utf8Json
PipeWriter

The UTF-8 PipeWriter to write to.

value
TValue

The value to convert.

options
JsonSerializerOptions

Options to control the conversion behavior.

cancellationToken
CancellationToken

The CancellationToken that can be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

utf8Json is null.

There is no compatible JsonConverter

for TValue or its serializable members.

Applies to

SerializeAsync<TValue>(PipeWriter, TValue, JsonTypeInfo<TValue>, CancellationToken)

Converts the provided value to UTF-8 encoded JSON text and write it to the PipeWriter.

public static System.Threading.Tasks.Task SerializeAsync<TValue> (System.IO.Pipelines.PipeWriter utf8Json, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Pipelines.PipeWriter * 'Value * System.Text.Json.Serialization.Metadata.JsonTypeInfo<'Value> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SerializeAsync(Of TValue) (utf8Json As PipeWriter, value As TValue, jsonTypeInfo As JsonTypeInfo(Of TValue), Optional cancellationToken As CancellationToken = Nothing) As Task

Type Parameters

TValue

The type of the value to serialize.

Parameters

utf8Json
PipeWriter

The UTF-8 PipeWriter to write to.

value
TValue

The value to convert.

jsonTypeInfo
JsonTypeInfo<TValue>

Metadata about the type to convert.

cancellationToken
CancellationToken

The CancellationToken that can be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

utf8Json is null.

Applies to

SerializeAsync<TValue>(Stream, TValue, JsonSerializerOptions, CancellationToken)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Asynchronously converts a value of a type specified by a generic type parameter to UTF-8 encoded JSON text and writes it to a stream.

public static System.Threading.Tasks.Task SerializeAsync<TValue> (System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
public static System.Threading.Tasks.Task SerializeAsync<TValue> (System.IO.Stream utf8Json, TValue value, System.Text.Json.JsonSerializerOptions options = default, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Stream * 'Value * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Shared Function SerializeAsync(Of TValue) (utf8Json As Stream, value As TValue, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task
Public Function SerializeAsync(Of TValue) (utf8Json As Stream, value As TValue, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As Task

Type Parameters

TValue

The type of the value to serialize.

Parameters

utf8Json
Stream

The UTF-8 stream to write to.

value
TValue

The value to convert.

options
JsonSerializerOptions

Options to control serialization behavior.

cancellationToken
CancellationToken

A token that may be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

utf8Json is null.

There is no compatible JsonConverter for TValue or its serializable members.

The cancellation token was canceled. This exception is stored into the returned task.

Remarks

For more information, see How to serialize and deserialize JSON.

Applies to

SerializeAsync<TValue>(Stream, TValue, JsonTypeInfo<TValue>, CancellationToken)

Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs
Source:
JsonSerializer.Write.Stream.cs

Converts the provided value to UTF-8 encoded JSON text and write it to the Stream.

public static System.Threading.Tasks.Task SerializeAsync<TValue> (System.IO.Stream utf8Json, TValue value, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default);
static member SerializeAsync : System.IO.Stream * 'Value * System.Text.Json.Serialization.Metadata.JsonTypeInfo<'Value> * System.Threading.CancellationToken -> System.Threading.Tasks.Task
Public Function SerializeAsync(Of TValue) (utf8Json As Stream, value As TValue, jsonTypeInfo As JsonTypeInfo(Of TValue), Optional cancellationToken As CancellationToken = Nothing) As Task

Type Parameters

TValue

The type of the value to serialize.

Parameters

utf8Json
Stream

The UTF-8 Stream to write to.

value
TValue

The value to convert.

jsonTypeInfo
JsonTypeInfo<TValue>

Metadata about the type to convert.

cancellationToken
CancellationToken

The CancellationToken that can be used to cancel the write operation.

Returns

A task that represents the asynchronous write operation.

Exceptions

utf8Json is null.

There is no compatible JsonConverter for TValue or its serializable members.

The cancellation token was canceled. This exception is stored into the returned task.

Applies to