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
855cf230
Commit
855cf230
authored
Apr 24, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
extended "add" to cope with arrays
parent
397ada22
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
3 deletions
+65
-3
src/json.hpp
src/json.hpp
+19
-1
src/json.hpp.re2c
src/json.hpp.re2c
+19
-1
test/unit.cpp
test/unit.cpp
+27
-1
No files found.
src/json.hpp
View file @
855cf230
...
...
@@ -9560,7 +9560,25 @@ basic_json_parser_63:
throw
std
::
domain_error
(
"'add' operation must have member 'value'"
);
}
result
[
ptr
]
=
it_value
->
second
;
const
auto
last_path
=
ptr
.
pop_back
();
basic_json
&
parent
=
result
.
at
(
ptr
);
if
(
parent
.
is_object
())
{
parent
[
last_path
]
=
it_value
->
second
;
}
else
if
(
parent
.
is_array
())
{
if
(
last_path
==
"-"
)
{
parent
.
push_back
(
it_value
->
second
);
}
else
{
parent
.
insert
(
parent
.
begin
()
+
std
::
stoi
(
last_path
),
it_value
->
second
);
}
}
}
else
if
(
op
==
"remove"
)
{
...
...
src/json.hpp.re2c
View file @
855cf230
...
...
@@ -8870,7 +8870,25 @@ class basic_json
throw std::domain_error("'add' operation must have member 'value'");
}
result[ptr] = it_value->second;
const auto last_path = ptr.pop_back();
basic_json& parent = result.at(ptr);
if (parent.is_object())
{
parent[last_path] = it_value->second;
}
else if (parent.is_array())
{
if (last_path == "-")
{
parent.push_back(it_value->second);
}
else
{
parent.insert(parent.begin() + std::stoi(last_path),
it_value->second);
}
}
}
else if (op == "remove")
{
...
...
test/unit.cpp
View file @
855cf230
...
...
@@ -12421,6 +12421,29 @@ TEST_CASE("JSON patch")
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
SECTION
(
"example A.2 - Adding an Array Element"
)
{
// An example target JSON document:
json
doc
=
R"(
{ "foo": [ "bar", "baz" ] }
)"
_json
;
// A JSON Patch document:
json
patch
=
R"(
[
{ "op": "add", "path": "/foo/1", "value": "qux" }
]
)"
_json
;
// The resulting JSON document:
json
expected
=
R"(
{ "foo": [ "bar", "qux", "baz" ] }
)"
_json
;
// check if patched value is as expected
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
SECTION
(
"example A.3 - Removing an Object Member"
)
{
// An example target JSON document:
...
...
@@ -12616,9 +12639,12 @@ TEST_CASE("JSON patch")
// an existing object, nor a member of an existing array.
CHECK_THROWS_AS
(
doc
.
apply_patch
(
patch
),
std
::
out_of_range
);
CHECK_THROWS_WITH
(
doc
.
apply_patch
(
patch
),
"
unresolved reference token 'bat'
"
);
CHECK_THROWS_WITH
(
doc
.
apply_patch
(
patch
),
"
key 'baz' not found
"
);
}
// A.13. Invalid JSON Patch Document
// not applicable
SECTION
(
"example A.14 - Escape Ordering"
)
{
// An example target JSON document:
...
...
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