mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
cJSON_GetStringValue
This commit is contained in:
parent
27a4303f87
commit
b2afbd3c9f
8
cJSON.c
8
cJSON.c
@ -73,6 +73,14 @@ CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void)
|
|||||||
return (const char*) (global_error.json + global_error.position);
|
return (const char*) (global_error.json + global_error.position);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item) {
|
||||||
|
if (!cJSON_IsString(item)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return item->valuestring;
|
||||||
|
}
|
||||||
|
|
||||||
/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
|
/* This is a safeguard to prevent copy-pasters from using incompatible C and header files */
|
||||||
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 6) || (CJSON_VERSION_PATCH != 0)
|
#if (CJSON_VERSION_MAJOR != 1) || (CJSON_VERSION_MINOR != 6) || (CJSON_VERSION_PATCH != 0)
|
||||||
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
|
#error cJSON.h and cJSON.c have different versions. Make sure that both have the same.
|
||||||
|
3
cJSON.h
3
cJSON.h
@ -165,6 +165,9 @@ CJSON_PUBLIC(cJSON_bool) cJSON_HasObjectItem(const cJSON *object, const char *st
|
|||||||
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
/* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */
|
||||||
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
CJSON_PUBLIC(const char *) cJSON_GetErrorPtr(void);
|
||||||
|
|
||||||
|
/* Check if the item is a string and return its valuestring */
|
||||||
|
CJSON_PUBLIC(char *) cJSON_GetStringValue(cJSON *item);
|
||||||
|
|
||||||
/* These functions check the type of an item */
|
/* These functions check the type of an item */
|
||||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsInvalid(const cJSON * const item);
|
||||||
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
CJSON_PUBLIC(cJSON_bool) cJSON_IsFalse(const cJSON * const item);
|
||||||
|
@ -450,6 +450,19 @@ static void skip_utf8_bom_should_not_skip_bom_if_not_at_beginning(void)
|
|||||||
TEST_ASSERT_NULL(skip_utf8_bom(&buffer));
|
TEST_ASSERT_NULL(skip_utf8_bom(&buffer));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void cjson_get_string_value_should_get_a_string(void)
|
||||||
|
{
|
||||||
|
cJSON *string = cJSON_CreateString("test");
|
||||||
|
cJSON *number = cJSON_CreateNumber(1);
|
||||||
|
|
||||||
|
TEST_ASSERT_TRUE(cJSON_GetStringValue(string) == string->valuestring);
|
||||||
|
TEST_ASSERT_NULL(cJSON_GetStringValue(number));
|
||||||
|
TEST_ASSERT_NULL(cJSON_GetStringValue(NULL));
|
||||||
|
|
||||||
|
cJSON_Delete(number);
|
||||||
|
cJSON_Delete(string);
|
||||||
|
}
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
UNITY_BEGIN();
|
UNITY_BEGIN();
|
||||||
@ -468,6 +481,7 @@ int main(void)
|
|||||||
RUN_TEST(ensure_should_fail_on_failed_realloc);
|
RUN_TEST(ensure_should_fail_on_failed_realloc);
|
||||||
RUN_TEST(skip_utf8_bom_should_skip_bom);
|
RUN_TEST(skip_utf8_bom_should_skip_bom);
|
||||||
RUN_TEST(skip_utf8_bom_should_not_skip_bom_if_not_at_beginning);
|
RUN_TEST(skip_utf8_bom_should_not_skip_bom_if_not_at_beginning);
|
||||||
|
RUN_TEST(cjson_get_string_value_should_get_a_string);
|
||||||
|
|
||||||
return UNITY_END();
|
return UNITY_END();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user