MCPcopy Create free account
hub / github.com/Netis/cloud-probe / merge_patch

Function merge_patch

cpworker/src/cJSON/cJSON_Utils.c:1345–1403  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1343CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON *const object) { sort_object(object, true); }
1344
1345static cJSON *merge_patch(cJSON *target, const cJSON *const patch, const cJSON_bool case_sensitive)
1346{
1347 cJSON *patch_child = NULL;
1348
1349 if (!cJSON_IsObject(patch))
1350 {
1351 /* scalar value, array or NULL, just duplicate */
1352 cJSON_Delete(target);
1353 return cJSON_Duplicate(patch, 1);
1354 }
1355
1356 if (!cJSON_IsObject(target))
1357 {
1358 cJSON_Delete(target);
1359 target = cJSON_CreateObject();
1360 }
1361
1362 patch_child = patch->child;
1363 while (patch_child != NULL)
1364 {
1365 if (cJSON_IsNull(patch_child))
1366 {
1367 /* NULL is the indicator to remove a value, see RFC7396 */
1368 if (case_sensitive)
1369 {
1370 cJSON_DeleteItemFromObjectCaseSensitive(target, patch_child->string);
1371 }
1372 else
1373 {
1374 cJSON_DeleteItemFromObject(target, patch_child->string);
1375 }
1376 }
1377 else
1378 {
1379 cJSON *replace_me = NULL;
1380 cJSON *replacement = NULL;
1381
1382 if (case_sensitive)
1383 {
1384 replace_me = cJSON_DetachItemFromObjectCaseSensitive(target, patch_child->string);
1385 }
1386 else
1387 {
1388 replace_me = cJSON_DetachItemFromObject(target, patch_child->string);
1389 }
1390
1391 replacement = merge_patch(replace_me, patch_child, case_sensitive);
1392 if (replacement == NULL)
1393 {
1394 cJSON_Delete(target);
1395 return NULL;
1396 }
1397
1398 cJSON_AddItemToObject(target, patch_child->string, replacement);
1399 }
1400 patch_child = patch_child->next;
1401 }
1402 return target;

Callers 2

cJSONUtils_MergePatchFunction · 0.85

Calls 10

cJSON_IsObjectFunction · 0.85
cJSON_DeleteFunction · 0.85
cJSON_DuplicateFunction · 0.85
cJSON_CreateObjectFunction · 0.85
cJSON_IsNullFunction · 0.85
cJSON_AddItemToObjectFunction · 0.85

Tested by

no test coverage detected