From 1e2556fccdbf7b0d126e7ca0f27f984f05358a22 Mon Sep 17 00:00:00 2001 From: Wolf Vollprecht Date: Sat, 16 Jul 2022 06:47:10 -0500 Subject: [PATCH] add patch_inplace function to json class --- docs/mkdocs/docs/api/basic_json/patch.md | 13 +++++++++++-- include/nlohmann/json.hpp | 16 ++++++++++------ 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/docs/mkdocs/docs/api/basic_json/patch.md b/docs/mkdocs/docs/api/basic_json/patch.md index b3ef963f04..ce4574f2f9 100644 --- a/docs/mkdocs/docs/api/basic_json/patch.md +++ b/docs/mkdocs/docs/api/basic_json/patch.md @@ -1,13 +1,20 @@ # nlohmann::basic_json::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 diff --git a/include/nlohmann/json.hpp b/include/nlohmann/json.hpp index c8a71e2598..f3c0edfee8 100644 --- a/include/nlohmann/json.hpp +++ b/include/nlohmann/json.hpp @@ -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.inplace_patch(json_patch); return result; } @@ -5049,7 +5054,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec return result; } - /// @} ////////////////////////////////