JsonSerializer.SerializeAsync 方法

定义

重载

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

将指定类型的值异步转换为 UTF-8 编码的 JSON 文本并将它写入指定流。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, JsonSerializerOptions, CancellationToken)

将泛型类型参数指定的类型的值异步转换为 UTF-8 编码的 JSON 文本并将它写入流。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, JsonSerializerOptions, CancellationToken)

将指定类型的值异步转换为 UTF-8 编码的 JSON 文本并将它写入指定流。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

参数

utf8Json
Stream

要写入的 UTF-8 流。The UTF-8 stream to write to.

value
Object

要转换的值。The value to convert.

inputType
Type

要转换的 value 的类型。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.

返回

Task

表示异步写入操作的任务。A task that represents the asynchronous write operation.

例外

inputTypevalue 不兼容。inputType is not compatible with value.

utf8JsoninputTypenullutf8Json or inputType is null.

inputType 或其可序列化成员没有兼容的 JsonConverterThere is no compatible JsonConverter for inputType or its serializable members.

注解

有关详细信息,请参阅 如何序列化和反序列化 JSONFor more information, see How to serialize and deserialize JSON.

适用于

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

将泛型类型参数指定的类型的值异步转换为 UTF-8 编码的 JSON 文本并将它写入流。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

类型参数

TValue

要序列化的值的类型。The type of the value to serialize.

参数

utf8Json
Stream

要写入的 UTF-8 流。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.

返回

Task

表示异步写入操作的任务。A task that represents the asynchronous write operation.

例外

utf8Jsonnullutf8Json is null.

TValue 或其可序列化成员没有兼容的 JsonConverterThere is no compatible JsonConverter for TValue or its serializable members.

注解

有关详细信息,请参阅 如何序列化和反序列化 JSONFor more information, see How to serialize and deserialize JSON.

适用于