JSON for Modern C++ 3.10.4

◆ merge_patch()

template<template< typename U, typename V, typename... Args > class ObjectType = std::map, template< typename U, typename... Args > class ArrayType = std::vector, class StringType = std::string, class BooleanType = bool, class NumberIntegerType = std::int64_t, class NumberUnsignedType = std::uint64_t, class NumberFloatType = double, template< typename U > class AllocatorType = std::allocator, template< typename T, typename SFINAE=void > class JSONSerializer = adl_serializer, class BinaryType = std::vector<std::uint8_t>>
void nlohmann::basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType >::merge_patch ( const basic_json< ObjectType, ArrayType, StringType, BooleanType, NumberIntegerType, NumberUnsignedType, NumberFloatType, AllocatorType, JSONSerializer, BinaryType > &  apply_patch)
inline

The merge patch format is primarily intended for use with the HTTP PATCH method as a means of describing a set of modifications to a target resource's content. This function applies a merge patch to the current JSON value.

The function implements the following algorithm from Section 2 of RFC 7396 (JSON Merge Patch):

define MergePatch(Target, Patch):
if Patch is an Object:
if Target is not an Object:
Target = {} // Ignore the contents and set it to an empty Object
for each Name/Value pair in Patch:
if Value is null:
if Name exists in Target:
remove the Name/Value pair from Target
else:
Target[Name] = MergePatch(Target[Name], Value)
return Target
else:
return Patch

Thereby, Target is the current object; that is, the patch is applied to the current value.

Parameters
[in]apply_patchthe patch to apply
Complexity
Linear in the lengths of patch.
Example
The following code shows how a JSON Merge Patch is applied to a JSON document.
1#include <iostream>
2#include <nlohmann/json.hpp>
3#include <iomanip> // for std::setw
4
5using json = nlohmann::json;
6
7int main()
8{
9 // the original document
10 json document = R"({
11 "title": "Goodbye!",
12 "author": {
13 "givenName": "John",
14 "familyName": "Doe"
15 },
16 "tags": [
17 "example",
18 "sample"
19 ],
20 "content": "This will be unchanged"
21 })"_json;
22
23 // the patch
24 json patch = R"({
25 "title": "Hello!",
26 "phoneNumber": "+01-123-456-7890",
27 "author": {
28 "familyName": null
29 },
30 "tags": [
31 "example"
32 ]
33 })"_json;
34
35 // apply the patch
36 document.merge_patch(patch);
37
38 // output original and patched document
39 std::cout << std::setw(4) << document << std::endl;
40}
void merge_patch(const basic_json &apply_patch)
applies a JSON Merge Patch
Definition: json.hpp:26441
basic_json patch(const basic_json &json_patch) const
applies a JSON patch
Definition: json.hpp:25952
basic_json<> json
default JSON class
Definition: json.hpp:3472

Output (play with this example online):
{
    "author": {
        "givenName": "John"
    },
    "content": "This will be unchanged",
    "phoneNumber": "+01-123-456-7890",
    "tags": [
        "example"
    ],
    "title": "Hello!"
}
The example code above can be translated with
g++ -std=c++11 -Isingle_include doc/examples/merge_patch.cpp -o merge_patch 
See also
see patch – apply a JSON patch
RFC 7396 (JSON Merge Patch)
Since
version 3.0.0

Definition at line 26441 of file json.hpp.