[API Proposal]: Save comma flag in `JsonReaderState`.

Open
#137,957 3 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Tech stack
csharp
Domain
backend

Research direction

Start by reading the JsonReaderState and Utf8JsonReader code that tracks commas, including the existing _trailingCommaBeforeComment flag. The issue proposes preserving comma state so streamed reads can advance to the next token; check the reader’s state and streaming tests to understand current behavior. Done means partial-token reads can resume at the token start after consuming the comma, with existing reader behavior covered.

Written by the indexing model from the issue text.

Description

api-suggestion area-System.Text.Json untriaged
Background and motivation

When large json is iterated in streamed way and doesn't fit the buffer, it is preferred to leave the buffer at the start of the next token since token is what we are gonna read. Commas are for syntax only, they are not meant to be consumed, reader doesn't even return them.
However, at the moment reader stops consuming right before the comma when next token is partial.
Assume that we are reading [ "token 1" , "token 2" ]. Minimum reading steps are: [, , "token 1", , , "token 2", , ].
First two whitespaces are independent since they are handled by SkipWhiteSpace(). However, since encountered comma is not saved in a JsonReaderState, reader can't advance past comma between two tokens, thus both comma and all whitespaces after it have to be present along with token 2, otherwise token 2 will throw as unexpected(because reader expects a comma).

API Proposal

Add a flag to JsonReaderState to note that comma is encountered. Thus reader will be able to consume past comma and stop right at the beginning of the token.
The flag probably can replace previous _trailingCommaBeforeComment as well.

API Usage
Alternative Designs

No response

Risks

No response

Dominant language
C#
Stars
18.5k
Forks
5.8k
Avg merge
2d 18h
Merged PRs (30d)
588

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/runtime

All issues in dotnet/runtime

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.