From 66f75619d9c073523b36c77100a86f9889bce4e7 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 1 May 2017 22:41:19 +0200 Subject: [PATCH] Add cJSONUtils_MergePatchCaseSensitive --- cJSON_Utils.c | 42 ++++++++++++++++++++++++++++++++++++++---- cJSON_Utils.h | 1 + 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 8c5cddd..5d61c76 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -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_DeleteItemFromObject(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 *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; diff --git a/cJSON_Utils.h b/cJSON_Utils.h index 3ecc1e9..03ec10c 100644 --- a/cJSON_Utils.h +++ b/cJSON_Utils.h @@ -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);