00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #include <string.h>
00027 #include <stdio.h>
00028 #include <math.h>
00029 #include <stdlib.h>
00030 #include <float.h>
00031 #include <limits.h>
00032 #include <ctype.h>
00033 #include "cJSON.h"
00034
00035 static const char *ep;
00036
00037 const char *cJSON_GetErrorPtr() {return ep;}
00038
00039 static int cJSON_strcasecmp(const char *s1,const char *s2)
00040 {
00041 if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
00042 for(; tolower(*s1) == tolower(*s2); ++s1, ++s2) if(*s1 == 0) return 0;
00043 return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
00044 }
00045
00046 static void *(*cJSON_malloc)(size_t sz) = malloc;
00047 static void (*cJSON_free)(void *ptr) = free;
00048
00049 static char* cJSON_strdup(const char* str)
00050 {
00051 size_t len;
00052 char* copy;
00053
00054 len = strlen(str) + 1;
00055 if (!(copy = (char*)cJSON_malloc(len))) return 0;
00056 memcpy(copy,str,len);
00057 return copy;
00058 }
00059
00060 void cJSON_InitHooks(cJSON_Hooks* hooks)
00061 {
00062 if (!hooks) {
00063 cJSON_malloc = malloc;
00064 cJSON_free = free;
00065 return;
00066 }
00067
00068 cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
00069 cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
00070 }
00071
00072
00073 static cJSON *cJSON_New_Item()
00074 {
00075 cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
00076 if (node) memset(node,0,sizeof(cJSON));
00077 return node;
00078 }
00079
00080
00081 void cJSON_Delete(cJSON *c)
00082 {
00083 cJSON *next;
00084 while (c)
00085 {
00086 next=c->next;
00087 if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
00088 if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
00089 if (c->string) cJSON_free(c->string);
00090 cJSON_free(c);
00091 c=next;
00092 }
00093 }
00094
00095
00096 static const char *parse_number(cJSON *item,const char *num)
00097 {
00098 double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
00099
00100
00101 if (*num=='-') sign=-1,num++;
00102 if (*num=='0') num++;
00103 if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9');
00104 if (*num=='.') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');}
00105 if (*num=='e' || *num=='E')
00106 { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++;
00107 while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0');
00108 }
00109
00110 n=sign*n*pow(10.0,(scale+subscale*signsubscale));
00111
00112 item->valuedouble=n;
00113 item->valueint=(int)n;
00114 item->type=cJSON_Number;
00115 return num;
00116 }
00117
00118
00119 static char *print_number(cJSON *item)
00120 {
00121 char *str;
00122 double d=item->valuedouble;
00123 if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
00124 {
00125 str=(char*)cJSON_malloc(21);
00126 if (str) sprintf(str,"%d",item->valueint);
00127 }
00128 else
00129 {
00130 str=(char*)cJSON_malloc(64);
00131 if (str)
00132 {
00133 if (fabs(floor(d)-d)<=DBL_EPSILON) sprintf(str,"%.0f",d);
00134 else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
00135 else sprintf(str,"%f",d);
00136 }
00137 }
00138 return str;
00139 }
00140
00141
00142 static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
00143 static const char *parse_string(cJSON *item,const char *str)
00144 {
00145 const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc;
00146 if (*str!='\"') {ep=str;return 0;}
00147
00148 while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++;
00149
00150 out=(char*)cJSON_malloc(len+1);
00151 if (!out) return 0;
00152
00153 ptr=str+1;ptr2=out;
00154 while (*ptr!='\"' && *ptr)
00155 {
00156 if (*ptr!='\\') *ptr2++=*ptr++;
00157 else
00158 {
00159 ptr++;
00160 switch (*ptr)
00161 {
00162 case 'b': *ptr2++='\b'; break;
00163 case 'f': *ptr2++='\f'; break;
00164 case 'n': *ptr2++='\n'; break;
00165 case 'r': *ptr2++='\r'; break;
00166 case 't': *ptr2++='\t'; break;
00167 case 'u':
00168 sscanf(ptr+1,"%4x",&uc);
00169 len=3;if (uc<0x80) len=1;else if (uc<0x800) len=2;ptr2+=len;
00170
00171 switch (len) {
00172 case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
00173 case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
00174 case 1: *--ptr2 =(uc | firstByteMark[len]);
00175 }
00176 ptr2+=len;ptr+=4;
00177 break;
00178 default: *ptr2++=*ptr; break;
00179 }
00180 ptr++;
00181 }
00182 }
00183 *ptr2=0;
00184 if (*ptr=='\"') ptr++;
00185 item->valuestring=out;
00186 item->type=cJSON_String;
00187 return ptr;
00188 }
00189
00190
00191 static char *print_string_ptr(const char *str)
00192 {
00193 const char *ptr;char *ptr2,*out;int len=0;unsigned char token;
00194
00195 if (!str) return cJSON_strdup("");
00196 ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
00197
00198 out=(char*)cJSON_malloc(len+3);
00199 if (!out) return 0;
00200
00201 ptr2=out;ptr=str;
00202 *ptr2++='\"';
00203 while (*ptr)
00204 {
00205 if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
00206 else
00207 {
00208 *ptr2++='\\';
00209 switch (token=*ptr++)
00210 {
00211 case '\\': *ptr2++='\\'; break;
00212 case '\"': *ptr2++='\"'; break;
00213 case '\b': *ptr2++='b'; break;
00214 case '\f': *ptr2++='f'; break;
00215 case '\n': *ptr2++='n'; break;
00216 case '\r': *ptr2++='r'; break;
00217 case '\t': *ptr2++='t'; break;
00218 default: sprintf(ptr2,"u%04x",token);ptr2+=5; break;
00219 }
00220 }
00221 }
00222 *ptr2++='\"';*ptr2++=0;
00223 return out;
00224 }
00225
00226 static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);}
00227
00228
00229 static const char *parse_value(cJSON *item,const char *value);
00230 static char *print_value(cJSON *item,int depth,int fmt);
00231 static const char *parse_array(cJSON *item,const char *value);
00232 static char *print_array(cJSON *item,int depth,int fmt);
00233 static const char *parse_object(cJSON *item,const char *value);
00234 static char *print_object(cJSON *item,int depth,int fmt);
00235
00236
00237 static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}
00238
00239
00240 cJSON *cJSON_Parse(const char *value)
00241 {
00242 cJSON *c=cJSON_New_Item();
00243 ep=0;
00244 if (!c) return 0;
00245
00246 if (!parse_value(c,skip(value))) {cJSON_Delete(c);return 0;}
00247 return c;
00248 }
00249
00250
00251 char *cJSON_Print(cJSON *item) {return print_value(item,0,1);}
00252 char *cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);}
00253
00254
00255 static const char *parse_value(cJSON *item,const char *value)
00256 {
00257 if (!value) return 0;
00258 if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }
00259 if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }
00260 if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }
00261 if (*value=='\"') { return parse_string(item,value); }
00262 if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }
00263 if (*value=='[') { return parse_array(item,value); }
00264 if (*value=='{') { return parse_object(item,value); }
00265
00266 ep=value;return 0;
00267 }
00268
00269
00270 static char *print_value(cJSON *item,int depth,int fmt)
00271 {
00272 char *out=0;
00273 if (!item) return 0;
00274 switch ((item->type)&255)
00275 {
00276 case cJSON_NULL: out=cJSON_strdup("null"); break;
00277 case cJSON_False: out=cJSON_strdup("false");break;
00278 case cJSON_True: out=cJSON_strdup("true"); break;
00279 case cJSON_Number: out=print_number(item);break;
00280 case cJSON_String: out=print_string(item);break;
00281 case cJSON_Array: out=print_array(item,depth,fmt);break;
00282 case cJSON_Object: out=print_object(item,depth,fmt);break;
00283 }
00284 return out;
00285 }
00286
00287
00288 static const char *parse_array(cJSON *item,const char *value)
00289 {
00290 cJSON *child;
00291 if (*value!='[') {ep=value;return 0;}
00292
00293 item->type=cJSON_Array;
00294 value=skip(value+1);
00295 if (*value==']') return value+1;
00296
00297 item->child=child=cJSON_New_Item();
00298 if (!item->child) return 0;
00299 value=skip(parse_value(child,skip(value)));
00300 if (!value) return 0;
00301
00302 while (*value==',')
00303 {
00304 cJSON *new_item;
00305 if (!(new_item=cJSON_New_Item())) return 0;
00306 child->next=new_item;new_item->prev=child;child=new_item;
00307 value=skip(parse_value(child,skip(value+1)));
00308 if (!value) return 0;
00309 }
00310
00311 if (*value==']') return value+1;
00312 ep=value;return 0;
00313 }
00314
00315
00316 static char *print_array(cJSON *item,int depth,int fmt)
00317 {
00318 char **entries;
00319 char *out=0,*ptr,*ret;int len=5;
00320 cJSON *child=item->child;
00321 int numentries=0,i=0,fail=0;
00322
00323
00324 while (child) numentries++,child=child->next;
00325
00326 entries=(char**)cJSON_malloc(numentries*sizeof(char*));
00327 if (!entries) return 0;
00328 memset(entries,0,numentries*sizeof(char*));
00329
00330 child=item->child;
00331 while (child && !fail)
00332 {
00333 ret=print_value(child,depth+1,fmt);
00334 entries[i++]=ret;
00335 if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
00336 child=child->next;
00337 }
00338
00339
00340 if (!fail) out=(char*)cJSON_malloc(len);
00341
00342 if (!out) fail=1;
00343
00344
00345 if (fail)
00346 {
00347 for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
00348 cJSON_free(entries);
00349 return 0;
00350 }
00351
00352
00353 *out='[';
00354 ptr=out+1;*ptr=0;
00355 for (i=0;i<numentries;i++)
00356 {
00357 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
00358 if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
00359 cJSON_free(entries[i]);
00360 }
00361 cJSON_free(entries);
00362 *ptr++=']';*ptr++=0;
00363 return out;
00364 }
00365
00366
00367 static const char *parse_object(cJSON *item,const char *value)
00368 {
00369 cJSON *child;
00370 if (*value!='{') {ep=value;return 0;}
00371
00372 item->type=cJSON_Object;
00373 value=skip(value+1);
00374 if (*value=='}') return value+1;
00375
00376 item->child=child=cJSON_New_Item();
00377 if (!item->child) return 0;
00378 value=skip(parse_string(child,skip(value)));
00379 if (!value) return 0;
00380 child->string=child->valuestring;child->valuestring=0;
00381 if (*value!=':') {ep=value;return 0;}
00382 value=skip(parse_value(child,skip(value+1)));
00383 if (!value) return 0;
00384
00385 while (*value==',')
00386 {
00387 cJSON *new_item;
00388 if (!(new_item=cJSON_New_Item())) return 0;
00389 child->next=new_item;new_item->prev=child;child=new_item;
00390 value=skip(parse_string(child,skip(value+1)));
00391 if (!value) return 0;
00392 child->string=child->valuestring;child->valuestring=0;
00393 if (*value!=':') {ep=value;return 0;}
00394 value=skip(parse_value(child,skip(value+1)));
00395 if (!value) return 0;
00396 }
00397
00398 if (*value=='}') return value+1;
00399 ep=value;return 0;
00400 }
00401
00402
00403 static char *print_object(cJSON *item,int depth,int fmt)
00404 {
00405 char **entries=0,**names=0;
00406 char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
00407 cJSON *child=item->child;
00408 int numentries=0,fail=0;
00409
00410 while (child) numentries++,child=child->next;
00411
00412 entries=(char**)cJSON_malloc(numentries*sizeof(char*));
00413 if (!entries) return 0;
00414 names=(char**)cJSON_malloc(numentries*sizeof(char*));
00415 if (!names) {cJSON_free(entries);return 0;}
00416 memset(entries,0,sizeof(char*)*numentries);
00417 memset(names,0,sizeof(char*)*numentries);
00418
00419
00420 child=item->child;depth++;if (fmt) len+=depth;
00421 while (child)
00422 {
00423 names[i]=str=print_string_ptr(child->string);
00424 entries[i++]=ret=print_value(child,depth,fmt);
00425 if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
00426 child=child->next;
00427 }
00428
00429
00430 if (!fail) out=(char*)cJSON_malloc(len);
00431 if (!out) fail=1;
00432
00433
00434 if (fail)
00435 {
00436 for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
00437 cJSON_free(names);cJSON_free(entries);
00438 return 0;
00439 }
00440
00441
00442 *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
00443 for (i=0;i<numentries;i++)
00444 {
00445 if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
00446 strcpy(ptr,names[i]);ptr+=strlen(names[i]);
00447 *ptr++=':';if (fmt) *ptr++='\t';
00448 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
00449 if (i!=numentries-1) *ptr++=',';
00450 if (fmt) *ptr++='\n';*ptr=0;
00451 cJSON_free(names[i]);cJSON_free(entries[i]);
00452 }
00453
00454 cJSON_free(names);cJSON_free(entries);
00455 if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
00456 *ptr++='}';*ptr++=0;
00457 return out;
00458 }
00459
00460
00461 int cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
00462 cJSON *cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
00463 cJSON *cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
00464
00465
00466 static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
00467
00468 static cJSON *create_reference(cJSON *item) {cJSON *ref=cJSON_New_Item();if (!ref) return 0;memcpy(ref,item,sizeof(cJSON));ref->string=0;ref->type|=cJSON_IsReference;ref->next=ref->prev=0;return ref;}
00469
00470
00471 void cJSON_AddItemToArray(cJSON *array, cJSON *item) {cJSON *c=array->child;if (!item) return; if (!c) {array->child=item;} else {while (c && c->next) c=c->next; suffix_object(c,item);}}
00472 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);}
00473 void cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}
00474 void cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
00475
00476 cJSON *cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
00477 if (c->prev) c->prev->next=c->next;if (c->next) c->next->prev=c->prev;if (c==array->child) array->child=c->next;c->prev=c->next=0;return c;}
00478 void cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
00479 cJSON *cJSON_DetachItemFromObject(cJSON *object,const char *string) {int i=0;cJSON *c=object->child;while (c && cJSON_strcasecmp(c->string,string)) i++,c=c->next;if (c) return cJSON_DetachItemFromArray(object,i);return 0;}
00480 void cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
00481
00482
00483 void cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
00484 newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
00485 if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
00486 void cJSON_ReplaceItemInObject(cJSON *object,const char *string,cJSON *newitem){int i=0;cJSON *c=object->child;while(c && cJSON_strcasecmp(c->string,string))i++,c=c->next;if(c){newitem->string=cJSON_strdup(string);cJSON_ReplaceItemInArray(object,i,newitem);}}
00487
00488
00489 cJSON *cJSON_CreateNull() {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
00490 cJSON *cJSON_CreateTrue() {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
00491 cJSON *cJSON_CreateFalse() {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
00492 cJSON *cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
00493 cJSON *cJSON_CreateNumber(double num) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=(int)num;}return item;}
00494 cJSON *cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
00495 cJSON *cJSON_CreateArray() {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
00496 cJSON *cJSON_CreateObject() {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}
00497
00498
00499 cJSON *cJSON_CreateIntArray(int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
00500 cJSON *cJSON_CreateFloatArray(float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
00501 cJSON *cJSON_CreateDoubleArray(double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
00502 cJSON *cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a && i<count;i++){n=cJSON_CreateString(strings[i]);if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}