| 2562 | |
| 2563 | |
| 2564 | static cJSON *merge_patch(cJSON *target, const cJSON * const patch) |
| 2565 | { |
| 2566 | cJSON *patch_child = NULL; |
| 2567 | |
| 2568 | if (!cJSON_IsObject(patch)) |
| 2569 | { |
| 2570 | /* scalar value, array or NULL, just duplicate */ |
| 2571 | cJSON_Delete(target); |
| 2572 | return cJSON_Duplicate(patch, 1); |
| 2573 | } |
| 2574 | |
| 2575 | if (!cJSON_IsObject(target)) |
| 2576 | { |
| 2577 | cJSON_Delete(target); |
| 2578 | target = cJSON_CreateObject(); |
| 2579 | } |
| 2580 | |
| 2581 | patch_child = patch->child; |
| 2582 | while (patch_child != NULL) |
| 2583 | { |
| 2584 | if (cJSON_IsNull(patch_child)) |
| 2585 | { |
| 2586 | cJSON_DeleteItemFromObject(target, patch_child->string); |
| 2587 | } |
| 2588 | else |
| 2589 | { |
| 2590 | cJSON *replace_me = NULL; |
| 2591 | cJSON *replacement = NULL; |
| 2592 | |
| 2593 | replace_me = cJSON_DetachItemFromObject(target, patch_child->string); |
| 2594 | |
| 2595 | replacement = merge_patch(replace_me, patch_child); |
| 2596 | if (replacement == NULL) |
| 2597 | { |
| 2598 | return NULL; |
| 2599 | } |
| 2600 | |
| 2601 | cJSON_AddItemToObject(target, patch_child->string, replacement); |
| 2602 | } |
| 2603 | patch_child = patch_child->next; |
| 2604 | } |
| 2605 | return target; |
| 2606 | } |
| 2607 | |
| 2608 | cJSON * cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch) |
| 2609 | { |
no test coverage detected