diff --git a/cJSON.c b/cJSON.c index ff5bd85..353b72d 100644 --- a/cJSON.c +++ b/cJSON.c @@ -249,15 +249,23 @@ static char *print_object(cJSON *item,int depth,int fmt); static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;} /* Parse an object - create a new root, and populate. */ -cJSON *cJSON_Parse(const char *value) +cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated) { + const char *end=0; cJSON *c=cJSON_New_Item(); ep=0; if (!c) return 0; /* memory fail */ - if (!parse_value(c,skip(value))) {cJSON_Delete(c);return 0;} + end=parse_value(c,skip(value)); + if (!end) {cJSON_Delete(c);return 0;} /* parse failure. ep is set. */ + + /* if we require null-terminated JSON without appended garbage, skip and then check for a null terminator */ + if (require_null_terminated) {end=skip(end);if (*end) {cJSON_Delete(c);ep=end;return 0;}} + if (return_parse_end) *return_parse_end=end; return c; } +/* Default options for cJSON_Parse */ +cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);} /* Render a cJSON item/entity/structure to text. */ char *cJSON_Print(cJSON *item) {return print_value(item,0,1);} diff --git a/cJSON.h b/cJSON.h index 4df09a7..d54a821 100644 --- a/cJSON.h +++ b/cJSON.h @@ -120,6 +120,10 @@ extern cJSON *cJSON_Duplicate(cJSON *item,int recurse); need to be released. With recurse!=0, it will duplicate any children connected to the item. The item->next and ->prev pointers are always zero on return from Duplicate. */ +/* ParseWithOpts allows you to require (and check) that the JSON is null terminated, and to retrieve the pointer to the final byte parsed */ +extern cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated); + + #define cJSON_AddNullToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateNull()) #define cJSON_AddTrueToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateTrue()) #define cJSON_AddFalseToObject(object,name) cJSON_AddItemToObject(object, name, cJSON_CreateFalse())