From 2ff0894010dc68a8e675aa1ddc3de321545f1094 Mon Sep 17 00:00:00 2001 From: No Default Name Date: Tue, 24 May 2022 21:57:11 +0200 Subject: [PATCH] Add cJSON_AddIntToObject() --- cJSON.c | 36 ++++++++++++++++++++++++------------ cJSON.h | 3 ++- tests/cjson_add.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 13 deletions(-) diff --git a/cJSON.c b/cJSON.c index a1abf5c..ec8bc3a 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2202,6 +2202,18 @@ CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * return NULL; } +CJSON_PUBLIC(cJSON*) cJSON_AddIntToObject(cJSON * const object, const char * const name, const cJSON_int number) +{ + cJSON *number_item = cJSON_CreateInt(number); + if (add_item_to_object(object, name, number_item, &global_hooks, false)) + { + return number_item; + } + + cJSON_Delete(number_item); + return NULL; +} + CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string) { cJSON *string_item = cJSON_CreateString(string); @@ -2492,18 +2504,6 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean) return item; } -CJSON_PUBLIC(cJSON *) cJSON_CreateInt(cJSON_int num) -{ - cJSON *item = cJSON_New_Item(&global_hooks); - if(item) - { - item->type = cJSON_Number; - cJSON_SetIntValue(item, num); - } - - return item; -} - CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(cJSON_float num) { cJSON *item = cJSON_New_Item(&global_hooks); @@ -2516,6 +2516,18 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(cJSON_float num) return item; } +CJSON_PUBLIC(cJSON *) cJSON_CreateInt(cJSON_int num) +{ + cJSON *item = cJSON_New_Item(&global_hooks); + if(item) + { + item->type = cJSON_Number; + cJSON_SetIntValue(item, num); + } + + return item; +} + CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string) { cJSON *item = cJSON_New_Item(&global_hooks); diff --git a/cJSON.h b/cJSON.h index 24e9c15..1f1b679 100644 --- a/cJSON.h +++ b/cJSON.h @@ -225,8 +225,8 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateNull(void); CJSON_PUBLIC(cJSON *) cJSON_CreateTrue(void); CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void); CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean); -CJSON_PUBLIC(cJSON *) cJSON_CreateInt(cJSON_int num); CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(cJSON_float num); +CJSON_PUBLIC(cJSON *) cJSON_CreateInt(cJSON_int num); CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string); /* raw json */ CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw); @@ -297,6 +297,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddTrueToObject(cJSON * const object, const char * co CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * const name); CJSON_PUBLIC(cJSON*) cJSON_AddBoolToObject(cJSON * const object, const char * const name, const cJSON_bool boolean); CJSON_PUBLIC(cJSON*) cJSON_AddNumberToObject(cJSON * const object, const char * const name, const cJSON_float number); +CJSON_PUBLIC(cJSON*) cJSON_AddIntToObject(cJSON * const object, const char * const name, const cJSON_int number); CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string); CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw); CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * const name); diff --git a/tests/cjson_add.c b/tests/cjson_add.c index b02f1e2..bc7bf59 100644 --- a/tests/cjson_add.c +++ b/tests/cjson_add.c @@ -278,6 +278,45 @@ static void cjson_add_number_should_fail_on_allocation_failure(void) cJSON_Delete(root); } +static void cjson_add_int_should_add_integer(void) +{ + cJSON *root = cJSON_CreateObject(); + cJSON *number = NULL; + + cJSON_AddIntToObject(root, "number", 42); + + TEST_ASSERT_NOT_NULL(number = cJSON_GetObjectItemCaseSensitive(root, "number")); + + TEST_ASSERT_EQUAL_INT(number->type, cJSON_Number | cJSON_PreferInt); + TEST_ASSERT_EQUAL_DOUBLE(number->valuedouble, 42); + TEST_ASSERT_EQUAL_INT(number->valueint, 42); + + cJSON_Delete(root); +} + +static void cjson_add_int_should_fail_with_null_pointers(void) +{ + cJSON *root = cJSON_CreateObject(); + + TEST_ASSERT_NULL(cJSON_AddIntToObject(NULL, "number", 42)); + TEST_ASSERT_NULL(cJSON_AddIntToObject(root, NULL, 42)); + + cJSON_Delete(root); +} + +static void cjson_add_int_should_fail_on_allocation_failure(void) +{ + cJSON *root = cJSON_CreateObject(); + + cJSON_InitHooks(&failing_hooks); + + TEST_ASSERT_NULL(cJSON_AddIntToObject(root, "number", 42)); + + cJSON_InitHooks(NULL); + + cJSON_Delete(root); +} + static void cjson_add_string_should_add_string(void) { cJSON *root = cJSON_CreateObject(); @@ -451,6 +490,10 @@ int CJSON_CDECL main(void) RUN_TEST(cjson_add_number_should_fail_with_null_pointers); RUN_TEST(cjson_add_number_should_fail_on_allocation_failure); + RUN_TEST(cjson_add_int_should_add_integer); + RUN_TEST(cjson_add_int_should_fail_with_null_pointers); + RUN_TEST(cjson_add_int_should_fail_on_allocation_failure); + RUN_TEST(cjson_add_string_should_add_string); RUN_TEST(cjson_add_string_should_fail_with_null_pointers); RUN_TEST(cjson_add_string_should_fail_on_allocation_failure);