diff --git a/cJSON.c b/cJSON.c index e8eb608..72e8a1c 100644 --- a/cJSON.c +++ b/cJSON.c @@ -77,8 +77,8 @@ void cJSON_Delete(cJSON *c) while (c) { next=c->next; - if (c->child) cJSON_Delete(c->child); - if (c->valuestring) cJSON_free(c->valuestring); + if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child); + if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring); if (c->string) cJSON_free(c->string); cJSON_free(c); c=next; @@ -257,7 +257,7 @@ static const char *parse_value(cJSON *item,const char *value) static char *print_value(cJSON *item,int depth,int fmt) { char *out=0; - switch (item->type) + switch ((item->type)&255) { case cJSON_NULL: out=cJSON_strdup("null"); break; case cJSON_False: out=cJSON_strdup("false");break; @@ -449,10 +449,15 @@ cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->c // Utility for array list handling. static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;} +// Utility for handling references. +static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;} // Add item to array/object. void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}} void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item) {if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);} +void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));} +void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));} + // Replace array/object items with new ones. void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return; diff --git a/cJSON.h b/cJSON.h index b0cb977..fbfb35f 100644 --- a/cJSON.h +++ b/cJSON.h @@ -36,6 +36,8 @@ extern "C" #define cJSON_String 4 #define cJSON_Array 5 #define cJSON_Object 6 + +#define cJSON_IsReference 256 // The cJSON structure: typedef struct cJSON { @@ -94,7 +96,11 @@ extern cJSON *cJSON_CreateStringArray(const char **strings,int count); // Append item to the specified array/object. extern void cJSON_AddItemToArray(cJSON *array, cJSON *item); extern void cJSON_AddItemToObject(cJSON *object,const char *string,cJSON *item); - +// 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_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item); + + // Update array items. extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem); extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem);