Added search operator to the remove operation

This commit is contained in:
JamesTheMaker 2024-03-07 11:01:29 -05:00
parent 3c8a8619d5
commit c808d207c9

View File

@ -66,7 +66,30 @@ namespace JsonPatching {
} }
Json applyRemoveOperation(Json const& base, Json const& op) { 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) { Json applyAddOperation(Json const& base, Json const& op) {