Skip to content

Commit a0a4e7c

Browse files
authored
Add front methods to json_pointers (implements #4889) (#5152)
* Added front, pop_front, and push_front methods to json_pointers in order to facilitate root-to-leaf traversals of JSON object trees. (#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>
1 parent 216c503 commit a0a4e7c

14 files changed

Lines changed: 274 additions & 2 deletions

File tree

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# <small>nlohmann::json_pointer::</small>front
2+
3+
```cpp
4+
const string_t& front() const;
5+
```
6+
7+
Return the first reference token.
8+
9+
## Return value
10+
11+
First reference token.
12+
13+
## Exceptions
14+
15+
Throws [out_of_range.405](../../home/exceptions.md#jsonexceptionout_of_range405) if the JSON pointer has no parent.
16+
17+
## Complexity
18+
19+
Constant.
20+
21+
## Examples
22+
23+
??? example
24+
25+
The example shows the usage of `front`.
26+
27+
```cpp
28+
--8<-- "examples/json_pointer__front.cpp"
29+
```
30+
31+
Output:
32+
33+
```json
34+
--8<-- "examples/json_pointer__front.output"
35+
```
36+
37+
## Version history

docs/mkdocs/docs/api/json_pointer/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ are the base for JSON patches.
3737
- [**pop_back**](pop_back.md) - remove the last reference token
3838
- [**back**](back.md) - return last reference token
3939
- [**push_back**](push_back.md) - append an unescaped token at the end of the pointer
40+
- [**pop_front**](pop_front.md) - remove the first reference token
41+
- [**front**](front.md) - return first reference token
42+
- [**push_front**](push_front.md) - append an unescaped token at the start of the pointer
4043
- [**empty**](empty.md) - return whether the pointer points to the root document
4144

4245
## Literals
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# <small>nlohmann::json_pointer::</small>pop_front
2+
3+
```cpp
4+
void pop_front();
5+
```
6+
7+
Remove the first reference token.
8+
9+
## Exceptions
10+
11+
Throws [out_of_range.405](../../home/exceptions.md#jsonexceptionout_of_range405) if the JSON pointer has no parent.
12+
13+
## Complexity
14+
15+
Linear in the number of reference tokens in the `json_pointer`.
16+
17+
## Examples
18+
19+
??? example
20+
21+
The example shows the usage of `pop_front`.
22+
23+
```cpp
24+
--8<-- "examples/json_pointer__pop_front.cpp"
25+
```
26+
27+
Output:
28+
29+
```json
30+
--8<-- "examples/json_pointer__pop_front.output"
31+
```
32+
33+
## Version history
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# <small>nlohmann::json_pointer::</small>push_front
2+
3+
```cpp
4+
void push_front(const string_t& token);
5+
6+
void push_front(string_t&& token);
7+
```
8+
9+
Append an unescaped token at the start of the reference pointer.
10+
11+
## Parameters
12+
13+
`token` (in)
14+
: token to add
15+
16+
## Complexity
17+
18+
Linear in the number of reference tokens in the `json_pointer`.
19+
20+
## Examples
21+
22+
??? example
23+
24+
The example shows the result of `push_front` for different JSON Pointers.
25+
26+
```cpp
27+
--8<-- "examples/json_pointer__push_front.cpp"
28+
```
29+
30+
Output:
31+
32+
```json
33+
--8<-- "examples/json_pointer__push_front.output"
34+
```
35+
36+
## Version history
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
// different JSON Pointers
9+
json::json_pointer ptr1("/foo");
10+
json::json_pointer ptr2("/foo/0");
11+
12+
// call empty()
13+
std::cout << "first reference token of \"" << ptr1 << "\" is \"" << ptr1.front() << "\"\n"
14+
<< "first reference token of \"" << ptr2 << "\" is \"" << ptr2.front() << "\"" << std::endl;
15+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
first reference token of "/foo" is "foo"
2+
first reference token of "/foo/0" is "foo"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
// create empty JSON Pointer
9+
json::json_pointer ptr("/foo/bar/baz");
10+
std::cout << "\"" << ptr << "\"\n";
11+
12+
// call pop_front()
13+
ptr.pop_front();
14+
std::cout << "\"" << ptr << "\"\n";
15+
16+
ptr.pop_front();
17+
std::cout << "\"" << ptr << "\"\n";
18+
19+
ptr.pop_front();
20+
std::cout << "\"" << ptr << "\"\n";
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
"/foo/bar/baz"
2+
"/bar/baz"
3+
"/baz"
4+
""
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <iostream>
2+
#include <nlohmann/json.hpp>
3+
4+
using json = nlohmann::json;
5+
6+
int main()
7+
{
8+
// create empty JSON Pointer
9+
json::json_pointer ptr;
10+
std::cout << "\"" << ptr << "\"\n";
11+
12+
// call push_front()
13+
ptr.push_front("foo");
14+
std::cout << "\"" << ptr << "\"\n";
15+
16+
ptr.push_front("0");
17+
std::cout << "\"" << ptr << "\"\n";
18+
19+
ptr.push_front("bar");
20+
std::cout << "\"" << ptr << "\"\n";
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
""
2+
"/foo"
3+
"/0/foo"
4+
"/bar/0/foo"

0 commit comments

Comments
 (0)