JsonSerializer.DeserializeAsync 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
多載
DeserializeAsync(Stream, Type, JsonSerializerContext, CancellationToken)
將代表單一 JSON 值的 UTF-8 編碼文字讀取成 returnType。
《Stream》將被完整朗讀。
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Stream utf8Json, Type returnType, System.Text.Json.Serialization.JsonSerializerContext context, System.Threading.CancellationToken cancellationToken = default);
static member DeserializeAsync : System.IO.Stream * Type * System.Text.Json.Serialization.JsonSerializerContext * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
Public Function DeserializeAsync (utf8Json As Stream, returnType As Type, context As JsonSerializerContext, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Object)
參數
- utf8Json
- Stream
JSON 資料要解析。
- returnType
- Type
要轉換並回傳的物件類型。
- context
- JsonSerializerContext
一個可序列化型別的元資料提供者。
- cancellationToken
- CancellationToken
CancellationToken可以用 that 來取消讀取操作。
傳回
JSON 值的 returnType 表示。
例外狀況
utf8Json, , returnType或 context 是 null。
JSON 無效、與 returnType JSON 不相容,或串流中有剩餘資料。
沒有相容 JsonConverter 的 或 returnType 其可序列化的成員。
GetTypeInfo(Type)所提供context的方法未回傳相容JsonTypeInfo的。returnType
取消令牌也被取消了。 此例外會儲存在回傳的任務中。
備註
此方法在任務中儲存所有非使用例外,該方法的同步對應程式可拋出這些例外。 如果回傳的任務中儲存了例外,該例外會在等待任務時拋出。 使用例外錯誤,例如 ArgumentException,仍會同步拋出。 關於儲存的例外,請參見由 Deserialize(Stream, Type, JsonSerializerContext)拋出的例外。
適用於
DeserializeAsync(Stream, Type, JsonSerializerOptions, CancellationToken)
非同步讀取代表單一 JSON 值的 UTF-8 編碼文字,進入指定類型的實例。 直播將被完整朗讀。
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Stream utf8Json, Type returnType, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Stream utf8Json, Type returnType, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Stream utf8Json, Type returnType, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
public static System.Threading.Tasks.ValueTask<object> DeserializeAsync(System.IO.Stream utf8Json, Type returnType, System.Text.Json.JsonSerializerOptions options = default, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member DeserializeAsync : System.IO.Stream * Type * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
static member DeserializeAsync : System.IO.Stream * Type * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member DeserializeAsync : System.IO.Stream * Type * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
Public Function DeserializeAsync (utf8Json As Stream, returnType As Type, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Object)
Public Shared Function DeserializeAsync (utf8Json As Stream, returnType As Type, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Object)
參數
- utf8Json
- Stream
要解析的 JSON 資料。
- returnType
- Type
要轉換並回傳的物件類型。
- options
- JsonSerializerOptions
閱讀時可控制行為的選項。
- cancellationToken
- CancellationToken
一個可用於取消讀取操作的消去標記。
傳回
JSON 值的 returnType 表示。
- 屬性
例外狀況
utf8Json 或 returnType 為 null。
沒有相容 JsonConverter 的 或 returnType 其可序列化的成員。
取消令牌也被取消了。 此例外會儲存在回傳的任務中。
備註
更多資訊請參閱 《如何序列化與反序列化 JSON》。
此方法在任務中儲存所有非使用例外,該方法的同步對應程式可拋出這些例外。 如果回傳的任務中儲存了例外,該例外會在等待任務時拋出。 使用例外錯誤,例如 ArgumentException,仍會同步拋出。 關於儲存的例外,請參見由 Deserialize(Stream, Type, JsonSerializerOptions)拋出的例外。
適用於
DeserializeAsync(PipeReader, Type, JsonSerializerContext, CancellationToken)
將代表單一 JSON 值的 UTF-8 編碼文字讀取成 returnType。
PipeReader 將被完整閱讀。
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Pipelines.PipeReader utf8Json, Type returnType, System.Text.Json.Serialization.JsonSerializerContext context, System.Threading.CancellationToken cancellationToken = default);
static member DeserializeAsync : System.IO.Pipelines.PipeReader * Type * System.Text.Json.Serialization.JsonSerializerContext * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
Public Function DeserializeAsync (utf8Json As PipeReader, returnType As Type, context As JsonSerializerContext, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Object)
參數
- utf8Json
- PipeReader
JSON 資料要解析。
- returnType
- Type
要轉換並回傳的物件類型。
- context
- JsonSerializerContext
一個可序列化型別的元資料提供者。
- cancellationToken
- CancellationToken
CancellationToken可以用 that 來取消讀取操作。
傳回
JSON 值的 returnType 表示。
例外狀況
utf8Json, , returnType或 context 是 null。
JSON 無效、與 returnType JSON 不相容,或 PipeReader 中還有剩餘資料。
沒有相容 JsonConverter 的 或 returnType 其可序列化的成員。
GetTypeInfo(Type)所提供context的方法未回傳相容JsonTypeInfo的。returnType
適用於
DeserializeAsync(Stream, JsonTypeInfo, CancellationToken)
將代表單一 JSON 值的 UTF-8 編碼文字讀取到由 jsonTypeInfo.
《Stream》將被完整朗讀。
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Stream utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default);
static member DeserializeAsync : System.IO.Stream * System.Text.Json.Serialization.Metadata.JsonTypeInfo * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
Public Function DeserializeAsync (utf8Json As Stream, jsonTypeInfo As JsonTypeInfo, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Object)
參數
- utf8Json
- Stream
JSON 資料要解析。
- jsonTypeInfo
- JsonTypeInfo
關於要轉換的類型資料。
- cancellationToken
- CancellationToken
CancellationToken可以用 that 來取消讀取操作。
傳回
JSON 值的 jsonTypeInfo 表示。
例外狀況
utf8Json 或 jsonTypeInfo 為 null。
JSON 無效,或是串流中有剩餘資料。
取消令牌也被取消了。 此例外會儲存在回傳的任務中。
適用於
DeserializeAsync(PipeReader, JsonTypeInfo, CancellationToken)
將代表單一 JSON 值的 UTF-8 編碼文字讀取到由 jsonTypeInfo.
PipeReader 將被完整閱讀。
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Pipelines.PipeReader utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default);
static member DeserializeAsync : System.IO.Pipelines.PipeReader * System.Text.Json.Serialization.Metadata.JsonTypeInfo * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
Public Function DeserializeAsync (utf8Json As PipeReader, jsonTypeInfo As JsonTypeInfo, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Object)
參數
- utf8Json
- PipeReader
JSON 資料要解析。
- jsonTypeInfo
- JsonTypeInfo
關於要轉換的類型資料。
- cancellationToken
- CancellationToken
CancellationToken可以用 that 來取消讀取操作。
傳回
JSON 值的 jsonTypeInfo 表示。
例外狀況
utf8Json 或 jsonTypeInfo 為 null。
JSON 是無效的,或是 PipeReader 裡還有剩餘的資料。
適用於
DeserializeAsync(PipeReader, Type, JsonSerializerOptions, CancellationToken)
將代表單一 JSON 值的 UTF-8 編碼文字讀取成 returnType。
PipeReader 將被完整閱讀。
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Pipelines.PipeReader utf8Json, Type returnType, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
public static System.Threading.Tasks.ValueTask<object?> DeserializeAsync(System.IO.Pipelines.PipeReader utf8Json, Type returnType, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member DeserializeAsync : System.IO.Pipelines.PipeReader * Type * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
static member DeserializeAsync : System.IO.Pipelines.PipeReader * Type * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
Public Function DeserializeAsync (utf8Json As PipeReader, returnType As Type, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of Object)
參數
- utf8Json
- PipeReader
JSON 資料要解析。
- returnType
- Type
要轉換並回傳的物件類型。
- options
- JsonSerializerOptions
閱讀時可控制行為的選項。
- cancellationToken
- CancellationToken
CancellationToken可以用 that 來取消讀取操作。
傳回
JSON 值的 returnType 表示。
- 屬性
例外狀況
utf8Json 或 returnType 為 null。
JSON 無效、與 returnType JSON 不相容,或 PipeReader 中還有剩餘資料。
沒有相容 JsonConverter 的 或 returnType 其可序列化的成員。
適用於
DeserializeAsync<TValue>(PipeReader, JsonSerializerOptions, CancellationToken)
將代表單一 JSON 值的 UTF-8 編碼文字讀取成 TValue。
PipeReader 將被完整閱讀。
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static System.Threading.Tasks.ValueTask<TValue?> DeserializeAsync<TValue>(System.IO.Pipelines.PipeReader utf8Json, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
public static System.Threading.Tasks.ValueTask<TValue?> DeserializeAsync<TValue>(System.IO.Pipelines.PipeReader utf8Json, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member DeserializeAsync : System.IO.Pipelines.PipeReader * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Value>
static member DeserializeAsync : System.IO.Pipelines.PipeReader * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Value>
Public Function DeserializeAsync(Of TValue) (utf8Json As PipeReader, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of TValue)
類型參數
- TValue
要將 JSON 值反序列化成的類型。
參數
- utf8Json
- PipeReader
JSON 資料要解析。
- options
- JsonSerializerOptions
閱讀時可控制行為的選項。
- cancellationToken
- CancellationToken
CancellationToken可以用 that 來取消讀取操作。
傳回
JSON 值的 TValue 表示。
- 屬性
例外狀況
utf8Json 是 null。
JSON 無效、 TValue 與 JSON 不相容,或 PipeReader 中還有剩餘資料。
沒有相容 JsonConverter 的 或 TValue 其可序列化的成員。
適用於
DeserializeAsync<TValue>(PipeReader, JsonTypeInfo<TValue>, CancellationToken)
將代表單一 JSON 值的 UTF-8 編碼文字讀取成 TValue。
PipeReader 將被完整閱讀。
public static System.Threading.Tasks.ValueTask<TValue?> DeserializeAsync<TValue>(System.IO.Pipelines.PipeReader utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default);
static member DeserializeAsync : System.IO.Pipelines.PipeReader * System.Text.Json.Serialization.Metadata.JsonTypeInfo<'Value> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Value>
Public Function DeserializeAsync(Of TValue) (utf8Json As PipeReader, jsonTypeInfo As JsonTypeInfo(Of TValue), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of TValue)
類型參數
- TValue
要將 JSON 值反序列化成的類型。
參數
- utf8Json
- PipeReader
JSON 資料要解析。
- jsonTypeInfo
- JsonTypeInfo<TValue>
關於要轉換的類型資料。
- cancellationToken
- CancellationToken
CancellationToken可以用 that 來取消讀取操作。
傳回
JSON 值的 TValue 表示。
例外狀況
utf8Json 或 jsonTypeInfo 為 null。
JSON 無效、 TValue 與 JSON 不相容,或 PipeReader 中還有剩餘資料。
適用於
DeserializeAsync<TValue>(Stream, JsonSerializerOptions, CancellationToken)
非同步讀取代表單一 JSON 值的 UTF-8 編碼文字,進入由一般型別參數指定的類型實例中。 直播將被完整朗讀。
[System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")]
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static System.Threading.Tasks.ValueTask<TValue?> DeserializeAsync<TValue>(System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
public static System.Threading.Tasks.ValueTask<TValue?> DeserializeAsync<TValue>(System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")]
public static System.Threading.Tasks.ValueTask<TValue?> DeserializeAsync<TValue>(System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions? options = default, System.Threading.CancellationToken cancellationToken = default);
public static System.Threading.Tasks.ValueTask<TValue> DeserializeAsync<TValue>(System.IO.Stream utf8Json, System.Text.Json.JsonSerializerOptions options = default, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresDynamicCode("JSON serialization and deserialization might require types that cannot be statically analyzed and might need runtime code generation. Use System.Text.Json source generation for native AOT applications.")>]
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member DeserializeAsync : System.IO.Stream * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Value>
static member DeserializeAsync : System.IO.Stream * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Value>
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("JSON serialization and deserialization might require types that cannot be statically analyzed. Use the overload that takes a JsonTypeInfo or JsonSerializerContext, or make sure all of the required types are preserved.")>]
static member DeserializeAsync : System.IO.Stream * System.Text.Json.JsonSerializerOptions * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Value>
Public Function DeserializeAsync(Of TValue) (utf8Json As Stream, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of TValue)
Public Shared Function DeserializeAsync(Of TValue) (utf8Json As Stream, Optional options As JsonSerializerOptions = Nothing, Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of TValue)
類型參數
- TValue
JSON 值的目標型別。
參數
- utf8Json
- Stream
要解析的 JSON 資料。
- options
- JsonSerializerOptions
閱讀時可控制行為的選項。
- cancellationToken
- CancellationToken
一個可用於取消讀取操作的標記。
傳回
JSON 值的 TValue 表示。
- 屬性
例外狀況
沒有相容 JsonConverter 的 或 TValue 其可序列化的成員。
utf8Json是 null。
取消令牌也被取消了。 此例外會儲存在回傳的任務中。
備註
更多資訊請參閱 《如何序列化與反序列化 JSON》。
適用於
DeserializeAsync<TValue>(Stream, JsonTypeInfo<TValue>, CancellationToken)
將代表單一 JSON 值的 UTF-8 編碼文字讀取成 TValue。
《Stream》將被完整朗讀。
public static System.Threading.Tasks.ValueTask<TValue?> DeserializeAsync<TValue>(System.IO.Stream utf8Json, System.Text.Json.Serialization.Metadata.JsonTypeInfo<TValue> jsonTypeInfo, System.Threading.CancellationToken cancellationToken = default);
static member DeserializeAsync : System.IO.Stream * System.Text.Json.Serialization.Metadata.JsonTypeInfo<'Value> * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Value>
Public Function DeserializeAsync(Of TValue) (utf8Json As Stream, jsonTypeInfo As JsonTypeInfo(Of TValue), Optional cancellationToken As CancellationToken = Nothing) As ValueTask(Of TValue)
類型參數
- TValue
要將 JSON 值反序列化成的類型。
參數
- utf8Json
- Stream
JSON 資料要解析。
- jsonTypeInfo
- JsonTypeInfo<TValue>
關於要轉換的類型資料。
- cancellationToken
- CancellationToken
CancellationToken可以用 that 來取消讀取操作。
傳回
JSON 值的 TValue 表示。
例外狀況
utf8Json 或 jsonTypeInfo 為 null。
JSON 無效、 TValue 與 JSON 不相容,或串流中有剩餘資料。
沒有相容 JsonConverter 的 或 TValue 其可序列化的成員。
取消令牌也被取消了。 此例外會儲存在回傳的任務中。