|
@ -327,28 +327,32 @@ static const char *parse_array(cJSON *item,const char *value) |
|
|
/* Render an array to text */ |
|
|
/* Render an array to text */ |
|
|
static char *print_array(cJSON *item,int depth,int fmt) |
|
|
static char *print_array(cJSON *item,int depth,int fmt) |
|
|
{ |
|
|
{ |
|
|
char **entries=0; |
|
|
char **entries; |
|
|
char *out=0,*ptr,*ret;int len=5; |
|
|
char *out=0,*ptr,*ret;int len=5; |
|
|
cJSON *child=item->child; |
|
|
cJSON *child=item->child; |
|
|
int numentries=0,i=0,fail=0; |
|
|
int numentries=0,i=0,fail=0; |
|
|
|
|
|
|
|
|
/* How many entries in the array? */ |
|
|
/* How many entries in the array? */ |
|
|
while (child) numentries++,child=child->next; |
|
|
while (child) numentries++,child=child->next; |
|
|
if (numentries) |
|
|
/* Explicitly handle numentries==0 */ |
|
|
|
|
|
if (!numentries) |
|
|
{ |
|
|
{ |
|
|
/* Allocate an array to hold the values for each */ |
|
|
out=(char*)cJSON_malloc(3); |
|
|
entries=(char**)cJSON_malloc(numentries*sizeof(char*)); |
|
|
if (out) strcpy(out,"[]"); |
|
|
if (!entries) return 0; |
|
|
return out; |
|
|
memset(entries,0,numentries*sizeof(char*)); |
|
|
} |
|
|
/* Retrieve all the results: */ |
|
|
/* Allocate an array to hold the values for each */ |
|
|
child=item->child; |
|
|
entries=(char**)cJSON_malloc(numentries*sizeof(char*)); |
|
|
while (child && !fail) |
|
|
if (!entries) return 0; |
|
|
{ |
|
|
memset(entries,0,numentries*sizeof(char*)); |
|
|
ret=print_value(child,depth+1,fmt); |
|
|
/* Retrieve all the results: */ |
|
|
entries[i++]=ret; |
|
|
child=item->child; |
|
|
if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; |
|
|
while (child && !fail) |
|
|
child=child->next; |
|
|
{ |
|
|
} |
|
|
ret=print_value(child,depth+1,fmt); |
|
|
|
|
|
entries[i++]=ret; |
|
|
|
|
|
if (ret) len+=strlen(ret)+2+(fmt?1:0); else fail=1; |
|
|
|
|
|
child=child->next; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/* If we didn't fail, try to malloc the output string */ |
|
|
/* If we didn't fail, try to malloc the output string */ |
|
@ -360,7 +364,7 @@ static char *print_array(cJSON *item,int depth,int fmt) |
|
|
if (fail) |
|
|
if (fail) |
|
|
{ |
|
|
{ |
|
|
for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]); |
|
|
for (i=0;i<numentries;i++) if (entries[i]) cJSON_free(entries[i]); |
|
|
if (entries) cJSON_free(entries); |
|
|
cJSON_free(entries); |
|
|
return 0; |
|
|
return 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -373,7 +377,7 @@ static char *print_array(cJSON *item,int depth,int fmt) |
|
|
if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;} |
|
|
if (i!=numentries-1) {*ptr++=',';if(fmt)*ptr++=' ';*ptr=0;} |
|
|
cJSON_free(entries[i]); |
|
|
cJSON_free(entries[i]); |
|
|
} |
|
|
} |
|
|
if (entries) cJSON_free(entries); |
|
|
cJSON_free(entries); |
|
|
*ptr++=']';*ptr++=0; |
|
|
*ptr++=']';*ptr++=0; |
|
|
return out; |
|
|
return out; |
|
|
} |
|
|
} |
|
@ -423,6 +427,16 @@ static char *print_object(cJSON *item,int depth,int fmt) |
|
|
int numentries=0,fail=0; |
|
|
int numentries=0,fail=0; |
|
|
/* Count the number of entries. */ |
|
|
/* Count the number of entries. */ |
|
|
while (child) numentries++,child=child->next; |
|
|
while (child) numentries++,child=child->next; |
|
|
|
|
|
/* Explicitly handle empty object case */ |
|
|
|
|
|
if (!numentries) |
|
|
|
|
|
{ |
|
|
|
|
|
out=cJSON_malloc(fmt?depth+3:3); |
|
|
|
|
|
if (!out) return 0; |
|
|
|
|
|
ptr=out;*ptr++='{'; |
|
|
|
|
|
if (fmt) {*ptr++='\n';for (i=0;i<depth-1;i++) *ptr++='\t';} |
|
|
|
|
|
*ptr++='}';*ptr++=0; |
|
|
|
|
|
return out; |
|
|
|
|
|
} |
|
|
/* Allocate space for the names and the objects */ |
|
|
/* Allocate space for the names and the objects */ |
|
|
entries=(char**)cJSON_malloc(numentries*sizeof(char*)); |
|
|
entries=(char**)cJSON_malloc(numentries*sizeof(char*)); |
|
|
if (!entries) return 0; |
|
|
if (!entries) return 0; |
|
|