mirror of
https://github.com/DaveGamble/cJSON
synced 2024-12-24 07:32:05 +00:00
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
This commit is contained in:
parent
5cca4e2c57
commit
b3bd706f8f
6
cJSON.c
6
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_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_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:
|
// Create basic types:
|
||||||
cJSON *cJSON_CreateNull() {cJSON *item=cJSON_New_Item();item->type=cJSON_NULL;return item;}
|
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;}
|
cJSON *cJSON_CreateTrue() {cJSON *item=cJSON_New_Item();item->type=cJSON_True;return item;}
|
||||||
|
4
cJSON.h
4
cJSON.h
@ -93,6 +93,10 @@ extern cJSON *cJSON_CreateStringArray(const char **strings,int count);
|
|||||||
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);
|
||||||
|
|
||||||
|
// 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_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull())
|
||||||
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
|
#define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue())
|
||||||
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
|
#define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())
|
||||||
|
Loading…
Reference in New Issue
Block a user