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(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<TValue>(Stream, TValue, JsonTypeInfo<TValue>, CancellationToken)

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

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(Stream, Object, Type, JsonSerializerContext, CancellationToken)

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

Task

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)

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

Task

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.

Remarks

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

Applies to

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

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

Task

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>(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.

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

Task

A task that represents the asynchronous write operation.

Exceptions

utf8Json is null.

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

Remarks

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

Applies to