Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
J
json
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chen Yisong
json
Commits
09e9f6dc
Commit
09e9f6dc
authored
Apr 24, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implemented "move"
parent
855cf230
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
152 additions
and
60 deletions
+152
-60
json.hpp
src/json.hpp
+45
-30
json.hpp.re2c
src/json.hpp.re2c
+45
-30
unit.cpp
test/unit.cpp
+62
-0
No files found.
src/json.hpp
View file @
09e9f6dc
...
@@ -9526,6 +9526,44 @@ basic_json_parser_63:
...
@@ -9526,6 +9526,44 @@ basic_json_parser_63:
throw
std
::
domain_error
(
"JSON patch must be an array of objects"
);
throw
std
::
domain_error
(
"JSON patch must be an array of objects"
);
}
}
const
auto
operation_add
=
[
&
result
](
json_pointer
&
ptr
,
basic_json
&
value
)
{
const
auto
last_path
=
ptr
.
pop_back
();
basic_json
&
parent
=
result
.
at
(
ptr
);
if
(
parent
.
is_object
())
{
parent
[
last_path
]
=
value
;
}
else
if
(
parent
.
is_array
())
{
if
(
last_path
==
"-"
)
{
parent
.
push_back
(
value
);
}
else
{
parent
.
insert
(
parent
.
begin
()
+
std
::
stoi
(
last_path
),
value
);
}
}
};
const
auto
operation_remove
=
[
&
result
](
json_pointer
&
ptr
)
{
const
auto
last_path
=
ptr
.
pop_back
();
basic_json
&
parent
=
result
.
at
(
ptr
);
if
(
parent
.
is_object
())
{
parent
.
erase
(
parent
.
find
(
last_path
));
}
else
if
(
parent
.
is_array
())
{
parent
.
erase
(
parent
.
begin
()
+
std
::
stoi
(
last_path
));
}
};
for
(
const
auto
&
val
:
patch
)
for
(
const
auto
&
val
:
patch
)
{
{
if
(
not
val
.
is_object
())
if
(
not
val
.
is_object
())
...
@@ -9560,38 +9598,11 @@ basic_json_parser_63:
...
@@ -9560,38 +9598,11 @@ basic_json_parser_63:
throw
std
::
domain_error
(
"'add' operation must have member 'value'"
);
throw
std
::
domain_error
(
"'add' operation must have member 'value'"
);
}
}
const
auto
last_path
=
ptr
.
pop_back
();
operation_add
(
ptr
,
it_value
->
second
);
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"
)
else
if
(
op
==
"remove"
)
{
{
const
auto
last_path
=
ptr
.
pop_back
();
operation_remove
(
ptr
);
basic_json
&
parent
=
result
.
at
(
ptr
);
if
(
parent
.
is_object
())
{
parent
.
erase
(
parent
.
find
(
last_path
));
}
else
if
(
parent
.
is_array
())
{
parent
.
erase
(
parent
.
begin
()
+
std
::
stoi
(
last_path
));
}
}
}
else
if
(
op
==
"replace"
)
else
if
(
op
==
"replace"
)
{
{
...
@@ -9610,7 +9621,11 @@ basic_json_parser_63:
...
@@ -9610,7 +9621,11 @@ basic_json_parser_63:
}
}
const
std
::
string
from_path
=
it_from
->
second
;
const
std
::
string
from_path
=
it_from
->
second
;
const
json_pointer
from_ptr
(
from_path
);
json_pointer
from_ptr
(
from_path
);
basic_json
v
=
result
[
from_ptr
];
operation_remove
(
from_ptr
);
operation_add
(
ptr
,
v
);
}
}
else
if
(
op
==
"copy"
)
else
if
(
op
==
"copy"
)
{
{
...
...
src/json.hpp.re2c
View file @
09e9f6dc
...
@@ -8836,6 +8836,44 @@ class basic_json
...
@@ -8836,6 +8836,44 @@ class basic_json
throw std::domain_error("JSON patch must be an array of objects");
throw std::domain_error("JSON patch must be an array of objects");
}
}
const auto operation_add = [&result](json_pointer & ptr,
basic_json & value)
{
const auto last_path = ptr.pop_back();
basic_json& parent = result.at(ptr);
if (parent.is_object())
{
parent[last_path] = value;
}
else if (parent.is_array())
{
if (last_path == "-")
{
parent.push_back(value);
}
else
{
parent.insert(parent.begin() + std::stoi(last_path),
value);
}
}
};
const auto operation_remove = [&result](json_pointer & ptr)
{
const auto last_path = ptr.pop_back();
basic_json& parent = result.at(ptr);
if (parent.is_object())
{
parent.erase(parent.find(last_path));
}
else if (parent.is_array())
{
parent.erase(parent.begin() + std::stoi(last_path));
}
};
for (const auto& val : patch)
for (const auto& val : patch)
{
{
if (not val.is_object())
if (not val.is_object())
...
@@ -8870,38 +8908,11 @@ class basic_json
...
@@ -8870,38 +8908,11 @@ class basic_json
throw std::domain_error("'add' operation must have member 'value'");
throw std::domain_error("'add' operation must have member 'value'");
}
}
const auto last_path = ptr.pop_back();
operation_add(ptr, it_value->second);
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")
else if (op == "remove")
{
{
const auto last_path = ptr.pop_back();
operation_remove(ptr);
basic_json& parent = result.at(ptr);
if (parent.is_object())
{
parent.erase(parent.find(last_path));
}
else if (parent.is_array())
{
parent.erase(parent.begin() + std::stoi(last_path));
}
}
}
else if (op == "replace")
else if (op == "replace")
{
{
...
@@ -8920,7 +8931,11 @@ class basic_json
...
@@ -8920,7 +8931,11 @@ class basic_json
}
}
const std::string from_path = it_from->second;
const std::string from_path = it_from->second;
const json_pointer from_ptr(from_path);
json_pointer from_ptr(from_path);
basic_json v = result[from_ptr];
operation_remove(from_ptr);
operation_add(ptr, v);
}
}
else if (op == "copy")
else if (op == "copy")
{
{
...
...
test/unit.cpp
View file @
09e9f6dc
...
@@ -12521,6 +12521,68 @@ TEST_CASE("JSON patch")
...
@@ -12521,6 +12521,68 @@ TEST_CASE("JSON patch")
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
}
SECTION
(
"example A.6 - Moving a Value"
)
{
// An example target JSON document:
json
doc
=
R"(
{
"foo": {
"bar": "baz",
"waldo": "fred"
},
"qux": {
"corge": "grault"
}
}
)"
_json
;
// A JSON Patch document:
json
patch
=
R"(
[
{ "op": "move", "from": "/foo/waldo", "path": "/qux/thud" }
]
)"
_json
;
// The resulting JSON document:
json
expected
=
R"(
{
"foo": {
"bar": "baz"
},
"qux": {
"corge": "grault",
"thud": "fred"
}
}
)"
_json
;
// check if patched value is as expected
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
SECTION
(
"example A.7 - Moving a Value"
)
{
// An example target JSON document:
json
doc
=
R"(
{ "foo": [ "all", "grass", "cows", "eat" ] }
)"
_json
;
// A JSON Patch document:
json
patch
=
R"(
[
{ "op": "move", "from": "/foo/1", "path": "/foo/3" }
]
)"
_json
;
// The resulting JSON document:
json
expected
=
R"(
{ "foo": [ "all", "cows", "eat", "grass" ] }
)"
_json
;
// check if patched value is as expected
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
SECTION
(
"example A.8 - Testing a Value: Success"
)
SECTION
(
"example A.8 - Testing a Value: Success"
)
{
{
// An example target JSON document:
// 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