From c808d207c91e577d12907ea9723d500849e0d61f Mon Sep 17 00:00:00 2001 From: JamesTheMaker Date: Thu, 7 Mar 2024 11:01:29 -0500 Subject: [PATCH] Added `search` operator to the `remove` operation --- source/core/StarJsonPatch.cpp | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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) {