add patch_inplace function to json class

This commit is contained in:
Wolf Vollprecht 2022-07-16 06:47:10 -05:00
parent d4daaa897f
commit fa9a1395c8
2 changed files with 21 additions and 8 deletions

View File

@ -1,13 +1,20 @@
# <small>nlohmann::basic_json::</small>patch
```cpp
// (1)
basic_json patch(const basic_json& json_patch) const;
// (2)
void patch_inplace(const basic_json& json_patch) const;
```
[JSON Patch](http://jsonpatch.com) defines a JSON document structure for expressing a sequence of operations to apply to
a JSON document. With this function, a JSON Patch is applied to the current JSON value by executing all operations from
the patch.
1. applies a JSON patch to a copy of the current object and returns the copy
2. applies a JSON patch in place and returns void
## Parameters
`json_patch` (in)
@ -15,11 +22,13 @@ the patch.
## Return value
patched document
1. patched document
2. void
## Exception safety
Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
1. Strong guarantee: if an exception is thrown, there are no changes in the JSON value.
2. No guarantees, value may be corruted.
## Exceptions

View File

@ -4641,13 +4641,11 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
/// @name JSON Patch functions
/// @{
/// @brief applies a JSON patch
/// @brief applies a JSON patch in-place without copying the object
/// @sa https://json.nlohmann.me/api/basic_json/patch/
basic_json patch(const basic_json& json_patch) const
void patch_inplace(const basic_json& json_patch)
{
// make a working copy to apply the patch to
basic_json result = *this;
basic_json& result = *this;
// the valid JSON Patch operations
enum class patch_operations {add, remove, replace, move, copy, test, invalid};
@ -4911,7 +4909,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
}
}
}
}
/// @brief applies a JSON patch to a copy of the current object
/// @sa https://json.nlohmann.me/api/basic_json/patch/
basic_json patch(const basic_json& json_patch) const
{
basic_json result = *this;
result.patch_inplace(json_patch);
return result;
}
@ -5049,7 +5054,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
return result;
}
/// @}
////////////////////////////////