|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using System.Collections.Generic; |
| 5 | +using System.Diagnostics; |
4 | 6 | using System.Diagnostics.CodeAnalysis; |
5 | 7 | using System.IO; |
6 | 8 | using System.Text.Json.Serialization; |
@@ -306,5 +308,108 @@ public static void Serialize( |
306 | 308 | JsonTypeInfo jsonTypeInfo = GetTypeInfo(context, inputType); |
307 | 309 | jsonTypeInfo.SerializeAsObject(utf8Json, value); |
308 | 310 | } |
| 311 | + |
| 312 | + /// <summary> |
| 313 | + /// Serializes each element of an <see cref="IAsyncEnumerable{TValue}"/> to the <see cref="System.IO.Stream"/> |
| 314 | + /// in a streaming manner. |
| 315 | + /// </summary> |
| 316 | + /// <typeparam name="TValue">The type of elements in the sequence to serialize.</typeparam> |
| 317 | + /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param> |
| 318 | + /// <param name="value">The <see cref="IAsyncEnumerable{T}"/> sequence to serialize.</param> |
| 319 | + /// <param name="topLevelValues"><see langword="true"/> to serialize as a sequence of newline-separated top-level JSON values (JSON Lines), |
| 320 | + /// or <see langword="false"/> to serialize as a single root-level JSON array.</param> |
| 321 | + /// <param name="options">Options to control the serialization behavior.</param> |
| 322 | + /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the write operation.</param> |
| 323 | + /// <returns>A task that represents the asynchronous write operation.</returns> |
| 324 | + /// <exception cref="ArgumentNullException"> |
| 325 | + /// <paramref name="utf8Json"/> or <paramref name="value"/> is <see langword="null"/>. |
| 326 | + /// </exception> |
| 327 | + [RequiresUnreferencedCode(SerializationUnreferencedCodeMessage)] |
| 328 | + [RequiresDynamicCode(SerializationRequiresDynamicCodeMessage)] |
| 329 | + public static Task SerializeAsyncEnumerable<TValue>( |
| 330 | + Stream utf8Json, |
| 331 | + IAsyncEnumerable<TValue> value, |
| 332 | + bool topLevelValues = false, |
| 333 | + JsonSerializerOptions? options = null, |
| 334 | + CancellationToken cancellationToken = default) |
| 335 | + { |
| 336 | + ArgumentNullException.ThrowIfNull(utf8Json); |
| 337 | + ArgumentNullException.ThrowIfNull(value); |
| 338 | + |
| 339 | + JsonTypeInfo<TValue> jsonTypeInfo = GetTypeInfo<TValue>(options); |
| 340 | + return topLevelValues |
| 341 | + ? SerializeAsyncEnumerableAsTopLevelValues(utf8Json, value, jsonTypeInfo, cancellationToken) |
| 342 | + : SerializeAsync<IAsyncEnumerable<TValue>>(utf8Json, value, options, cancellationToken); |
| 343 | + } |
| 344 | + |
| 345 | + /// <summary> |
| 346 | + /// Serializes each element of an <see cref="IAsyncEnumerable{TValue}"/> to the <see cref="System.IO.Stream"/> |
| 347 | + /// in a streaming manner. |
| 348 | + /// </summary> |
| 349 | + /// <typeparam name="TValue">The type of elements in the sequence to serialize.</typeparam> |
| 350 | + /// <param name="utf8Json">The UTF-8 <see cref="System.IO.Stream"/> to write to.</param> |
| 351 | + /// <param name="value">The <see cref="IAsyncEnumerable{T}"/> sequence to serialize.</param> |
| 352 | + /// <param name="jsonTypeInfo">Metadata about the type of elements to serialize.</param> |
| 353 | + /// <param name="topLevelValues"><see langword="true"/> to serialize as a sequence of newline-separated top-level JSON values (JSON Lines), |
| 354 | + /// or <see langword="false"/> to serialize as a single root-level JSON array.</param> |
| 355 | + /// <param name="cancellationToken">The <see cref="System.Threading.CancellationToken"/> that can be used to cancel the write operation.</param> |
| 356 | + /// <returns>A task that represents the asynchronous write operation.</returns> |
| 357 | + /// <exception cref="ArgumentNullException"> |
| 358 | + /// <paramref name="utf8Json"/>, <paramref name="value"/>, or <paramref name="jsonTypeInfo"/> is <see langword="null"/>. |
| 359 | + /// </exception> |
| 360 | + public static Task SerializeAsyncEnumerable<TValue>( |
| 361 | + Stream utf8Json, |
| 362 | + IAsyncEnumerable<TValue> value, |
| 363 | + JsonTypeInfo<TValue> jsonTypeInfo, |
| 364 | + bool topLevelValues = false, |
| 365 | + CancellationToken cancellationToken = default) |
| 366 | + { |
| 367 | + ArgumentNullException.ThrowIfNull(utf8Json); |
| 368 | + ArgumentNullException.ThrowIfNull(value); |
| 369 | + ArgumentNullException.ThrowIfNull(jsonTypeInfo); |
| 370 | + |
| 371 | + jsonTypeInfo.EnsureConfigured(); |
| 372 | + if (topLevelValues) |
| 373 | + { |
| 374 | + return SerializeAsyncEnumerableAsTopLevelValues(utf8Json, value, jsonTypeInfo, cancellationToken); |
| 375 | + } |
| 376 | + |
| 377 | + // Array mode: serialize the IAsyncEnumerable<T> as a JSON array using existing infrastructure. |
| 378 | + JsonTypeInfo<IAsyncEnumerable<TValue>> collectionTypeInfo = (JsonTypeInfo<IAsyncEnumerable<TValue>>)jsonTypeInfo.Options.GetTypeInfo(typeof(IAsyncEnumerable<TValue>)); |
| 379 | + return collectionTypeInfo.SerializeAsync(utf8Json, value, cancellationToken); |
| 380 | + } |
| 381 | + |
| 382 | + private static async Task SerializeAsyncEnumerableAsTopLevelValues<TValue>( |
| 383 | + Stream utf8Json, |
| 384 | + IAsyncEnumerable<TValue> value, |
| 385 | + JsonTypeInfo<TValue> jsonTypeInfo, |
| 386 | + CancellationToken cancellationToken) |
| 387 | + { |
| 388 | + Debug.Assert(jsonTypeInfo.IsConfigured); |
| 389 | + |
| 390 | + JsonWriterOptions writerOptions = jsonTypeInfo.Options.GetWriterOptions(); |
| 391 | + ReadOnlyMemory<byte> newLine = JsonHelpers.GetNewLineBytes(writerOptions); |
| 392 | + |
| 393 | + using var bufferWriter = new PooledByteBufferWriter(jsonTypeInfo.Options.DefaultBufferSize, utf8Json); |
| 394 | + using var writer = new Utf8JsonWriter(bufferWriter, writerOptions); |
| 395 | + |
| 396 | + bool first = true; |
| 397 | + await foreach (TValue item in value.WithCancellation(cancellationToken).ConfigureAwait(false)) |
| 398 | + { |
| 399 | + if (!first) |
| 400 | + { |
| 401 | + writer.Reset(bufferWriter); |
| 402 | + } |
| 403 | + |
| 404 | + first = false; |
| 405 | + jsonTypeInfo.Serialize(writer, item); |
| 406 | + |
| 407 | + ReadOnlySpan<byte> nl = newLine.Span; |
| 408 | + Span<byte> dest = bufferWriter.GetSpan(nl.Length); |
| 409 | + nl.CopyTo(dest); |
| 410 | + bufferWriter.Advance(nl.Length); |
| 411 | + await bufferWriter.FlushAsync(cancellationToken).ConfigureAwait(false); |
| 412 | + } |
| 413 | + } |
309 | 414 | } |
310 | 415 | } |
0 commit comments