Function cJSON_SetValuestring
Synopsis
#include <cJSON.h>
char * cJSON_SetValuestring(cJSON *object, const char *valuestring)
Description
Change the valuestring of a cJSON_String object, only takes effect when type of object is cJSON_String
Mentioned in
- Getting Started / Working with the data structure
- Changelog / Features:
Source
Lines 400-425 in cJSON.c. Line 280 in cJSON.h.
CJSON_PUBLIC(char*) cJSON_SetValuestring(cJSON *object, const char *valuestring)
{
char *copy = NULL;
/* if object's type is not cJSON_String or is cJSON_IsReference, it should not set valuestring */
if (!(object->type & cJSON_String) || (object->type & cJSON_IsReference))
{
return NULL;
}
if (strlen(valuestring) <= strlen(object->valuestring))
{
strcpy(object->valuestring, valuestring);
return object->valuestring;
}
copy = (char*) cJSON_strdup((const unsigned char*)valuestring, &global_hooks);
if (copy == NULL)
{
return NULL;
}
if (object->valuestring != NULL)
{
cJSON_free(object->valuestring);
}
object->valuestring = copy;
return copy;
}