Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarJsonPatch.cpp
diff options
context:
space:
mode:
authorJamesTheMaker <jamesthemaker2005@gmail.com>2024-03-07 11:01:29 -0500
committerJamesTheMaker <jamesthemaker2005@gmail.com>2024-03-07 11:01:29 -0500
commitc808d207c91e577d12907ea9723d500849e0d61f (patch)
treef9b9421557822a31c5a1755563be672c2ea5939d /source/core/StarJsonPatch.cpp
parent3c8a8619d57b0432d2a5d32d8853fd9d0e52a8b1 (diff)
Added `search` operator to the `remove` operation
Diffstat (limited to 'source/core/StarJsonPatch.cpp')
-rw-r--r--source/core/StarJsonPatch.cpp25
1 files changed, 24 insertions, 1 deletions
diff --git a/source/core/StarJsonPatch.cpp b/source/core/StarJsonPatch.cpp
index 34c6a82..9ef11ff 100644
--- a/source/core/StarJsonPatch.cpp
+++ b/source/core/StarJsonPatch.cpp
@@ -66,7 +66,30 @@ namespace JsonPatching {
}
Json applyRemoveOperation(Json const& base, Json const& op) {
- return JsonPath::Pointer(op.getString("path")).remove(base);
+ if (op.contains("search")) {
+ String path = op.getString("path");
+ auto pointer = JsonPath::Pointer(path);
+ Json searchArray = pointer.get(base);
+ Json searchValue = op.get("search");
+ if (searchArray.type() == Json::Type::Array) {
+ size_t index = 0;
+ bool found = false;
+ for (auto& e : searchArray.toArray()) {
+ if (jsonCompare(e, searchValue)) {
+ found = true;
+ break;
+ }
+ index++;
+ }
+ if (found)
+ searchArray = searchArray.eraseIndex(index);
+ return pointer.add(pointer.remove(base), searchArray);
+ } else {
+ throw JsonPatchException(strf("Search operation failure, value at {} is not an array.", path));
+ }
+ } else {
+ return JsonPath::Pointer(op.getString("path")).remove(base);
+ }
}
Json applyAddOperation(Json const& base, Json const& op) {