diff options
author | JamesTheMaker <jamesthemaker2005@gmail.com> | 2024-03-07 11:01:29 -0500 |
---|---|---|
committer | JamesTheMaker <jamesthemaker2005@gmail.com> | 2024-03-07 11:01:29 -0500 |
commit | c808d207c91e577d12907ea9723d500849e0d61f (patch) | |
tree | f9b9421557822a31c5a1755563be672c2ea5939d /source/core/StarJsonPatch.cpp | |
parent | 3c8a8619d57b0432d2a5d32d8853fd9d0e52a8b1 (diff) |
Added `search` operator to the `remove` operation
Diffstat (limited to 'source/core/StarJsonPatch.cpp')
-rw-r--r-- | source/core/StarJsonPatch.cpp | 25 |
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) { |