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
397ada22
Commit
397ada22
authored
Apr 20, 2016
by
Niels
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
implemented remove
parent
fa03cf0c
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
4 deletions
+97
-4
src/json.hpp
src/json.hpp
+24
-2
src/json.hpp.re2c
src/json.hpp.re2c
+24
-2
test/unit.cpp
test/unit.cpp
+49
-0
No files found.
src/json.hpp
View file @
397ada22
...
@@ -8969,6 +8969,18 @@ basic_json_parser_63:
...
@@ -8969,6 +8969,18 @@ basic_json_parser_63:
:
reference_tokens
(
split
(
s
))
:
reference_tokens
(
split
(
s
))
{}
{}
std
::
string
pop_back
()
{
if
(
reference_tokens
.
empty
())
{
throw
std
::
domain_error
(
"JSON pointer has no parent"
);
}
auto
last
=
reference_tokens
.
back
();
reference_tokens
.
pop_back
();
return
last
;
}
private:
private:
/*!
/*!
@brief create and return a reference to the pointed to value
@brief create and return a reference to the pointed to value
...
@@ -9420,7 +9432,7 @@ basic_json_parser_63:
...
@@ -9420,7 +9432,7 @@ basic_json_parser_63:
private:
private:
/// the reference tokens
/// the reference tokens
const
std
::
vector
<
std
::
string
>
reference_tokens
{};
std
::
vector
<
std
::
string
>
reference_tokens
{};
};
};
////////////////////////////
////////////////////////////
...
@@ -9539,7 +9551,7 @@ basic_json_parser_63:
...
@@ -9539,7 +9551,7 @@ basic_json_parser_63:
const
std
::
string
op
=
it_op
->
second
;
const
std
::
string
op
=
it_op
->
second
;
const
std
::
string
path
=
it_path
->
second
;
const
std
::
string
path
=
it_path
->
second
;
const
json_pointer
ptr
(
path
);
json_pointer
ptr
(
path
);
if
(
op
==
"add"
)
if
(
op
==
"add"
)
{
{
...
@@ -9552,6 +9564,16 @@ basic_json_parser_63:
...
@@ -9552,6 +9564,16 @@ basic_json_parser_63:
}
}
else
if
(
op
==
"remove"
)
else
if
(
op
==
"remove"
)
{
{
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
));
}
}
}
else
if
(
op
==
"replace"
)
else
if
(
op
==
"replace"
)
{
{
...
...
src/json.hpp.re2c
View file @
397ada22
...
@@ -8279,6 +8279,18 @@ class basic_json
...
@@ -8279,6 +8279,18 @@ class basic_json
: reference_tokens(split(s))
: reference_tokens(split(s))
{}
{}
std::string pop_back()
{
if (reference_tokens.empty())
{
throw std::domain_error("JSON pointer has no parent");
}
auto last = reference_tokens.back();
reference_tokens.pop_back();
return last;
}
private:
private:
/*!
/*!
@brief create and return a reference to the pointed to value
@brief create and return a reference to the pointed to value
...
@@ -8730,7 +8742,7 @@ class basic_json
...
@@ -8730,7 +8742,7 @@ class basic_json
private:
private:
/// the reference tokens
/// the reference tokens
const
std::vector<std::string> reference_tokens {};
std::vector<std::string> reference_tokens {};
};
};
////////////////////////////
////////////////////////////
...
@@ -8849,7 +8861,7 @@ class basic_json
...
@@ -8849,7 +8861,7 @@ class basic_json
const std::string op = it_op->second;
const std::string op = it_op->second;
const std::string path = it_path->second;
const std::string path = it_path->second;
const
json_pointer ptr(path);
json_pointer ptr(path);
if (op == "add")
if (op == "add")
{
{
...
@@ -8862,6 +8874,16 @@ class basic_json
...
@@ -8862,6 +8874,16 @@ class basic_json
}
}
else if (op == "remove")
else if (op == "remove")
{
{
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));
}
}
}
else if (op == "replace")
else if (op == "replace")
{
{
...
...
test/unit.cpp
View file @
397ada22
...
@@ -12421,6 +12421,55 @@ TEST_CASE("JSON patch")
...
@@ -12421,6 +12421,55 @@ TEST_CASE("JSON patch")
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
}
SECTION
(
"example A.3 - Removing an Object Member"
)
{
// An example target JSON document:
json
doc
=
R"(
{
"baz": "qux",
"foo": "bar"
}
)"
_json
;
// A JSON Patch document:
json
patch
=
R"(
[
{ "op": "remove", "path": "/baz" }
]
)"
_json
;
// The resulting JSON document:
json
expected
=
R"(
{ "foo": "bar" }
)"
_json
;
// check if patched value is as expected
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
SECTION
(
"example A.4 - Removing an Array Element"
)
{
// An example target JSON document:
json
doc
=
R"(
{ "foo": [ "bar", "qux", "baz" ] }
)"
_json
;
// A JSON Patch document:
json
patch
=
R"(
[
{ "op": "remove", "path": "/foo/1" }
]
)"
_json
;
// The resulting JSON document:
json
expected
=
R"(
{ "foo": [ "bar", "baz" ] }
)"
_json
;
// check if patched value is as expected
CHECK
(
doc
.
apply_patch
(
patch
)
==
expected
);
}
SECTION
(
"example A.5 - Replacing a Value"
)
SECTION
(
"example A.5 - Replacing a Value"
)
{
{
// 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