diff --git a/cJSON.c b/cJSON.c index 391ae7e..a1abf5c 100644 --- a/cJSON.c +++ b/cJSON.c @@ -3007,6 +3007,16 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item) return (item->type & 0xFF) == cJSON_Number; } +CJSON_PUBLIC(cJSON_bool) cJSON_IsInt(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return ((item->type & 0xFF) == cJSON_Number) && (item->type & cJSON_PreferInt); +} + CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item) { if (item == NULL) diff --git a/cJSON.h b/cJSON.h index c38827f..24e9c15 100644 --- a/cJSON.h +++ b/cJSON.h @@ -214,6 +214,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_IsTrue(const cJSON * const item); CJSON_PUBLIC(cJSON_bool) cJSON_IsBool(const cJSON * const item); CJSON_PUBLIC(cJSON_bool) cJSON_IsNull(const cJSON * const item); CJSON_PUBLIC(cJSON_bool) cJSON_IsNumber(const cJSON * const item); +CJSON_PUBLIC(cJSON_bool) cJSON_IsInt(const cJSON * const item); CJSON_PUBLIC(cJSON_bool) cJSON_IsString(const cJSON * const item); CJSON_PUBLIC(cJSON_bool) cJSON_IsArray(const cJSON * const item); CJSON_PUBLIC(cJSON_bool) cJSON_IsObject(const cJSON * const item); diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 979770f..df8e242 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -188,6 +188,12 @@ static void typecheck_functions_should_check_type(void) TEST_ASSERT_FALSE(cJSON_IsNumber(NULL)); TEST_ASSERT_FALSE(cJSON_IsNumber(invalid)); TEST_ASSERT_TRUE(cJSON_IsNumber(item)); + TEST_ASSERT_FALSE(cJSON_IsInt(item)); + + item->type = cJSON_Number | cJSON_StringIsConst | cJSON_PreferInt; + TEST_ASSERT_FALSE(cJSON_IsInt(NULL)); + TEST_ASSERT_FALSE(cJSON_IsInt(invalid)); + TEST_ASSERT_TRUE(cJSON_IsInt(item)); item->type = cJSON_String | cJSON_StringIsConst; TEST_ASSERT_FALSE(cJSON_IsString(NULL)); @@ -381,6 +387,7 @@ static void cjson_functions_should_not_crash_with_null_pointers(void) TEST_ASSERT_FALSE(cJSON_IsBool(NULL)); TEST_ASSERT_FALSE(cJSON_IsNull(NULL)); TEST_ASSERT_FALSE(cJSON_IsNumber(NULL)); + TEST_ASSERT_FALSE(cJSON_IsInt(NULL)); TEST_ASSERT_FALSE(cJSON_IsString(NULL)); TEST_ASSERT_FALSE(cJSON_IsArray(NULL)); TEST_ASSERT_FALSE(cJSON_IsObject(NULL));