Пример #1
0
/* Render an object to text. */
static char *print_object(cJSON *item,int depth,int fmt)
{
    char **entries=0,**names=0;
    char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
    cJSON *child=item->child;
    int numentries=0,fail=0;
    /* Count the number of entries. */
    while (child) numentries++,child=child->next;
    /* Allocate space for the names and the objects */
    entries=(char**)cJSON_malloc(numentries*sizeof(char*));
    if (!entries) return 0;
    names=(char**)cJSON_malloc(numentries*sizeof(char*));
    if (!names) {cJSON_free(entries);return 0;}
    memset(entries,0,sizeof(char*)*numentries);
    memset(names,0,sizeof(char*)*numentries);

    /* Collect all the results into our arrays: */
    child=item->child;depth++;if (fmt) len+=depth;
    while (child)
    {
        names[i]=str=print_string_ptr(child->string);
        entries[i++]=ret=print_value(child,depth,fmt);
        if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
        child=child->next;
    }
    
    /* Try to allocate the output string */
    if (!fail) out=(char*)cJSON_malloc(len);
    if (!out) fail=1;

    /* Handle failure */
    if (fail)
    {
        for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
        cJSON_free(names);cJSON_free(entries);
        return 0;
    }
    
    /* Compose the output: */
    *out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
    for (i=0;i<numentries;i++)
    {
        if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
        strcpy(ptr,names[i]);ptr+=strlen(names[i]);
        *ptr++=':';if (fmt) *ptr++='\t';
        strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
        if (i!=numentries-1) *ptr++=',';
        if (fmt) {
            *ptr++='\n';
            *ptr=0;
        }
        cJSON_free(names[i]);
        cJSON_free(entries[i]);
    }
    
    cJSON_free(names);cJSON_free(entries);
    if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
    *ptr++='}';*ptr++=0;
    return out;    
}
Пример #2
0
static char *print_binary(cJSON *item)  {
  char* b64;
  int  size=base64_encode_alloc( item->valuedata, item->valuesize, &b64 );
  char* out = print_string_ptr( b64 );
  free( b64 );
  return out;
}
Пример #3
0
/* Render an object to text. */
static char *print_object(cJSON *item,int depth,int fmt,cJSON_Buf* buf)
{
	int j=0;
	cJSON *child=item->child;
	depth++;
	if(!cJSON_Buf_Copy_Char(buf,'{'))return 0;
	if(fmt&&!cJSON_Buf_Copy_Char(buf,'\n'))return 0;
	while (child)
	{
		for (j=0;fmt&&j<depth;j++) if(!cJSON_Buf_Copy_Char(buf,'\t'))return 0;
		if(!print_string_ptr(child->string,buf))return 0;
		if(!cJSON_Buf_Copy_Char(buf,':'))return 0;
		if(fmt&&!cJSON_Buf_Copy_Char(buf,'\t'))return 0;
		if(!print_value(child,depth,fmt,buf))return 0;
		if (child->next && !cJSON_Buf_Copy_Char(buf,','))return 0;
		if(fmt&&!cJSON_Buf_Copy_Char(buf,'\n'))return 0;
		
		child=child->next;
	}
	if(!cJSON_Buf_Copy_Char(buf,'}'))return 0;
	return buf->buf;	
}
Пример #4
0
/* Invote print_string_ptr (which is useful) on an item. */
static char *ICACHE_FLASH_ATTR
print_string(cJSON *item)
{
    return print_string_ptr(item->valuestring);
}
Пример #5
0
/// Internal: Render an object to text.
static char *print_object(JSON *item, int depth, bool fmt)
{
    char **entries = 0, **names = 0;
    char *out = 0, *ptr, *ret, *str;
    int len=7, i=0;

    JSON *child = item->firstchild;
    int numentries=0, fail=0;
    // Count the number of entries.
    while (child) numentries++, child = child->next;

    if(!numentries) // Explicitly handle empty object case
    {
        out=new char[fmt?depth+4: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
    entries = new char *[numentries*sizeof(char*)];
    if(!entries) return 0;
    names = new char *[numentries*sizeof(char*)];

    memset(entries, 0, sizeof(char*)*numentries);
    memset(names, 0, sizeof(char*)*numentries);

    // Collect all the results into our arrays:
    child = item->firstchild;
    depth++;
    if(fmt) len+=depth;
    while (child)
    {
        names[i] = str = print_string_ptr(child->name);
        entries[i++]= ret = print_value(child, depth, fmt);
        if(str && ret) len+= strlen(ret) + strlen(str) + 2 + (fmt ? depth+2 : 0);
        else fail=1;
        child = child->next;
    }

    // Try to allocate the output string
    if(!fail) out = new char[len];

    // Handle failure
    if(fail)
    {
        loopi(numentries) { delete[] names[i]; delete[] entries[i];}
        delete[] names;
        delete[] entries;
        return 0;
    }

    // Compose the output:
    *out = '{'; ptr = out+1;
    if(fmt) *ptr++ = '\n'; *ptr = 0;
    loopi(numentries)
    {
        if(fmt) loopj(depth) *ptr++ = '\t';
        strcpy(ptr, names[i]);
        ptr += strlen(names[i]);
        *ptr++=':'; if(fmt) *ptr++ = '\t';

        strcpy(ptr, entries[i]); ptr += strlen(entries[i]);
        if(i != numentries-1) *ptr++ = ',';
        if(fmt) *ptr++='\n'; *ptr = 0;
        delete[] names[i];
        delete[] entries[i];
    }

    delete[] names;
    delete[] entries;
    if(fmt) loopi(depth-1) *ptr++='\t';
    *ptr++='}';*ptr++=0;
    return out;
}
Пример #6
0
/// Internal: Invote print_string_ptr (which is useful) on an item.
static char *print_string(JSON *item) { return print_string_ptr(item->valuestring); }
Пример #7
0
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(struct json *item)
{
	return print_string_ptr(item->valuestring);
}
Пример #8
0
/* Render an object to text. */
static char *print_object(cJSON *item,int depth,int fmt,printbuffer *p)
{
	char **entries=0,**names=0;
	char *out=0,*ptr,*ret,*str;int len=7,i=0,j;
	cJSON *child=item->child;
	int numentries=0,fail=0;
	size_t tmplen=0;
	/* Count the number of entries. */
	while (child) numentries++,child=child->next;
	/* Explicitly handle empty object case */
	if (!numentries)
	{
		if (p) out=ensure(p,fmt?depth+4:3);
		else	out=(char*)cJSON_malloc(fmt?depth+4: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;
	}
	if (p)
	{
		/* Compose the output: */
		i=p->offset;
		len=fmt?2:1;	ptr=ensure(p,len+1);	if (!ptr) return 0;
		*ptr++='{';	if (fmt) *ptr++='\n';	*ptr=0;	p->offset+=len;
		child=item->child;depth++;
		while (child)
		{
			if (fmt)
			{
				ptr=ensure(p,depth);	if (!ptr) return 0;
				for (j=0;j<depth;j++) *ptr++='\t';
				p->offset+=depth;
			}
			print_string_ptr(child->string,p);
			p->offset=update(p);
			
			len=fmt?2:1;
			ptr=ensure(p,len);	if (!ptr) return 0;
			*ptr++=':';if (fmt) *ptr++='\t';
			p->offset+=len;
			
			print_value(child,depth,fmt,p);
			p->offset=update(p);

			len=(fmt?1:0)+(child->next?1:0);
			ptr=ensure(p,len+1); if (!ptr) return 0;
			if (child->next) *ptr++=',';
			if (fmt) *ptr++='\n';*ptr=0;
			p->offset+=len;
			child=child->next;
		}
		ptr=ensure(p,fmt?(depth+1):2);	 if (!ptr) return 0;
		if (fmt)	for (i=0;i<depth-1;i++) *ptr++='\t';
		*ptr++='}';*ptr=0;
		out=(p->buffer)+i;
	}
	else
	{
		/* Allocate space for the names and the objects */
		entries=(char**)cJSON_malloc(numentries*sizeof(char*));
		if (!entries) return 0;
		names=(char**)cJSON_malloc(numentries*sizeof(char*));
		if (!names) {cJSON_free(entries);return 0;}
		memset(entries,0,sizeof(char*)*numentries);
		memset(names,0,sizeof(char*)*numentries);

		/* Collect all the results into our arrays: */
		child=item->child;depth++;if (fmt) len+=depth;
		while (child)
		{
			names[i]=str=print_string_ptr(child->string,0);
			entries[i++]=ret=print_value(child,depth,fmt,0);
			if (str && ret) len+=strlen(ret)+strlen(str)+2+(fmt?2+depth:0); else fail=1;
			child=child->next;
		}
		
		/* Try to allocate the output string */
		if (!fail)	out=(char*)cJSON_malloc(len);
		if (!out) fail=1;

		/* Handle failure */
		if (fail)
		{
			for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
			cJSON_free(names);cJSON_free(entries);
			return 0;
		}
		
		/* Compose the output: */
		*out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
		for (i=0;i<numentries;i++)
		{
			if (fmt) for (j=0;j<depth;j++) *ptr++='\t';
			tmplen=strlen(names[i]);memcpy(ptr,names[i],tmplen);ptr+=tmplen;
			*ptr++=':';if (fmt) *ptr++='\t';
			strcpy(ptr,entries[i]);ptr+=strlen(entries[i]);
			if (i!=numentries-1) *ptr++=',';
			if (fmt) *ptr++='\n';*ptr=0;
			cJSON_free(names[i]);cJSON_free(entries[i]);
		}
		
		cJSON_free(names);cJSON_free(entries);
		if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
		*ptr++='}';*ptr++=0;
	}
	return out;	
}
Пример #9
0
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(cJSON *item,printbuffer *p)	{return print_string_ptr(item->valuestring,p);}
Пример #10
0
/* Render an object to text. */
static char *print_object(cJSON *item,int depth,int fmt)
{
	char **entries=0,**names=0;
	char *out=0,*ptr,*ret,*str;
	int len=1024,i=0,j;
	int  num_size = 0;
	cJSON *child=item->child;
	int numentries=0,fail=0;

	//return 0;
	/* Count the number of entries. */
	while (child) 
	{
		numentries++;
		child=child->next;
	}

	/* Explicitly handle empty object case */
	if (!numentries)
	{		

		out=(char*)cJSON_malloc(fmt?depth+4: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;
		
	}
	
	num_size = numentries*sizeof(char*);

	/* Allocate space for the names and the objects */
	entries=(char**)cJSON_malloc(num_size);
	if (!entries) return 0;

	names=(char**)cJSON_malloc(num_size);
	if (!names) 
	{
		cJSON_free(entries);
		return 0;
	}
	
	//debug code 
	//if(depth >1 )
	//{
		//_hx_printf("entries=%X,names=%X",entries,names);
		//return 0;
	//}
	
	//mymemset(entries,0,num_size);
	//mymemset(names,0,num_size);
	//return 0;
	
	
	/* Collect all the results into our arrays: */
	child = item->child; 
	depth ++;
	if (fmt) 
	{
		len+=depth;
	}
		
	while (child)
	{
		names[i]        = str = print_string_ptr(child->string);
		//debug code 
		//_hx_printf("numentries=%d,i=%d,fmt=%d,depth=%d,type=%d",numentries,i,fmt,depth,(child->type)&255);
		entries[i]    = ret = print_value(child,depth,fmt);
		i++;

		if (str && ret) 
		{
			len += mystrlen(ret)+mystrlen(str)+2+(fmt?2+depth:0); 
		}
		else 
		{
			fail=1;
		}

		child=child->next;
		
	}
	
	//return 0;
	
	/* Try to allocate the output string */
	if (!fail) out=(char*)cJSON_malloc(len);
	if (!out) fail=1;

	/* Handle failure */
	if (fail)
	{
		for (i=0;i<numentries;i++) {if (names[i]) cJSON_free(names[i]);if (entries[i]) cJSON_free(entries[i]);}
		cJSON_free(names);cJSON_free(entries);
		return 0;
	}
	
	
	/* Compose the output: */
	*out='{';ptr=out+1;if (fmt)*ptr++='\n';*ptr=0;
	for (i=0;i<numentries;i++)
	{
		if (fmt) 
		{
			//for (j=0;j<depth;j++) 
			//{
			//	*ptr++ ='\t';
			//}
		}
		
		mystrcpy(ptr,names[i]);ptr+=mystrlen(names[i]);
		*ptr++=':';if (fmt) *ptr++='\t';
		
		mystrcpy(ptr,entries[i]);ptr+=mystrlen(entries[i]);
		
		if (i!=numentries-1) *ptr++=',';
		if (fmt) *ptr++='\n';*ptr=0;
		
		cJSON_free(names[i]);cJSON_free(entries[i]);
	}
	
	//return 0;
	cJSON_free(names);cJSON_free(entries);
	if (fmt) for (i=0;i<depth-1;i++) *ptr++='\t';
	*ptr++='}';*ptr++=0;

	
	return out;	
}
Пример #11
0
/* Render an object to text. */
static char *print_object(const cJSON *item, int depth, cjbool fmt, printbuffer *p)
{
    char **entries = NULL;
    char **names = NULL;
    char *out = NULL;
    char *ptr = NULL;
    char *ret = NULL;
    char *str = NULL;
    int len = 7;
    int i = 0;
    int j = 0;
    cJSON *child = item->child;
    int numentries = 0;
    cjbool fail = false;
    size_t tmplen = 0;

    /* Count the number of entries. */
    while (child)
    {
        numentries++;
        child = child->next;
    }

    /* Explicitly handle empty object case */
    if (!numentries)
    {
        if (p)
        {
            out = ensure(p, fmt ? depth + 4 : 3);
        }
        else
        {
            out = (char*)cJSON_malloc(fmt ? depth + 4 : 3);
        }
        if (!out)
        {
            return NULL;
        }
        ptr = out;
        *ptr++ = '{';
        if (fmt) {
            *ptr++ = '\n';
            for (i = 0; i < depth; i++)
            {
                *ptr++ = '\t';
            }
        }
        *ptr++ = '}';
        *ptr++ = '\0';

        return out;
    }

    if (p)
    {
        /* Compose the output: */
        i = p->offset;
        len = fmt ? 2 : 1; /* fmt: {\n */
        ptr = ensure(p, len + 1);
        if (!ptr)
        {
            return NULL;
        }

        *ptr++ = '{';
        if (fmt)
        {
            *ptr++ = '\n';
        }
        *ptr = '\0';
        p->offset += len;

        child = item->child;
        depth++;
        while (child)
        {
            if (fmt)
            {
                ptr = ensure(p, depth);
                if (!ptr)
                {
                    return NULL;
                }
                for (j = 0; j < depth; j++)
                {
                    *ptr++ = '\t';
                }
                p->offset += depth;
            }

            /* print key */
            if (!print_string_ptr(child->string, p))
            {
                return NULL;
            }
            p->offset = update(p);

            len = fmt ? 2 : 1;
            ptr = ensure(p, len);
            if (!ptr)
            {
                return NULL;
            }
            *ptr++ = ':';
            if (fmt)
            {
                *ptr++ = '\t';
            }
            p->offset+=len;

            /* print value */
            if (!print_value(child, depth, fmt, p))
            {
                return NULL;
            };
            p->offset = update(p);

            /* print comma if not last */
            len = (fmt ? 1 : 0) + (child->next ? 1 : 0);
            ptr = ensure(p, len + 1);
            if (!ptr)
            {
                return NULL;
            }
            if (child->next)
            {
                *ptr++ = ',';
            }

            if (fmt)
            {
                *ptr++ = '\n';
            }
            *ptr = '\0';
            p->offset += len;

            child = child->next;
        }

        ptr = ensure(p, fmt ? (depth + 1) : 2);
        if (!ptr)
        {
            return NULL;
        }
        if (fmt)
        {
            for (i = 0; i < (depth - 1); i++)
            {
                *ptr++ = '\t';
            }
        }
        *ptr++ = '}';
        *ptr = '\0';
        out = (p->buffer) + i;
    }
    else
    {
        /* Allocate space for the names and the objects */
        entries = (char**)cJSON_malloc(numentries * sizeof(char*));
        if (!entries)
        {
            return NULL;
        }
        names = (char**)cJSON_malloc(numentries * sizeof(char*));
        if (!names)
        {
            cJSON_free(entries);
            return NULL;
        }
        memset(entries, '\0', sizeof(char*) * numentries);
        memset(names, '\0', sizeof(char*) * numentries);

        /* Collect all the results into our arrays: */
        child = item->child;
        depth++;
        if (fmt)
        {
            len += depth;
        }
        while (child && !fail)
        {
            names[i] = str = print_string_ptr(child->string, 0); /* print key */
            entries[i++] = ret = print_value(child, depth, fmt, 0);
            if (str && ret)
            {
                len += strlen(ret) + strlen(str) + 2 + (fmt ? 2 + depth : 0);
            }
            else
            {
                fail = true;
            }
            child = child->next;
        }

        /* Try to allocate the output string */
        if (!fail)
        {
            out = (char*)cJSON_malloc(len);
        }
        if (!out)
        {
            fail = true;
        }

        /* Handle failure */
        if (fail)
        {
            /* free all the printed keys and values */
            for (i = 0; i < numentries; i++)
            {
                if (names[i])
                {
                    cJSON_free(names[i]);
                }
                if (entries[i])
                {
                    cJSON_free(entries[i]);
                }
            }
            cJSON_free(names);
            cJSON_free(entries);
            return NULL;
        }

        /* Compose the output: */
        *out = '{';
        ptr = out + 1;
        if (fmt)
        {
            *ptr++ = '\n';
        }
        *ptr = '\0';
        for (i = 0; i < numentries; i++)
        {
            if (fmt)
            {
                for (j = 0; j < depth; j++)
                {
                    *ptr++='\t';
                }
            }
            tmplen = strlen(names[i]);
            memcpy(ptr, names[i], tmplen);
            ptr += tmplen;
            *ptr++ = ':';
            if (fmt)
            {
                *ptr++ = '\t';
            }
            strcpy(ptr, entries[i]);
            ptr += strlen(entries[i]);
            if (i != (numentries - 1))
            {
                *ptr++ = ',';
            }
            if (fmt)
            {
                *ptr++ = '\n';
            }
            *ptr = '\0';
            cJSON_free(names[i]);
            cJSON_free(entries[i]);
        }

        cJSON_free(names);
        cJSON_free(entries);
        if (fmt)
        {
            for (i = 0; i < (depth - 1); i++)
            {
                *ptr++ = '\t';
            }
        }
        *ptr++ = '}';
        *ptr++ = '\0';
    }

    return out;
}
Пример #12
0
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(srjson_doc_t *doc, srjson_t *item) {
    return print_string_ptr(doc, item->valuestring);
}
Пример #13
0
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(shjson_t *item)	{return print_string_ptr(item->valuestring);}
Пример #14
0
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(cJSON *item, ngx_pool_t *pool)	{return print_string_ptr(item->valuestring, pool);}
Пример #15
0
/* Invote print_string_ptr (which is useful) on an item. */
static char *print_string(cJSON *item,cJSON_Buf* buf)	{return print_string_ptr(item->valuestring,buf);}
Пример #16
0
/* Invote print_string_ptr (which is useful) on an item. */
static char *
print_string(cJSON *item)
{
    return print_string_ptr(item->valuestring, item->type == cJSON_String);
}