System.Text.Json IAsyncEnumerable serialization

System.Text.Json now supports serializing and deserializing of IAsyncEnumerable<T> instances.

Previous behavior

In previous versions, System.Text.Json serialized IAsyncEnumerable<T> instances as empty JSON objects ({}). Deserialization failed with a JsonException.

New behavior

Asynchronous serialization methods now enumerate any IAsyncEnumerable<T> instances in an object graph and then serialize them as JSON arrays. Synchronous serializations methods do not support IAsyncEnumerable<T> serialization and throw a NotSupportedException.

Version introduced

.NET 6

Type of breaking change

This change can affect source compatibility. If you retarget to .NET 6, you won't get any compile-time errors, but you may encounter run-time serialization behavior differences.

Reason for change

This change was introduced to add support for streaming IAsyncEnumerable<T> responses in ASP.NET Core MVC.

Check if your serialization models contain types that implement IAsyncEnumerable<T> and determine if emitting the enumeration in the JSON output is desirable. You can disable IAsyncEnumerable<T> serialization in one of the following ways:

Affected APIs

See also