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
fa03cf0c
Commit
fa03cf0c
authored
Apr 20, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
replace and copy
parent
70fc5835
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
108 additions
and
0 deletions
+108
-0
src/json.hpp
src/json.hpp
+19
-0
src/json.hpp.re2c
src/json.hpp.re2c
+19
-0
test/unit.cpp
test/unit.cpp
+70
-0
No files found.
src/json.hpp
View file @
fa03cf0c
...
...
@@ -9525,6 +9525,7 @@ basic_json_parser_63:
const
auto
it_op
=
val
.
m_value
.
object
->
find
(
"op"
);
const
auto
it_path
=
val
.
m_value
.
object
->
find
(
"path"
);
const
auto
it_value
=
val
.
m_value
.
object
->
find
(
"value"
);
const
auto
it_from
=
val
.
m_value
.
object
->
find
(
"from"
);
if
(
it_op
==
val
.
m_value
.
object
->
end
()
or
not
it_op
->
second
.
is_string
())
{
...
...
@@ -9558,12 +9559,30 @@ basic_json_parser_63:
{
throw
std
::
domain_error
(
"'replace' operation must have member 'value'"
);
}
result
.
at
(
ptr
)
=
it_value
->
second
;
}
else
if
(
op
==
"move"
)
{
if
(
it_from
==
val
.
m_value
.
object
->
end
())
{
throw
std
::
domain_error
(
"'move' operation must have member 'from'"
);
}
const
std
::
string
from_path
=
it_from
->
second
;
const
json_pointer
from_ptr
(
from_path
);
}
else
if
(
op
==
"copy"
)
{
if
(
it_from
==
val
.
m_value
.
object
->
end
())
{
throw
std
::
domain_error
(
"'copy' operation must have member 'from'"
);
}
const
std
::
string
from_path
=
it_from
->
second
;
const
json_pointer
from_ptr
(
from_path
);
result
[
ptr
]
=
result
.
at
(
from_ptr
);
}
else
if
(
op
==
"test"
)
{
...
...
src/json.hpp.re2c
View file @
fa03cf0c
...
...
@@ -8835,6 +8835,7 @@ class basic_json
const auto it_op = val.m_value.object->find("op");
const auto it_path = val.m_value.object->find("path");
const auto it_value = val.m_value.object->find("value");
const auto it_from = val.m_value.object->find("from");
if (it_op == val.m_value.object->end() or not it_op->second.is_string())
{
...
...
@@ -8868,12 +8869,30 @@ class basic_json
{
throw std::domain_error("'replace' operation must have member 'value'");
}
result.at(ptr) = it_value->second;
}
else if (op == "move")
{
if (it_from == val.m_value.object->end())
{
throw std::domain_error("'move' operation must have member 'from'");
}
const std::string from_path = it_from->second;
const json_pointer from_ptr(from_path);
}
else if (op == "copy")
{
if (it_from == val.m_value.object->end())
{
throw std::domain_error("'copy' operation must have member 'from'");
}
const std::string from_path = it_from->second;
const json_pointer from_ptr(from_path);
result[ptr] = result.at(from_ptr);
}
else if (op == "test")
{
...
...
test/unit.cpp
View file @
fa03cf0c
...
...
@@ -12421,6 +12421,34 @@ TEST_CASE("JSON patch")
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
SECTION
(
"example A.5 - Replacing a Value"
)
{
// An example target JSON document:
json
doc
=
R"(
{
"baz": "qux",
"foo": "bar"
}
)"
_json
;
// A JSON Patch document:
json
patch
=
R"(
[
{ "op": "replace", "path": "/baz", "value": "boo" }
]
)"
_json
;
json
expected
=
R"(
{
"baz": "boo",
"foo": "bar"
}
)"
_json
;
// check if patched value is as expected
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
SECTION
(
"example A.8 - Testing a Value: Success"
)
{
// An example target JSON document:
...
...
@@ -12615,6 +12643,48 @@ TEST_CASE("JSON patch")
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
}
SECTION
(
"own examples"
)
{
SECTION
(
"copy"
)
{
// 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": "copy", "from": "/foo/waldo", "path": "/qux/thud" }
]
)"
_json
;
json
expected
=
R"(
{
"foo": {
"bar": "baz",
"waldo": "fred"
},
"qux": {
"corge": "grault",
"thud": "fred"
}
}
)"
_json
;
// check if patched value is as expected
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
}
}
TEST_CASE
(
"regression tests"
)
...
...
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