JsonSerializer.SerializeAsync 方法

定义

重载

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

将提供的值转换为 UTF-8 编码的 JSON 文本,并将其写入 。Stream

SerializeAsync(Stream, Object, JsonTypeInfo, CancellationToken)

将提供的值转换为 UTF-8 编码的 JSON 文本,并将其写入 。Stream

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

将指定类型的值异步转换为 UTF-8 编码的 JSON 文本并将它写入指定流。

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

将提供的值转换为 UTF-8 编码的 JSON 文本,并将其写入 。Stream

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

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

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

将提供的值转换为 UTF-8 编码的 JSON 文本,并将其写入 。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

参数

utf8Json
Stream

要写入的 UTF-8 Stream

value
Object

要转换的值。

inputType
Type

要转换的 value 的类型。

context
JsonSerializerContext

可序列化类型的元数据提供程序。

cancellationToken
CancellationToken

CancellationToken可用于取消写入操作的 。

返回

表示异步写入操作的任务。

例外

inputTypevalue 不兼容。

utf8JsoninputTypecontextnull

inputType 或其可序列化成员没有兼容的 JsonConverter

取消令牌已取消。 此异常存储在返回的任务中。

注解

此方法存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 Serialize(Stream, Object, Type, JsonSerializerContext)异常。

适用于

SerializeAsync(Stream, Object, JsonTypeInfo, CancellationToken)

将提供的值转换为 UTF-8 编码的 JSON 文本,并将其写入 。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

参数

utf8Json
Stream

要写入的 UTF-8 Stream

value
Object

要转换的值。

jsonTypeInfo
JsonTypeInfo

有关要转换的类型的元数据。

cancellationToken
CancellationToken

CancellationToken可用于取消写入操作的 。

返回

表示异步写入操作的任务。

例外

utf8Jsonnull

value 与 的类型 jsonTypeInfo不匹配。

取消令牌已取消。 此异常存储在返回的任务中。

适用于

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

将指定类型的值异步转换为 UTF-8 编码的 JSON 文本并将它写入指定流。

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

参数

utf8Json
Stream

要写入的 UTF-8 流。

value
Object

要转换的值。

inputType
Type

要转换的 value 的类型。

options
JsonSerializerOptions

用于控制序列化行为的选项。

cancellationToken
CancellationToken

可能用于取消写入操作的标记。

返回

表示异步写入操作的任务。

例外

inputTypevalue 不兼容。

utf8JsoninputTypenull

inputType 或其可序列化成员没有兼容的 JsonConverter

取消令牌已取消。 此异常存储在返回的任务中。

注解

有关详细信息,请参阅 如何序列化和反序列化 JSON

此方法存储在任务中,它返回该方法的同步对应项可能引发的所有非使用异常。 如果异常存储在返回的任务中,则在等待任务时将引发该异常。 使用异常(如 ArgumentException)仍会同步引发。 有关存储的异常,请参阅 引发的 Serialize(Stream, Object, Type, JsonSerializerOptions)异常。

适用于

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

将提供的值转换为 UTF-8 编码的 JSON 文本,并将其写入 。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

类型参数

TValue

要序列化的值的类型。

参数

utf8Json
Stream

要写入的 UTF-8 Stream

value
TValue

要转换的值。

jsonTypeInfo
JsonTypeInfo<TValue>

有关要转换的类型的元数据。

cancellationToken
CancellationToken

CancellationToken可用于取消写入操作的 。

返回

表示异步写入操作的任务。

例外

utf8Jsonnull

TValue 或其可序列化成员没有兼容的 JsonConverter

取消令牌已取消。 此异常存储在返回的任务中。

适用于

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

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

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

类型参数

TValue

要序列化的值的类型。

参数

utf8Json
Stream

要写入的 UTF-8 流。

value
TValue

要转换的值。

options
JsonSerializerOptions

用于控制序列化行为的选项。

cancellationToken
CancellationToken

可能用于取消写入操作的标记。

返回

表示异步写入操作的任务。

例外

utf8Jsonnull

TValue 或其可序列化成员没有兼容的 JsonConverter

取消令牌已取消。 此异常存储在返回的任务中。

注解

有关详细信息,请参阅 如何序列化和反序列化 JSON

适用于