Merge pull request #90 from DaveGamble/cJSON_Raw

Add support for raw JSON
This commit is contained in:
Max Bruckner 2017-01-09 12:22:00 +01:00 committed by GitHub
commit b2da44d6cb
3 changed files with 52 additions and 1 deletions

View File

@ -14,6 +14,7 @@ Contributors
* Ian Mobley * Ian Mobley
* Irwan Djadjadi * Irwan Djadjadi
* [IvanVoid](https://github.com/npi3pak) * [IvanVoid](https://github.com/npi3pak)
* [Jiri Zouhar](https://github.com/loigu)
* [Jonathan Fether](https://github.com/jfether) * [Jonathan Fether](https://github.com/jfether)
* [Kevin Branigan](https://github.com/kbranigan) * [Kevin Branigan](https://github.com/kbranigan)
* [Kyle Chisholm](https://github.com/ChisholmKyle) * [Kyle Chisholm](https://github.com/ChisholmKyle)

46
cJSON.c
View File

@ -88,6 +88,11 @@ static char* cJSON_strdup(const char* str)
size_t len = 0; size_t len = 0;
char *copy = NULL; char *copy = NULL;
if (str == NULL)
{
return NULL;
}
len = strlen(str) + 1; len = strlen(str) + 1;
if (!(copy = (char*)cJSON_malloc(len))) if (!(copy = (char*)cJSON_malloc(len)))
{ {
@ -989,6 +994,27 @@ static char *print_value(const cJSON *item, int depth, cjbool fmt, printbuffer *
case cJSON_Number: case cJSON_Number:
out = print_number(item, p); out = print_number(item, p);
break; break;
case cJSON_Raw:
{
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)
{
memcpy(out, item->valuestring, raw_length);
}
break;
}
case cJSON_String: case cJSON_String:
out = print_string(item, p); out = print_string(item, p);
break; break;
@ -1016,6 +1042,9 @@ static char *print_value(const cJSON *item, int depth, cjbool fmt, printbuffer *
case cJSON_Number: case cJSON_Number:
out = print_number(item, 0); out = print_number(item, 0);
break; break;
case cJSON_Raw:
out = cJSON_strdup(item->valuestring);
break;
case cJSON_String: case cJSON_String:
out = print_string(item, 0); out = print_string(item, 0);
break; break;
@ -1988,6 +2017,23 @@ cJSON *cJSON_CreateString(const char *string)
return item; 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 NULL;
}
}
return item;
}
cJSON *cJSON_CreateArray(void) cJSON *cJSON_CreateArray(void)
{ {
cJSON *item = cJSON_New_Item(); cJSON *item = cJSON_New_Item();

View File

@ -38,6 +38,7 @@ extern "C"
#define cJSON_String (1 << 4) #define cJSON_String (1 << 4)
#define cJSON_Array (1 << 5) #define cJSON_Array (1 << 5)
#define cJSON_Object (1 << 6) #define cJSON_Object (1 << 6)
#define cJSON_Raw (1 << 7) /* raw json */
#define cJSON_IsReference 256 #define cJSON_IsReference 256
#define cJSON_StringIsConst 512 #define cJSON_StringIsConst 512
@ -54,7 +55,7 @@ typedef struct cJSON
/* The type of the item, as above. */ /* The type of the item, as above. */
int type; int type;
/* The item's string, if type==cJSON_String */ /* The item's string, if type==cJSON_String and type == cJSON_Raw */
char *valuestring; char *valuestring;
/* The item's number, if type==cJSON_Number */ /* The item's number, if type==cJSON_Number */
int valueint; int valueint;
@ -105,6 +106,8 @@ extern cJSON *cJSON_CreateFalse(void);
extern cJSON *cJSON_CreateBool(int b); extern cJSON *cJSON_CreateBool(int b);
extern cJSON *cJSON_CreateNumber(double num); extern cJSON *cJSON_CreateNumber(double num);
extern cJSON *cJSON_CreateString(const char *string); extern cJSON *cJSON_CreateString(const char *string);
/* raw json */
extern cJSON *cJSON_CreateRaw(const char *raw);
extern cJSON *cJSON_CreateArray(void); extern cJSON *cJSON_CreateArray(void);
extern cJSON *cJSON_CreateObject(void); extern cJSON *cJSON_CreateObject(void);
@ -155,6 +158,7 @@ extern void cJSON_Minify(char *json);
#define cJSON_AddBoolToObject(object,name,b) cJSON_AddItemToObject(object, name, cJSON_CreateBool(b)) #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_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_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. */ /* 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)) #define cJSON_SetIntValue(object,val) ((object) ? (object)->valueint = (object)->valuedouble = (val) : (val))