Skip to content

JsonSerializer.DeserializeAsyncEnumerable fails on a valid JSON stream that DeserializeAsync handles correctly #118346

@rampaa

Description

@rampaa

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

  1. Download Test.json.

  2. 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.'
    
  3. 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.

  4. 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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions