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