Skip to content

Add front methods to json_pointers (implements #4889) - #5152

Merged
nlohmann merged 6 commits into
nlohmann:developfrom
trdesilva:jsonPointerFront4889
May 13, 2026
Merged

nlohmann merged 6 commits into
nlohmann:developfrom
trdesilva:jsonPointerFront4889

Conversation

@trdesilva

@trdesilva trdesilva commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Added front, pop_front, and push_front methods to json_pointers in order to facilitate root-to-leaf traversals of JSON object trees. (#4889) These methods complement back, pop_back, and push_back, allowing json_pointers to be used as deques instead of just stacks. Example use case from the linked issue:

This is the pattern that I'm using for the case in the original post:

if(pointer.front() == INDIRECTION_TOKEN)
{
    pointer.pop_front();
    objectToPatch = fetchObject(pointer.front());
    pointer.pop_front();
    patchOperations[i]["path"] = pointer.to_string();
}
objectToPatch.patch(patchOperations);

More generally, pop_front() is useful for manually traversing the path specified by a json_pointer when you can't use json::operator[] on the entire pointer because you need to do some intermediate work during the traversal.

while(!pointer.empty())
{
    jsonObject = jsonObject[pointer.front()];
    doSomething(jsonObject);
    pointer.pop_front();
}

Also made a minor update in unit_json-pointer.cpp to use CHECK_THROWS_WITH_AS instead of CHECK_THROWS_WITH to conform with current Contribution Guidelines. Didn't have make available in Windows dev environment, so amalgamate was run manually and make pretty was skipped; hopefully CI can do this?

  • The changes are described in detail, both the what and why.
  • If applicable, an existing issue is referenced.
  • The Code coverage remained at 100%. A test case for every new line of code.
  • If applicable, the documentation is updated.
  • The source code is amalgamated by running make amalgamate.

Read the Contribution Guidelines for detailed information.

…der to facilitate root-to-leaf traversals of JSON object trees. (nlohmann#4889)

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>
Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>
@trdesilva
trdesilva force-pushed the jsonPointerFront4889 branch from c216462 to acb6b66 Compare April 28, 2026 03:05
@github-actions

Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @trdesilva
Please read and follow the Contribution Guidelines.

@nlohmann nlohmann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please run the amalgamation.

…hods in mkdocs, and fixed errors in documented complexity for those methods.

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>
@trdesilva
trdesilva requested a review from nlohmann May 11, 2026 06:08

@nlohmann nlohmann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>
@trdesilva
trdesilva force-pushed the jsonPointerFront4889 branch from 1372365 to 0616a72 Compare May 12, 2026 00:03
Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>
@github-actions

Copy link
Copy Markdown

🔴 Amalgamation check failed! 🔴

The source code has not been amalgamated. @trdesilva
Please read and follow the Contribution Guidelines.

@nlohmann nlohmann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Please amalgamate the source code.

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

@nlohmann nlohmann left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Looks good to me.

@nlohmann nlohmann added this to the Release 3.12.1 milestone May 13, 2026
@nlohmann
nlohmann merged commit a0a4e7c into nlohmann:develop May 13, 2026
142 checks passed
@nlohmann

Copy link
Copy Markdown
Owner

Thank you!

GhostVaibhav pushed a commit to GhostVaibhav/json that referenced this pull request May 19, 2026
…nn#5152)

* Added front, pop_front, and push_front methods to json_pointers in order to facilitate root-to-leaf traversals of JSON object trees. (nlohmann#4889)

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* undid VS autoformatting in irrelevant code

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* Ran make amalgamate, added navigation to json_pointer's new front methods in mkdocs, and fixed errors in documented complexity for those methods.

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* Fixed GCC 4.8 compile error caused by const iterators

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* Fixed another gcc-4.8 compile error

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* amalgamated

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

---------

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>
Signed-off-by: Vaibhav Sharma <48472541+GhostVaibhav@users.noreply.github.com>
jwnimmer-tri pushed a commit to jwnimmer-tri/json that referenced this pull request Sep 21, 2026
…nn#5152)

* Added front, pop_front, and push_front methods to json_pointers in order to facilitate root-to-leaf traversals of JSON object trees. (nlohmann#4889)

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* undid VS autoformatting in irrelevant code

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* Ran make amalgamate, added navigation to json_pointer's new front methods in mkdocs, and fixed errors in documented complexity for those methods.

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* Fixed GCC 4.8 compile error caused by const iterators

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* Fixed another gcc-4.8 compile error

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

* amalgamated

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>

---------

Signed-off-by: trdesilva <5818730+trdesilva@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants