cJSON.cpp
Go to the documentation of this file.
00001 /*
00002   Copyright (c) 2009 Dave Gamble
00003 
00004   Permission is hereby granted, free of charge, to any person obtaining a copy
00005   of this software and associated documentation files (the "Software"), to deal
00006   in the Software without restriction, including without limitation the rights
00007   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008   copies of the Software, and to permit persons to whom the Software is
00009   furnished to do so, subject to the following conditions:
00010 
00011   The above copyright notice and this permission notice shall be included in
00012   all copies or substantial portions of the Software.
00013 
00014   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020   THE SOFTWARE.
00021 */
00022 
00023 /* cJSON */
00024 /* JSON parser in C. */
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 <pcl/outofcore/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(* reinterpret_cast<const unsigned char *> (s1) ) - tolower(* reinterpret_cast<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 = static_cast<char*> ( cJSON_malloc(len))))
00056       {
00057         return (0);
00058       }
00059       
00060       memcpy(copy,str,len);
00061       return (copy);
00062 }
00063 
00064 void cJSON_InitHooks(cJSON_Hooks* hooks)
00065 {
00066     if (!hooks) { /* Reset hooks */
00067         cJSON_malloc = malloc;
00068         cJSON_free = free;
00069         return;
00070     }
00071 
00072         cJSON_malloc = (hooks->malloc_fn)?hooks->malloc_fn:malloc;
00073         cJSON_free       = (hooks->free_fn)?hooks->free_fn:free;
00074 }
00075 
00076 /* Internal constructor. */
00077 static cJSON *cJSON_New_Item()
00078 {
00079         cJSON* node = static_cast<cJSON*> (cJSON_malloc(sizeof(cJSON)));
00080         if (node) memset(node,0,sizeof(cJSON));
00081         return node;
00082 }
00083 
00084 /* Delete a cJSON structure. */
00085 void cJSON_Delete(cJSON *c)
00086 {
00087         cJSON *next;
00088         while (c)
00089         {
00090                 next=c->next;
00091                 if (!(c->type&cJSON_IsReference) && c->child) cJSON_Delete(c->child);
00092                 if (!(c->type&cJSON_IsReference) && c->valuestring) cJSON_free(c->valuestring);
00093                 if (c->string) cJSON_free(c->string);
00094                 cJSON_free(c);
00095                 c=next;
00096         }
00097 }
00098 
00099 /* Parse the input text to generate a number, and populate the result into item. */
00100 static const char *parse_number(cJSON *item,const char *num)
00101 {
00102         double n=0,sign=1,scale=0;int subscale=0,signsubscale=1;
00103 
00104         /* Could use sscanf for this? */
00105         if (*num=='-') sign=-1,num++;   /* Has sign? */
00106         if (*num=='0') num++;                   /* is zero */
00107         if (*num>='1' && *num<='9')     do      n=(n*10.0)+(*num++ -'0');       while (*num>='0' && *num<='9'); /* Number? */
00108         if (*num=='.') {num++;          do      n=(n*10.0)+(*num++ -'0'),scale--; while (*num>='0' && *num<='9');}      /* Fractional part? */
00109         if (*num=='e' || *num=='E')             /* Exponent? */
00110         {       num++;if (*num=='+') num++;     else if (*num=='-') signsubscale=-1,num++;              /* With sign? */
00111                 while (*num>='0' && *num<='9') subscale=(subscale*10)+(*num++ - '0');   /* Number? */
00112         }
00113 
00114         n=sign*n*pow(10.0,(scale+subscale*signsubscale));       /* number = +/- number.fraction * 10^+/- exponent */
00115         
00116         item->valuedouble=n;
00117         item->valueint=static_cast<int> (n);
00118         item->type=cJSON_Number;
00119         return num;
00120 }
00121 
00122 /* Render the number nicely from the given item into a string. */
00123 static char *print_number(cJSON *item)
00124 {
00125         char *str;
00126         double d=item->valuedouble;
00127         if (fabs((static_cast<double>(item->valueint)-d))<=DBL_EPSILON && d<=INT_MAX && d>=INT_MIN)
00128         {
00129                 str=static_cast<char*>(cJSON_malloc(21));       /* 2^64+1 can be represented in 21 chars. */
00130                 if (str) sprintf(str,"%d",item->valueint);
00131         }
00132         else
00133         {
00134                 str=static_cast<char*>(cJSON_malloc(64));       /* This is a nice tradeoff. */
00135                 if (str)
00136                 {
00137                         if (fabs(floor(d)-d)<=DBL_EPSILON)                      sprintf(str,"%.0f",d);
00138                         else sprintf(str,"%.16g",d);
00139                 }
00140         }
00141         return str;
00142 }
00143 
00144 /* Parse the input text into an unescaped cstring, and populate item. */
00145 static const unsigned char firstByteMark[7] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC };
00146 static const char *parse_string(cJSON *item,const char *str)
00147 {
00148         const char *ptr=str+1; char *ptr2; char *out; int len=0; unsigned uc;
00149         if (*str!='\"') {ep=str;return 0;}      /* not a string! */
00150         
00151         while (*ptr!='\"' && *ptr && ++len) if (*ptr++ == '\\') ptr++;  /* Skip escaped quotes. */
00152         
00153         out=static_cast<char*> (cJSON_malloc(len+1));   /* This is how long we need for the string, roughly. */
00154         if (!out) return 0;
00155         
00156         ptr=str+1;ptr2=out;
00157         while (*ptr!='\"' && *ptr)
00158         {
00159                 if (*ptr!='\\') *ptr2++=*ptr++;
00160                 else
00161                 {
00162                         ptr++;
00163                         switch (*ptr)
00164                         {
00165                                 case 'b': *ptr2++='\b'; break;
00166                                 case 'f': *ptr2++='\f'; break;
00167                                 case 'n': *ptr2++='\n'; break;
00168                                 case 'r': *ptr2++='\r'; break;
00169                                 case 't': *ptr2++='\t'; break;
00170                                 case 'u':        /* transcode utf16 to utf8. DOES NOT SUPPORT SURROGATE PAIRS CORRECTLY. */
00171                                         sscanf(ptr+1,"%4x",&uc);        /* get the unicode char. */
00172                                         len=3;if (uc<0x80) len=1;else if (uc<0x800) len=2;ptr2+=len;
00173                                         
00174                                         switch (len) {
00175                                                 case 3: *--ptr2 = static_cast<char>(( (uc) | 0x80) & 0xBF ); 
00176               uc >>= 6;
00177                                                 case 2: *--ptr2 = static_cast<char>(( (uc) | 0x80) & 0xBF );
00178               uc >>= 6;
00179                                                 case 1: *--ptr2 = static_cast<char>( (uc) | firstByteMark[len] );
00180                                         }
00181                                         ptr2+=len;ptr+=4;
00182                                         break;
00183                                 default:  *ptr2++=*ptr; break;
00184                         }
00185                         ptr++;
00186                 }
00187         }
00188         *ptr2=0;
00189         if (*ptr=='\"') ptr++;
00190         item->valuestring=out;
00191         item->type=cJSON_String;
00192         return ptr;
00193 }
00194 
00195 /* Render the cstring provided to an escaped version that can be printed. */
00196 static char *print_string_ptr(const char *str)
00197 {
00198         const char *ptr;char *ptr2,*out;int len=0;unsigned char token;
00199         
00200         if (!str) return cJSON_strdup("");
00201         ptr=str;while ((token=*ptr) && ++len) {if (strchr("\"\\\b\f\n\r\t",token)) len++; else if (token<32) len+=5;ptr++;}
00202         
00203         out=static_cast<char*>(cJSON_malloc(len+3));
00204         if (!out) return 0;
00205 
00206         ptr2=out;ptr=str;
00207         *ptr2++='\"';
00208         while (*ptr)
00209         {
00210                 if (static_cast<unsigned char>(*ptr)>31 && *ptr!='\"' && *ptr!='\\') *ptr2++=*ptr++;
00211                 else
00212                 {
00213                         *ptr2++='\\';
00214                         switch (token=*ptr++)
00215                         {
00216                                 case '\\':      *ptr2++='\\';   break;
00217                                 case '\"':      *ptr2++='\"';   break;
00218                                 case '\b':      *ptr2++='b';    break;
00219                                 case '\f':      *ptr2++='f';    break;
00220                                 case '\n':      *ptr2++='n';    break;
00221                                 case '\r':      *ptr2++='r';    break;
00222                                 case '\t':      *ptr2++='t';    break;
00223                                 default: sprintf(ptr2,"u%04x",token);ptr2+=5;   break;  /* escape and print */
00224                         }
00225                 }
00226         }
00227         *ptr2++='\"';*ptr2++=0;
00228         return out;
00229 }
00230 /* Invote print_string_ptr (which is useful) on an item. */
00231 static char *print_string(cJSON *item)  {return print_string_ptr(item->valuestring);}
00232 
00233 /* Predeclare these prototypes. */
00234 static const char *parse_value(cJSON *item,const char *value);
00235 static char *print_value(cJSON *item,int depth,int fmt);
00236 static const char *parse_array(cJSON *item,const char *value);
00237 static char *print_array(cJSON *item,int depth,int fmt);
00238 static const char *parse_object(cJSON *item,const char *value);
00239 static char *print_object(cJSON *item,int depth,int fmt);
00240 
00241 /* Utility to jump whitespace and cr/lf */
00242 static const char *skip(const char *in) {while (in && *in && static_cast<unsigned char>(*in)<=32) in++; return in;}
00243 
00244 /* Parse an object - create a new root, and populate. */
00245 cJSON *cJSON_Parse(const char *value)
00246 {
00247         cJSON *c=cJSON_New_Item();
00248         ep=0;
00249         if (!c) return 0;       /* memory fail */
00250 
00251         if (!parse_value(c,skip(value))) {cJSON_Delete(c);return 0;}
00252         return c;
00253 }
00254 
00255 /* Render a cJSON item/entity/structure to text. */
00256 char *cJSON_Print(cJSON *item)                          {return print_value(item,0,1);}
00257 char *cJSON_PrintUnformatted(cJSON *item)       {return print_value(item,0,0);}
00258 
00259 void cJSON_PrintStr(cJSON *item, std::string& s)
00260 {
00261         char* c = cJSON_Print(item);
00262         s.assign(c);
00263         free(c);
00264 }
00265 
00266 void cJSON_PrintUnformattedStr(cJSON *item, std::string& s)
00267 {
00268         char* c = cJSON_PrintUnformatted(item);
00269         s.assign(c);
00270         free(c);
00271 }
00272 
00273 /* Parser core - when encountering text, process appropriately. */
00274 static const char *parse_value(cJSON *item,const char *value)
00275 {
00276         if (!value)                                             return 0;       /* Fail on null. */
00277         if (!strncmp(value,"null",4))   { item->type=cJSON_NULL;  return value+4; }
00278         if (!strncmp(value,"false",5))  { item->type=cJSON_False; return value+5; }
00279         if (!strncmp(value,"true",4))   { item->type=cJSON_True; item->valueint=1;      return value+4; }
00280         if (*value=='\"')                               { return parse_string(item,value); }
00281         if (*value=='-' || (*value>='0' && *value<='9'))        { return parse_number(item,value); }
00282         if (*value=='[')                                { return parse_array(item,value); }
00283         if (*value=='{')                                { return parse_object(item,value); }
00284 
00285         ep=value;return 0;      /* failure. */
00286 }
00287 
00288 /* Render a value to text. */
00289 static char *print_value(cJSON *item,int depth,int fmt)
00290 {
00291         char *out=0;
00292         if (!item) return 0;
00293         switch ((item->type)&255)
00294         {
00295                 case cJSON_NULL:        out=cJSON_strdup("null");       break;
00296                 case cJSON_False:       out=cJSON_strdup("false");break;
00297                 case cJSON_True:        out=cJSON_strdup("true"); break;
00298                 case cJSON_Number:      out=print_number(item);break;
00299                 case cJSON_String:      out=print_string(item);break;
00300                 case cJSON_Array:       out=print_array(item,depth,fmt);break;
00301                 case cJSON_Object:      out=print_object(item,depth,fmt);break;
00302         }
00303         return out;
00304 }
00305 
00306 /* Build an array from input text. */
00307 static const char *parse_array(cJSON *item,const char *value)
00308 {
00309         cJSON *child;
00310         if (*value!='[')        {ep=value;return 0;}    /* not an array! */
00311 
00312         item->type=cJSON_Array;
00313         value=skip(value+1);
00314         if (*value==']') return value+1;        /* empty array. */
00315 
00316         item->child=child=cJSON_New_Item();
00317         if (!item->child) return 0;              /* memory fail */
00318         value=skip(parse_value(child,skip(value)));     /* skip any spacing, get the value. */
00319         if (!value) return 0;
00320 
00321         while (*value==',')
00322         {
00323                 cJSON *new_item;
00324                 if (!(new_item=cJSON_New_Item())) return 0;     /* memory fail */
00325                 child->next=new_item;new_item->prev=child;child=new_item;
00326                 value=skip(parse_value(child,skip(value+1)));
00327                 if (!value) return 0;   /* memory fail */
00328         }
00329 
00330         if (*value==']') return value+1;        /* end of array */
00331         ep=value;return 0;      /* malformed. */
00332 }
00333 
00334 /* Render an array to text */
00335 static char *print_array(cJSON *item,int depth,int fmt)
00336 {
00337         char **entries;
00338         char *out=0,*ptr,*ret;size_t len=5;
00339         cJSON *child=item->child;
00340         int numentries=0,i=0,fail=0;
00341         
00342         /* How many entries in the array? */
00343         while (child) numentries++,child=child->next;
00344         /* Allocate an array to hold the values for each */
00345         entries=static_cast<char**>(cJSON_malloc(numentries*sizeof(char*)));
00346         if (!entries) return 0;
00347         memset(entries,0,numentries*sizeof(char*));
00348         /* Retrieve all the results: */
00349         child=item->child;
00350         while (child && !fail)
00351         {
00352                 ret=print_value(child,depth+1,fmt);
00353                 entries[i++]=ret;
00354                 if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1;
00355                 child=child->next;
00356         }
00357         
00358         /* If we didn't fail, try to malloc the output string */
00359         if (!fail) out=static_cast<char*>(cJSON_malloc(len));
00360         /* If that fails, we fail. */
00361         if (!out) fail=1;
00362 
00363         /* Handle failure. */
00364         if (fail)
00365         {
00366                 for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]);
00367                 cJSON_free(entries);
00368                 return 0;
00369         }
00370         
00371         /* Compose the output array. */
00372         *out='[';
00373         ptr=out+1;*ptr=0;
00374         for (i=0;i<numentries;i++)
00375         {
00376                 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
00377                 if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;}
00378                 cJSON_free(entries[i]);
00379         }
00380         cJSON_free(entries);
00381         *ptr++=']';*ptr++=0;
00382         return out;     
00383 }
00384 
00385 /* Build an object from the text. */
00386 static const char *parse_object(cJSON *item,const char *value)
00387 {
00388         cJSON *child;
00389         if (*value!='{')        {ep=value;return 0;}    /* not an object! */
00390         
00391         item->type=cJSON_Object;
00392         value=skip(value+1);
00393         if (*value=='}') return value+1;        /* empty array. */
00394         
00395         item->child=child=cJSON_New_Item();
00396         if (!item->child) return 0;
00397         value=skip(parse_string(child,skip(value)));
00398         if (!value) return 0;
00399         child->string=child->valuestring;child->valuestring=0;
00400         if (*value!=':') {ep=value;return 0;}   /* fail! */
00401         value=skip(parse_value(child,skip(value+1)));   /* skip any spacing, get the value. */
00402         if (!value) return 0;
00403         
00404         while (*value==',')
00405         {
00406                 cJSON *new_item;
00407                 if (!(new_item=cJSON_New_Item()))       return 0; /* memory fail */
00408                 child->next=new_item;new_item->prev=child;child=new_item;
00409                 value=skip(parse_string(child,skip(value+1)));
00410                 if (!value) return 0;
00411                 child->string=child->valuestring;child->valuestring=0;
00412                 if (*value!=':') {ep=value;return 0;}   /* fail! */
00413                 value=skip(parse_value(child,skip(value+1)));   /* skip any spacing, get the value. */
00414                 if (!value) return 0;
00415         }
00416         
00417         if (*value=='}') return value+1;        /* end of array */
00418         ep=value;return 0;      /* malformed. */
00419 }
00420 
00421 /* Render an object to text. */
00422 static char *print_object(cJSON *item,int depth,int fmt)
00423 {
00424         char **entries=0,**names=0;
00425         char *out=0,*ptr,*ret,*str;size_t len=7,i=0,j;
00426         cJSON *child=item->child;
00427         size_t numentries=0,fail=0;
00428         /* Count the number of entries. */
00429         while (child) numentries++,child=child->next;
00430         /* Allocate space for the names and the objects */
00431         entries=static_cast<char**>(cJSON_malloc(numentries*sizeof(char*)));
00432         if (!entries) return 0;
00433         names=static_cast<char**>(cJSON_malloc(numentries*sizeof(char*)));
00434         if (!names) {cJSON_free(entries);return 0;}
00435         memset(entries,0,sizeof(char*)*numentries);
00436         memset(names,0,sizeof(char*)*numentries);
00437 
00438         /* Collect all the results into our arrays: */
00439         child=item->child;depth++;if (fmt) len+=depth;
00440         while (child)
00441         {
00442                 names[i]=str=print_string_ptr(child->string);
00443                 entries[i++]=ret=print_value(child,depth,fmt);
00444                 if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
00445                 child=child->next;
00446         }
00447         
00448         /* Try to allocate the output string */
00449         if (!fail) out=static_cast<char*> (cJSON_malloc(len));
00450         if (!out) fail=1;
00451 
00452         /* Handle failure */
00453         if (fail)
00454         {
00455                 for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
00456                 cJSON_free(names);cJSON_free(entries);
00457                 return 0;
00458         }
00459         
00460         /* Compose the output: */
00461         *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
00462         for (i=0;i<numentries;i++)
00463         {
00464                 if (fmt) for (j=0;j<static_cast<size_t> (depth);j++) *ptr++='\t';
00465                 strcpy(ptr,names[i]);ptr+=strlen(names[i]);
00466                 *ptr++=':';if (fmt) *ptr++='\t';
00467                 strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
00468                 if (i!=numentries-1) *ptr++=',';
00469                 if (fmt) *ptr++='\n';*ptr=0;
00470                 cJSON_free(names[i]);cJSON_free(entries[i]);
00471         }
00472         
00473         cJSON_free(names);cJSON_free(entries);
00474         if (fmt) for (i=0;i<static_cast<size_t>(depth-1);i++) *ptr++='\t';
00475         *ptr++='}';*ptr++=0;
00476         return out;     
00477 }
00478 
00479 /* Get Array size/item / object item. */
00480 int    cJSON_GetArraySize(cJSON *array)                                                 {cJSON *c=array->child;int i=0;while(c)i++,c=c->next;return i;}
00481 cJSON *cJSON_GetArrayItem(cJSON *array,int item)                                {cJSON *c=array->child;  while (c && item>0) item--,c=c->next; return c;}
00482 cJSON *cJSON_GetObjectItem(cJSON *object,const char *string)    {cJSON *c=object->child; while (c && cJSON_strcasecmp(c->string,string)) c=c->next; return c;}
00483 
00484 /* Utility for array list handling. */
00485 static void suffix_object(cJSON *prev,cJSON *item) {prev->next=item;item->prev=prev;}
00486 /* Utility for handling references. */
00487 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;}
00488 
00489 /* Add item to array/object. */
00490 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);}}
00491 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);}
00492 void    cJSON_AddItemReferenceToArray(cJSON *array, cJSON *item)                                                {cJSON_AddItemToArray(array,create_reference(item));}
00493 void    cJSON_AddItemReferenceToObject(cJSON *object,const char *string,cJSON *item)    {cJSON_AddItemToObject(object,string,create_reference(item));}
00494 
00495 cJSON *cJSON_DetachItemFromArray(cJSON *array,int which)                        {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return 0;
00496         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;}
00497 void   cJSON_DeleteItemFromArray(cJSON *array,int which)                        {cJSON_Delete(cJSON_DetachItemFromArray(array,which));}
00498 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;}
00499 void   cJSON_DeleteItemFromObject(cJSON *object,const char *string) {cJSON_Delete(cJSON_DetachItemFromObject(object,string));}
00500 
00501 /* Replace array/object items with new ones. */
00502 void   cJSON_ReplaceItemInArray(cJSON *array,int which,cJSON *newitem)          {cJSON *c=array->child;while (c && which>0) c=c->next,which--;if (!c) return;
00503         newitem->next=c->next;newitem->prev=c->prev;if (newitem->next) newitem->next->prev=newitem;
00504         if (c==array->child) array->child=newitem; else newitem->prev->next=newitem;c->next=c->prev=0;cJSON_Delete(c);}
00505 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);}}
00506 
00507 /* Create basic types: */
00508 cJSON *cJSON_CreateNull()                                               {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_NULL;return item;}
00509 cJSON *cJSON_CreateTrue()                                               {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_True;return item;}
00510 cJSON *cJSON_CreateFalse()                                              {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_False;return item;}
00511 cJSON *cJSON_CreateBool(int b)                                  {cJSON *item=cJSON_New_Item();if(item)item->type=b?cJSON_True:cJSON_False;return item;}
00512 cJSON *cJSON_CreateNumber(double num)                   {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_Number;item->valuedouble=num;item->valueint=static_cast<int>(num);}return item;}
00513 cJSON *cJSON_CreateString(const char *string)   {cJSON *item=cJSON_New_Item();if(item){item->type=cJSON_String;item->valuestring=cJSON_strdup(string);}return item;}
00514 cJSON *cJSON_CreateArray()                                              {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Array;return item;}
00515 cJSON *cJSON_CreateObject()                                             {cJSON *item=cJSON_New_Item();if(item)item->type=cJSON_Object;return item;}
00516 
00517 /* Create Arrays: */
00518 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;}
00519 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;}
00520 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;}
00521 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;}


pcl
Author(s): Open Perception
autogenerated on Wed Aug 26 2015 15:22:36