From 575724a15fe46d7392b419a8afbdabd00eed3b7c Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Mon, 9 Feb 2015 17:25:55 +0000 Subject: [PATCH 01/15] optimises for number/string print git-svn-id: svn://svn.code.sf.net/p/cjson/code@59 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index 31c43dd..0ce5f08 100644 --- a/cJSON.c +++ b/cJSON.c @@ -122,7 +122,16 @@ static char *print_number(cJSON *item) if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN) { str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */ - if (str) sprintf(str,"%d",item->valueint); + if (str) + { + int i,j,t; + + if (d<0) t=-d; else t=d; + for (i=0;t>0;t/=10) str[i++]='0'+(t%10); + if (d<0) str[i++]='-'; + str[i--]=0; + for (j=0;j Date: Mon, 9 Feb 2015 17:28:26 +0000 Subject: [PATCH 02/15] patch to optimise array/object printing. git-svn-id: svn://svn.code.sf.net/p/cjson/code@60 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index 0ce5f08..3cb8254 100644 --- a/cJSON.c +++ b/cJSON.c @@ -383,6 +383,7 @@ static char *print_array(cJSON *item,int depth,int fmt) char *out=0,*ptr,*ret;int len=5; cJSON *child=item->child; int numentries=0,i=0,fail=0; + size_t tmplen=0; /* How many entries in the array? */ while (child) numentries++,child=child->next; @@ -425,7 +426,7 @@ static char *print_array(cJSON *item,int depth,int fmt) ptr=out+1;*ptr=0; for (i=0;ichild; int numentries=0,fail=0; + size_t tmplen=0; /* Count the number of entries. */ while (child) numentries++,child=child->next; /* Explicitly handle empty object case */ @@ -524,7 +526,7 @@ static char *print_object(cJSON *item,int depth,int fmt) for (i=0;i Date: Mon, 9 Feb 2015 17:34:35 +0000 Subject: [PATCH 03/15] Added buffered printing methods for faster printing. git-svn-id: svn://svn.code.sf.net/p/cjson/code@61 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 356 ++++++++++++++++++++++++++++++++++++++------------------ cJSON.h | 4 + 2 files changed, 246 insertions(+), 114 deletions(-) diff --git a/cJSON.c b/cJSON.c index 3cb8254..c6c5603 100644 --- a/cJSON.c +++ b/cJSON.c @@ -86,7 +86,7 @@ void cJSON_Delete(cJSON *c) next=c->next; 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); + if (!(c->type&cJSON_StringIsConst) && c->string) cJSON_free(c->string); cJSON_free(c); c=next; } @@ -114,28 +114,56 @@ static const char *parse_number(cJSON *item,const char *num) return num; } -/* Render the number nicely from the given item into a string. */ -static char *print_number(cJSON *item) +static int pow2gt (int x) { --x; x|=x>>1; x|=x>>2; x|=x>>4; x|=x>>8; x|=x>>16; return x+1; } + +typedef struct {char *buffer; int length; int offset; } printbuffer; + +static char* ensure(printbuffer *p,int needed) +{ + char *newbuffer;int newsize; + if (!p || !p->buffer) return 0; + needed+=p->offset; + if (needed<=p->length) return p->buffer+p->offset; + + newsize=pow2gt(needed); + newbuffer=(char*)cJSON_malloc(newsize); + if (!newbuffer) {cJSON_free(p->buffer);p->length=0,p->buffer=0;return 0;} + if (newbuffer) memcpy(newbuffer,p->buffer,p->length); + cJSON_free(p->buffer); + p->length=newsize; + p->buffer=newbuffer; + return newbuffer+p->offset; +} + +static int update(printbuffer *p) { char *str; + if (!p || !p->buffer) return 0; + str=p->buffer+p->offset; + return p->offset+strlen(str); +} + +/* Render the number nicely from the given item into a string. */ +static char *print_number(cJSON *item,printbuffer *p) +{ + char *str=0; double d=item->valuedouble; - if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN) + if (d==0) { - str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */ - if (str) - { - int i,j,t; - - if (d<0) t=-d; else t=d; - for (i=0;t>0;t/=10) str[i++]='0'+(t%10); - if (d<0) str[i++]='-'; - str[i--]=0; - for (j=0;jvalueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN) + { + if (p) str=ensure(p,21); + else str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */ + if (str) sprintf(str,"%d",item->valueint); } else { - str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */ + if (p) str=ensure(p,64); + else str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */ if (str) { if (fabs(floor(d)-d)<=DBL_EPSILON && fabs(d)<1.0e60)sprintf(str,"%.0f",d); @@ -231,15 +259,16 @@ static int escapable[256]={ 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}; /* Render the cstring provided to an escaped version that can be printed. */ -static char *print_string_ptr(const char *str) +static char *print_string_ptr(const char *str,printbuffer *p) { const char *ptr;char *ptr2,*out;int len=0,flag=0;unsigned char token; - + ptr=str;while (*ptr) flag|=escapable[*ptr++]; if (!flag) { len=ptr-str; - out=(char*)cJSON_malloc(len+3); + if (p) out=ensure(p,len+3); + else out=(char*)cJSON_malloc(len+3); if (!out) return 0; ptr2=out;*ptr2++='\"'; strcpy(ptr2,str); @@ -248,10 +277,18 @@ static char *print_string_ptr(const char *str) return out; } - if (!str) return cJSON_strdup(""); + if (!str) + { + if (p) out=ensure(p,3); + else out=(char*)cJSON_malloc(3); + if (!out) return 0; + strcpy(out,"\"\""); + return out; + } ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;} - out=(char*)cJSON_malloc(len+3); + if (p) out=ensure(p,len+3); + else out=(char*)cJSON_malloc(len+3); if (!out) return 0; ptr2=out;ptr=str; @@ -279,15 +316,15 @@ static char *print_string_ptr(const char *str) return out; } /* Invote print_string_ptr (which is useful) on an item. */ -static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);} +static char *print_string(cJSON *item,printbuffer *p) {return print_string_ptr(item->valuestring,p);} /* Predeclare these prototypes. */ static const char *parse_value(cJSON *item,const char *value); -static char *print_value(cJSON *item,int depth,int fmt); +static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p); static const char *parse_array(cJSON *item,const char *value); -static char *print_array(cJSON *item,int depth,int fmt); +static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p); static const char *parse_object(cJSON *item,const char *value); -static char *print_object(cJSON *item,int depth,int fmt); +static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p); /* Utility to jump whitespace and cr/lf */ static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;} @@ -312,8 +349,19 @@ cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int r cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);} /* Render a cJSON item/entity/structure to text. */ -char *cJSON_Print(cJSON *item) {return print_value(item,0,1);} -char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);} +char *cJSON_Print(cJSON *item) {return print_value(item,0,1,0);} +char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0,0);} + +char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt) +{ + printbuffer p; + p.buffer=(char*)cJSON_malloc(prebuffer); + p.length=prebuffer; + p.offset=0; + return print_value(item,0,fmt,&p); + return p.buffer; +} + /* Parser core - when encountering text, process appropriately. */ static const char *parse_value(cJSON *item,const char *value) @@ -331,19 +379,35 @@ static const char *parse_value(cJSON *item,const char *value) } /* Render a value to text. */ -static char *print_value(cJSON *item,int depth,int fmt) +static char *print_value(cJSON *item,int depth,int fmt,printbuffer *p) { char *out=0; if (!item) return 0; - switch ((item->type)&255) + if (p) { - case cJSON_NULL: out=cJSON_strdup("null"); break; - case cJSON_False: out=cJSON_strdup("false");break; - case cJSON_True: out=cJSON_strdup("true"); break; - case cJSON_Number: out=print_number(item);break; - case cJSON_String: out=print_string(item);break; - case cJSON_Array: out=print_array(item,depth,fmt);break; - case cJSON_Object: out=print_object(item,depth,fmt);break; + switch ((item->type)&255) + { + case cJSON_NULL: {out=ensure(p,5); if (out) strcpy(out,"null"); break;} + case cJSON_False: {out=ensure(p,6); if (out) strcpy(out,"false"); break;} + case cJSON_True: {out=ensure(p,5); if (out) strcpy(out,"true"); break;} + case cJSON_Number: out=print_number(item,p);break; + case cJSON_String: out=print_string(item,p);break; + case cJSON_Array: out=print_array(item,depth,fmt,p);break; + case cJSON_Object: out=print_object(item,depth,fmt,p);break; + } + } + else + { + switch ((item->type)&255) + { + case cJSON_NULL: out=cJSON_strdup("null"); break; + case cJSON_False: out=cJSON_strdup("false");break; + case cJSON_True: out=cJSON_strdup("true"); break; + case cJSON_Number: out=print_number(item,0);break; + case cJSON_String: out=print_string(item,0);break; + case cJSON_Array: out=print_array(item,depth,fmt,0);break; + case cJSON_Object: out=print_object(item,depth,fmt,0);break; + } } return out; } @@ -377,7 +441,7 @@ static const char *parse_array(cJSON *item,const char *value) } /* Render an array to text */ -static char *print_array(cJSON *item,int depth,int fmt) +static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p) { char **entries; char *out=0,*ptr,*ret;int len=5; @@ -390,48 +454,69 @@ static char *print_array(cJSON *item,int depth,int fmt) /* Explicitly handle numentries==0 */ if (!numentries) { - out=(char*)cJSON_malloc(3); + if (p) out=ensure(p,3); + else out=(char*)cJSON_malloc(3); if (out) strcpy(out,"[]"); return out; } - /* Allocate an array to hold the values for each */ - entries=(char**)cJSON_malloc(numentries*sizeof(char*)); - if (!entries) return 0; - memset(entries,0,numentries*sizeof(char*)); - /* Retrieve all the results: */ - child=item->child; - while (child && !fail) - { - ret=print_value(child,depth+1,fmt); - entries[i++]=ret; - if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; - child=child->next; - } - - /* If we didn't fail, try to malloc the output string */ - if (!fail) out=(char*)cJSON_malloc(len); - /* If that fails, we fail. */ - if (!out) fail=1; - /* Handle failure. */ - if (fail) + if (p) { - for (i=0;ioffset; + ptr=ensure(p,1);if (!ptr) return 0; *ptr='['; p->offset++; + child=item->child; + while (child && !fail) + { + print_value(child,depth+1,fmt,p); + p->offset=update(p); + if (child->next) {len=fmt?2:1;ptr=ensure(p,len+1);if (!ptr) return 0;*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;p->offset+=len;} + child=child->next; + } + ptr=ensure(p,2);if (!ptr) return 0; *ptr++=']';*ptr=0; + out=(p->buffer)+i; } - - /* Compose the output array. */ - *out='['; - ptr=out+1;*ptr=0; - for (i=0;ichild; + while (child && !fail) + { + ret=print_value(child,depth+1,fmt,0); + entries[i++]=ret; + if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; + child=child->next; + } + + /* If we didn't fail, try to malloc the output string */ + if (!fail) out=(char*)cJSON_malloc(len); + /* If that fails, we fail. */ + if (!out) fail=1; + + /* Handle failure. */ + if (fail) + { + for (i=0;ioffset; + len=fmt?2:1; ptr=ensure(p,len+1); if (!ptr) return 0; + *ptr++='{'; if (fmt) *ptr++='\n'; *ptr=0; p->offset+=len; + child=item->child;depth++; + while (child) + { + if (fmt) + { + ptr=ensure(p,depth); if (!ptr) return 0; + for (j=0;joffset+=depth; + } + print_string_ptr(child->string,p); + p->offset=update(p); + + len=fmt?2:1; + ptr=ensure(p,len); if (!ptr) return 0; + *ptr++=':';if (fmt) *ptr++='\t'; + p->offset+=len; + + print_value(child,depth,fmt,p); + p->offset=update(p); - /* Collect all the results into our arrays: */ - child=item->child;depth++;if (fmt) len+=depth; - while (child) - { - names[i]=str=print_string_ptr(child->string); - entries[i++]=ret=print_value(child,depth,fmt); - if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1; - child=child->next; + len=(fmt?1:0)+(child->next?1:0); + ptr=ensure(p,len+1); if (!ptr) return 0; + if (child->next) *ptr++=','; + if (fmt) *ptr++='\n';*ptr=0; + p->offset+=len; + child=child->next; + } + ptr=ensure(p,fmt?(depth+1):2); if (!ptr) return 0; + if (fmt) for (i=0;ibuffer)+i; } - - /* Try to allocate the output string */ - if (!fail) out=(char*)cJSON_malloc(len); - if (!out) fail=1; + else + { + /* Allocate space for the names and the objects */ + entries=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!entries) return 0; + names=(char**)cJSON_malloc(numentries*sizeof(char*)); + if (!names) {cJSON_free(entries);return 0;} + memset(entries,0,sizeof(char*)*numentries); + memset(names,0,sizeof(char*)*numentries); - /* Handle failure */ - if (fail) - { - for (i=0;ichild;depth++;if (fmt) len+=depth; + while (child) + { + names[i]=str=print_string_ptr(child->string,0); + entries[i++]=ret=print_value(child,depth,fmt,0); + if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1; + child=child->next; + } + + /* Try to allocate the output string */ + if (!fail) out=(char*)cJSON_malloc(len); + if (!out) fail=1; + + /* Handle failure */ + if (fail) + { + for (i=0;ichild;if (!item) return; 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) return; if (item->string) cJSON_free(item->string);item->string=cJSON_strdup(string);cJSON_AddItemToArray(object,item);} +void cJSON_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item) {if (!item) return; if (!(item->type&cJSON_StringIsConst) && item->string) cJSON_free(item->string);item->string=(char*)string;item->type|=cJSON_StringIsConst;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));} @@ -627,4 +755,4 @@ void cJSON_Minify(char *json) else *into++=*json++; // All other characters. } *into=0; // and null-terminate. -} \ No newline at end of file +} diff --git a/cJSON.h b/cJSON.h index 867b7c3..55b85ea 100644 --- a/cJSON.h +++ b/cJSON.h @@ -38,6 +38,7 @@ extern "C" #define cJSON_Object 6 #define cJSON_IsReference 256 +#define cJSON_StringIsConst 512 /* The cJSON structure: */ typedef struct cJSON { @@ -68,6 +69,8 @@ extern cJSON *cJSON_Parse(const char *value); extern char *cJSON_Print(cJSON *item); /* Render a cJSON entity to text for transfer/storage without any formatting. Free the char* when finished. */ extern char *cJSON_PrintUnformatted(cJSON *item); +/* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ +extern char *cJSON_PrintBuffered(cJSON *item,int prebuffer,int fmt); /* Delete a cJSON entity and all subentities. */ extern void cJSON_Delete(cJSON *c); @@ -100,6 +103,7 @@ 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_AddItemToObjectCS(cJSON *object,const char *string,cJSON *item); /* Use this when string is definitely const (i.e. a literal, or as good as), and will definitely survive the cJSON object */ /* 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); From 2abfb716d10b4fe7edd4f4fe3c3374c3c1e4553c Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Mon, 9 Feb 2015 17:38:18 +0000 Subject: [PATCH 04/15] fix formatting. git-svn-id: svn://svn.code.sf.net/p/cjson/code@62 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index c6c5603..6917aec 100644 --- a/cJSON.c +++ b/cJSON.c @@ -510,7 +510,7 @@ static char *print_array(cJSON *item,int depth,int fmt,printbuffer *p) ptr=out+1;*ptr=0; for (i=0;i Date: Mon, 9 Feb 2015 18:29:05 +0000 Subject: [PATCH 05/15] add cJSON_Utils which includes JSON Pointer implementation git-svn-id: svn://svn.code.sf.net/p/cjson/code@63 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON_Utils.c | 43 +++++++++++++++++++++++++++++++++++++++++++ cJSON_Utils.h | 5 +++++ test_utils.c | 30 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) create mode 100644 cJSON_Utils.c create mode 100644 cJSON_Utils.h create mode 100644 test_utils.c diff --git a/cJSON_Utils.c b/cJSON_Utils.c new file mode 100644 index 0000000..f5584d3 --- /dev/null +++ b/cJSON_Utils.c @@ -0,0 +1,43 @@ +#include +#include +#include +#include "cJSON_Utils.h" + +cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) +{ + cJSON *target=object;int which=0;const char *s=0;int len=0;char *element=0,*e=0; + + while (*pointer=='/' && object) + { + pointer++; + if (object->type==cJSON_Array) + { + which=0; + while (*pointer>='0' && *pointer<='9') {which*=10;which+=*pointer++ - '0';} + if (*pointer && *pointer!='/') return 0; + object=cJSON_GetArrayItem(object,which); + } + else if (object->type==cJSON_Object) + { + + s=pointer;len=0; + while (*s && *s!='/') {if (*s!='~') len++; s++;} + e=element=malloc(len+1); if (!element) return 0; + element[len]=0; + + while (*pointer && *pointer!='/') + { + if (*pointer=='~' && pointer[1]=='0') *e++='~',pointer+=2; + else if (*pointer=='~' && pointer[1]=='1') *e++='/',pointer+=2; + else if (*pointer=='~') {free(element); return 0;} // Invalid encoding. + else *e++=*pointer++; + } + object=cJSON_GetObjectItem(object,element); + free(element); + } + else return 0; + } + return object; +} + + diff --git a/cJSON_Utils.h b/cJSON_Utils.h new file mode 100644 index 0000000..3f7b0ce --- /dev/null +++ b/cJSON_Utils.h @@ -0,0 +1,5 @@ +#include "cJSON.h" + +// Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. +cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer); + diff --git a/test_utils.c b/test_utils.c new file mode 100644 index 0000000..74d4a33 --- /dev/null +++ b/test_utils.c @@ -0,0 +1,30 @@ +#include +#include +#include "cJSON_Utils.h" + +int main() +{ + const char *json="{" + "\"foo\": [\"bar\", \"baz\"]," + "\"\": 0," + "\"a/b\": 1," + "\"c%d\": 2," + "\"e^f\": 3," + "\"g|h\": 4," + "\"i\\\\j\": 5," + "\"k\\\"l\": 6," + "\" \": 7," + "\"m~n\": 8" + "}"; + + const char *tests[12]={"","/foo","/foo/0","/","/a~1b","/c%d","/e^f","/g|h","/i\\j","/k\"l","/ ","/m~0n"}; + + printf("JSON Pointer Tests\n"); + cJSON *root=cJSON_Parse(json); + for (int i=0;i<12;i++) + { + printf("Test %d:\n%s\n\n",i+1,cJSON_Print(cJSONUtils_GetPointer(root,tests[i]))); + } + + +} From 858671cc7f8ac16e91d450fd528cdf3910d99059 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Tue, 10 Feb 2015 12:14:59 +0000 Subject: [PATCH 06/15] rework cJSONUtils_GetPointer to avoid malloc/free by using strcmp with pointer-encoding awareness. git-svn-id: svn://svn.code.sf.net/p/cjson/code@64 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON_Utils.c | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index f5584d3..26bc225 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -1,39 +1,35 @@ -#include -#include -#include +#include #include "cJSON_Utils.h" +static int cJSONUtils_Pstrcasecmp(const char *a,const char *e) +{ + if (!a || !e) return (a==e)?0:1; + for (;*a && *e && *e!='/';a++,e++) { + if (*e=='~') {if (!(e[1]=='0' && *a=='~') && !(e[1]=='1' && *a=='/')) return 1; else e++;} + else if (tolower(*a)!=tolower(*e)) return 1; + } + if ((*e!=0 && *e!='/') != (*a!=0)) return 1; + return 0; +} + + cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) { - cJSON *target=object;int which=0;const char *s=0;int len=0;char *element=0,*e=0; + cJSON *target=object;int which=0;const char *element=0; while (*pointer=='/' && object) { pointer++; if (object->type==cJSON_Array) { - which=0; - while (*pointer>='0' && *pointer<='9') {which*=10;which+=*pointer++ - '0';} + which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0'; if (*pointer && *pointer!='/') return 0; object=cJSON_GetArrayItem(object,which); } else if (object->type==cJSON_Object) { - - s=pointer;len=0; - while (*s && *s!='/') {if (*s!='~') len++; s++;} - e=element=malloc(len+1); if (!element) return 0; - element[len]=0; - - while (*pointer && *pointer!='/') - { - if (*pointer=='~' && pointer[1]=='0') *e++='~',pointer+=2; - else if (*pointer=='~' && pointer[1]=='1') *e++='/',pointer+=2; - else if (*pointer=='~') {free(element); return 0;} // Invalid encoding. - else *e++=*pointer++; - } - object=cJSON_GetObjectItem(object,element); - free(element); + element=pointer; while (*pointer && *pointer!='/') pointer++; + object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,element)) object=object->next; // GetObjectItem. } else return 0; } From 3c6b3cc617491dea2bcce0e07971e7c4d34c1963 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Tue, 10 Feb 2015 14:17:59 +0000 Subject: [PATCH 07/15] start of JSON Patch implementation. cJSON gained a cJSON_InsertItemToArray which pushes elements up by one. This is needed for JSON Patch. Everything but Test is implemented for ApplyPatches. git-svn-id: svn://svn.code.sf.net/p/cjson/code@65 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 2 + cJSON.h | 1 + cJSON_Utils.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++- cJSON_Utils.h | 14 +++++++ test_utils.c | 37 ++++++++++++++++- 5 files changed, 158 insertions(+), 3 deletions(-) diff --git a/cJSON.c b/cJSON.c index 6917aec..d8d1cca 100644 --- a/cJSON.c +++ b/cJSON.c @@ -691,6 +691,8 @@ cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJS void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));} /* Replace array/object items with new ones. */ +void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) {cJSON_AddItemToArray(array,newitem);return;} + newitem->next=c;newitem->prev=c->prev;c->prev=newitem;if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;} 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;c->next=c->prev=0;cJSON_Delete(c);} diff --git a/cJSON.h b/cJSON.h index 55b85ea..9b04cb1 100644 --- a/cJSON.h +++ b/cJSON.h @@ -115,6 +115,7 @@ extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string); extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string); /* Update array items. */ +extern void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem); // Shifts pre-existing items to the right. extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem); extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 26bc225..cfc1619 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -1,6 +1,9 @@ #include +#include +#include #include "cJSON_Utils.h" +// JSON Pointer implementation: static int cJSONUtils_Pstrcasecmp(const char *a,const char *e) { if (!a || !e) return (a==e)?0:1; @@ -12,7 +15,6 @@ static int cJSONUtils_Pstrcasecmp(const char *a,const char *e) return 0; } - cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) { cJSON *target=object;int which=0;const char *element=0; @@ -36,4 +38,107 @@ cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) return object; } +// JSON Patch implementation. +static void cJSONUtils_InplaceDecodePointerString(char *string) +{ + char *s2=string; + for (;*string;s2++,string++) + *s2=(*string!='~')?(*string):((*(++string)=='0')?'~':'/'); + *s2=0; +} + +static cJSON *cJSONUtils_PatchDetach(cJSON *object,const char *path) +{ + char *parentptr=0,*childptr=0;cJSON *parent=0; + + parentptr=strdup(path); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0; + parent=cJSONUtils_GetPointer(object,parentptr); + cJSONUtils_InplaceDecodePointerString(childptr); + + cJSON *ret=0; + if (!parent) ret=0; // Couldn't find object to remove child from. + else if (parent->type==cJSON_Array) ret=cJSON_DetachItemFromArray(parent,atoi(childptr)); + else if (parent->type==cJSON_Object) ret=cJSON_DetachItemFromObject(parent,childptr); + free(parentptr); + return ret; +} + +static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) +{ + cJSON *op=0,*path=0,*value=0;int opcode=0; + + op=cJSON_GetObjectItem(patch,"op"); + path=cJSON_GetObjectItem(patch,"path"); + if (!op || !path) return 2; // malformed patch. + + if (!strcmp(op->valuestring,"add")) opcode=0; + else if (!strcmp(op->valuestring,"remove")) opcode=1; + else if (!strcmp(op->valuestring,"replace"))opcode=2; + else if (!strcmp(op->valuestring,"move")) opcode=3; + else if (!strcmp(op->valuestring,"copy")) opcode=4; + else if (!strcmp(op->valuestring,"test")) opcode=5; + else return 3; // unknown opcode. + + if (opcode==5) return 10; // TEST IS CURRENTLY UNIMPLEMENTED. + + if (opcode==1 || opcode==2) // Remove/Replace + { + cJSON_Delete(cJSONUtils_PatchDetach(object,path->valuestring)); // Get rid of old. + if (opcode==1) return 0; // For Remove, this is job done. + } + + if (opcode==3 || opcode==4) // Copy/Move uses "from". + { + cJSON *from=cJSON_GetObjectItem(patch,"from"); if (!from) return 4; // missing "from" for copy/move. + + if (opcode==3) value=cJSONUtils_PatchDetach(object,from->valuestring); + if (opcode==4) value=cJSONUtils_GetPointer(object,from->valuestring); + if (!value) return 5; // missing "from" for copy/move. + if (opcode==4) value=cJSON_Duplicate(value,1); + if (!value) return 6; // out of memory for copy/move. + } + else // Add/Replace uses "value". + { + value=cJSON_GetObjectItem(patch,"value"); + if (!value) return 7; // missing "value" for add/replace. + value=cJSON_Duplicate(value,1); + if (!value) return 8; // out of memory for add/replace. + } + + // Now, just add "value" to "path". + char *parentptr=0,*childptr=0;cJSON *parent=0; + + parentptr=strdup(path->valuestring); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0; + parent=cJSONUtils_GetPointer(object,parentptr); + cJSONUtils_InplaceDecodePointerString(childptr); + + // add, remove, replace, move, copy, test. + if (!parent) {free(parentptr); return 9;} // Couldn't find object to add to. + else if (parent->type==cJSON_Array) + { + if (!strcmp(childptr,"-")) cJSON_AddItemToArray(parent,value); + else cJSON_InsertItemInArray(parent,atoi(childptr),value); + } + else if (parent->type==cJSON_Object) + { + cJSON_DeleteItemFromObject(parent,childptr); + cJSON_AddItemToObject(parent,childptr,value); + } + free(parentptr); + return 0; +} + + +int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches) +{ + int err; + if (!patches->type==cJSON_Array) return 1; // malformed patches. + if (patches) patches=patches->child; + while (patches) + { + if ((err=cJSONUtils_ApplyPatch(object,patches))) return err; + patches=patches->next; + } + return 0; +} diff --git a/cJSON_Utils.h b/cJSON_Utils.h index 3f7b0ce..91ee08c 100644 --- a/cJSON_Utils.h +++ b/cJSON_Utils.h @@ -3,3 +3,17 @@ // Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer); +// Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. +//cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to); // Not yet implemented. +int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches); // Returns 0 for success. + +// Note that ApplyPatches is NOT atomic on failure. To implement an atomic ApplyPatches, use: +//int cJSONUtils_AtomicApplyPatches(cJSON **object, cJSON *patches) +//{ +// cJSON *modme=cJSON_Duplicate(*object,1); +// int error=cJSONUtils_ApplyPatches(modme,patches); +// if (!error) {cJSON_Delete(*object);*object=modme;} +// else cJSON_Delete(modme); +// return error; +//} +// Code not added to library since this strategy is a LOT slower. diff --git a/test_utils.c b/test_utils.c index 74d4a33..fc222de 100644 --- a/test_utils.c +++ b/test_utils.c @@ -4,6 +4,7 @@ int main() { + // JSON Pointer tests: const char *json="{" "\"foo\": [\"bar\", \"baz\"]," "\"\": 0," @@ -23,8 +24,40 @@ int main() cJSON *root=cJSON_Parse(json); for (int i=0;i<12;i++) { - printf("Test %d:\n%s\n\n",i+1,cJSON_Print(cJSONUtils_GetPointer(root,tests[i]))); + char *output=cJSON_Print(cJSONUtils_GetPointer(root,tests[i])); + printf("Test %d:\n%s\n\n",i+1,output); + free(output); + } + cJSON_Delete(root); + + // JSON Patch tests: + const char *patches[15][2]={ + {"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]"}, + {"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]"}, + {"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]"}, + {"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]"}, + {"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]"}, + {"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]"}, + {"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]"}, + {"{\"baz\": \"qux\",\"foo\": [ \"a\", 2, \"c\" ]}","[{ \"op\": \"test\", \"path\": \"/baz\", \"value\": \"qux\" },{ \"op\": \"test\", \"path\": \"/foo/1\", \"value\": 2 }]"}, + {"{ \"baz\": \"qux\" }","[ { \"op\": \"test\", \"path\": \"/baz\", \"value\": \"bar\" }]"}, + {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/child\", \"value\": { \"grandchild\": { } } }]"}, + {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]"}, + {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz/bat\", \"value\": \"qux\" }]"}, + {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": 10}]"}, + {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]"}, + {"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]"}}; + + printf("JSON Patch Tests\n"); + for (int i=0;i<15;i++) + { + cJSON *object=cJSON_Parse(patches[i][0]); + cJSON *patch=cJSON_Parse(patches[i][1]); + int err=cJSONUtils_ApplyPatches(object,patch); + char *output=cJSON_Print(object); + printf("Test %d (err %d):\n%s\n\n",i+1,err,output); + free(output); } - + } From 060e6563c01b6906c238ffea61e6cbb9955a0dd1 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Wed, 11 Feb 2015 01:29:40 +0000 Subject: [PATCH 08/15] Add more patch functionality and some more utils. Also add a new #define to cJSON.h for SetNumberValue. git-svn-id: svn://svn.code.sf.net/p/cjson/code@66 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.h | 1 + cJSON_Utils.c | 165 ++++++++++++++++++++++++++++++++++++++++++++++---- cJSON_Utils.h | 5 +- test_utils.c | 62 +++++++++++++------ 4 files changed, 201 insertions(+), 32 deletions(-) diff --git a/cJSON.h b/cJSON.h index 9b04cb1..2cea73d 100644 --- a/cJSON.h +++ b/cJSON.h @@ -140,6 +140,7 @@ extern void cJSON_Minify(char *json); /* When assigning an integer value, it needs to be propagated to valuedouble too. */ #define cJSON_SetIntValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) +#define cJSON_SetNumberValue(object,val) ((object)?(object)->valueint=(object)->valuedouble=(val):(val)) #ifdef __cplusplus } diff --git a/cJSON_Utils.c b/cJSON_Utils.c index cfc1619..7d325e8 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -1,6 +1,7 @@ #include #include #include +#include #include "cJSON_Utils.h" // JSON Pointer implementation: @@ -15,23 +16,65 @@ static int cJSONUtils_Pstrcasecmp(const char *a,const char *e) return 0; } +static int cJSONUtils_PointerEncodedstrlen(const char *s) {int l=0;for (;*s;s++,l++) if (*s=='~' || *s=='/') l++;return l;} + +static void cJSONUtils_PointerEncodedstrcpy(char *d,const char *s) +{ + for (;*s;s++) + { + if (*s=='/') {*d++='~';*d++='1';} + else if (*s=='~') {*d++='~';*d++='0';} + else *d++=*s; + } + *d=0; +} + +char *cJSONUtils_FindPointerFromObjectTo(cJSON *object,cJSON *target) +{ + if (object==target) return strdup(""); + + int type=object->type,c=0; + for (cJSON *obj=object->child;obj;obj=obj->next,c++) + { + char *found=cJSONUtils_FindPointerFromObjectTo(obj,target); + if (found) + { + if (type==cJSON_Array) + { + char *ret=(char*)malloc(strlen(found)+23); + sprintf(ret,"/%d%s",c,found); + free(found); + return ret; + } + else if (type==cJSON_Object) + { + char *ret=(char*)malloc(strlen(found)+cJSONUtils_PointerEncodedstrlen(obj->string)+2); + *ret='/';cJSONUtils_PointerEncodedstrcpy(ret+1,obj->string); + strcat(ret,found); + free(found); + return ret; + } + free(found); + return 0; + } + } + return 0; +} + cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) { - cJSON *target=object;int which=0;const char *element=0; - - while (*pointer=='/' && object) + while (*pointer++=='/' && object) { - pointer++; if (object->type==cJSON_Array) { - which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0'; + int which=0; while (*pointer>='0' && *pointer<='9') which=(10*which) + *pointer++ - '0'; if (*pointer && *pointer!='/') return 0; object=cJSON_GetArrayItem(object,which); } else if (object->type==cJSON_Object) { - element=pointer; while (*pointer && *pointer!='/') pointer++; - object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,element)) object=object->next; // GetObjectItem. + object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,pointer)) object=object->next; // GetObjectItem. + while (*pointer && *pointer!='/') pointer++; } else return 0; } @@ -42,8 +85,7 @@ cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) static void cJSONUtils_InplaceDecodePointerString(char *string) { char *s2=string; - for (;*string;s2++,string++) - *s2=(*string!='~')?(*string):((*(++string)=='0')?'~':'/'); + for (;*string;s2++,string++) *s2=(*string!='~')?(*string):((*(++string)=='0')?'~':'/'); *s2=0; } @@ -63,6 +105,28 @@ static cJSON *cJSONUtils_PatchDetach(cJSON *object,const char *path) return ret; } +static int cJSONUtils_Compare(cJSON *a,cJSON *b) +{ + if (a->type!=b->type) return -1; // mismatched type. + switch (a->type) + { + case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; // numeric mismatch. + case cJSON_String: return (strcmp(a->valuestring,b->valuestring)!=0)?-3:0; // string mismatch. + case cJSON_Array: for (a=a->child,b=b->child;a && b;a=a->next,b=b->next) {int err=cJSONUtils_Compare(a,b);if (err) return err;} + return (a || b)?-4:0; // array size mismatch. + case cJSON_Object: + if (cJSON_GetArraySize(a)!=cJSON_GetArraySize(b)) return -5; // object length mismatch. + for (a=a->child;a;a=a->next) + { + int err=0;cJSON *s=cJSON_GetObjectItem(b,a->string); if (!s) return -6; // missing object member. + err=cJSONUtils_Compare(a,s);if (err) return err; + } + return 0; + default: break; + } + return 0; +} + static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) { cJSON *op=0,*path=0,*value=0;int opcode=0; @@ -76,10 +140,8 @@ static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) else if (!strcmp(op->valuestring,"replace"))opcode=2; else if (!strcmp(op->valuestring,"move")) opcode=3; else if (!strcmp(op->valuestring,"copy")) opcode=4; - else if (!strcmp(op->valuestring,"test")) opcode=5; + else if (!strcmp(op->valuestring,"test")) return cJSONUtils_Compare(cJSONUtils_GetPointer(object,path->valuestring),cJSON_GetObjectItem(patch,"value")); else return 3; // unknown opcode. - - if (opcode==5) return 10; // TEST IS CURRENTLY UNIMPLEMENTED. if (opcode==1 || opcode==2) // Remove/Replace { @@ -142,3 +204,82 @@ int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches) return 0; } +static void cJSONUtils_GeneratePatch(cJSON *patches,const char *op,const char *path,const char *suffix,cJSON *val) +{ + cJSON *patch=cJSON_CreateObject(); + cJSON_AddItemToObject(patch,"op",cJSON_CreateString(op)); + if (suffix) + { + char *newpath=(char*)malloc(strlen(path)+cJSONUtils_PointerEncodedstrlen(suffix)+2); + cJSONUtils_PointerEncodedstrcpy(newpath+sprintf(newpath,"%s/",path),suffix); + cJSON_AddItemToObject(patch,"path",cJSON_CreateString(newpath)); + free(newpath); + } + else cJSON_AddItemToObject(patch,"path",cJSON_CreateString(path)); + if (val) cJSON_AddItemToObject(patch,"value",cJSON_Duplicate(val,1)); + cJSON_AddItemToArray(patches,patch); +} + +void cJSONUtils_AddPatchToArray(cJSON *array,const char *op,const char *path,cJSON *val) {cJSONUtils_GeneratePatch(array,op,path,0,val);} + +static void cJSONUtils_CompareToPatch(cJSON *patches,const char *path,cJSON *from,cJSON *to) +{ + if (from->type!=to->type) {cJSONUtils_GeneratePatch(patches,"replace",path,0,to); return; } + + switch (from->type) + { + case cJSON_Number: + if (from->valueint!=to->valueint || from->valuedouble!=to->valuedouble) + cJSONUtils_GeneratePatch(patches,"replace",path,0,to); + return; + + case cJSON_String: + if (strcmp(from->valuestring,to->valuestring)!=0) + cJSONUtils_GeneratePatch(patches,"replace",path,0,to); + return; + + case cJSON_Array: + { + int c;char *newpath=(char*)malloc(strlen(path)+23); // Allow space for 64bit int. + for (c=0,from=from->child,to=to->child;from && to;from=from->next,to=to->next,c++){ + sprintf(newpath,"%s/%d",path,c); cJSONUtils_CompareToPatch(patches,newpath,from,to); + } + for (;from;from=from->next,c++) {sprintf(newpath,"%d",c); cJSONUtils_GeneratePatch(patches,"remove",path,newpath,0); } + for (;to;to=to->next,c++) cJSONUtils_GeneratePatch(patches,"add",path,"-",to); + free(newpath); + return; + } + + case cJSON_Object: + for (cJSON *a=from->child;a;a=a->next) + { + if (!cJSON_GetObjectItem(to,a->string)) cJSONUtils_GeneratePatch(patches,"remove",path,a->string,0); + } + for (cJSON *a=to->child;a;a=a->next) + { + cJSON *other=cJSON_GetObjectItem(from,a->string); + if (!other) cJSONUtils_GeneratePatch(patches,"add",path,a->string,a); + else + { + char *newpath=(char*)malloc(strlen(path)+cJSONUtils_PointerEncodedstrlen(a->string)+2); + cJSONUtils_PointerEncodedstrcpy(newpath+sprintf(newpath,"%s/",path),a->string); + cJSONUtils_CompareToPatch(patches,newpath,other,a); + free(newpath); + } + } + return; + + default: break; + } +} + + +cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to) +{ + cJSON *patches=cJSON_CreateArray(); + cJSONUtils_CompareToPatch(patches,"",from,to); + return patches; +} + + + diff --git a/cJSON_Utils.h b/cJSON_Utils.h index 91ee08c..0b68d3c 100644 --- a/cJSON_Utils.h +++ b/cJSON_Utils.h @@ -4,7 +4,8 @@ cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer); // Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. -//cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to); // Not yet implemented. +cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to); +void cJSONUtils_AddPatchToArray(cJSON *array,const char *op,const char *path,cJSON *val); // Utility for generating patch array entries. int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches); // Returns 0 for success. // Note that ApplyPatches is NOT atomic on failure. To implement an atomic ApplyPatches, use: @@ -17,3 +18,5 @@ int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches); // Returns 0 for succ // return error; //} // Code not added to library since this strategy is a LOT slower. + +char *cJSONUtils_FindPointerFromObjectTo(cJSON *object,cJSON *target); // Given a root object and a target object, construct a pointer from one to the other. diff --git a/test_utils.c b/test_utils.c index fc222de..cc9f270 100644 --- a/test_utils.c +++ b/test_utils.c @@ -1,5 +1,6 @@ #include #include +#include #include "cJSON_Utils.h" int main() @@ -30,25 +31,25 @@ int main() } cJSON_Delete(root); - // JSON Patch tests: - const char *patches[15][2]={ - {"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]"}, - {"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]"}, - {"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]"}, - {"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]"}, - {"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]"}, - {"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]"}, - {"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]"}, - {"{\"baz\": \"qux\",\"foo\": [ \"a\", 2, \"c\" ]}","[{ \"op\": \"test\", \"path\": \"/baz\", \"value\": \"qux\" },{ \"op\": \"test\", \"path\": \"/foo/1\", \"value\": 2 }]"}, - {"{ \"baz\": \"qux\" }","[ { \"op\": \"test\", \"path\": \"/baz\", \"value\": \"bar\" }]"}, - {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/child\", \"value\": { \"grandchild\": { } } }]"}, - {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]"}, - {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz/bat\", \"value\": \"qux\" }]"}, - {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": 10}]"}, - {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]"}, - {"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]"}}; + // JSON Apply Patch tests: + const char *patches[15][3]={ + {"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]","{\"baz\": \"qux\",\"foo\": \"bar\"}"}, + {"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]","{\"foo\": [ \"bar\", \"qux\", \"baz\" ] }"}, + {"{\"baz\": \"qux\",\"foo\": \"bar\"}"," [{ \"op\": \"remove\", \"path\": \"/baz\" }]","{\"foo\": \"bar\" }"}, + {"{ \"foo\": [ \"bar\", \"qux\", \"baz\" ] }","[{ \"op\": \"remove\", \"path\": \"/foo/1\" }]","{\"foo\": [ \"bar\", \"baz\" ] }"}, + {"{ \"baz\": \"qux\",\"foo\": \"bar\"}","[{ \"op\": \"replace\", \"path\": \"/baz\", \"value\": \"boo\" }]","{\"baz\": \"boo\",\"foo\": \"bar\"}"}, + {"{\"foo\": {\"bar\": \"baz\",\"waldo\": \"fred\"},\"qux\": {\"corge\": \"grault\"}}","[{ \"op\": \"move\", \"from\": \"/foo/waldo\", \"path\": \"/qux/thud\" }]","{\"foo\": {\"bar\": \"baz\"},\"qux\": {\"corge\": \"grault\",\"thud\": \"fred\"}}"}, + {"{ \"foo\": [ \"all\", \"grass\", \"cows\", \"eat\" ] }","[ { \"op\": \"move\", \"from\": \"/foo/1\", \"path\": \"/foo/3\" }]","{ \"foo\": [ \"all\", \"cows\", \"eat\", \"grass\" ] }"}, + {"{\"baz\": \"qux\",\"foo\": [ \"a\", 2, \"c\" ]}","[{ \"op\": \"test\", \"path\": \"/baz\", \"value\": \"qux\" },{ \"op\": \"test\", \"path\": \"/foo/1\", \"value\": 2 }]",""}, + {"{ \"baz\": \"qux\" }","[ { \"op\": \"test\", \"path\": \"/baz\", \"value\": \"bar\" }]",""}, + {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/child\", \"value\": { \"grandchild\": { } } }]","{\"foo\": \"bar\",\"child\": {\"grandchild\": {}}}"}, + {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\", \"xyz\": 123 }]","{\"foo\": \"bar\",\"baz\": \"qux\"}"}, + {"{ \"foo\": \"bar\" }","[{ \"op\": \"add\", \"path\": \"/baz/bat\", \"value\": \"qux\" }]",""}, + {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": 10}]",""}, + {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]",""}, + {"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]","{\"foo\": [\"bar\", [\"abc\", \"def\"]] }"}}; - printf("JSON Patch Tests\n"); + printf("JSON Apply Patch Tests\n"); for (int i=0;i<15;i++) { cJSON *object=cJSON_Parse(patches[i][0]); @@ -56,8 +57,31 @@ int main() int err=cJSONUtils_ApplyPatches(object,patch); char *output=cJSON_Print(object); printf("Test %d (err %d):\n%s\n\n",i+1,err,output); - free(output); + free(output);cJSON_Delete(object);cJSON_Delete(patch); } + // JSON Generate Patch tests: + printf("JSON Generate Patch Tests\n"); + for (int i=0;i<15;i++) + { + if (!strlen(patches[i][2])) continue; + cJSON *from=cJSON_Parse(patches[i][0]); + cJSON *to=cJSON_Parse(patches[i][2]); + cJSON *patch=cJSONUtils_GeneratePatches(from,to); + char *out=cJSON_Print(patch); + printf("Test %d: (patch: %s):\n%s\n\n",i+1,patches[i][1],out); + free(out);cJSON_Delete(from);cJSON_Delete(to);cJSON_Delete(patch); + } + + // Misc tests: + printf("JSON Pointer construct\n"); + int numbers[10]={0,1,2,3,4,5,6,7,8,9}; + cJSON *object=cJSON_CreateObject(); + cJSON *nums=cJSON_CreateIntArray(numbers,10); + cJSON *num6=cJSON_GetArrayItem(nums,6); + cJSON_AddItemToObject(object,"numbers",nums); + printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,num6)); + printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,nums)); + printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,object)); } From b44a338c6d4100396434d6a982a5312d9ccc8216 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Wed, 11 Feb 2015 11:31:35 +0000 Subject: [PATCH 09/15] Optimise out escapable array. git-svn-id: svn://svn.code.sf.net/p/cjson/code@67 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/cJSON.c b/cJSON.c index d8d1cca..8b5f0b2 100644 --- a/cJSON.c +++ b/cJSON.c @@ -248,22 +248,12 @@ static const char *parse_string(cJSON *item,const char *str) return ptr; } - -static int escapable[256]={ 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, 1,1,1,1, - 0,0,1,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 1,0,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, - 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0}; - /* Render the cstring provided to an escaped version that can be printed. */ static char *print_string_ptr(const char *str,printbuffer *p) { const char *ptr;char *ptr2,*out;int len=0,flag=0;unsigned char token; - ptr=str;while (*ptr) flag|=escapable[*ptr++]; + for (ptr=str;*ptr;ptr++) flag|=((*ptr>=0 && *ptr<32)||(*ptr=='\"')||(*ptr=='\\'))?1:0; if (!flag) { len=ptr-str; From 9d0a4cbf2aeb3eee6d31476105e64b9aef82de16 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Wed, 11 Feb 2015 11:34:01 +0000 Subject: [PATCH 10/15] adjust the test very slightly git-svn-id: svn://svn.code.sf.net/p/cjson/code@68 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index 8b5f0b2..d510cb9 100644 --- a/cJSON.c +++ b/cJSON.c @@ -253,7 +253,7 @@ static char *print_string_ptr(const char *str,printbuffer *p) { const char *ptr;char *ptr2,*out;int len=0,flag=0;unsigned char token; - for (ptr=str;*ptr;ptr++) flag|=((*ptr>=0 && *ptr<32)||(*ptr=='\"')||(*ptr=='\\'))?1:0; + for (ptr=str;*ptr;ptr++) flag|=((*ptr>0 && *ptr<32)||(*ptr=='\"')||(*ptr=='\\'))?1:0; if (!flag) { len=ptr-str; From 1d9b70e706b75a80cfdffb771259b0d5eb55740f Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Fri, 13 Feb 2015 19:07:29 +0000 Subject: [PATCH 11/15] Fix comment style for proper ansi c compliance. git-svn-id: svn://svn.code.sf.net/p/cjson/code@69 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON.c | 12 ++++++------ cJSON.h | 2 +- test.c | 22 ++++++++++++++-------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/cJSON.c b/cJSON.c index d510cb9..a330f69 100644 --- a/cJSON.c +++ b/cJSON.c @@ -738,13 +738,13 @@ void cJSON_Minify(char *json) while (*json) { if (*json==' ') json++; - else if (*json=='\t') json++; // Whitespace characters. + else if (*json=='\t') json++; /* Whitespace characters. */ else if (*json=='\r') json++; else if (*json=='\n') json++; - else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; // double-slash comments, to end of line. - else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} // multiline comments. - else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} // string literals, which are \" sensitive. - else *into++=*json++; // All other characters. + else if (*json=='/' && json[1]=='/') while (*json && *json!='\n') json++; /* double-slash comments, to end of line. */ + else if (*json=='/' && json[1]=='*') {while (*json && !(*json=='*' && json[1]=='/')) json++;json+=2;} /* multiline comments. */ + else if (*json=='\"'){*into++=*json++;while (*json && *json!='\"'){if (*json=='\\') *into++=*json++;*into++=*json++;}*into++=*json++;} /* string literals, which are \" sensitive. */ + else *into++=*json++; /* All other characters. */ } - *into=0; // and null-terminate. + *into=0; /* and null-terminate. */ } diff --git a/cJSON.h b/cJSON.h index 2cea73d..466d10d 100644 --- a/cJSON.h +++ b/cJSON.h @@ -115,7 +115,7 @@ extern cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string); extern void cJSON_DeleteItemFromObject(cJSON *object,const char *string); /* Update array items. */ -extern void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem); // Shifts pre-existing items to the right. +extern void cJSON_InsertItemInArray(cJSON *array,int which,cJSON *newitem); /* Shifts pre-existing items to the right. */ extern void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem); extern void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem); diff --git a/test.c b/test.c index b308a92..3b462bf 100644 --- a/test.c +++ b/test.c @@ -43,8 +43,10 @@ void doit(char *text) /* Read a file, parse, render back, etc. */ void dofile(char *filename) { - FILE *f=fopen(filename,"rb");fseek(f,0,SEEK_END);long len=ftell(f);fseek(f,0,SEEK_SET); - char *data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f); + FILE *f;long len;char *data; + + f=fopen(filename,"rb");fseek(f,0,SEEK_END);len=ftell(f);fseek(f,0,SEEK_SET); + data=(char*)malloc(len+1);fread(data,1,len,f);fclose(f); doit(data); free(data); } @@ -56,6 +58,16 @@ struct record {const char *precision;double lat,lon;const char *address,*city,*s void create_objects() { cJSON *root,*fmt,*img,*thm,*fld;char *out;int i; /* declare a few. */ + /* Our "days of the week" array: */ + const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; + /* Our matrix: */ + int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}}; + /* Our "gallery" item: */ + int ids[4]={116,943,234,38793}; + /* Our array of "records": */ + struct record fields[2]={ + {"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"}, + {"zip",37.371991,-1.22026e+2,"","SUNNYVALE","CA","94085","US"}}; /* Here we construct some JSON standards, from the JSON site. */ @@ -72,13 +84,11 @@ void create_objects() out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Print to text, Delete the cJSON, print it, release the string. */ /* Our "days of the week" array: */ - const char *strings[7]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"}; root=cJSON_CreateStringArray(strings,7); out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Our matrix: */ - int numbers[3][3]={{0,-1,0},{1,0,0},{0,0,1}}; root=cJSON_CreateArray(); for (i=0;i<3;i++) cJSON_AddItemToArray(root,cJSON_CreateIntArray(numbers[i],3)); @@ -88,7 +98,6 @@ void create_objects() /* Our "gallery" item: */ - int ids[4]={116,943,234,38793}; root=cJSON_CreateObject(); cJSON_AddItemToObject(root, "Image", img=cJSON_CreateObject()); cJSON_AddNumberToObject(img,"Width",800); @@ -103,9 +112,6 @@ void create_objects() out=cJSON_Print(root); cJSON_Delete(root); printf("%s\n",out); free(out); /* Our array of "records": */ - struct record fields[2]={ - {"zip",37.7668,-1.223959e+2,"","SAN FRANCISCO","CA","94107","US"}, - {"zip",37.371991,-1.22026e+2,"","SUNNYVALE","CA","94085","US"}}; root=cJSON_CreateArray(); for (i=0;i<2;i++) From c4ddd7aaa36b7c9d1342fcc79e5323abef293095 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Fri, 13 Feb 2015 19:19:26 +0000 Subject: [PATCH 12/15] Correct comment and variable declaration style to be more compatible. Added cJSONUtils_SortObject for sorting the members of an object alphabetically. git-svn-id: svn://svn.code.sf.net/p/cjson/code@70 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON_Utils.c | 116 ++++++++++++++++++++++++++++++++++---------------- cJSON_Utils.h | 14 +++--- test_utils.c | 72 ++++++++++++++++++++----------- 3 files changed, 136 insertions(+), 66 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 7d325e8..8b5cf64 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -4,7 +4,7 @@ #include #include "cJSON_Utils.h" -// JSON Pointer implementation: +/* JSON Pointer implementation: */ static int cJSONUtils_Pstrcasecmp(const char *a,const char *e) { if (!a || !e) return (a==e)?0:1; @@ -31,10 +31,11 @@ static void cJSONUtils_PointerEncodedstrcpy(char *d,const char *s) char *cJSONUtils_FindPointerFromObjectTo(cJSON *object,cJSON *target) { + int type=object->type,c=0;cJSON *obj=0; + if (object==target) return strdup(""); - int type=object->type,c=0; - for (cJSON *obj=object->child;obj;obj=obj->next,c++) + for (obj=object->child;obj;obj=obj->next,c++) { char *found=cJSONUtils_FindPointerFromObjectTo(obj,target); if (found) @@ -73,7 +74,7 @@ cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) } else if (object->type==cJSON_Object) { - object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,pointer)) object=object->next; // GetObjectItem. + object=object->child; while (object && cJSONUtils_Pstrcasecmp(object->string,pointer)) object=object->next; /* GetObjectItem. */ while (*pointer && *pointer!='/') pointer++; } else return 0; @@ -81,7 +82,7 @@ cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer) return object; } -// JSON Patch implementation. +/* JSON Patch implementation. */ static void cJSONUtils_InplaceDecodePointerString(char *string) { char *s2=string; @@ -91,14 +92,13 @@ static void cJSONUtils_InplaceDecodePointerString(char *string) static cJSON *cJSONUtils_PatchDetach(cJSON *object,const char *path) { - char *parentptr=0,*childptr=0;cJSON *parent=0; + char *parentptr=0,*childptr=0;cJSON *parent=0,*ret=0; parentptr=strdup(path); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0; parent=cJSONUtils_GetPointer(object,parentptr); cJSONUtils_InplaceDecodePointerString(childptr); - cJSON *ret=0; - if (!parent) ret=0; // Couldn't find object to remove child from. + if (!parent) ret=0; /* Couldn't find object to remove child from. */ else if (parent->type==cJSON_Array) ret=cJSON_DetachItemFromArray(parent,atoi(childptr)); else if (parent->type==cJSON_Object) ret=cJSON_DetachItemFromObject(parent,childptr); free(parentptr); @@ -107,18 +107,18 @@ static cJSON *cJSONUtils_PatchDetach(cJSON *object,const char *path) static int cJSONUtils_Compare(cJSON *a,cJSON *b) { - if (a->type!=b->type) return -1; // mismatched type. + if (a->type!=b->type) return -1; /* mismatched type. */ switch (a->type) { - case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; // numeric mismatch. - case cJSON_String: return (strcmp(a->valuestring,b->valuestring)!=0)?-3:0; // string mismatch. + case cJSON_Number: return (a->valueint!=b->valueint || a->valuedouble!=b->valuedouble)?-2:0; /* numeric mismatch. */ + case cJSON_String: return (strcmp(a->valuestring,b->valuestring)!=0)?-3:0; /* string mismatch. */ case cJSON_Array: for (a=a->child,b=b->child;a && b;a=a->next,b=b->next) {int err=cJSONUtils_Compare(a,b);if (err) return err;} - return (a || b)?-4:0; // array size mismatch. + return (a || b)?-4:0; /* array size mismatch. */ case cJSON_Object: - if (cJSON_GetArraySize(a)!=cJSON_GetArraySize(b)) return -5; // object length mismatch. + if (cJSON_GetArraySize(a)!=cJSON_GetArraySize(b)) return -5; /* object length mismatch. */ for (a=a->child;a;a=a->next) { - int err=0;cJSON *s=cJSON_GetObjectItem(b,a->string); if (!s) return -6; // missing object member. + int err=0;cJSON *s=cJSON_GetObjectItem(b,a->string); if (!s) return -6; /* missing object member. */ err=cJSONUtils_Compare(a,s);if (err) return err; } return 0; @@ -129,11 +129,11 @@ static int cJSONUtils_Compare(cJSON *a,cJSON *b) static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) { - cJSON *op=0,*path=0,*value=0;int opcode=0; - + cJSON *op=0,*path=0,*value=0,*parent=0;int opcode=0;char *parentptr=0,*childptr=0; + op=cJSON_GetObjectItem(patch,"op"); path=cJSON_GetObjectItem(patch,"path"); - if (!op || !path) return 2; // malformed patch. + if (!op || !path) return 2; /* malformed patch. */ if (!strcmp(op->valuestring,"add")) opcode=0; else if (!strcmp(op->valuestring,"remove")) opcode=1; @@ -141,41 +141,40 @@ static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) else if (!strcmp(op->valuestring,"move")) opcode=3; else if (!strcmp(op->valuestring,"copy")) opcode=4; else if (!strcmp(op->valuestring,"test")) return cJSONUtils_Compare(cJSONUtils_GetPointer(object,path->valuestring),cJSON_GetObjectItem(patch,"value")); - else return 3; // unknown opcode. + else return 3; /* unknown opcode. */ - if (opcode==1 || opcode==2) // Remove/Replace + if (opcode==1 || opcode==2) /* Remove/Replace */ { - cJSON_Delete(cJSONUtils_PatchDetach(object,path->valuestring)); // Get rid of old. - if (opcode==1) return 0; // For Remove, this is job done. + cJSON_Delete(cJSONUtils_PatchDetach(object,path->valuestring)); /* Get rid of old. */ + if (opcode==1) return 0; /* For Remove, this is job done. */ } - if (opcode==3 || opcode==4) // Copy/Move uses "from". + if (opcode==3 || opcode==4) /* Copy/Move uses "from". */ { - cJSON *from=cJSON_GetObjectItem(patch,"from"); if (!from) return 4; // missing "from" for copy/move. + cJSON *from=cJSON_GetObjectItem(patch,"from"); if (!from) return 4; /* missing "from" for copy/move. */ if (opcode==3) value=cJSONUtils_PatchDetach(object,from->valuestring); if (opcode==4) value=cJSONUtils_GetPointer(object,from->valuestring); - if (!value) return 5; // missing "from" for copy/move. + if (!value) return 5; /* missing "from" for copy/move. */ if (opcode==4) value=cJSON_Duplicate(value,1); - if (!value) return 6; // out of memory for copy/move. + if (!value) return 6; /* out of memory for copy/move. */ } - else // Add/Replace uses "value". + else /* Add/Replace uses "value". */ { value=cJSON_GetObjectItem(patch,"value"); - if (!value) return 7; // missing "value" for add/replace. + if (!value) return 7; /* missing "value" for add/replace. */ value=cJSON_Duplicate(value,1); - if (!value) return 8; // out of memory for add/replace. + if (!value) return 8; /* out of memory for add/replace. */ } - // Now, just add "value" to "path". - char *parentptr=0,*childptr=0;cJSON *parent=0; + /* Now, just add "value" to "path". */ parentptr=strdup(path->valuestring); childptr=strrchr(parentptr,'/'); if (childptr) *childptr++=0; parent=cJSONUtils_GetPointer(object,parentptr); cJSONUtils_InplaceDecodePointerString(childptr); - // add, remove, replace, move, copy, test. - if (!parent) {free(parentptr); return 9;} // Couldn't find object to add to. + /* add, remove, replace, move, copy, test. */ + if (!parent) {free(parentptr); return 9;} /* Couldn't find object to add to. */ else if (parent->type==cJSON_Array) { if (!strcmp(childptr,"-")) cJSON_AddItemToArray(parent,value); @@ -194,7 +193,7 @@ static int cJSONUtils_ApplyPatch(cJSON *object,cJSON *patch) int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches) { int err; - if (!patches->type==cJSON_Array) return 1; // malformed patches. + if (!patches->type==cJSON_Array) return 1; /* malformed patches. */ if (patches) patches=patches->child; while (patches) { @@ -240,7 +239,7 @@ static void cJSONUtils_CompareToPatch(cJSON *patches,const char *path,cJSON *fro case cJSON_Array: { - int c;char *newpath=(char*)malloc(strlen(path)+23); // Allow space for 64bit int. + int c;char *newpath=(char*)malloc(strlen(path)+23); /* Allow space for 64bit int. */ for (c=0,from=from->child,to=to->child;from && to;from=from->next,to=to->next,c++){ sprintf(newpath,"%s/%d",path,c); cJSONUtils_CompareToPatch(patches,newpath,from,to); } @@ -251,11 +250,13 @@ static void cJSONUtils_CompareToPatch(cJSON *patches,const char *path,cJSON *fro } case cJSON_Object: - for (cJSON *a=from->child;a;a=a->next) + { + cJSON *a; + for (a=from->child;a;a=a->next) { if (!cJSON_GetObjectItem(to,a->string)) cJSONUtils_GeneratePatch(patches,"remove",path,a->string,0); } - for (cJSON *a=to->child;a;a=a->next) + for (a=to->child;a;a=a->next) { cJSON *other=cJSON_GetObjectItem(from,a->string); if (!other) cJSONUtils_GeneratePatch(patches,"add",path,a->string,a); @@ -268,6 +269,7 @@ static void cJSONUtils_CompareToPatch(cJSON *patches,const char *path,cJSON *fro } } return; + } default: break; } @@ -281,5 +283,47 @@ cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to) return patches; } +static int cJSONUtils_strcasecmp(const char *s1,const char *s2) +{ + if (!s1) return (s1==s2)?0:1;if (!s2) return 1; + for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0; + return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2); +} + +static cJSON *cJSONUtils_SortList(cJSON *list) +{ + cJSON *first=list,*second=list,*ptr=list; + + if (!list || !list->next) return list; /* One entry is sorted already. */ + + while (ptr) {second=second->next;ptr=ptr->next;if (ptr) ptr=ptr->next;} /* Walk two pointers to find the middle. */ + if (second && second->prev) second->prev->next=0; /* Split the lists */ + + first=cJSONUtils_SortList(first); /* Recursively sort the sub-lists. */ + second=cJSONUtils_SortList(second); + list=ptr=0; + + while (first && second) /* Merge the sub-lists */ + { + if (cJSONUtils_strcasecmp(first->string,second->string)<0) + { + if (!list) list=ptr=first; + else {ptr->next=first;first->prev=ptr;ptr=first;} + first=first->next; + } + else + { + if (!list) list=ptr=second; + else {ptr->next=second;second->prev=ptr;ptr=second;} + second=second->next; + } + } + if (first) { if (!list) return first; ptr->next=first; first->prev=ptr; } /* Append any tails. */ + if (second) { if (!list) return second; ptr->next=second; second->prev=ptr; } + + return list; +} + +void cJSONUtils_SortObject(cJSON *object) {object->child=cJSONUtils_SortList(object->child);} diff --git a/cJSON_Utils.h b/cJSON_Utils.h index 0b68d3c..0e9dfb4 100644 --- a/cJSON_Utils.h +++ b/cJSON_Utils.h @@ -1,13 +1,14 @@ #include "cJSON.h" -// Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. +/* Implement RFC6901 (https://tools.ietf.org/html/rfc6901) JSON Pointer spec. */ cJSON *cJSONUtils_GetPointer(cJSON *object,const char *pointer); -// Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. +/* Implement RFC6902 (https://tools.ietf.org/html/rfc6902) JSON Patch spec. */ cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to); -void cJSONUtils_AddPatchToArray(cJSON *array,const char *op,const char *path,cJSON *val); // Utility for generating patch array entries. -int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches); // Returns 0 for success. +void cJSONUtils_AddPatchToArray(cJSON *array,const char *op,const char *path,cJSON *val); /* Utility for generating patch array entries. */ +int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches); /* Returns 0 for success. */ +/* // Note that ApplyPatches is NOT atomic on failure. To implement an atomic ApplyPatches, use: //int cJSONUtils_AtomicApplyPatches(cJSON **object, cJSON *patches) //{ @@ -18,5 +19,8 @@ int cJSONUtils_ApplyPatches(cJSON *object,cJSON *patches); // Returns 0 for succ // return error; //} // Code not added to library since this strategy is a LOT slower. +*/ -char *cJSONUtils_FindPointerFromObjectTo(cJSON *object,cJSON *target); // Given a root object and a target object, construct a pointer from one to the other. +char *cJSONUtils_FindPointerFromObjectTo(cJSON *object,cJSON *target); /* Given a root object and a target object, construct a pointer from one to the other. */ + +void cJSONUtils_SortObject(cJSON *object); /* Sorts the members of the object into alphabetical order. */ diff --git a/test_utils.c b/test_utils.c index cc9f270..e166c8c 100644 --- a/test_utils.c +++ b/test_utils.c @@ -5,7 +5,9 @@ int main() { - // JSON Pointer tests: + int i; + /* JSON Pointer tests: */ + cJSON *root; const char *json="{" "\"foo\": [\"bar\", \"baz\"]," "\"\": 0," @@ -21,17 +23,7 @@ int main() const char *tests[12]={"","/foo","/foo/0","/","/a~1b","/c%d","/e^f","/g|h","/i\\j","/k\"l","/ ","/m~0n"}; - printf("JSON Pointer Tests\n"); - cJSON *root=cJSON_Parse(json); - for (int i=0;i<12;i++) - { - char *output=cJSON_Print(cJSONUtils_GetPointer(root,tests[i])); - printf("Test %d:\n%s\n\n",i+1,output); - free(output); - } - cJSON_Delete(root); - - // JSON Apply Patch tests: + /* JSON Apply Patch tests: */ const char *patches[15][3]={ {"{ \"foo\": \"bar\"}", "[{ \"op\": \"add\", \"path\": \"/baz\", \"value\": \"qux\" }]","{\"baz\": \"qux\",\"foo\": \"bar\"}"}, {"{ \"foo\": [ \"bar\", \"baz\" ] }", "[{ \"op\": \"add\", \"path\": \"/foo/1\", \"value\": \"qux\" }]","{\"foo\": [ \"bar\", \"qux\", \"baz\" ] }"}, @@ -49,8 +41,27 @@ int main() {"{\"/\": 9,\"~1\": 10}","[{\"op\": \"test\", \"path\": \"/~01\", \"value\": \"10\"}]",""}, {"{ \"foo\": [\"bar\"] }","[ { \"op\": \"add\", \"path\": \"/foo/-\", \"value\": [\"abc\", \"def\"] }]","{\"foo\": [\"bar\", [\"abc\", \"def\"]] }"}}; + /* Misc tests */ + int numbers[10]={0,1,2,3,4,5,6,7,8,9}; + const char *random="QWERTYUIOPASDFGHJKLZXCVBNM"; + char buf[2]={0,0},*before,*after; + cJSON *object,*nums,*num6,*sortme; + + + + printf("JSON Pointer Tests\n"); + root=cJSON_Parse(json); + for (i=0;i<12;i++) + { + char *output=cJSON_Print(cJSONUtils_GetPointer(root,tests[i])); + printf("Test %d:\n%s\n\n",i+1,output); + free(output); + } + cJSON_Delete(root); + + printf("JSON Apply Patch Tests\n"); - for (int i=0;i<15;i++) + for (i=0;i<15;i++) { cJSON *object=cJSON_Parse(patches[i][0]); cJSON *patch=cJSON_Parse(patches[i][1]); @@ -60,28 +71,39 @@ int main() free(output);cJSON_Delete(object);cJSON_Delete(patch); } - // JSON Generate Patch tests: + /* JSON Generate Patch tests: */ printf("JSON Generate Patch Tests\n"); - for (int i=0;i<15;i++) + for (i=0;i<15;i++) { + cJSON *from,*to,*patch;char *out; if (!strlen(patches[i][2])) continue; - cJSON *from=cJSON_Parse(patches[i][0]); - cJSON *to=cJSON_Parse(patches[i][2]); - cJSON *patch=cJSONUtils_GeneratePatches(from,to); - char *out=cJSON_Print(patch); + from=cJSON_Parse(patches[i][0]); + to=cJSON_Parse(patches[i][2]); + patch=cJSONUtils_GeneratePatches(from,to); + out=cJSON_Print(patch); printf("Test %d: (patch: %s):\n%s\n\n",i+1,patches[i][1],out); free(out);cJSON_Delete(from);cJSON_Delete(to);cJSON_Delete(patch); } - // Misc tests: + /* Misc tests: */ printf("JSON Pointer construct\n"); - int numbers[10]={0,1,2,3,4,5,6,7,8,9}; - cJSON *object=cJSON_CreateObject(); - cJSON *nums=cJSON_CreateIntArray(numbers,10); - cJSON *num6=cJSON_GetArrayItem(nums,6); + object=cJSON_CreateObject(); + nums=cJSON_CreateIntArray(numbers,10); + num6=cJSON_GetArrayItem(nums,6); cJSON_AddItemToObject(object,"numbers",nums); printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,num6)); printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,nums)); printf("Pointer: [%s]\n",cJSONUtils_FindPointerFromObjectTo(object,object)); - + + /* JSON Sort test: */ + sortme=cJSON_CreateObject(); + for (i=0;i<26;i++) + { + buf[0]=random[i];cJSON_AddItemToObject(sortme,buf,cJSON_CreateNumber(1)); + } + before=cJSON_Print(sortme); + cJSONUtils_SortObject(sortme); + after=cJSON_Print(sortme); + printf("Before: [%s]\nAfter: [%s]\n\n",before,after); + free(before);free(after);cJSON_Delete(sortme); } From 491cf029b29ad819da210df05d14ed7dae4116c1 Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Fri, 13 Feb 2015 19:53:27 +0000 Subject: [PATCH 13/15] Rework cJSON_Utils object compare functions to use an O(N) algorithm on sorted lists. It's a bit of a shame to use the sorts, because cJSON is otherwise incredibly stable (json->cJSON->json is unmodified modulo formatting), but it means we get usable performance, rather than O(N^2) which will make CPUs cry. git-svn-id: svn://svn.code.sf.net/p/cjson/code@71 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON_Utils.c | 51 ++++++++++++++++++++++++++++++--------------------- test_utils.c | 4 ++-- 2 files changed, 32 insertions(+), 23 deletions(-) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 8b5cf64..85913c0 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -4,6 +4,13 @@ #include #include "cJSON_Utils.h" +static int cJSONUtils_strcasecmp(const char *s1,const char *s2) +{ + if (!s1) return (s1==s2)?0:1;if (!s2) return 1; + for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0; + return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2); +} + /* JSON Pointer implementation: */ static int cJSONUtils_Pstrcasecmp(const char *a,const char *e) { @@ -115,13 +122,18 @@ static int cJSONUtils_Compare(cJSON *a,cJSON *b) case cJSON_Array: for (a=a->child,b=b->child;a && b;a=a->next,b=b->next) {int err=cJSONUtils_Compare(a,b);if (err) return err;} return (a || b)?-4:0; /* array size mismatch. */ case cJSON_Object: - if (cJSON_GetArraySize(a)!=cJSON_GetArraySize(b)) return -5; /* object length mismatch. */ - for (a=a->child;a;a=a->next) + cJSONUtils_SortObject(a); + cJSONUtils_SortObject(b); + a=a->child,b=b->child; + while (a && b) { - int err=0;cJSON *s=cJSON_GetObjectItem(b,a->string); if (!s) return -6; /* missing object member. */ - err=cJSONUtils_Compare(a,s);if (err) return err; + int err; + if (cJSONUtils_strcasecmp(a->string,b->string)) return -6; /* missing member */ + err=cJSONUtils_Compare(a,b);if (err) return err; + a=a->next,b=b->next; } - return 0; + return (a || b)?-5:0; /* object length mismatch */ + default: break; } return 0; @@ -251,22 +263,25 @@ static void cJSONUtils_CompareToPatch(cJSON *patches,const char *path,cJSON *fro case cJSON_Object: { - cJSON *a; - for (a=from->child;a;a=a->next) + cJSON *a,*b; + cJSONUtils_SortObject(from); + cJSONUtils_SortObject(to); + + a=from->child,b=to->child; + while (a || b) { - if (!cJSON_GetObjectItem(to,a->string)) cJSONUtils_GeneratePatch(patches,"remove",path,a->string,0); - } - for (a=to->child;a;a=a->next) - { - cJSON *other=cJSON_GetObjectItem(from,a->string); - if (!other) cJSONUtils_GeneratePatch(patches,"add",path,a->string,a); - else + int diff=(!a)?1:(!b)?-1:cJSONUtils_strcasecmp(a->string,b->string); + if (!diff) { char *newpath=(char*)malloc(strlen(path)+cJSONUtils_PointerEncodedstrlen(a->string)+2); cJSONUtils_PointerEncodedstrcpy(newpath+sprintf(newpath,"%s/",path),a->string); - cJSONUtils_CompareToPatch(patches,newpath,other,a); + cJSONUtils_CompareToPatch(patches,newpath,a,b); free(newpath); + a=a->next; + b=b->next; } + else if (diff<0) {cJSONUtils_GeneratePatch(patches,"remove",path,a->string,0); a=a->next;} + else {cJSONUtils_GeneratePatch(patches,"add",path,b->string,b); b=b->next;} } return; } @@ -283,12 +298,6 @@ cJSON* cJSONUtils_GeneratePatches(cJSON *from,cJSON *to) return patches; } -static int cJSONUtils_strcasecmp(const char *s1,const char *s2) -{ - if (!s1) return (s1==s2)?0:1;if (!s2) return 1; - for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0; - return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2); -} static cJSON *cJSONUtils_SortList(cJSON *list) { diff --git a/test_utils.c b/test_utils.c index e166c8c..e018081 100644 --- a/test_utils.c +++ b/test_utils.c @@ -101,9 +101,9 @@ int main() { buf[0]=random[i];cJSON_AddItemToObject(sortme,buf,cJSON_CreateNumber(1)); } - before=cJSON_Print(sortme); + before=cJSON_PrintUnformatted(sortme); cJSONUtils_SortObject(sortme); - after=cJSON_Print(sortme); + after=cJSON_PrintUnformatted(sortme); printf("Before: [%s]\nAfter: [%s]\n\n",before,after); free(before);free(after);cJSON_Delete(sortme); } From 174c62902c68e9fc7d5028b54666b4fd3630a30b Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Fri, 13 Feb 2015 20:31:33 +0000 Subject: [PATCH 14/15] Since we anticipate repeated comparisons, add in a test for sorted lists, which can reduce cost for sorted objects to O(N) git-svn-id: svn://svn.code.sf.net/p/cjson/code@72 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON_Utils.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index 85913c0..dc20fd7 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -304,6 +304,9 @@ static cJSON *cJSONUtils_SortList(cJSON *list) cJSON *first=list,*second=list,*ptr=list; if (!list || !list->next) return list; /* One entry is sorted already. */ + + while (ptr && ptr->next && cJSONUtils_strcasecmp(ptr->string,ptr->next->string)<0) ptr=ptr->next; /* Test for list sorted. */ + if (!ptr || !ptr->next) return list; /* Leave sorted lists unmodified. */ while (ptr) {second=second->next;ptr=ptr->next;if (ptr) ptr=ptr->next;} /* Walk two pointers to find the middle. */ if (second && second->prev) second->prev->next=0; /* Split the lists */ From 8004ed31158caf50b7d2a3f8777a7ebd6c66240d Mon Sep 17 00:00:00 2001 From: Dave Gamble Date: Fri, 13 Feb 2015 20:33:06 +0000 Subject: [PATCH 15/15] Fix bug with the sort. git-svn-id: svn://svn.code.sf.net/p/cjson/code@73 e3330c51-1366-4df0-8b21-3ccf24e3d50e --- cJSON_Utils.c | 1 + 1 file changed, 1 insertion(+) diff --git a/cJSON_Utils.c b/cJSON_Utils.c index dc20fd7..c4a0ae0 100644 --- a/cJSON_Utils.c +++ b/cJSON_Utils.c @@ -307,6 +307,7 @@ static cJSON *cJSONUtils_SortList(cJSON *list) while (ptr && ptr->next && cJSONUtils_strcasecmp(ptr->string,ptr->next->string)<0) ptr=ptr->next; /* Test for list sorted. */ if (!ptr || !ptr->next) return list; /* Leave sorted lists unmodified. */ + ptr=list; while (ptr) {second=second->next;ptr=ptr->next;if (ptr) ptr=ptr->next;} /* Walk two pointers to find the middle. */ if (second && second->prev) second->prev->next=0; /* Split the lists */