From c24c3e02858fbc3f27ce12da6925f87f1ab180ad Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 1 May 2017 23:15:00 +0200 Subject: [PATCH 1/8] Add cJSON_DetachItemViaPointer --- cJSON.c | 30 ++++++++++++++++++++++++++++++ cJSON.h | 1 + tests/misc_tests.c | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+) diff --git a/cJSON.c b/cJSON.c index 3c3f10b..e3fd23c 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1814,6 +1814,36 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *str cJSON_AddItemToObject(object, string, create_reference(item, &global_hooks)); } +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item) +{ + if ((parent == NULL) || (item == NULL)) + { + return NULL; + } + + if (item->prev != NULL) + { + /* not the first element */ + item->prev->next = item->next; + } + if (item->next != NULL) + { + /* not the last element */ + item->next->prev = item->prev; + } + + if (item == parent->child) + { + /* first element */ + parent->child = item->next; + } + /* make sure the detached item doesn't point anywhere anymore */ + item->prev = NULL; + item->next = NULL; + + return item; +} + static cJSON *DetachItemFromArray(cJSON *array, size_t which) { cJSON *c = array->child; diff --git a/cJSON.h b/cJSON.h index 1ef70f0..69868b0 100644 --- a/cJSON.h +++ b/cJSON.h @@ -203,6 +203,7 @@ CJSON_PUBLIC(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item); CJSON_PUBLIC(void) cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item); /* Remove/Detatch items from Arrays/Objects. */ +CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const item); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 249500d..31a115b 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -218,6 +218,45 @@ static void cjson_set_number_value_should_set_numbers(void) TEST_ASSERT_EQUAL_DOUBLE(-1 + (double)INT_MIN, number->valuedouble); } +static void cjson_detach_item_via_pointer_should_detach_items(void) +{ + cJSON list[4]; + cJSON parent[1]; + + memset(list, '\0', sizeof(list)); + + /* link the list */ + list[0].next = &(list[1]); + list[1].next = &(list[2]); + list[2].next = &(list[3]); + + list[3].prev = &(list[2]); + list[2].prev = &(list[1]); + list[1].prev = &(list[0]); + + parent->child = &list[0]; + + /* detach in the middle (list[1]) */ + TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[1])) == &(list[1]), "Failed to detach in the middle."); + TEST_ASSERT_TRUE_MESSAGE((list[1].prev == NULL) && (list[1].next == NULL), "Didn't set pointers of detached item to NULL."); + TEST_ASSERT_TRUE((list[0].next == &(list[2])) && (list[2].prev == &(list[0]))); + + /* detach beginning (list[0]) */ + TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[0])) == &(list[0]), "Failed to detach beginning."); + TEST_ASSERT_TRUE_MESSAGE((list[0].prev == NULL) && (list[0].next == NULL), "Didn't set pointers of detached item to NULL."); + TEST_ASSERT_TRUE_MESSAGE((list[2].prev == NULL) && (parent->child == &(list[2])), "Didn't set the new beginning."); + + /* detach end (list[3])*/ + TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &(list[3])) == &(list[3]), "Failed to detach end."); + TEST_ASSERT_TRUE_MESSAGE((list[3].prev == NULL) && (list[3].next == NULL), "Didn't set pointers of detached item to NULL."); + TEST_ASSERT_TRUE_MESSAGE((list[2].next == NULL) && (parent->child == &(list[2])), "Didn't set the new end"); + + /* detach single item (list[2]) */ + TEST_ASSERT_TRUE_MESSAGE(cJSON_DetachItemViaPointer(parent, &list[2]) == &list[2], "Failed to detach single item."); + TEST_ASSERT_TRUE_MESSAGE((list[2].prev == NULL) && (list[2].next == NULL), "Didn't set pointers of detached item to NULL."); + TEST_ASSERT_NULL_MESSAGE(parent->child, "Child of the parent wasn't set to NULL."); +} + int main(void) { UNITY_BEGIN(); @@ -229,6 +268,7 @@ int main(void) RUN_TEST(typecheck_functions_should_check_type); RUN_TEST(cjson_should_not_parse_to_deeply_nested_jsons); RUN_TEST(cjson_set_number_value_should_set_numbers); + RUN_TEST(cjson_detach_item_via_pointer_should_detach_items); return UNITY_END(); } From acb0ca88df25b399056215eb55833b7bc561580a Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 1 May 2017 23:59:14 +0200 Subject: [PATCH 2/8] Use cJSON_DetachItemViaPointer internally --- cJSON.c | 30 ++---------------------------- 1 file changed, 2 insertions(+), 28 deletions(-) diff --git a/cJSON.c b/cJSON.c index e3fd23c..a8d8efd 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1852,28 +1852,8 @@ static cJSON *DetachItemFromArray(cJSON *array, size_t which) c = c->next; which--; } - if (!c) - { - /* item doesn't exist */ - return NULL; - } - if (c->prev) - { - /* not the first element */ - c->prev->next = c->next; - } - if (c->next) - { - c->next->prev = c->prev; - } - if (c==array->child) - { - array->child = c->next; - } - /* make sure the detached item doesn't point anywhere anymore */ - c->prev = c->next = NULL; - return c; + return cJSON_DetachItemViaPointer(array, c); } CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) { @@ -1892,19 +1872,13 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) { - size_t i = 0; cJSON *c = object->child; while (c && (case_insensitive_strcmp((unsigned char*)c->string, (const unsigned char*)string) != 0)) { - i++; c = c->next; } - if (c) - { - return DetachItemFromArray(object, i); - } - return NULL; + return cJSON_DetachItemViaPointer(object, c); } CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) From 33193ea557b1d84ddec2d238b781487f30490289 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 2 May 2017 00:14:02 +0200 Subject: [PATCH 3/8] Internal function get_array_item --- cJSON.c | 87 +++++++++++++++++++++++++++++++-------------------------- cJSON.h | 2 +- 2 files changed, 49 insertions(+), 40 deletions(-) diff --git a/cJSON.c b/cJSON.c index a8d8efd..853b723 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1665,16 +1665,33 @@ CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array) return (int)i; } -CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item) +static cJSON* get_array_item(const cJSON *array, size_t index) { - cJSON *c = array ? array->child : NULL; - while (c && item > 0) + cJSON *current_child = NULL; + + if (array == NULL) { - item--; - c = c->next; + return NULL; } - return c; + current_child = array->child; + while ((current_child != NULL) && (index > 0)) + { + index--; + current_child = current_child->next; + } + + return current_child; +} + +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index) +{ + if (index < 0) + { + return NULL; + } + + return get_array_item(array, (size_t)index); } static cJSON *get_object_item(const cJSON * const object, const char * const name, const cJSON_bool case_sensitive) @@ -1844,17 +1861,6 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const it return item; } -static cJSON *DetachItemFromArray(cJSON *array, size_t which) -{ - cJSON *c = array->child; - while (c && (which > 0)) - { - c = c->next; - which--; - } - - return cJSON_DetachItemViaPointer(array, c); -} CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) { if (which < 0) @@ -1862,7 +1868,7 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which) return NULL; } - return DetachItemFromArray(array, (size_t)which); + return cJSON_DetachItemViaPointer(array, get_array_item(array, (size_t)which)); } CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) @@ -1889,21 +1895,24 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) /* Replace array/object items with new ones. */ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) { - cJSON *c = array->child; - while (c && (which > 0)) + cJSON *after_inserted = NULL; + + if (which < 0) { - c = c->next; - which--; + return; } - if (!c) + + after_inserted = get_array_item(array, (size_t)which); + if (after_inserted == NULL) { cJSON_AddItemToArray(array, newitem); return; } - newitem->next = c; - newitem->prev = c->prev; - c->prev = newitem; - if (c == array->child) + + newitem->next = after_inserted; + newitem->prev = after_inserted->prev; + after_inserted->prev = newitem; + if (after_inserted == array->child) { array->child = newitem; } @@ -1915,23 +1924,20 @@ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newit static void ReplaceItemInArray(cJSON *array, size_t which, cJSON *newitem) { - cJSON *c = array->child; - while (c && (which > 0)) - { - c = c->next; - which--; - } - if (!c) + cJSON *replaced = get_array_item(array, which); + + if (replaced == NULL) { return; } - newitem->next = c->next; - newitem->prev = c->prev; + + newitem->next = replaced->next; + newitem->prev = replaced->prev; if (newitem->next) { newitem->next->prev = newitem; } - if (c == array->child) + if (replaced == array->child) { array->child = newitem; } @@ -1939,8 +1945,11 @@ static void ReplaceItemInArray(cJSON *array, size_t which, cJSON *newitem) { newitem->prev->next = newitem; } - c->next = c->prev = NULL; - cJSON_Delete(c); + + replaced->next = NULL; + replaced->prev = NULL; + + cJSON_Delete(replaced); } CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { diff --git a/cJSON.h b/cJSON.h index 69868b0..7247be7 100644 --- a/cJSON.h +++ b/cJSON.h @@ -153,7 +153,7 @@ CJSON_PUBLIC(void) cJSON_Delete(cJSON *c); /* Returns the number of items in an array (or object). */ CJSON_PUBLIC(int) cJSON_GetArraySize(const cJSON *array); /* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */ -CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int item); +CJSON_PUBLIC(cJSON *) cJSON_GetArrayItem(const cJSON *array, int index); /* Get item "string" from object. Case insensitive. */ CJSON_PUBLIC(cJSON *) cJSON_GetObjectItem(const cJSON *object, const char *string); CJSON_PUBLIC(cJSON *) cJSON_GetObjectItemCaseSensitive(const cJSON * const object, const char * const string); From 8816a568ab02ee907b0b6e1909680846b43d49e1 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 2 May 2017 00:34:17 +0200 Subject: [PATCH 4/8] Add cJSON_DetachItemFromObjectCaseSensitive --- cJSON.c | 15 +++++++++------ cJSON.h | 1 + 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/cJSON.c b/cJSON.c index 853b723..74f17ba 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1878,13 +1878,16 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which) CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string) { - cJSON *c = object->child; - while (c && (case_insensitive_strcmp((unsigned char*)c->string, (const unsigned char*)string) != 0)) - { - c = c->next; - } + cJSON *to_detach = cJSON_GetObjectItem(object, string); - return cJSON_DetachItemViaPointer(object, c); + return cJSON_DetachItemViaPointer(object, to_detach); +} + +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON *to_detach = cJSON_GetObjectItemCaseSensitive(object, string); + + return cJSON_DetachItemViaPointer(object, to_detach); } CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) diff --git a/cJSON.h b/cJSON.h index 7247be7..0d69601 100644 --- a/cJSON.h +++ b/cJSON.h @@ -207,6 +207,7 @@ CJSON_PUBLIC(cJSON *) cJSON_DetachItemViaPointer(cJSON *parent, cJSON * const it CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromArray(cJSON *array, int which); CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); /* Update array items. */ From 2d07bbc9b2c50f895ce7191caba747916f7e805f Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 2 May 2017 00:36:10 +0200 Subject: [PATCH 5/8] Add cJSON_DeleteItemFromObjectCaseSensitive --- cJSON.c | 5 +++++ cJSON.h | 1 + 2 files changed, 6 insertions(+) diff --git a/cJSON.c b/cJSON.c index 74f17ba..5b32d01 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1895,6 +1895,11 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string) cJSON_Delete(cJSON_DetachItemFromObject(object, string)); } +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string) +{ + cJSON_Delete(cJSON_DetachItemFromObjectCaseSensitive(object, string)); +} + /* Replace array/object items with new ones. */ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem) { diff --git a/cJSON.h b/cJSON.h index 0d69601..684efea 100644 --- a/cJSON.h +++ b/cJSON.h @@ -209,6 +209,7 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromArray(cJSON *array, int which); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObject(cJSON *object, const char *string); CJSON_PUBLIC(cJSON *) cJSON_DetachItemFromObjectCaseSensitive(cJSON *object, const char *string); CJSON_PUBLIC(void) cJSON_DeleteItemFromObject(cJSON *object, const char *string); +CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const char *string); /* Update array items. */ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ From 8b953d1202c591c86474a8ee287060d37f4e5030 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 2 May 2017 00:49:46 +0200 Subject: [PATCH 6/8] Add cJSON_ReplaceItemViaPointer --- cJSON.c | 67 +++++++++++++++++++--------------------------- cJSON.h | 1 + tests/misc_tests.c | 48 +++++++++++++++++++++++++++++++++ 3 files changed, 77 insertions(+), 39 deletions(-) diff --git a/cJSON.c b/cJSON.c index 5b32d01..94f45b2 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1930,35 +1930,41 @@ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newit } } -static void ReplaceItemInArray(cJSON *array, size_t which, cJSON *newitem) +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement) { - cJSON *replaced = get_array_item(array, which); - - if (replaced == NULL) + if ((parent == NULL) || (replacement == NULL)) { - return; + return false; } - newitem->next = replaced->next; - newitem->prev = replaced->prev; - if (newitem->next) + if (replacement == item) { - newitem->next->prev = newitem; - } - if (replaced == array->child) - { - array->child = newitem; - } - else - { - newitem->prev->next = newitem; + return true; } - replaced->next = NULL; - replaced->prev = NULL; + replacement->next = item->next; + replacement->prev = item->prev; - cJSON_Delete(replaced); + if (replacement->next != NULL) + { + replacement->next->prev = replacement; + } + if (replacement->prev != NULL) + { + replacement->prev->next = replacement; + } + if (parent->child == item) + { + parent->child = replacement; + } + + item->next = NULL; + item->prev = NULL; + cJSON_Delete(item); + + return true; } + CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem) { if (which < 0) @@ -1966,29 +1972,12 @@ CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newi return; } - ReplaceItemInArray(array, (size_t)which, newitem); + cJSON_ReplaceItemViaPointer(array, get_array_item(array, (size_t)which), newitem); } CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON *newitem) { - size_t i = 0; - cJSON *c = object->child; - while(c && (case_insensitive_strcmp((unsigned char*)c->string, (const unsigned char*)string) != 0)) - { - i++; - c = c->next; - } - if(c) - { - /* free the old string if not const */ - if (!(newitem->type & cJSON_StringIsConst) && newitem->string) - { - global_hooks.deallocate(newitem->string); - } - - newitem->string = (char*)cJSON_strdup((const unsigned char*)string, &global_hooks); - ReplaceItemInArray(object, i, newitem); - } + cJSON_ReplaceItemViaPointer(object, cJSON_GetObjectItem(object, string), newitem); } /* Create basic types: */ diff --git a/cJSON.h b/cJSON.h index 684efea..eec1121 100644 --- a/cJSON.h +++ b/cJSON.h @@ -213,6 +213,7 @@ CJSON_PUBLIC(void) cJSON_DeleteItemFromObjectCaseSensitive(cJSON *object, const /* Update array items. */ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newitem); /* Shifts pre-existing items to the right. */ +CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 31a115b..24a390d 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -257,6 +257,53 @@ static void cjson_detach_item_via_pointer_should_detach_items(void) TEST_ASSERT_NULL_MESSAGE(parent->child, "Child of the parent wasn't set to NULL."); } +static void cjson_replace_item_via_pointer_should_replace_items(void) +{ + cJSON replacements[3]; + cJSON *beginning = NULL; + cJSON *middle = NULL; + cJSON *end = NULL; + cJSON *array = NULL; + + beginning = cJSON_CreateNull(); + TEST_ASSERT_NOT_NULL(beginning); + middle = cJSON_CreateNull(); + TEST_ASSERT_NOT_NULL(middle); + end = cJSON_CreateNull(); + TEST_ASSERT_NOT_NULL(end); + + array = cJSON_CreateArray(); + TEST_ASSERT_NOT_NULL(array); + + cJSON_AddItemToArray(array, beginning); + cJSON_AddItemToArray(array, middle); + cJSON_AddItemToArray(array, end); + + + memset(replacements, '\0', sizeof(replacements)); + + /* replace beginning */ + TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, beginning, &(replacements[0]))); + TEST_ASSERT_NULL(replacements[0].prev); + TEST_ASSERT_TRUE(replacements[0].next == middle); + TEST_ASSERT_TRUE(middle->prev == &(replacements[0])); + TEST_ASSERT_TRUE(array->child == &(replacements[0])); + + /* replace middle */ + TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, middle, &(replacements[1]))); + TEST_ASSERT_TRUE(replacements[1].prev == &(replacements[0])); + TEST_ASSERT_TRUE(replacements[1].next == end); + TEST_ASSERT_TRUE(end->prev == &(replacements[1])); + + /* replace end */ + TEST_ASSERT_TRUE(cJSON_ReplaceItemViaPointer(array, end, &(replacements[2]))); + TEST_ASSERT_TRUE(replacements[2].prev == &(replacements[1])); + TEST_ASSERT_NULL(replacements[2].next); + TEST_ASSERT_TRUE(replacements[1].next == &(replacements[2])); + + cJSON_free(array); +} + int main(void) { UNITY_BEGIN(); @@ -269,6 +316,7 @@ int main(void) RUN_TEST(cjson_should_not_parse_to_deeply_nested_jsons); RUN_TEST(cjson_set_number_value_should_set_numbers); RUN_TEST(cjson_detach_item_via_pointer_should_detach_items); + RUN_TEST(cjson_replace_item_via_pointer_should_replace_items); return UNITY_END(); } From dede4e32463ecb77d48dd4954bfee505d894cbc6 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 2 May 2017 00:52:41 +0200 Subject: [PATCH 7/8] Add cJSON_ReplaceItemInObjectCaseSensitive --- cJSON.c | 5 +++++ cJSON.h | 1 + 2 files changed, 6 insertions(+) diff --git a/cJSON.c b/cJSON.c index 94f45b2..9e19236 100644 --- a/cJSON.c +++ b/cJSON.c @@ -1980,6 +1980,11 @@ CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object, const char *string, cJSON_ReplaceItemViaPointer(object, cJSON_GetObjectItem(object, string), newitem); } +CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object, const char *string, cJSON *newitem) +{ + cJSON_ReplaceItemViaPointer(object, cJSON_GetObjectItemCaseSensitive(object, string), newitem); +} + /* Create basic types: */ CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void) { diff --git a/cJSON.h b/cJSON.h index eec1121..b990a3f 100644 --- a/cJSON.h +++ b/cJSON.h @@ -216,6 +216,7 @@ CJSON_PUBLIC(void) cJSON_InsertItemInArray(cJSON *array, int which, cJSON *newit CJSON_PUBLIC(cJSON_bool) cJSON_ReplaceItemViaPointer(cJSON * const parent, cJSON * const item, cJSON * replacement); CJSON_PUBLIC(void) cJSON_ReplaceItemInArray(cJSON *array, int which, cJSON *newitem); CJSON_PUBLIC(void) cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); +CJSON_PUBLIC(void) cJSON_ReplaceItemInObjectCaseSensitive(cJSON *object,const char *string,cJSON *newitem); /* Duplicate a cJSON item */ CJSON_PUBLIC(cJSON *) cJSON_Duplicate(const cJSON *item, cJSON_bool recurse); From bc622fcc151b6b0ee56ad93c74ea28d852b5121f Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Tue, 2 May 2017 00:56:28 +0200 Subject: [PATCH 8/8] README.md: Use cJSON_GetObjectItemCaseSensitive --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6b344cb..1d6fb8a 100644 --- a/README.md +++ b/README.md @@ -138,8 +138,8 @@ This is an object. We're in C. We don't have objects. But we do have structs. What's the framerate? ```c -cJSON *format = cJSON_GetObjectItem(root, "format"); -cJSON *framerate_item = cJSON_GetObjectItem(format, "frame rate"); +cJSON *format = cJSON_GetObjectItemCaseSensitive(root, "format"); +cJSON *framerate_item = cJSON_GetObjectItemCaseSensitive(format, "frame rate"); double framerate = 0; if (cJSON_IsNumber(framerate_item)) { @@ -150,7 +150,7 @@ if (cJSON_IsNumber(framerate_item)) Want to change the framerate? ```c -cJSON *framerate_item = cJSON_GetObjectItem(format, "frame rate"); +cJSON *framerate_item = cJSON_GetObjectItemCaseSensitive(format, "frame rate"); cJSON_SetNumberValue(framerate_item, 25); ```