1 // __ _____ _____ _____ 2 // __| | __| | | | JSON for Modern C++ (supporting code) 3 // | | |__ | | | | | | version 3.11.2 4 // |_____|_____|_____|_|___| https://github.com/nlohmann/json 5 // 6 // SPDX-FileCopyrightText: 2013-2022 Niels Lohmann <https://nlohmann.me> 7 // SPDX-License-Identifier: MIT 8 9 #include "doctest_compatibility.h" 10 11 #include <nlohmann/json.hpp> 12 using nlohmann::json; 13 #ifdef JSON_TEST_NO_GLOBAL_UDLS 14 using namespace nlohmann::literals; // NOLINT(google-build-using-namespace) 15 #endif 16 17 TEST_CASE("JSON Merge Patch") 18 { 19 SECTION("examples from RFC 7396") 20 { 21 SECTION("Section 1") 22 { 23 json document = R"({ 24 "a": "b", 25 "c": { 26 "d": "e", 27 "f": "g" 28 } 29 })"_json; 30 31 json patch = R"({ 32 "a": "z", 33 "c": { 34 "f": null 35 } 36 })"_json; 37 38 json expected = R"({ 39 "a": "z", 40 "c": { 41 "d": "e" 42 } 43 })"_json; 44 45 document.merge_patch(patch); 46 CHECK(document == expected); 47 } 48 49 SECTION("Section 3") 50 { 51 json document = R"({ 52 "title": "Goodbye!", 53 "author": { 54 "givenName": "John", 55 "familyName": "Doe" 56 }, 57 "tags": [ 58 "example", 59 "sample" 60 ], 61 "content": "This will be unchanged" 62 })"_json; 63 64 json patch = R"({ 65 "title": "Hello!", 66 "phoneNumber": "+01-123-456-7890", 67 "author": { 68 "familyName": null 69 }, 70 "tags": [ 71 "example" 72 ] 73 })"_json; 74 75 json expected = R"({ 76 "title": "Hello!", 77 "author": { 78 "givenName": "John" 79 }, 80 "tags": [ 81 "example" 82 ], 83 "content": "This will be unchanged", 84 "phoneNumber": "+01-123-456-7890" 85 })"_json; 86 87 document.merge_patch(patch); 88 CHECK(document == expected); 89 } 90 91 SECTION("Appendix A") 92 { 93 SECTION("Example 1") 94 { 95 json original = R"({"a":"b"})"_json; 96 json patch = R"({"a":"c"})"_json; 97 json result = R"({"a":"c"})"_json; 98 99 original.merge_patch(patch); 100 CHECK(original == result); 101 } 102 103 SECTION("Example 2") 104 { 105 json original = R"({"a":"b"})"_json; 106 json patch = R"({"b":"c"})"_json; 107 json result = R"({"a":"b", "b":"c"})"_json; 108 109 original.merge_patch(patch); 110 CHECK(original == result); 111 } 112 113 SECTION("Example 3") 114 { 115 json original = R"({"a":"b"})"_json; 116 json patch = R"({"a":null})"_json; 117 json result = R"({})"_json; 118 119 original.merge_patch(patch); 120 CHECK(original == result); 121 } 122 123 SECTION("Example 4") 124 { 125 json original = R"({"a":"b","b":"c"})"_json; 126 json patch = R"({"a":null})"_json; 127 json result = R"({"b":"c"})"_json; 128 129 original.merge_patch(patch); 130 CHECK(original == result); 131 } 132 133 SECTION("Example 5") 134 { 135 json original = R"({"a":["b"]})"_json; 136 json patch = R"({"a":"c"})"_json; 137 json result = R"({"a":"c"})"_json; 138 139 original.merge_patch(patch); 140 CHECK(original == result); 141 } 142 143 SECTION("Example 6") 144 { 145 json original = R"({"a":"c"})"_json; 146 json patch = R"({"a":["b"]})"_json; 147 json result = R"({"a":["b"]})"_json; 148 149 original.merge_patch(patch); 150 CHECK(original == result); 151 } 152 153 SECTION("Example 7") 154 { 155 json original = R"({"a":{"b": "c"}})"_json; 156 json patch = R"({"a":{"b":"d","c":null}})"_json; 157 json result = R"({"a": {"b": "d"}})"_json; 158 159 original.merge_patch(patch); 160 CHECK(original == result); 161 } 162 163 SECTION("Example 8") 164 { 165 json original = R"({"a":[{"b":"c"}]})"_json; 166 json patch = R"({"a":[1]})"_json; 167 json result = R"({"a":[1]})"_json; 168 169 original.merge_patch(patch); 170 CHECK(original == result); 171 } 172 173 SECTION("Example 9") 174 { 175 json original = R"(["a","b"])"_json; 176 json patch = R"(["c","d"])"_json; 177 json result = R"(["c","d"])"_json; 178 179 original.merge_patch(patch); 180 CHECK(original == result); 181 } 182 183 SECTION("Example 10") 184 { 185 json original = R"({"a":"b"})"_json; 186 json patch = R"(["c"])"_json; 187 json result = R"(["c"])"_json; 188 189 original.merge_patch(patch); 190 CHECK(original == result); 191 } 192 193 SECTION("Example 11") 194 { 195 json original = R"({"a":"foo"})"_json; 196 json patch = R"(null)"_json; 197 json result = R"(null)"_json; 198 199 original.merge_patch(patch); 200 CHECK(original == result); 201 } 202 203 SECTION("Example 12") 204 { 205 json original = R"({"a":"foo"})"_json; 206 json patch = R"("bar")"_json; 207 json result = R"("bar")"_json; 208 209 original.merge_patch(patch); 210 CHECK(original == result); 211 } 212 213 SECTION("Example 13") 214 { 215 json original = R"({"e":null})"_json; 216 json patch = R"({"a":1})"_json; 217 json result = R"({"e":null,"a":1})"_json; 218 219 original.merge_patch(patch); 220 CHECK(original == result); 221 } 222 223 SECTION("Example 14") 224 { 225 json original = R"([1,2])"_json; 226 json patch = R"({"a":"b","c":null})"_json; 227 json result = R"({"a":"b"})"_json; 228 229 original.merge_patch(patch); 230 CHECK(original == result); 231 } 232 233 SECTION("Example 15") 234 { 235 json original = R"({})"_json; 236 json patch = R"({"a":{"bb":{"ccc":null}}})"_json; 237 json result = R"({"a":{"bb":{}}})"_json; 238 239 original.merge_patch(patch); 240 CHECK(original == result); 241 } 242 } 243 } 244 } 245