Skip to content

Commit

Permalink
add inplace_patch
Browse files Browse the repository at this point in the history
  • Loading branch information
wolfv committed Jul 16, 2022
1 parent d4daaa8 commit 604ea2d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
13 changes: 11 additions & 2 deletions docs/mkdocs/docs/api/basic_json/patch.md
Original file line number Diff line number Diff line change
@@ -1,25 +1,34 @@
# <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)
: JSON patch document
## 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
Expand Down
16 changes: 10 additions & 6 deletions include/nlohmann/json.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand Down Expand Up @@ -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.inplace_patch(json_patch);
return result;
}

Expand Down Expand Up @@ -5049,7 +5054,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec

return result;
}

/// @}

////////////////////////////////
Expand Down

0 comments on commit 604ea2d

Please sign in to comment.