mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Merge 8f5978b88a
into cb8693b058
This commit is contained in:
commit
544ed6ccee
49
cJSON.c
49
cJSON.c
@ -206,6 +206,26 @@ static unsigned char* cJSON_strdup(const unsigned char* string, const internal_h
|
||||
return copy;
|
||||
}
|
||||
|
||||
static unsigned char* cJSON_strdup_with_length(const unsigned char* string, size_t length, const internal_hooks * const hooks)
|
||||
{
|
||||
unsigned char *copy = NULL;
|
||||
|
||||
if (string == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
copy = (unsigned char*)hooks->allocate(length + 1);
|
||||
if (copy == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
memcpy(copy, string, length);
|
||||
copy[length] = '\0';
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(void) cJSON_InitHooks(cJSON_Hooks* hooks)
|
||||
{
|
||||
if (hooks == NULL)
|
||||
@ -2148,6 +2168,18 @@ CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char *
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringWithLengthToObject(cJSON * const object, const char * const name, const char * const string, size_t length)
|
||||
{
|
||||
cJSON *string_item = cJSON_CreateStringWithLength(string, length);
|
||||
if (add_item_to_object(object, name, string_item, &global_hooks, false))
|
||||
{
|
||||
return string_item;
|
||||
}
|
||||
|
||||
cJSON_Delete(string_item);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddRawToObject(cJSON * const object, const char * const name, const char * const raw)
|
||||
{
|
||||
cJSON *raw_item = cJSON_CreateRaw(raw);
|
||||
@ -2469,6 +2501,23 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string)
|
||||
return item;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringWithLength(const char *string, size_t length)
|
||||
{
|
||||
cJSON *item = cJSON_New_Item(&global_hooks);
|
||||
if(item)
|
||||
{
|
||||
item->type = cJSON_String;
|
||||
item->valuestring = (char*)cJSON_strdup_with_length((const unsigned char*)string, length, &global_hooks);
|
||||
if(!item->valuestring)
|
||||
{
|
||||
cJSON_Delete(item);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return item;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringReference(const char *string)
|
||||
{
|
||||
cJSON *item = cJSON_New_Item(&global_hooks);
|
||||
|
2
cJSON.h
2
cJSON.h
@ -198,6 +198,7 @@ CJSON_PUBLIC(cJSON *) cJSON_CreateFalse(void);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateBool(cJSON_bool boolean);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateNumber(double num);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateString(const char *string);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateStringWithLength(const char *string, size_t length);
|
||||
/* raw json */
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateRaw(const char *raw);
|
||||
CJSON_PUBLIC(cJSON *) cJSON_CreateArray(void);
|
||||
@ -267,6 +268,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddFalseToObject(cJSON * const object, const char * c
|
||||
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 double number);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringToObject(cJSON * const object, const char * const name, const char * const string);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddStringWithLengthToObject(cJSON * const object, const char * const name, const char * const string, size_t length);
|
||||
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);
|
||||
CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name);
|
||||
|
@ -315,6 +315,49 @@ static void cjson_add_string_should_fail_on_allocation_failure(void)
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
static void cjson_add_string_with_length_should_add_string(void)
|
||||
{
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
cJSON *string = NULL;
|
||||
|
||||
|
||||
cJSON_AddStringWithLengthToObject(root, "string", "Hello World!", strlen("Hello World!"));
|
||||
cJSON_AddStringWithLengthToObject(root, "substring", "Hello World!", strlen("Hello World!") - 7);
|
||||
|
||||
TEST_ASSERT_NOT_NULL(string = cJSON_GetObjectItemCaseSensitive(root, "string"));
|
||||
TEST_ASSERT_EQUAL_INT(string->type, cJSON_String);
|
||||
TEST_ASSERT_EQUAL_STRING(string->valuestring, "Hello World!");
|
||||
|
||||
TEST_ASSERT_NOT_NULL(string = cJSON_GetObjectItemCaseSensitive(root, "substring"));
|
||||
TEST_ASSERT_EQUAL_INT(string->type, cJSON_String);
|
||||
TEST_ASSERT_EQUAL_STRING(string->valuestring, "Hello");
|
||||
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
static void cjson_add_string_with_length_should_fail_with_null_pointers(void)
|
||||
{
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
|
||||
TEST_ASSERT_NULL(cJSON_AddStringWithLengthToObject(NULL, "string", "string", strlen("string")));
|
||||
TEST_ASSERT_NULL(cJSON_AddStringWithLengthToObject(root, NULL, "string", strlen("string")));
|
||||
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
static void cjson_add_string_with_length_should_fail_on_allocation_failure(void)
|
||||
{
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
|
||||
cJSON_InitHooks(&failing_hooks);
|
||||
|
||||
TEST_ASSERT_NULL(cJSON_AddStringWithLengthToObject(root, "string", "string", strlen("string")));
|
||||
|
||||
cJSON_InitHooks(NULL);
|
||||
|
||||
cJSON_Delete(root);
|
||||
}
|
||||
|
||||
static void cjson_add_raw_should_add_raw(void)
|
||||
{
|
||||
cJSON *root = cJSON_CreateObject();
|
||||
@ -455,6 +498,10 @@ int CJSON_CDECL main(void)
|
||||
RUN_TEST(cjson_add_string_should_fail_with_null_pointers);
|
||||
RUN_TEST(cjson_add_string_should_fail_on_allocation_failure);
|
||||
|
||||
RUN_TEST(cjson_add_string_with_length_should_add_string);
|
||||
RUN_TEST(cjson_add_string_with_length_should_fail_with_null_pointers);
|
||||
RUN_TEST(cjson_add_string_with_length_should_fail_on_allocation_failure);
|
||||
|
||||
RUN_TEST(cjson_add_raw_should_add_raw);
|
||||
RUN_TEST(cjson_add_raw_should_fail_with_null_pointers);
|
||||
RUN_TEST(cjson_add_raw_should_fail_on_allocation_failure);
|
||||
|
Loading…
Reference in New Issue
Block a user