mirror of
https://github.com/DaveGamble/cJSON.git
synced 2025-05-17 15:06:48 +08:00
add new function of setValuestringToObject
This commit is contained in:
25
cJSON.c
25
cJSON.c
@ -368,6 +368,31 @@ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number)
|
||||
return object->valuedouble = number;
|
||||
}
|
||||
|
||||
CJSON_PUBLIC(char*) cJSON_SetValuestringToObject(cJSON *object, const char *valuestring)
|
||||
{
|
||||
size_t length = 0;
|
||||
char *copy = NULL;
|
||||
/* if object->valuestring is NULL, it should not set valuestring */
|
||||
if (object->valuestring == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
length = strlen(valuestring) + sizeof("");
|
||||
copy = (char*) cJSON_malloc(length);
|
||||
if (copy == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
memcpy(copy, valuestring, length);
|
||||
if (!(object->type & cJSON_IsReference) && (object->valuestring != NULL))
|
||||
{
|
||||
cJSON_free(object->valuestring);
|
||||
}
|
||||
object->valuestring = copy;
|
||||
|
||||
return copy;
|
||||
}
|
||||
|
||||
typedef struct
|
||||
{
|
||||
unsigned char *buffer;
|
||||
|
Reference in New Issue
Block a user