Add cJSONUtils_MergePatchCaseSensitive

This commit is contained in:
Max Bruckner 2017-05-01 22:41:19 +02:00
parent 7f22948eec
commit 66f75619d9
2 changed files with 39 additions and 4 deletions

View File

@ -1222,7 +1222,7 @@ CJSON_PUBLIC(void) cJSONUtils_SortObjectCaseSensitive(cJSON * const object)
sort_object(object, true);
}
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch)
static cJSON *merge_patch(cJSON *target, const cJSON * const patch, const cJSON_bool case_sensitive)
{
cJSON *patch_child = NULL;
@ -1245,18 +1245,52 @@ CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const p
if (cJSON_IsNull(patch_child))
{
/* NULL is the indicator to remove a value, see RFC7396 */
cJSON_DeleteItemFromObject(target, patch_child->string);
if (case_sensitive)
{
cJSON_DeleteItemFromObjectCaseSensitive(target, patch_child->string);
}
else
{
cJSON *replace_me = cJSON_DetachItemFromObject(target, patch_child->string);
cJSON_AddItemToObject(target, patch_child->string, cJSONUtils_MergePatch(replace_me, patch_child));
cJSON_DeleteItemFromObject(target, patch_child->string);
}
}
else
{
cJSON *replace_me = NULL;
cJSON *replacement = NULL;
if (case_sensitive)
{
replace_me = cJSON_DetachItemFromObjectCaseSensitive(target, patch_child->string);
}
else
{
replace_me = cJSON_DetachItemFromObject(target, patch_child->string);
}
replacement = merge_patch(replace_me, patch_child, case_sensitive);
if (replacement == NULL)
{
return NULL;
}
cJSON_AddItemToObject(target, patch_child->string, replacement);
}
patch_child = patch_child->next;
}
return target;
}
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch)
{
return merge_patch(target, patch, false);
}
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch)
{
return merge_patch(target, patch, true);
}
static cJSON *generate_merge_patch(cJSON * const from, cJSON * const to, const cJSON_bool case_sensitive)
{
cJSON *from_child = NULL;

View File

@ -60,6 +60,7 @@ CJSON_PUBLIC(int) cJSONUtils_ApplyPatchesCaseSensitive(cJSON * const object, con
/* Implement RFC7386 (https://tools.ietf.org/html/rfc7396) JSON Merge Patch spec. */
/* target will be modified by patch. return value is new ptr for target. */
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatch(cJSON *target, const cJSON * const patch);
CJSON_PUBLIC(cJSON *) cJSONUtils_MergePatchCaseSensitive(cJSON *target, const cJSON * const patch);
/* generates a patch to move from -> to */
/* NOTE: This modifies objects in 'from' and 'to' by sorting the elements by their key */
CJSON_PUBLIC(cJSON *) cJSONUtils_GenerateMergePatch(cJSON * const from, cJSON * const to);