reformatting: cJSONUtils_MergePatch

This commit is contained in:
Max Bruckner 2016-10-29 12:40:41 +07:00
parent c65514fcb4
commit 01a813f642

View File

@ -725,19 +725,33 @@ void cJSONUtils_SortObject(cJSON *object)
cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch) cJSON* cJSONUtils_MergePatch(cJSON *target, cJSON *patch)
{ {
if (!patch || patch->type != cJSON_Object) {cJSON_Delete(target);return cJSON_Duplicate(patch,1);} if (!patch || (patch->type != cJSON_Object))
if (!target || target->type != cJSON_Object) {cJSON_Delete(target);target=cJSON_CreateObject();} {
/* scalar value, array or NULL, just duplicate */
cJSON_Delete(target);
return cJSON_Duplicate(patch, 1);
}
patch=patch->child; if (!target || (target->type != cJSON_Object))
{
cJSON_Delete(target);
target = cJSON_CreateObject();
}
patch = patch->child;
while (patch) while (patch)
{ {
if (patch->type == cJSON_NULL) cJSON_DeleteItemFromObject(target,patch->string); if (patch->type == cJSON_NULL)
{
/* NULL is the indicator to remove a value, see RFC7396 */
cJSON_DeleteItemFromObject(target, patch->string);
}
else else
{ {
cJSON *replaceme=cJSON_DetachItemFromObject(target,patch->string); cJSON *replaceme = cJSON_DetachItemFromObject(target, patch->string);
cJSON_AddItemToObject(target,patch->string,cJSONUtils_MergePatch(replaceme,patch)); cJSON_AddItemToObject(target, patch->string, cJSONUtils_MergePatch(replaceme, patch));
} }
patch=patch->next; patch = patch->next;
} }
return target; return target;
} }