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