mirror of
https://github.com/DaveGamble/cJSON.git
synced 2023-08-10 21:13:26 +03:00
Wrap the value types in a union to reduce required memory
Also added -g flag for the test build, so valgrind shows line numbers
This commit is contained in:
parent
a1c022fef6
commit
76523ba29c
@ -43,9 +43,9 @@ install (FILES cJSON_Utils.h DESTINATION include/cJSON)
|
|||||||
option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF)
|
option(ENABLE_CJSON_TEST "Enable building cJSON test" OFF)
|
||||||
if(ENABLE_CJSON_TEST)
|
if(ENABLE_CJSON_TEST)
|
||||||
set(TEST_CJSON cJSON_test)
|
set(TEST_CJSON cJSON_test)
|
||||||
|
set( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g" )
|
||||||
add_executable(${TEST_CJSON} test.c)
|
add_executable(${TEST_CJSON} test.c)
|
||||||
target_link_libraries(${TEST_CJSON} ${PROJ_CJSON}.shared)
|
target_link_libraries(${TEST_CJSON} ${PROJ_CJSON}.shared)
|
||||||
|
|
||||||
set(TEST_CJSON_UTILS cJSON_test_utils)
|
set(TEST_CJSON_UTILS cJSON_test_utils)
|
||||||
add_executable(${TEST_CJSON_UTILS} test_utils.c)
|
add_executable(${TEST_CJSON_UTILS} test_utils.c)
|
||||||
target_link_libraries(${TEST_CJSON_UTILS} ${PROJ_CJSON_UTILS}.shared)
|
target_link_libraries(${TEST_CJSON_UTILS} ${PROJ_CJSON_UTILS}.shared)
|
||||||
|
4
cJSON.c
4
cJSON.c
@ -116,7 +116,7 @@ void cJSON_Delete(cJSON *c)
|
|||||||
{
|
{
|
||||||
cJSON_Delete(c->child);
|
cJSON_Delete(c->child);
|
||||||
}
|
}
|
||||||
if (!(c->type & cJSON_IsReference) && c->valuestring)
|
if ((c->type & cJSON_String) && !(c->type & cJSON_IsReference) && c->valuestring)
|
||||||
{
|
{
|
||||||
cJSON_free(c->valuestring);
|
cJSON_free(c->valuestring);
|
||||||
}
|
}
|
||||||
@ -895,7 +895,7 @@ static const char *parse_value(cJSON *item, const char *value, const char **ep)
|
|||||||
return parse_object(item, value, ep);
|
return parse_object(item, value, ep);
|
||||||
}
|
}
|
||||||
|
|
||||||
*ep=value;return 0; /* failure. */
|
*ep=value;return 0; /* failure. */
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Render a value to text. */
|
/* Render a value to text. */
|
||||||
|
23
cJSON.h
23
cJSON.h
@ -52,12 +52,15 @@ typedef struct cJSON
|
|||||||
/* The type of the item, as above. */
|
/* The type of the item, as above. */
|
||||||
int type;
|
int type;
|
||||||
|
|
||||||
/* The item's string, if type==cJSON_String */
|
union
|
||||||
char *valuestring;
|
{
|
||||||
/* The item's number, if type==cJSON_Number */
|
/* The item's string, if type==cJSON_String */
|
||||||
int valueint;
|
char *valuestring;
|
||||||
/* The item's number, if type==cJSON_Number */
|
/* The item's number, if type==cJSON_Number */
|
||||||
double valuedouble;
|
int valueint;
|
||||||
|
/* The item's number, if type==cJSON_Number */
|
||||||
|
double valuedouble;
|
||||||
|
};
|
||||||
|
|
||||||
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
/* The item's name string, if this item is the child of, or is in the list of subitems of an object. */
|
||||||
char *string;
|
char *string;
|
||||||
@ -85,7 +88,7 @@ extern char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, int fmt);
|
|||||||
extern void cJSON_Delete(cJSON *c);
|
extern void cJSON_Delete(cJSON *c);
|
||||||
|
|
||||||
/* Returns the number of items in an array (or object). */
|
/* Returns the number of items in an array (or object). */
|
||||||
extern int cJSON_GetArraySize(const cJSON *array);
|
extern int cJSON_GetArraySize(const cJSON *array);
|
||||||
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
|
/* Retrieve item number "item" from array "array". Returns NULL if unsuccessful. */
|
||||||
extern cJSON *cJSON_GetArrayItem(const cJSON *array, int item);
|
extern cJSON *cJSON_GetArrayItem(const cJSON *array, int item);
|
||||||
/* Get item "string" from object. Case insensitive. */
|
/* Get item "string" from object. Case insensitive. */
|
||||||
@ -112,11 +115,11 @@ extern cJSON *cJSON_CreateStringArray(const char **strings, int count);
|
|||||||
|
|
||||||
/* Append item to the specified array/object. */
|
/* Append item to the specified array/object. */
|
||||||
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
extern void cJSON_AddItemToArray(cJSON *array, cJSON *item);
|
||||||
extern void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
extern void cJSON_AddItemToObject(cJSON *object, const char *string, cJSON *item);
|
||||||
extern void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
|
extern void cJSON_AddItemToObjectCS(cJSON *object, const char *string, cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */
|
||||||
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
/* Append reference to item to the specified array/object. Use this when you want to add an existing cJSON to a new cJSON, but don't want to corrupt your existing cJSON. */
|
||||||
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
extern void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item);
|
||||||
extern void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
extern void cJSON_AddItemReferenceToObject(cJSON *object, const char *string, cJSON *item);
|
||||||
|
|
||||||
/* Remove/Detatch items from Arrays/Objects. */
|
/* Remove/Detatch items from Arrays/Objects. */
|
||||||
extern cJSON *cJSON_DetachItemFromArray(cJSON *array, int which);
|
extern cJSON *cJSON_DetachItemFromArray(cJSON *array, int which);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user