Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
J
json
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
json
Commits
a06e7f5d
Commit
a06e7f5d
authored
Jan 24, 2019
by
Patrick Boettcher
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
JSON-pointer: add operator+() returning a new json_pointer
parent
e89c9464
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
96 additions
and
4 deletions
+96
-4
include/nlohmann/detail/json_pointer.hpp
include/nlohmann/detail/json_pointer.hpp
+21
-2
single_include/nlohmann/json.hpp
single_include/nlohmann/json.hpp
+21
-2
test/src/unit-json_pointer.cpp
test/src/unit-json_pointer.cpp
+54
-0
No files found.
include/nlohmann/detail/json_pointer.hpp
View file @
a06e7f5d
...
...
@@ -114,14 +114,33 @@ class json_pointer
}
/*!
@brief remove and return last reference pointer
@throw out_of_range.405 if JSON pointer has no parent
@brief append a token at the end of the reference pointer
*/
void
push_back
(
const
std
::
string
&
tok
)
{
reference_tokens
.
push_back
(
tok
);
}
/*!
@brief append a key-token at the end of the reference pointer and return a new json-pointer.
*/
json_pointer
operator
+
(
const
std
::
string
&
tok
)
const
{
auto
ptr
=
*
this
;
ptr
.
push_back
(
tok
);
return
ptr
;
}
/*!
@brief append a array-index-token at the end of the reference pointer and return a new json-pointer.
*/
json_pointer
operator
+
(
const
size_t
&
index
)
const
{
auto
ptr
=
*
this
;
ptr
.
push_back
(
std
::
to_string
(
index
));
return
ptr
;
}
private:
/// return whether pointer points to the root document
bool
is_root
()
const
noexcept
...
...
single_include/nlohmann/json.hpp
View file @
a06e7f5d
...
...
@@ -11924,14 +11924,33 @@ class json_pointer
}
/*!
@brief remove and return last reference pointer
@throw out_of_range.405 if JSON pointer has no parent
@brief append a token at the end of the reference pointer
*/
void
push_back
(
const
std
::
string
&
tok
)
{
reference_tokens
.
push_back
(
tok
);
}
/*!
@brief append a key-token at the end of the reference pointer and return a new json-pointer.
*/
json_pointer
operator
+
(
const
std
::
string
&
tok
)
const
{
auto
ptr
=
*
this
;
ptr
.
push_back
(
tok
);
return
ptr
;
}
/*!
@brief append a array-index-token at the end of the reference pointer and return a new json-pointer.
*/
json_pointer
operator
+
(
const
size_t
&
index
)
const
{
auto
ptr
=
*
this
;
ptr
.
push_back
(
std
::
to_string
(
index
));
return
ptr
;
}
private:
/// return whether pointer points to the root document
bool
is_root
()
const
noexcept
...
...
test/src/unit-json_pointer.cpp
View file @
a06e7f5d
...
...
@@ -513,4 +513,58 @@ TEST_CASE("JSON pointers")
CHECK
(
j
[
ptr
]
==
j
[
"object"
][
"/"
]);
CHECK
(
ptr
.
to_string
()
==
"/object/~1"
);
}
SECTION
(
"operators"
)
{
const
json
j
=
{
{
""
,
"Hello"
},
{
"pi"
,
3.141
},
{
"happy"
,
true
},
{
"name"
,
"Niels"
},
{
"nothing"
,
nullptr
},
{
"answer"
,
{
{
"everything"
,
42
}
}
},
{
"list"
,
{
1
,
0
,
2
}},
{
"object"
,
{
{
"currency"
,
"USD"
},
{
"value"
,
42.99
},
{
""
,
"empty string"
},
{
"/"
,
"slash"
},
{
"~"
,
"tilde"
},
{
"~1"
,
"tilde1"
}
}
}
};
// empty json_pointer returns the root JSON-object
auto
ptr
=
""
_json_pointer
;
CHECK
(
j
[
ptr
]
==
j
);
// simple field access
ptr
=
ptr
+
"pi"
;
CHECK
(
j
[
ptr
]
==
j
[
"pi"
]);
ptr
.
pop_back
();
CHECK
(
j
[
ptr
]
==
j
);
// object and children access
ptr
=
ptr
+
"answer"
;
ptr
=
ptr
+
"everything"
;
CHECK
(
j
[
ptr
]
==
j
[
"answer"
][
"everything"
]);
ptr
.
pop_back
();
ptr
.
pop_back
();
CHECK
(
j
[
ptr
]
==
j
);
// push key which has to be encoded
ptr
=
ptr
+
"object"
;
ptr
=
ptr
+
"/"
;
CHECK
(
j
[
ptr
]
==
j
[
"object"
][
"/"
]);
CHECK
(
ptr
.
to_string
()
==
"/object/~1"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment