| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Diagnostics; |
| | | 6 | | using System.Diagnostics.CodeAnalysis; |
| | | 7 | | using System.IO; |
| | | 8 | | using System.Runtime.CompilerServices; |
| | | 9 | | using System.Text.Json.Serialization; |
| | | 10 | | using System.Text.Json.Serialization.Metadata; |
| | | 11 | | using System.Threading; |
| | | 12 | | using System.Threading.Tasks; |
| | | 13 | | |
| | | 14 | | namespace System.Text.Json |
| | | 15 | | { |
| | | 16 | | public static partial class JsonSerializer |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>. |
| | | 20 | | /// The Stream will be read to completion. |
| | | 21 | | /// </summary> |
| | | 22 | | /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam> |
| | | 23 | | /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns> |
| | | 24 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 25 | | /// <param name="options">Options to control the behavior during reading.</param> |
| | | 26 | | /// <param name="cancellationToken"> |
| | | 27 | | /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation. |
| | | 28 | | /// </param> |
| | | 29 | | /// <exception cref="System.ArgumentNullException"> |
| | | 30 | | /// <paramref name="utf8Json"/> is <see langword="null"/>. |
| | | 31 | | /// </exception> |
| | | 32 | | /// <exception cref="JsonException"> |
| | | 33 | | /// The JSON is invalid, |
| | | 34 | | /// <typeparamref name="TValue"/> is not compatible with the JSON, |
| | | 35 | | /// or when there is remaining data in the Stream. |
| | | 36 | | /// </exception> |
| | | 37 | | /// <exception cref="NotSupportedException"> |
| | | 38 | | /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> |
| | | 39 | | /// for <typeparamref name="TValue"/> or its serializable members. |
| | | 40 | | /// </exception> |
| | | 41 | | [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] |
| | | 42 | | [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] |
| | | 43 | | public static ValueTask<TValue?> DeserializeAsync<TValue>( |
| | | 44 | | Stream utf8Json, |
| | | 45 | | JsonSerializerOptions? options = null, |
| | | 46 | | CancellationToken cancellationToken = default) |
| | 0 | 47 | | { |
| | 0 | 48 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | | 49 | | |
| | 0 | 50 | | JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options); |
| | 0 | 51 | | return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken); |
| | 0 | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>. |
| | | 56 | | /// The Stream will be read to completion. |
| | | 57 | | /// </summary> |
| | | 58 | | /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam> |
| | | 59 | | /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns> |
| | | 60 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 61 | | /// <param name="options">Options to control the behavior during reading.</param> |
| | | 62 | | /// <exception cref="System.ArgumentNullException"> |
| | | 63 | | /// <paramref name="utf8Json"/> is <see langword="null"/>. |
| | | 64 | | /// </exception> |
| | | 65 | | /// <exception cref="JsonException"> |
| | | 66 | | /// The JSON is invalid, |
| | | 67 | | /// <typeparamref name="TValue"/> is not compatible with the JSON, |
| | | 68 | | /// or when there is remaining data in the Stream. |
| | | 69 | | /// </exception> |
| | | 70 | | /// <exception cref="NotSupportedException"> |
| | | 71 | | /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> |
| | | 72 | | /// for <typeparamref name="TValue"/> or its serializable members. |
| | | 73 | | /// </exception> |
| | | 74 | | [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] |
| | | 75 | | [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] |
| | | 76 | | public static TValue? Deserialize<TValue>( |
| | | 77 | | Stream utf8Json, |
| | | 78 | | JsonSerializerOptions? options = null) |
| | 0 | 79 | | { |
| | 0 | 80 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | | 81 | | |
| | 0 | 82 | | JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options); |
| | 0 | 83 | | return jsonTypeInfo.Deserialize(utf8Json); |
| | 0 | 84 | | } |
| | | 85 | | |
| | | 86 | | /// <summary> |
| | | 87 | | /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>. |
| | | 88 | | /// The Stream will be read to completion. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns> |
| | | 91 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 92 | | /// <param name="returnType">The type of the object to convert to and return.</param> |
| | | 93 | | /// <param name="options">Options to control the behavior during reading.</param> |
| | | 94 | | /// <param name="cancellationToken"> |
| | | 95 | | /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation. |
| | | 96 | | /// </param> |
| | | 97 | | /// <exception cref="System.ArgumentNullException"> |
| | | 98 | | /// <paramref name="utf8Json"/> or <paramref name="returnType"/> is <see langword="null"/>. |
| | | 99 | | /// </exception> |
| | | 100 | | /// <exception cref="JsonException"> |
| | | 101 | | /// The JSON is invalid, |
| | | 102 | | /// the <paramref name="returnType"/> is not compatible with the JSON, |
| | | 103 | | /// or when there is remaining data in the Stream. |
| | | 104 | | /// </exception> |
| | | 105 | | /// <exception cref="NotSupportedException"> |
| | | 106 | | /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> |
| | | 107 | | /// for <paramref name="returnType"/> or its serializable members. |
| | | 108 | | /// </exception> |
| | | 109 | | [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] |
| | | 110 | | [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] |
| | | 111 | | public static ValueTask<object?> DeserializeAsync( |
| | | 112 | | Stream utf8Json, |
| | | 113 | | Type returnType, |
| | | 114 | | JsonSerializerOptions? options = null, |
| | | 115 | | CancellationToken cancellationToken = default) |
| | 27962 | 116 | | { |
| | 27962 | 117 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 27962 | 118 | | ArgumentNullException.ThrowIfNull(returnType); |
| | | 119 | | |
| | 27962 | 120 | | JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType); |
| | 27962 | 121 | | return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken); |
| | 27962 | 122 | | } |
| | | 123 | | |
| | | 124 | | /// <summary> |
| | | 125 | | /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>. |
| | | 126 | | /// The Stream will be read to completion. |
| | | 127 | | /// </summary> |
| | | 128 | | /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns> |
| | | 129 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 130 | | /// <param name="returnType">The type of the object to convert to and return.</param> |
| | | 131 | | /// <param name="options">Options to control the behavior during reading.</param> |
| | | 132 | | /// <exception cref="System.ArgumentNullException"> |
| | | 133 | | /// <paramref name="utf8Json"/> or <paramref name="returnType"/> is <see langword="null"/>. |
| | | 134 | | /// </exception> |
| | | 135 | | /// <exception cref="JsonException"> |
| | | 136 | | /// The JSON is invalid, |
| | | 137 | | /// the <paramref name="returnType"/> is not compatible with the JSON, |
| | | 138 | | /// or when there is remaining data in the Stream. |
| | | 139 | | /// </exception> |
| | | 140 | | /// <exception cref="NotSupportedException"> |
| | | 141 | | /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> |
| | | 142 | | /// for <paramref name="returnType"/> or its serializable members. |
| | | 143 | | /// </exception> |
| | | 144 | | [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] |
| | | 145 | | [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] |
| | | 146 | | public static object? Deserialize( |
| | | 147 | | Stream utf8Json, |
| | | 148 | | Type returnType, |
| | | 149 | | JsonSerializerOptions? options = null) |
| | 0 | 150 | | { |
| | 0 | 151 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 0 | 152 | | ArgumentNullException.ThrowIfNull(returnType); |
| | | 153 | | |
| | 0 | 154 | | JsonTypeInfo jsonTypeInfo = GetTypeInfo(options, returnType); |
| | 0 | 155 | | return jsonTypeInfo.DeserializeAsObject(utf8Json); |
| | 0 | 156 | | } |
| | | 157 | | |
| | | 158 | | /// <summary> |
| | | 159 | | /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>. |
| | | 160 | | /// The Stream will be read to completion. |
| | | 161 | | /// </summary> |
| | | 162 | | /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam> |
| | | 163 | | /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns> |
| | | 164 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 165 | | /// <param name="jsonTypeInfo">Metadata about the type to convert.</param> |
| | | 166 | | /// <param name="cancellationToken"> |
| | | 167 | | /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation. |
| | | 168 | | /// </param> |
| | | 169 | | /// <exception cref="System.ArgumentNullException"> |
| | | 170 | | /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. |
| | | 171 | | /// </exception> |
| | | 172 | | /// <exception cref="JsonException"> |
| | | 173 | | /// The JSON is invalid, |
| | | 174 | | /// <typeparamref name="TValue"/> is not compatible with the JSON, |
| | | 175 | | /// or when there is remaining data in the Stream. |
| | | 176 | | /// </exception> |
| | | 177 | | public static ValueTask<TValue?> DeserializeAsync<TValue>( |
| | | 178 | | Stream utf8Json, |
| | | 179 | | JsonTypeInfo<TValue> jsonTypeInfo, |
| | | 180 | | CancellationToken cancellationToken = default) |
| | 0 | 181 | | { |
| | 0 | 182 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 0 | 183 | | ArgumentNullException.ThrowIfNull(jsonTypeInfo); |
| | | 184 | | |
| | 0 | 185 | | jsonTypeInfo.EnsureConfigured(); |
| | 0 | 186 | | return jsonTypeInfo.DeserializeAsync(utf8Json, cancellationToken); |
| | 0 | 187 | | } |
| | | 188 | | |
| | | 189 | | /// <summary> |
| | | 190 | | /// Reads the UTF-8 encoded text representing a single JSON value into an instance specified by the <paramref na |
| | | 191 | | /// The Stream will be read to completion. |
| | | 192 | | /// </summary> |
| | | 193 | | /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns> |
| | | 194 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 195 | | /// <param name="jsonTypeInfo">Metadata about the type to convert.</param> |
| | | 196 | | /// <param name="cancellationToken"> |
| | | 197 | | /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation. |
| | | 198 | | /// </param> |
| | | 199 | | /// <exception cref="System.ArgumentNullException"> |
| | | 200 | | /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. |
| | | 201 | | /// </exception> |
| | | 202 | | /// <exception cref="JsonException"> |
| | | 203 | | /// The JSON is invalid, |
| | | 204 | | /// or when there is remaining data in the Stream. |
| | | 205 | | /// </exception> |
| | | 206 | | public static ValueTask<object?> DeserializeAsync( |
| | | 207 | | Stream utf8Json, |
| | | 208 | | JsonTypeInfo jsonTypeInfo, |
| | | 209 | | CancellationToken cancellationToken = default) |
| | 0 | 210 | | { |
| | 0 | 211 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 0 | 212 | | ArgumentNullException.ThrowIfNull(jsonTypeInfo); |
| | | 213 | | |
| | 0 | 214 | | jsonTypeInfo.EnsureConfigured(); |
| | 0 | 215 | | return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken); |
| | 0 | 216 | | } |
| | | 217 | | |
| | | 218 | | /// <summary> |
| | | 219 | | /// Reads the UTF-8 encoded text representing a single JSON value into a <typeparamref name="TValue"/>. |
| | | 220 | | /// The Stream will be read to completion. |
| | | 221 | | /// </summary> |
| | | 222 | | /// <typeparam name="TValue">The type to deserialize the JSON value into.</typeparam> |
| | | 223 | | /// <returns>A <typeparamref name="TValue"/> representation of the JSON value.</returns> |
| | | 224 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 225 | | /// <param name="jsonTypeInfo">Metadata about the type to convert.</param> |
| | | 226 | | /// <exception cref="System.ArgumentNullException"> |
| | | 227 | | /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. |
| | | 228 | | /// </exception> |
| | | 229 | | /// <exception cref="JsonException"> |
| | | 230 | | /// The JSON is invalid, |
| | | 231 | | /// <typeparamref name="TValue"/> is not compatible with the JSON, |
| | | 232 | | /// or when there is remaining data in the Stream. |
| | | 233 | | /// </exception> |
| | | 234 | | public static TValue? Deserialize<TValue>( |
| | | 235 | | Stream utf8Json, |
| | | 236 | | JsonTypeInfo<TValue> jsonTypeInfo) |
| | 0 | 237 | | { |
| | 0 | 238 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 0 | 239 | | ArgumentNullException.ThrowIfNull(jsonTypeInfo); |
| | | 240 | | |
| | 0 | 241 | | jsonTypeInfo.EnsureConfigured(); |
| | 0 | 242 | | return jsonTypeInfo.Deserialize(utf8Json); |
| | 0 | 243 | | } |
| | | 244 | | |
| | | 245 | | /// <summary> |
| | | 246 | | /// Reads the UTF-8 encoded text representing a single JSON value into an instance specified by the <paramref na |
| | | 247 | | /// The Stream will be read to completion. |
| | | 248 | | /// </summary> |
| | | 249 | | /// <returns>A <paramref name="jsonTypeInfo"/> representation of the JSON value.</returns> |
| | | 250 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 251 | | /// <param name="jsonTypeInfo">Metadata about the type to convert.</param> |
| | | 252 | | /// <exception cref="System.ArgumentNullException"> |
| | | 253 | | /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. |
| | | 254 | | /// </exception> |
| | | 255 | | /// <exception cref="JsonException"> |
| | | 256 | | /// The JSON is invalid, |
| | | 257 | | /// or when there is remaining data in the Stream. |
| | | 258 | | /// </exception> |
| | | 259 | | public static object? Deserialize( |
| | | 260 | | Stream utf8Json, |
| | | 261 | | JsonTypeInfo jsonTypeInfo) |
| | 0 | 262 | | { |
| | 0 | 263 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 0 | 264 | | ArgumentNullException.ThrowIfNull(jsonTypeInfo); |
| | | 265 | | |
| | 0 | 266 | | jsonTypeInfo.EnsureConfigured(); |
| | 0 | 267 | | return jsonTypeInfo.DeserializeAsObject(utf8Json); |
| | 0 | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>. |
| | | 272 | | /// The Stream will be read to completion. |
| | | 273 | | /// </summary> |
| | | 274 | | /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns> |
| | | 275 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 276 | | /// <param name="returnType">The type of the object to convert to and return.</param> |
| | | 277 | | /// <param name="context">A metadata provider for serializable types.</param> |
| | | 278 | | /// <param name="cancellationToken"> |
| | | 279 | | /// The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the read operation. |
| | | 280 | | /// </param> |
| | | 281 | | /// <exception cref="System.ArgumentNullException"> |
| | | 282 | | /// <paramref name="utf8Json"/>, <paramref name="returnType"/>, or <paramref name="context"/> is <see langword=" |
| | | 283 | | /// </exception> |
| | | 284 | | /// <exception cref="JsonException"> |
| | | 285 | | /// The JSON is invalid, |
| | | 286 | | /// the <paramref name="returnType"/> is not compatible with the JSON, |
| | | 287 | | /// or when there is remaining data in the Stream. |
| | | 288 | | /// </exception> |
| | | 289 | | /// <exception cref="NotSupportedException"> |
| | | 290 | | /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> |
| | | 291 | | /// for <paramref name="returnType"/> or its serializable members. |
| | | 292 | | /// </exception> |
| | | 293 | | /// <exception cref="InvalidOperationException"> |
| | | 294 | | /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method on the provided <paramref name="context"/> |
| | | 295 | | /// did not return a compatible <see cref="JsonTypeInfo"/> for <paramref name="returnType"/>. |
| | | 296 | | /// </exception> |
| | | 297 | | public static ValueTask<object?> DeserializeAsync( |
| | | 298 | | Stream utf8Json, |
| | | 299 | | Type returnType, |
| | | 300 | | JsonSerializerContext context, |
| | | 301 | | CancellationToken cancellationToken = default) |
| | 0 | 302 | | { |
| | 0 | 303 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 0 | 304 | | ArgumentNullException.ThrowIfNull(returnType); |
| | 0 | 305 | | ArgumentNullException.ThrowIfNull(context); |
| | | 306 | | |
| | 0 | 307 | | JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType); |
| | 0 | 308 | | return jsonTypeInfo.DeserializeAsObjectAsync(utf8Json, cancellationToken); |
| | 0 | 309 | | } |
| | | 310 | | |
| | | 311 | | /// <summary> |
| | | 312 | | /// Reads the UTF-8 encoded text representing a single JSON value into a <paramref name="returnType"/>. |
| | | 313 | | /// The Stream will be read to completion. |
| | | 314 | | /// </summary> |
| | | 315 | | /// <returns>A <paramref name="returnType"/> representation of the JSON value.</returns> |
| | | 316 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 317 | | /// <param name="returnType">The type of the object to convert to and return.</param> |
| | | 318 | | /// <param name="context">A metadata provider for serializable types.</param> |
| | | 319 | | /// <exception cref="System.ArgumentNullException"> |
| | | 320 | | /// <paramref name="utf8Json"/>, <paramref name="returnType"/>, or <paramref name="context"/> is <see langword=" |
| | | 321 | | /// </exception> |
| | | 322 | | /// <exception cref="JsonException"> |
| | | 323 | | /// The JSON is invalid, |
| | | 324 | | /// the <paramref name="returnType"/> is not compatible with the JSON, |
| | | 325 | | /// or when there is remaining data in the Stream. |
| | | 326 | | /// </exception> |
| | | 327 | | /// <exception cref="NotSupportedException"> |
| | | 328 | | /// There is no compatible <see cref="System.Text.Json.Serialization.JsonConverter"/> |
| | | 329 | | /// for <paramref name="returnType"/> or its serializable members. |
| | | 330 | | /// </exception> |
| | | 331 | | /// <exception cref="InvalidOperationException"> |
| | | 332 | | /// The <see cref="JsonSerializerContext.GetTypeInfo(Type)"/> method on the provided <paramref name="context"/> |
| | | 333 | | /// did not return a compatible <see cref="JsonTypeInfo"/> for <paramref name="returnType"/>. |
| | | 334 | | /// </exception> |
| | | 335 | | public static object? Deserialize( |
| | | 336 | | Stream utf8Json, |
| | | 337 | | Type returnType, |
| | | 338 | | JsonSerializerContext context) |
| | 0 | 339 | | { |
| | 0 | 340 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 0 | 341 | | ArgumentNullException.ThrowIfNull(returnType); |
| | 0 | 342 | | ArgumentNullException.ThrowIfNull(context); |
| | | 343 | | |
| | 0 | 344 | | JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, returnType); |
| | 0 | 345 | | return jsonTypeInfo.DeserializeAsObject(utf8Json); |
| | 0 | 346 | | } |
| | | 347 | | |
| | | 348 | | /// <summary> |
| | | 349 | | /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" /> |
| | | 350 | | /// that can be used to deserialize root-level JSON arrays in a streaming manner. |
| | | 351 | | /// </summary> |
| | | 352 | | /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam> |
| | | 353 | | /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON array.</returns> |
| | | 354 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 355 | | /// <param name="options">Options to control the behavior during reading.</param> |
| | | 356 | | /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can |
| | | 357 | | /// <exception cref="System.ArgumentNullException"> |
| | | 358 | | /// <paramref name="utf8Json"/> is <see langword="null"/>. |
| | | 359 | | /// </exception> |
| | | 360 | | [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] |
| | | 361 | | [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] |
| | | 362 | | public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>( |
| | | 363 | | Stream utf8Json, |
| | | 364 | | JsonSerializerOptions? options = null, |
| | | 365 | | CancellationToken cancellationToken = default) |
| | 0 | 366 | | { |
| | 0 | 367 | | return DeserializeAsyncEnumerable<TValue>(utf8Json, topLevelValues: false, options, cancellationToken); |
| | 0 | 368 | | } |
| | | 369 | | |
| | | 370 | | /// <summary> |
| | | 371 | | /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" /> |
| | | 372 | | /// that can be used to deserialize sequences of JSON values in a streaming manner. |
| | | 373 | | /// </summary> |
| | | 374 | | /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam> |
| | | 375 | | /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON sequence.</returns> |
| | | 376 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 377 | | /// <param name="topLevelValues"><see langword="true"/> to deserialize from a sequence of top-level JSON values, |
| | | 378 | | /// <param name="options">Options to control the behavior during reading.</param> |
| | | 379 | | /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can |
| | | 380 | | /// <exception cref="System.ArgumentNullException"> |
| | | 381 | | /// <paramref name="utf8Json"/> is <see langword="null"/>. |
| | | 382 | | /// </exception> |
| | | 383 | | /// <remarks> |
| | | 384 | | /// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the stream as a sequence of |
| | | 385 | | /// whitespace separated top-level JSON values and attempts to deserialize each value into <typeparamref name="T |
| | | 386 | | /// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the stream as a JSON array |
| | | 387 | | /// attempts to serialize each element into <typeparamref name="TValue"/>. |
| | | 388 | | /// </remarks> |
| | | 389 | | [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] |
| | | 390 | | [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] |
| | | 391 | | public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>( |
| | | 392 | | Stream utf8Json, |
| | | 393 | | bool topLevelValues, |
| | | 394 | | JsonSerializerOptions? options = null, |
| | | 395 | | CancellationToken cancellationToken = default) |
| | 0 | 396 | | { |
| | 0 | 397 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | | 398 | | |
| | 0 | 399 | | JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options); |
| | 0 | 400 | | return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken); |
| | 0 | 401 | | } |
| | | 402 | | |
| | | 403 | | /// <summary> |
| | | 404 | | /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" /> |
| | | 405 | | /// that can be used to deserialize root-level JSON arrays in a streaming manner. |
| | | 406 | | /// </summary> |
| | | 407 | | /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam> |
| | | 408 | | /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON array.</returns> |
| | | 409 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 410 | | /// <param name="jsonTypeInfo">Metadata about the element type to convert.</param> |
| | | 411 | | /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can |
| | | 412 | | /// <exception cref="System.ArgumentNullException"> |
| | | 413 | | /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. |
| | | 414 | | /// </exception> |
| | | 415 | | public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>( |
| | | 416 | | Stream utf8Json, |
| | | 417 | | JsonTypeInfo<TValue> jsonTypeInfo, |
| | | 418 | | CancellationToken cancellationToken = default) |
| | 0 | 419 | | { |
| | 0 | 420 | | return DeserializeAsyncEnumerable(utf8Json, jsonTypeInfo, topLevelValues: false, cancellationToken); |
| | 0 | 421 | | } |
| | | 422 | | |
| | | 423 | | /// <summary> |
| | | 424 | | /// Wraps the UTF-8 encoded text into an <see cref="IAsyncEnumerable{TValue}" /> |
| | | 425 | | /// that can be used to deserialize sequences of JSON values in a streaming manner. |
| | | 426 | | /// </summary> |
| | | 427 | | /// <typeparam name="TValue">The element type to deserialize asynchronously.</typeparam> |
| | | 428 | | /// <returns>An <see cref="IAsyncEnumerable{TValue}" /> representation of the provided JSON sequence.</returns> |
| | | 429 | | /// <param name="utf8Json">JSON data to parse.</param> |
| | | 430 | | /// <param name="jsonTypeInfo">Metadata about the element type to convert.</param> |
| | | 431 | | /// <param name="topLevelValues">Whether to deserialize from a sequence of top-level JSON values.</param> |
| | | 432 | | /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to can |
| | | 433 | | /// <exception cref="System.ArgumentNullException"> |
| | | 434 | | /// <paramref name="utf8Json"/> or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. |
| | | 435 | | /// </exception> |
| | | 436 | | /// <remarks> |
| | | 437 | | /// When <paramref name="topLevelValues"/> is set to <see langword="true" />, treats the stream as a sequence of |
| | | 438 | | /// whitespace separated top-level JSON values and attempts to deserialize each value into <typeparamref name="T |
| | | 439 | | /// When <paramref name="topLevelValues"/> is set to <see langword="false" />, treats the stream as a JSON array |
| | | 440 | | /// attempts to serialize each element into <typeparamref name="TValue"/>. |
| | | 441 | | /// </remarks> |
| | | 442 | | public static IAsyncEnumerable<TValue?> DeserializeAsyncEnumerable<TValue>( |
| | | 443 | | Stream utf8Json, |
| | | 444 | | JsonTypeInfo<TValue> jsonTypeInfo, |
| | | 445 | | bool topLevelValues, |
| | | 446 | | CancellationToken cancellationToken = default) |
| | 0 | 447 | | { |
| | 0 | 448 | | ArgumentNullException.ThrowIfNull(utf8Json); |
| | 0 | 449 | | ArgumentNullException.ThrowIfNull(jsonTypeInfo); |
| | | 450 | | |
| | 0 | 451 | | jsonTypeInfo.EnsureConfigured(); |
| | 0 | 452 | | return DeserializeAsyncEnumerableCore(utf8Json, jsonTypeInfo, topLevelValues, cancellationToken); |
| | 0 | 453 | | } |
| | | 454 | | |
| | | 455 | | private static IAsyncEnumerable<T?> DeserializeAsyncEnumerableCore<T>( |
| | | 456 | | Stream utf8Json, |
| | | 457 | | JsonTypeInfo<T> jsonTypeInfo, |
| | | 458 | | bool topLevelValues, |
| | | 459 | | CancellationToken cancellationToken) |
| | 0 | 460 | | { |
| | 0 | 461 | | Debug.Assert(jsonTypeInfo.IsConfigured); |
| | | 462 | | |
| | | 463 | | JsonTypeInfo<List<T?>> listTypeInfo; |
| | 0 | 464 | | JsonReaderOptions readerOptions = jsonTypeInfo.Options.GetReaderOptions(); |
| | 0 | 465 | | if (topLevelValues) |
| | 0 | 466 | | { |
| | 0 | 467 | | listTypeInfo = GetOrAddListTypeInfoForRootLevelValueMode(jsonTypeInfo); |
| | 0 | 468 | | readerOptions.AllowMultipleValues = true; |
| | 0 | 469 | | } |
| | | 470 | | else |
| | 0 | 471 | | { |
| | 0 | 472 | | listTypeInfo = GetOrAddListTypeInfoForArrayMode(jsonTypeInfo); |
| | 0 | 473 | | } |
| | | 474 | | |
| | 0 | 475 | | return CreateAsyncEnumerableFromArray(utf8Json, listTypeInfo, readerOptions, cancellationToken); |
| | | 476 | | |
| | | 477 | | static async IAsyncEnumerable<T?> CreateAsyncEnumerableFromArray( |
| | | 478 | | Stream utf8Json, |
| | | 479 | | JsonTypeInfo<List<T?>> listTypeInfo, |
| | | 480 | | JsonReaderOptions readerOptions, |
| | | 481 | | [EnumeratorCancellation] CancellationToken cancellationToken) |
| | 0 | 482 | | { |
| | 0 | 483 | | Debug.Assert(listTypeInfo.IsConfigured); |
| | | 484 | | |
| | 0 | 485 | | ReadStack readStack = default; |
| | 0 | 486 | | readStack.Initialize(listTypeInfo, supportContinuation: true); |
| | 0 | 487 | | JsonReaderState jsonReaderState = new(readerOptions); |
| | | 488 | | // Note: The StreamReadBufferState ctor rents pooled buffers. |
| | 0 | 489 | | StreamReadBufferState bufferState = new StreamReadBufferState(listTypeInfo.Options.DefaultBufferSize); |
| | | 490 | | |
| | | 491 | | try |
| | 0 | 492 | | { |
| | | 493 | | bool success; |
| | | 494 | | do |
| | 0 | 495 | | { |
| | 0 | 496 | | bufferState = await bufferState.ReadAsync(utf8Json, cancellationToken, fillBuffer: false).Config |
| | 0 | 497 | | success = listTypeInfo.ContinueDeserialize<StreamReadBufferState, Stream>( |
| | 0 | 498 | | ref bufferState, |
| | 0 | 499 | | ref jsonReaderState, |
| | 0 | 500 | | ref readStack, |
| | 0 | 501 | | out List<T?>? _); |
| | | 502 | | |
| | 0 | 503 | | if (readStack.Current.ReturnValue is { } returnValue) |
| | 0 | 504 | | { |
| | 0 | 505 | | var list = (List<T?>)returnValue; |
| | 0 | 506 | | foreach (T? item in list) |
| | 0 | 507 | | { |
| | 0 | 508 | | yield return item; |
| | 0 | 509 | | } |
| | | 510 | | |
| | 0 | 511 | | list.Clear(); |
| | 0 | 512 | | } |
| | | 513 | | |
| | 0 | 514 | | } while (!success); |
| | 0 | 515 | | } |
| | | 516 | | finally |
| | 0 | 517 | | { |
| | 0 | 518 | | bufferState.Dispose(); |
| | 0 | 519 | | } |
| | 0 | 520 | | } |
| | 0 | 521 | | } |
| | | 522 | | } |
| | | 523 | | } |