esl_json.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. Copyright (c) 2009 Dave Gamble
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. /* cJSON */
  20. /* JSON parser in C. */
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <stdlib.h>
  25. #include <float.h>
  26. #include <limits.h>
  27. #include <ctype.h>
  28. #include "esl_json.h"
  29. #include "esl.h"
  30. static const char *ep;
  31. ESL_DECLARE(const char *)cJSON_GetErrorPtr(void) {return ep;}
  32. static int cJSON_strcasecmp(const char *s1,const char *s2)
  33. {
  34. if (!s1) return (s1==s2)?0:1;if (!s2) return 1;
  35. for(; tolower(*(const unsigned char *)s1) == tolower(*(const unsigned char *)s2); ++s1, ++s2) if(*s1 == 0) return 0;
  36. return tolower(*(const unsigned char *)s1) - tolower(*(const unsigned char *)s2);
  37. }
  38. static void *glue_malloc(size_t theSize)
  39. {
  40. return(malloc(theSize));
  41. }
  42. static void glue_free(void *thePtr)
  43. {
  44. free(thePtr);
  45. }
  46. static void *(*cJSON_malloc)(size_t sz) = glue_malloc;
  47. static void (*cJSON_free)(void *ptr) = glue_free;
  48. static char* cJSON_strdup(const char* str)
  49. {
  50. size_t len;
  51. char* copy;
  52. const char *s = str ? str : "";
  53. len = strlen(s) + 1;
  54. if (!(copy = (char*)cJSON_malloc(len))) return 0;
  55. memcpy(copy,s,len);
  56. return copy;
  57. }
  58. ESL_DECLARE(void)cJSON_InitHooks(cJSON_Hooks* hooks)
  59. {
  60. if (!hooks) { /* Reset hooks */
  61. cJSON_malloc = malloc;
  62. cJSON_free = free;
  63. return;
  64. }
  65. cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
  66. cJSON_free = (hooks->free_fn)?hooks->free_fn:free;
  67. }
  68. /* Internal constructor. */
  69. static cJSON *cJSON_New_Item(void)
  70. {
  71. cJSON* node = (cJSON*)cJSON_malloc(sizeof(cJSON));
  72. if (node) memset(node,0,sizeof(cJSON));
  73. return node;
  74. }
  75. /* Delete a cJSON structure. */
  76. ESL_DECLARE(void)cJSON_Delete(cJSON *c)
  77. {
  78. cJSON *next;
  79. while (c)
  80. {
  81. next=c->next;
  82. if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
  83. if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
  84. if (c->string) cJSON_free(c->string);
  85. cJSON_free(c);
  86. c=next;
  87. }
  88. }
  89. /* Parse the input text to generate a number, and populate the result into item. */
  90. static const char *parse_number(cJSON *item,const char *num)
  91. {
  92. double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
  93. /* Could use sscanf for this? */
  94. if (*num=='-') sign=-1,num++; /* Has sign? */
  95. if (*num=='0') num++; /* is zero */
  96. if (*num>='1' && *num<='9') do n=(n*10.0)+(*num++ -'0'); while (*num>='0' && *num<='9'); /* Number? */
  97. if (*num=='.' && num[1]>='0' && num[1]<='9') {num++; do n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');} /* Fractional part? */
  98. if (*num=='e' || *num=='E') /* Exponent? */
  99. { num++;if (*num=='+') num++; else if (*num=='-') signsubscale=-1,num++; /* With sign? */
  100. while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0'); /* Number? */
  101. }
  102. n=sign*n*pow(10.0,(scale+subscale*signsubscale)); /* number = +/- number.fraction * 10^+/- exponent */
  103. item->valuedouble=n;
  104. item->valueint=(int)n;
  105. item->type=cJSON_Number;
  106. return num;
  107. }
  108. /* Render the number nicely from the given item into a string. */
  109. static char *print_number(cJSON *item)
  110. {
  111. char *str;
  112. double d=item->valuedouble;
  113. if (fabs(((double)item->valueint)-d)<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
  114. {
  115. str=(char*)cJSON_malloc(21); /* 2^64+1 can be represented in 21 chars. */
  116. if (str) sprintf(str,"%d",item->valueint);
  117. }
  118. else
  119. {
  120. str=(char*)cJSON_malloc(64); /* This is a nice tradeoff. */
  121. if (str)
  122. {
  123. if (fabs(floor(d)-d)<=DBL_EPSILON) sprintf(str,"%.0f",d);
  124. else if (fabs(d)<1.0e-6 || fabs(d)>1.0e9) sprintf(str,"%e",d);
  125. else sprintf(str,"%f",d);
  126. }
  127. }
  128. return str;
  129. }
  130. /* Parse the input text into an unescaped cstring, and populate item. */
  131. static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
  132. static const char *parse_string(cJSON *item,const char *str)
  133. {
  134. const char *ptr=str+1;char *ptr2;char *out;int len=0;unsigned uc,uc2;
  135. if (*str!='\"') {ep=str;return 0;} /* not a string! */
  136. while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++; /* Skip escaped quotes. */
  137. out=(char*)cJSON_malloc(len+1); /* This is how long we need for the string, roughly. */
  138. if (!out) return 0;
  139. ptr=str+1;ptr2=out;
  140. while (*ptr!='\"' && *ptr)
  141. {
  142. if (*ptr!='\\') *ptr2++=*ptr++;
  143. else
  144. {
  145. ptr++;
  146. switch (*ptr)
  147. {
  148. case 'b': *ptr2++='\b'; break;
  149. case 'f': *ptr2++='\f'; break;
  150. case 'n': *ptr2++='\n'; break;
  151. case 'r': *ptr2++='\r'; break;
  152. case 't': *ptr2++='\t'; break;
  153. case 'u': /* transcode utf16 to utf8. */
  154. if (sscanf(ptr+1,"%4x",&uc) < 1) break;
  155. ptr+=4; /* get the unicode char. */
  156. if ((uc>=0xDC00 && uc<=0xDFFF) || uc==0) break; // check for invalid.
  157. if (uc>=0xD800 && uc<=0xDBFF) // UTF16 surrogate pairs.
  158. {
  159. if (ptr[1]!='\\' || ptr[2]!='u') break; // missing second-half of surrogate.
  160. if (sscanf(ptr+3,"%4x",&uc2) < 1) break;
  161. ptr+=6;
  162. if (uc2<0xDC00 || uc2>0xDFFF) break; // invalid second-half of surrogate.
  163. uc=0x10000 | ((uc&0x3FF)<<10) | (uc2&0x3FF);
  164. }
  165. len=4;if (uc<0x80) len=1;else if (uc<0x800) len=2;else if (uc<0x10000) len=3; ptr2+=len;
  166. switch (len) {
  167. case 4: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
  168. case 3: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
  169. case 2: *--ptr2 =((uc | 0x80) & 0xBF); uc >>= 6;
  170. case 1: *--ptr2 =(char)(uc | firstByteMark[len]);
  171. }
  172. ptr2+=len;
  173. break;
  174. default: *ptr2++=*ptr; break;
  175. }
  176. ptr++;
  177. }
  178. }
  179. *ptr2=0;
  180. if (*ptr=='\"') ptr++;
  181. item->valuestring=out;
  182. item->type=cJSON_String;
  183. return ptr;
  184. }
  185. /* Render the cstring provided to an escaped version that can be printed. */
  186. static char *print_string_ptr(const char *str)
  187. {
  188. const char *ptr;char *ptr2,*out;int len=0;unsigned char token;
  189. if (!str) return cJSON_strdup("");
  190. ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
  191. out=(char*)cJSON_malloc(len+3);
  192. if (!out) return 0;
  193. ptr2=out;ptr=str;
  194. *ptr2++='\"';
  195. while (*ptr)
  196. {
  197. if ((unsigned char)*ptr>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
  198. else
  199. {
  200. *ptr2++='\\';
  201. switch (token=*ptr++)
  202. {
  203. case '\\': *ptr2++='\\'; break;
  204. case '\"': *ptr2++='\"'; break;
  205. case '\b': *ptr2++='b'; break;
  206. case '\f': *ptr2++='f'; break;
  207. case '\n': *ptr2++='n'; break;
  208. case '\r': *ptr2++='r'; break;
  209. case '\t': *ptr2++='t'; break;
  210. default: sprintf(ptr2,"u%04x",token);ptr2+=5; break; /* escape and print */
  211. }
  212. }
  213. }
  214. *ptr2++='\"';*ptr2++=0;
  215. return out;
  216. }
  217. /* Invote print_string_ptr (which is useful) on an item. */
  218. static char *print_string(cJSON *item) {return print_string_ptr(item->valuestring);}
  219. /* Predeclare these prototypes. */
  220. static const char *parse_value(cJSON *item,const char *value);
  221. static char *print_value(cJSON *item,int depth,int fmt);
  222. static const char *parse_array(cJSON *item,const char *value);
  223. static char *print_array(cJSON *item,int depth,int fmt);
  224. static const char *parse_object(cJSON *item,const char *value);
  225. static char *print_object(cJSON *item,int depth,int fmt);
  226. /* Utility to jump whitespace and cr/lf */
  227. static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}
  228. /* Parse an object - create a new root, and populate. */
  229. ESL_DECLARE(cJSON *)cJSON_Parse(const char *value)
  230. {
  231. cJSON *c=cJSON_New_Item();
  232. ep=0;
  233. if (!c) return 0; /* memory fail */
  234. if (!parse_value(c,skip(value))) {cJSON_Delete(c);return 0;}
  235. return c;
  236. }
  237. /* Render a cJSON item/entity/structure to text. */
  238. ESL_DECLARE(char *) cJSON_Print(cJSON *item) {return print_value(item,0,1);}
  239. ESL_DECLARE(char *) cJSON_PrintUnformatted(cJSON *item) {return print_value(item,0,0);}
  240. /* Parser core - when encountering text, process appropriately. */
  241. static const char *parse_value(cJSON *item,const char *value)
  242. {
  243. if (!value) return 0; /* Fail on null. */
  244. if (!strncmp(value,"null",4)) { item->type=cJSON_NULL; return value+4; }
  245. if (!strncmp(value,"false",5)) { item->type=cJSON_False; return value+5; }
  246. if (!strncmp(value,"true",4)) { item->type=cJSON_True; item->valueint=1; return value+4; }
  247. if (*value=='\"') { return parse_string(item,value); }
  248. if (*value=='-' || (*value>='0' && *value<='9')) { return parse_number(item,value); }
  249. if (*value=='[') { return parse_array(item,value); }
  250. if (*value=='{') { return parse_object(item,value); }
  251. ep=value;return 0; /* failure. */
  252. }
  253. /* Render a value to text. */
  254. static char *print_value(cJSON *item,int depth,int fmt)
  255. {
  256. char *out=0;
  257. if (!item) return 0;
  258. switch ((item->type)&255)
  259. {
  260. case cJSON_NULL: out=cJSON_strdup("null"); break;
  261. case cJSON_False: out=cJSON_strdup("false");break;
  262. case cJSON_True: out=cJSON_strdup("true"); break;
  263. case cJSON_Number: out=print_number(item);break;
  264. case cJSON_String: out=print_string(item);break;
  265. case cJSON_Array: out=print_array(item,depth,fmt);break;
  266. case cJSON_Object: out=print_object(item,depth,fmt);break;
  267. }
  268. return out;
  269. }
  270. /* Build an array from input text. */
  271. static const char *parse_array(cJSON *item,const char *value)
  272. {
  273. cJSON *child;
  274. if (*value!='[') {ep=value;return 0;} /* not an array! */
  275. item->type=cJSON_Array;
  276. value=skip(value+1);
  277. if (*value==']') return value+1; /* empty array. */
  278. item->child=child=cJSON_New_Item();
  279. if (!item->child) return 0; /* memory fail */
  280. value=skip(parse_value(child,skip(value))); /* skip any spacing, get the value. */
  281. if (!value) return 0;
  282. while (*value==',')
  283. {
  284. cJSON *new_item;
  285. if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
  286. child->next=new_item;new_item->prev=child;child=new_item;
  287. value=skip(parse_value(child,skip(value+1)));
  288. if (!value) return 0; /* memory fail */
  289. }
  290. if (*value==']') return value+1; /* end of array */
  291. ep=value;return 0; /* malformed. */
  292. }
  293. /* Render an array to text */
  294. static char *print_array(cJSON *item,int depth,int fmt)
  295. {
  296. char **entries;
  297. char *out=0,*ptr,*ret;int len=5;
  298. cJSON *child=item->child;
  299. int numentries=0,i=0,fail=0;
  300. /* How many entries in the array? */
  301. while (child) numentries++,child=child->next;
  302. /* Allocate an array to hold the values for each */
  303. entries=(char**)cJSON_malloc(numentries*sizeof(char*));
  304. if (!entries) return 0;
  305. memset(entries,0,numentries*sizeof(char*));
  306. /* Retrieve all the results: */
  307. child=item->child;
  308. while (child && !fail)
  309. {
  310. ret=print_value(child,depth+1,fmt);
  311. entries[i++]=ret;
  312. if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
  313. child=child->next;
  314. }
  315. /* If we didn't fail, try to malloc the output string */
  316. if (!fail) out=(char*)cJSON_malloc(len);
  317. /* If that fails, we fail. */
  318. if (!out) fail=1;
  319. /* Handle failure. */
  320. if (fail)
  321. {
  322. for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
  323. cJSON_free(entries);
  324. return 0;
  325. }
  326. /* Compose the output array. */
  327. *out='[';
  328. ptr=out+1;*ptr=0;
  329. for (i=0;i<numentries;i++)
  330. {
  331. strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
  332. if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
  333. cJSON_free(entries[i]);
  334. }
  335. cJSON_free(entries);
  336. *ptr++=']';*ptr++=0;
  337. return out;
  338. }
  339. /* Build an object from the text. */
  340. static const char *parse_object(cJSON *item,const char *value)
  341. {
  342. cJSON *child;
  343. if (*value!='{') {ep=value;return 0;} /* not an object! */
  344. item->type=cJSON_Object;
  345. value=skip(value+1);
  346. if (*value=='}') return value+1; /* empty array. */
  347. item->child=child=cJSON_New_Item();
  348. if (!item->child) return 0;
  349. value=skip(parse_string(child,skip(value)));
  350. if (!value) return 0;
  351. child->string=child->valuestring;child->valuestring=0;
  352. if (*value!=':') {ep=value;return 0;} /* fail! */
  353. value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
  354. if (!value) return 0;
  355. while (*value==',')
  356. {
  357. cJSON *new_item;
  358. if (!(new_item=cJSON_New_Item())) return 0; /* memory fail */
  359. child->next=new_item;new_item->prev=child;child=new_item;
  360. value=skip(parse_string(child,skip(value+1)));
  361. if (!value) return 0;
  362. child->string=child->valuestring;child->valuestring=0;
  363. if (*value!=':') {ep=value;return 0;} /* fail! */
  364. value=skip(parse_value(child,skip(value+1))); /* skip any spacing, get the value. */
  365. if (!value) return 0;
  366. }
  367. if (*value=='}') return value+1; /* end of array */
  368. ep=value;return 0; /* malformed. */
  369. }
  370. /* Render an object to text. */
  371. static char *print_object(cJSON *item,int depth,int fmt)
  372. {
  373. char **entries=0,**names=0;
  374. char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
  375. cJSON *child=item->child;
  376. int numentries=0,fail=0;
  377. /* Count the number of entries. */
  378. while (child) numentries++,child=child->next;
  379. /* Allocate space for the names and the objects */
  380. entries=(char**)cJSON_malloc(numentries*sizeof(char*));
  381. if (!entries) return 0;
  382. names=(char**)cJSON_malloc(numentries*sizeof(char*));
  383. if (!names) {cJSON_free(entries);return 0;}
  384. memset(entries,0,sizeof(char*)*numentries);
  385. memset(names,0,sizeof(char*)*numentries);
  386. /* Collect all the results into our arrays: */
  387. child=item->child;depth++;if (fmt) len+=depth;
  388. while (child)
  389. {
  390. names[i]=str=print_string_ptr(child->string);
  391. entries[i++]=ret=print_value(child,depth,fmt);
  392. if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
  393. child=child->next;
  394. }
  395. /* Try to allocate the output string */
  396. if (!fail) out=(char*)cJSON_malloc(len);
  397. if (!out) fail=1;
  398. /* Handle failure */
  399. if (fail)
  400. {
  401. for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
  402. cJSON_free(names);cJSON_free(entries);
  403. return 0;
  404. }
  405. /* Compose the output: */
  406. *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
  407. for (i=0;i<numentries;i++)
  408. {
  409. if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
  410. strcpy(ptr,names[i]);ptr+=strlen(names[i]);
  411. *ptr++=':';if (fmt) *ptr++='\t';
  412. strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
  413. if (i!=numentries-1) *ptr++=',';
  414. if (fmt) *ptr++='\n';*ptr=0;
  415. cJSON_free(names[i]);cJSON_free(entries[i]);
  416. }
  417. cJSON_free(names);cJSON_free(entries);
  418. if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
  419. *ptr++='}';*ptr++=0;
  420. return out;
  421. }
  422. /* Get Array size/item / object item. */
  423. ESL_DECLARE(int) cJSON_GetArraySize(cJSON *array) {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
  424. ESL_DECLARE(cJSON *)cJSON_GetArrayItem(cJSON *array,int item) {cJSON *c=array->child; while (c && item>0) item--,c=c->next; return c;}
  425. ESL_DECLARE(cJSON *)cJSON_GetObjectItem(cJSON *object,const char *string) {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
  426. /* Utility for array list handling. */
  427. static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
  428. /* Utility for handling references. */
  429. 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;}
  430. /* Add item to array/object. */
  431. ESL_DECLARE(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);}}
  432. ESL_DECLARE(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);}
  433. ESL_DECLARE(void) cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item) {cJSON_AddItemToArray(array,create_reference(item));}
  434. ESL_DECLARE(void) cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item) {cJSON_AddItemToObject(object,string,create_reference(item));}
  435. ESL_DECLARE(cJSON *)cJSON_DetachItemFromArray(cJSON *array,int which) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
  436. 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;}
  437. ESL_DECLARE(void) cJSON_DeleteItemFromArray(cJSON *array,int which) {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
  438. ESL_DECLARE(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;}
  439. ESL_DECLARE(void) cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
  440. /* Replace array/object items with new ones. */
  441. ESL_DECLARE(void) cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem) {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
  442. newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
  443. if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
  444. ESL_DECLARE(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);}}
  445. /* Create basic types: */
  446. ESL_DECLARE(cJSON *)cJSON_CreateNull(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
  447. ESL_DECLARE(cJSON *)cJSON_CreateTrue(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
  448. ESL_DECLARE(cJSON *)cJSON_CreateFalse(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
  449. ESL_DECLARE(cJSON *)cJSON_CreateBool(int b) {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
  450. ESL_DECLARE(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;}
  451. ESL_DECLARE(cJSON *)cJSON_CreateString(const char *string) {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
  452. ESL_DECLARE(cJSON *)cJSON_CreateArray(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
  453. ESL_DECLARE(cJSON *)cJSON_CreateObject(void) {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}
  454. /* Create Arrays: */
  455. ESL_DECLARE(cJSON *)cJSON_CreateIntArray(int *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a!=0 && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!n)return 0;if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
  456. ESL_DECLARE(cJSON *)cJSON_CreateFloatArray(float *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a!=0 && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!n)return 0;if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
  457. ESL_DECLARE(cJSON *)cJSON_CreateDoubleArray(double *numbers,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a!=0 && i<count;i++){n=cJSON_CreateNumber(numbers[i]);if(!n)return 0;if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
  458. ESL_DECLARE(cJSON *)cJSON_CreateStringArray(const char **strings,int count) {int i;cJSON *n=0,*p=0,*a=cJSON_CreateArray();for(i=0;a!=0 && i<count;i++){n=cJSON_CreateString(strings[i]);if(!n)return 0;if(!i)a->child=n;else suffix_object(p,n);p=n;}return a;}
  459. /* For Emacs:
  460. * Local Variables:
  461. * mode:c
  462. * indent-tabs-mode:t
  463. * tab-width:4
  464. * c-basic-offset:4
  465. * End:
  466. * For VIM:
  467. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  468. */