diff --git a/cJSON.c b/cJSON.c index 766e558ef06d4e97525a85f937aae4709f26d808..3dc6491b77508546911654c324c4a640186bbc1d 100644 --- a/cJSON.c +++ b/cJSON.c @@ -886,6 +886,16 @@ char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, cjbool fmt) return print_value(item, 0, fmt, &p); } +int cJSON_PrintMallocedBuffer(cJSON *item,char *buf,const int len, cjbool fmt) +{ + char *out; + printbuffer p; + p.buffer = buf; + p.length = len; + p.offset = 0; + out = print_value(item,0,fmt,&p); + return (out != buf ? -1 : 0); +} /* Parser core - when encountering text, process appropriately. */ static const char *parse_value(cJSON *item, const char *value, const char **ep) diff --git a/cJSON.h b/cJSON.h index dbbb739fae7efe4da088e9c1c1d1e19f55c6a1ee..1f1ed7b6e0fe9878a700211071b87da4d69ac6dc 100644 --- a/cJSON.h +++ b/cJSON.h @@ -83,6 +83,8 @@ extern char *cJSON_Print(const cJSON *item); extern char *cJSON_PrintUnformatted(const cJSON *item); /* Render a cJSON entity to text using a buffered strategy. prebuffer is a guess at the final size. guessing well reduces reallocation. fmt=0 gives unformatted, =1 gives formatted */ extern char *cJSON_PrintBuffered(const cJSON *item, int prebuffer, int fmt); +/* Render a cJSON entity to text using a buffer already allocated in memory with length buf_len */ +extern int cJSON_PrintMallocedBuffer(cJSON *item, char *buf, const int len, int fmt); /* Delete a cJSON entity and all subentities. */ extern void cJSON_Delete(cJSON *c);