Skip to content

Update JSON path after promoteNameToValue - #3098

Merged
eamonnmcmanus merged 3 commits into
google:mainfrom
tanvir-ux:fix/promote-name-to-value-json-path
Aug 27, 2026
Merged

eamonnmcmanus merged 3 commits into
google:mainfrom
tanvir-ux:fix/promote-name-to-value-json-path

Conversation

@tanvir-ux

Copy link
Copy Markdown
Contributor

Fixes #1768.

MapTypeAdapterFactory reads object-encoded keys through promoteNameToValue, which turned a property name into a value without recording it in pathNames. After the key was consumed, getPath() stayed at $., so a later parse error (for example an unterminated string value) omitted the property name.

JsonReader now records the promoted name once it is actually read, and JsonTreeReader.promoteNameToValue writes the name into the path immediately. Duplicate-key messages therefore include $.key as well.

I already have a Google CLA on file (auto#2123).

@eamonnmcmanus eamonnmcmanus left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! Just one small request.

I believe @Marcono1234 is familiar with the issues here and may want to weigh in.

JsonReader reader = factory.create("{\"name\":\"value\"}");
reader.beginObject();
JsonReaderInternalAccess.INSTANCE.promoteNameToValue(reader);
assertThat(reader.nextString()).isEqualTo("name");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this would be slightly easier to read like this:

String s1 = reader.nextString();
assertThat(s1).isEqualTo("name");
assertThat(reader.getPreviousPath()).isEqualTo("$.name");

The idea being to avoid calling methods with side-effects inside assertThat. As written the difference between what's happening in the first assertion and in the second is not obvious.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Split the assertions as requested so nextString() is no longer called inside assertThat.

@tanvir-ux

Copy link
Copy Markdown
Contributor Author

Thanks a lot, @eamonnmcmanus 🙂

Map deserialization reads object keys via promoteNameToValue, which previously left pathNames unset, so errors while reading the value reported path $. instead of $.key (google#1768).
The JSON path now includes the last consumed map key, so the trailing-comma error is at $.a rather than $..
@tanvir-ux
tanvir-ux force-pushed the fix/promote-name-to-value-json-path branch from 15844b5 to 232762b Compare August 27, 2026 00:50
@eamonnmcmanus
eamonnmcmanus merged commit b3f4ca2 into google:main Aug 27, 2026
21 checks passed
@Marcono1234

Copy link
Copy Markdown
Contributor

I believe @Marcono1234 is familiar with the issues here and may want to weigh in.

Thanks a lot for this pull request! Looks good to me and I think you covered all corner cases.

I am wondering though if the recordPromotedName calls for case PEEKED_LONG are redundant; have created #3117 for that. @tanvir-ux, please let me know what you think, and whether I am overlooking something.

(Sorry for this late comment.)

@tanvir-ux

Copy link
Copy Markdown
Contributor Author

Thanks @Marcono1234 — you're not overlooking anything.

promoteNameToValue only ever leaves a name token (string forms), so PEEKED_LONG can't happen while promotedName is set. The Long.toString(peekedLong) calls were dead for that path and just added cost. #3117 looks right to me (and the "1" / nextLong coverage is a nice addition).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JsonReader / JsonTreeReader.promoteNameToValue() causes incorrect JSON path

3 participants