From b3bd706f8f67cbadddfbbe2da36e0fad96f01705 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Tue, 10 Nov 2009 23:32:11 +0000 Subject: [PATCH] prototype versions of ReplaceObject. Based on an idea from Daniel Harcek; these are designed to allow you to replace entries in an object or array with new values. The old values get deleted and the new ones are wired into place. This leads to a structure like this: cJSON_ReplaceItemInObject(myobject, "spooncount", cJSON_CreateNumber(24)); cJSON +NEVER+ type checks, so it's perfectly legal to replace an object with a string (to cJSON) though it may not be in your schema! git-svn-id: http://svn.code.sf.net/p/cjson/code@13 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 6 ++++++ cJSON.h | 10 +++++++--- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/cJSON.c b/cJSON.c index 552cf46..e6df5a7 100644 --- a/cJSON.c +++ b/cJSON.c @@ -450,6 +450,12 @@ static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=p 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);} +// 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; + newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem; + if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;cJSON_Delete(c);} +void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && strcasecmp(c->string,string))i++,c=c->next;if(c)cJSON_ReplaceItemInArray(object,i,newitem);} + // Create basic types: cJSON *cJSON_CreateNull() {cJSON *item=cJSON_New_Item();item->type=cJSON_NULL;return item;} cJSON *cJSON_CreateTrue() {cJSON *item=cJSON_New_Item();item->type=cJSON_True;return item;} diff --git a/cJSON.h b/cJSON.h index e760be2..4cf1e15 100644 --- a/cJSON.h +++ b/cJSON.h @@ -73,7 +73,7 @@ extern int cJSON_GetArraySize(cJSON *array); extern cJSON *cJSON_GetArrayItem(cJSON *array,int item); // Get item "string" from object. Case insensitive. extern cJSON *cJSON_GetObjectItem(cJSON *object,const char *string); - + // These calls create a cJSON item of the appropriate type. extern cJSON *cJSON_CreateNull(); extern cJSON *cJSON_CreateTrue(); @@ -90,8 +90,12 @@ extern cJSON *cJSON_CreateDoubleArray(double *numbers,int count); 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); +extern void cJSON_AddItemToArray(cJSON *array, cJSON *item); +extern void cJSON_AddItemToObject(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); #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())