Skip to content

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

Open
trdesilva wants to merge 2 commits intonlohmann:developfrom
trdesilva:jsonPointerFront4889
Open

Add front methods to json_pointers (implements #4889)#5152
trdesilva wants to merge 2 commits intonlohmann:developfrom
trdesilva:jsonPointerFront4889

Conversation

@trdesilva
Copy link
Copy Markdown

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.

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.

1 participant