1111

Committer:
TaylorGy
Date:
Wed Mar 29 03:05:19 2017 +0000
Revision:
0:c809010834e2
Child:
1:9a1a30e27d1d
IoT(Internet of Thing) Demo for ASC Platform. ; Performed an example for getting data form Sensors,  then send data to OneNet Cloud by using EDP protocol.; Use NUCLEO-F411RE and Grove_UART_wifi module(based on ESP8266), and other Grove_sonsers.

Who changed what in which revision?

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