From 06008b0444d25f4a3f032a9c1c297e8eb2f0cd69 Mon Sep 17 00:00:00 2001 From: Jiri Zouhar Date: Mon, 14 Nov 2016 11:20:10 +0100 Subject: [PATCH 1/6] add support to insert raw json --- cJSON.c | 27 +++++++++++++++++++++++++++ cJSON.h | 6 +++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/cJSON.c b/cJSON.c index ae70fc6..36addb3 100644 --- a/cJSON.c +++ b/cJSON.c @@ -960,6 +960,13 @@ static char *print_value(const cJSON *item, int depth, int fmt, printbuffer *p) case cJSON_Number: out = print_number(item, p); break; + case cJSON_Raw: + out = ensure(p, strlen(item->valuestring)); + if (out) + { + strcpy(out, item->valuestring); + } + break; case cJSON_String: out = print_string(item, p); break; @@ -987,6 +994,9 @@ static char *print_value(const cJSON *item, int depth, int fmt, printbuffer *p) case cJSON_Number: out = print_number(item, 0); break; + case cJSON_Raw: + out = cJSON_strdup(item->valuestring); + break; case cJSON_String: out = print_string(item, 0); break; @@ -1947,6 +1957,23 @@ cJSON *cJSON_CreateString(const char *string) return item; } +extern cJSON *cJSON_CreateRaw(const char *raw) +{ + cJSON *item = cJSON_New_Item(); + if(item) + { + item->type = cJSON_Raw; + item->valuestring = cJSON_strdup(raw); + if(!item->valuestring) + { + cJSON_Delete(item); + return 0; + } + } + + return item; +} + cJSON *cJSON_CreateArray(void) { cJSON *item = cJSON_New_Item(); diff --git a/cJSON.h b/cJSON.h index c2b8d57..538d05e 100644 --- a/cJSON.h +++ b/cJSON.h @@ -36,6 +36,7 @@ extern "C" #define cJSON_String (1 << 4) #define cJSON_Array (1 << 5) #define cJSON_Object (1 << 6) +#define cJSON_Raw (1 << 7) //< raw json #define cJSON_IsReference 256 #define cJSON_StringIsConst 512 @@ -52,7 +53,7 @@ typedef struct cJSON /* The type of the item, as above. */ int type; - /* The item's string, if type==cJSON_String */ + /* The item's string, if type==cJSON_String and type == cJSON_Raw */ char *valuestring; /* The item's number, if type==cJSON_Number */ int valueint; @@ -101,6 +102,8 @@ extern cJSON *cJSON_CreateFalse(void); extern cJSON *cJSON_CreateBool(int b); extern cJSON *cJSON_CreateNumber(double num); extern cJSON *cJSON_CreateString(const char *string); +// raw json +extern cJSON *cJSON_CreateRaw(const char *raw); extern cJSON *cJSON_CreateArray(void); extern cJSON *cJSON_CreateObject(void); @@ -148,6 +151,7 @@ extern void cJSON_Minify(char *json); #define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) #define cJSON_AddNumberToObject(object,name,n) cJSON_AddItemToObject(object, name, cJSON_CreateNumber(n)) #define cJSON_AddStringToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateString(s)) +#define cJSON_AddRawToObject(object,name,s) cJSON_AddItemToObject(object, name, cJSON_CreateRaw(s)) /* When assigning an integer value, it needs to be propagated to valuedouble too. */ #define cJSON_SetIntValue(object,val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val)) From 9ef44fc0b61c05e8197c1e3aa4ea246fb25455f0 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 5 Jan 2017 21:12:10 +0100 Subject: [PATCH 2/6] Remove C++ comment in header --- cJSON.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cJSON.h b/cJSON.h index 70641a2..63b50f7 100644 --- a/cJSON.h +++ b/cJSON.h @@ -38,7 +38,7 @@ extern "C" #define cJSON_String (1 << 4) #define cJSON_Array (1 << 5) #define cJSON_Object (1 << 6) -#define cJSON_Raw (1 << 7) //< raw json +#define cJSON_Raw (1 << 7) /* raw json */ #define cJSON_IsReference 256 #define cJSON_StringIsConst 512 @@ -106,7 +106,7 @@ extern cJSON *cJSON_CreateFalse(void); extern cJSON *cJSON_CreateBool(int b); extern cJSON *cJSON_CreateNumber(double num); extern cJSON *cJSON_CreateString(const char *string); -// raw json +/* raw json */ extern cJSON *cJSON_CreateRaw(const char *raw); extern cJSON *cJSON_CreateArray(void); extern cJSON *cJSON_CreateObject(void); From ddadb44a67f9c1dedaddc6cd4bf4b682b2dd7fbc Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 5 Jan 2017 21:30:37 +0100 Subject: [PATCH 3/6] cJSON_Raw: Additional checks in print_value --- cJSON.c | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index 237bd34..d0aad03 100644 --- a/cJSON.c +++ b/cJSON.c @@ -990,12 +990,26 @@ static char *print_value(const cJSON *item, int depth, cjbool fmt, printbuffer * out = print_number(item, p); break; case cJSON_Raw: - out = ensure(p, strlen(item->valuestring)); + { + size_t raw_length = 0; + if (item->valuestring == NULL) + { + if (!p->noalloc) + { + cJSON_free(p->buffer); + } + out = NULL; + break; + } + + raw_length = strlen(item->valuestring) + sizeof('\0'); + out = ensure(p, raw_length); if (out) { - strcpy(out, item->valuestring); + memcpy(out, item->valuestring, raw_length); } break; + } case cJSON_String: out = print_string(item, p); break; From 1df987a170a7d3c8692f53982eca0934d6e0e282 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 5 Jan 2017 21:31:17 +0100 Subject: [PATCH 4/6] cJSON_strdup: Check for NULL string --- cJSON.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/cJSON.c b/cJSON.c index d0aad03..6ded617 100644 --- a/cJSON.c +++ b/cJSON.c @@ -88,6 +88,11 @@ static char* cJSON_strdup(const char* str) size_t len = 0; char *copy = NULL; + if (str == NULL) + { + return NULL; + } + len = strlen(str) + 1; if (!(copy = (char*)cJSON_malloc(len))) { From e3e0b5150b58ae7341cfbd38d999d9bee79bdb63 Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Thu, 5 Jan 2017 21:33:52 +0100 Subject: [PATCH 5/6] cJSON_CreateRaw: Format fixes --- cJSON.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/cJSON.c b/cJSON.c index 6ded617..b7de473 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2019,19 +2019,19 @@ cJSON *cJSON_CreateString(const char *string) extern cJSON *cJSON_CreateRaw(const char *raw) { - cJSON *item = cJSON_New_Item(); - if(item) - { - item->type = cJSON_Raw; - item->valuestring = cJSON_strdup(raw); - if(!item->valuestring) - { - cJSON_Delete(item); - return 0; - } - } - - return item; + cJSON *item = cJSON_New_Item(); + if(item) + { + item->type = cJSON_Raw; + item->valuestring = cJSON_strdup(raw); + if(!item->valuestring) + { + cJSON_Delete(item); + return NULL; + } + } + + return item; } cJSON *cJSON_CreateArray(void) From f6998a6a340a7d04a2530dddeb9f6d36fb4ce2bd Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Mon, 9 Jan 2017 12:02:21 +0100 Subject: [PATCH 6/6] Contributors: Add Jiri Zouhar --- CONTRIBUTORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 9cc27a0..6ab649b 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -14,6 +14,7 @@ Contributors * Ian Mobley * Irwan Djadjadi * [IvanVoid](https://github.com/npi3pak) +* [Jiri Zouhar](https://github.com/loigu) * [Jonathan Fether](https://github.com/jfether) * [Kevin Branigan](https://github.com/kbranigan) * [Kyle Chisholm](https://github.com/ChisholmKyle)