| 1319 | } |
| 1320 | |
| 1321 | static cJSON *merge_patch(cJSON *target, const cJSON * const patch, const cJSON_bool case_sensitive) |
| 1322 | { |
| 1323 | cJSON *patch_child = NULL; |
| 1324 | |
| 1325 | if (!cJSON_IsObject(patch)) |
| 1326 | { |
| 1327 | /* scalar value, array or NULL, just duplicate */ |
| 1328 | cJSON_Delete(target); |
| 1329 | return cJSON_Duplicate(patch, 1); |
| 1330 | } |
| 1331 | |
| 1332 | if (!cJSON_IsObject(target)) |
| 1333 | { |
| 1334 | cJSON_Delete(target); |
| 1335 | target = cJSON_CreateObject(); |
| 1336 | } |
| 1337 | |
| 1338 | patch_child = patch->child; |
| 1339 | while (patch_child != NULL) |
| 1340 | { |
| 1341 | if (cJSON_IsNull(patch_child)) |
| 1342 | { |
| 1343 | /* NULL is the indicator to remove a value, see RFC7396 */ |
| 1344 | if (case_sensitive) |
| 1345 | { |
| 1346 | cJSON_DeleteItemFromObjectCaseSensitive(target, patch_child->string); |
| 1347 | } |
| 1348 | else |
| 1349 | { |
| 1350 | cJSON_DeleteItemFromObject(target, patch_child->string); |
| 1351 | } |
| 1352 | } |
| 1353 | else |
| 1354 | { |
| 1355 | cJSON *replace_me = NULL; |
| 1356 | cJSON *replacement = NULL; |
| 1357 | |
| 1358 | if (case_sensitive) |
| 1359 | { |
| 1360 | replace_me = cJSON_DetachItemFromObjectCaseSensitive(target, patch_child->string); |
| 1361 | } |
| 1362 | else |
| 1363 | { |
| 1364 | replace_me = cJSON_DetachItemFromObject(target, patch_child->string); |
| 1365 | } |
| 1366 | |
| 1367 | replacement = merge_patch(replace_me, patch_child, case_sensitive); |
| 1368 | if (replacement == NULL) |
| 1369 | { |
| 1370 | cJSON_Delete(target); |
| 1371 | return NULL; |
| 1372 | } |
| 1373 | |
| 1374 | cJSON_AddItemToObject(target, patch_child->string, replacement); |
| 1375 | } |
| 1376 | patch_child = patch_child->next; |
| 1377 | } |
| 1378 | return target; |
no test coverage detected
searching dependent graphs…