When using the System.Text.Json source generator, attempting to serialize a compiler-generated instance of IAsyncEnumerable<T>
throws an exception saying a TypeInfo
could not be found.
using System.Text.Json; using System.Text.Json.Serialization; var stream = new MemoryStream(); var todos = GetTodosAsync(10); await JsonSerializer.SerializeAsync(stream, todos, todos.GetType(), AppJsonSerializerContext.Default); async IAsyncEnumerable<Todo> GetTodosAsync(int count) { for (int i = 0; i < count; i++) { await Task.Delay(10); yield return new Todo { Id = i, Name = $"Todo {i}" }; } } class Todo { public int Id { get; set; } public string? Name { get; set; } } [JsonSerializable(typeof(IAsyncEnumerable<Todo>))] internal partial class AppJsonSerializerContext : JsonSerializerContext { }Expected behavior
The result gets serialized.
Actual behaviorAn exception is thrown:
System.InvalidOperationException
HResult=0x80131509
Message=JsonTypeInfo metadata for type 'Program+<<<Main>$>g__GetTodosAsync|0_0>d' was not provided by TypeInfoResolver of type 'AppJsonSerializerContext'. If using source generation, ensure that all root types passed to the serializer have been annotated with 'JsonSerializableAttribute', along with any types that might be serialized polymorphically.
Source=System.Text.Json
StackTrace:
at System.Text.Json.ThrowHelper.ThrowInvalidOperationException_NoMetadataForType(Type type, IJsonTypeInfoResolver resolver) in /_/src/libraries/System.Text.Json/src/System/Text/Json/ThrowHelper.Serialization.cs:line 743
at System.Text.Json.JsonSerializer.GetTypeInfo(JsonSerializerContext context, Type inputType) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Helpers.cs:line 50
at System.Text.Json.JsonSerializer.Serialize(Object value, Type inputType, JsonSerializerContext context) in /_/src/libraries/System.Text.Json/src/System/Text/Json/Serialization/JsonSerializer.Write.String.cs:line 152
at Program.<Main>$(String[] args) in C:\Users\damia\source\repos\JsonAsyncEnumerable\JsonAsyncEnumerable\Program.cs:line 5
Regression?
No, but this was expected to work after the recent changes to support unspeakable types in the JSON source generator.
Known WorkaroundsNone.
ConfigurationSDK version: 8.0.100-preview.3.23178.7
Runtime version: 8.0.0-preview.3.23174.8
OS: Windows 11 x64 22H2
No response
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4