Skip to content

Using System.Text.Json, IAsyncEnumerable, and source generation together doesn't work #58221

@richlander

Description

@richlander

I'm using .NET 6 RC2.

using System.Text.Json;
using System.Text.Json.Serialization;

using Stream stream = Console.OpenStandardOutput();
Measurements data = new(DateTime.Now, GetValues(10));
await JsonSerializer.SerializeAsync<Measurements>(stream, data, JsonContext.Default.Measurements);
Console.WriteLine();

static async IAsyncEnumerable<int> GetValues(int n)
{
    for (int i = 0; i < n; i++) yield return Random.Shared.Next(i);
}
    
internal record Measurements(DateTime time, IAsyncEnumerable<int> Values);

// Source generator definition
[JsonSerializable(typeof(Measurements))]
internal partial class JsonContext : JsonSerializerContext
{
}

This code produces the following result:

{"time":"2021-08-26T15:08:56.93747-07:00","Values":{}}

If I switch out the JsonSerializer call to the following, I get the following desired result.

await JsonSerializer.SerializeAsync<Measurements>(stream, data);

and

{"time":"2021-08-26T15:07:58.698976-07:00","Values":[0,0,0,1,0,3,1,3,0,1]}

Is this scenario unsupported or I'm doing something wrong?

Metadata

Metadata

Assignees

Labels

Priority:1Work that is critical for the release, but we could probably ship withoutarea-System.Text.Json

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions