-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Description
Description
When deserializing a JSON stream, JsonSerializer.DeserializeAsyncEnumerable<T>
throws a System.Text.Json.JsonException
on data that appears to be perfectly valid. However, using the JsonSerializer.DeserializeAsync<T>
method on the exact same stream succeeds without error.
The failure does not appear to be inherent to the JSON content itself; a minified version of the same data, or a smaller subset of the records, parses correctly with DeserializeAsyncEnumerable
.
Reproduction Steps
-
Download Test.json.
-
Try deserializing it using the following code:
FileStream fileStream = File.OpenRead(jsonFilePath); await using (fileStream.ConfigureAwait(false)) { IAsyncEnumerable<ReadOnlyMemory<JsonElement>> jsonElementsEnumerable = JsonSerializer.DeserializeAsyncEnumerable<ReadOnlyMemory<JsonElement>>(fileStream); }
You'll see the following exception:
System.Text.Json.JsonException: 'The JSON value could not be converted to System.ReadOnlyMemory`1[System.Text.Json.JsonElement]. Path: $[1] | LineNumber: 2950 | BytePositionInLine: 10.'
-
Now try deserializing it using the following code:
FileStream fileStream = File.OpenRead(jsonFilePath); await using (fileStream.ConfigureAwait(false)) { ReadOnlyMemory<ReadOnlyMemory<JsonElement>> jsonElementsRom = await JsonSerializer .DeserializeAsync<ReadOnlyMemory<ReadOnlyMemory<JsonElement>>>(fileStream) .ConfigureAwait(false); }
This time, it works without any errors.
-
Finally, try deserializing the minified version of the same JSON file (TestMinified.json) using the same code as in step 2. Notice that it also works without any errors.
Expected behavior
DeserializeAsyncEnumerable
should successfully parse the JSON without throwing any exceptions. Whether the JSON file is minified or not should not affect the behavior of DeserializeAsyncEnumerable
.
Actual behavior
DeserializeAsyncEnumerable
fails to parse the JSON file with the following exception, and its behavior is affected by whether the JSON file is minified:
```
System.Text.Json.JsonException: 'The JSON value could not be converted to System.ReadOnlyMemory`1[System.Text.Json.JsonElement]. Path: $[1] | LineNumber: 2950 | BytePositionInLine: 10.'
```
Regression?
No response
Known Workarounds
Using DeserializeAsync
instead of DeserializeAsyncEnumerable
Configuration
Which version of .NET is the code running on?
.NET 9
What OS and version, and what distro if applicable?
Windows 10 22H2
What is the architecture (x64, x86, ARM, ARM64)?
x64
Other information
No response