From c26d53f0d74b10b4f8fb290ee663cfdd14d0115a Mon Sep 17 00:00:00 2001 From: Max Bruckner Date: Sun, 26 Feb 2017 14:30:50 +0100 Subject: [PATCH] Helper function to check the type of an item This is necessary, because you can get it wrong if you do it manually. (when you forget the & 0xFF in the comparison) --- cJSON.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ cJSON.h | 12 +++++++ 2 files changed, 112 insertions(+) diff --git a/cJSON.c b/cJSON.c index cf43ce9..39593b7 100644 --- a/cJSON.c +++ b/cJSON.c @@ -2149,3 +2149,103 @@ void cJSON_Minify(char *json) /* and null-terminate. */ *into = '\0'; } + +extern cjbool cJSON_IsInvalid(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Invalid; +} + +extern cjbool cJSON_IsFalse(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_False; +} + +extern cjbool cJSON_IsTrue(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xff) == cJSON_True; +} + + +extern cjbool cJSON_IsBool(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & (cJSON_True | cJSON_False)) != 0; +} +extern cjbool cJSON_IsNull(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_NULL; +} + +extern cjbool cJSON_IsNumber(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Number; +} + +extern cjbool cJSON_IsString(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_String; +} + +extern cjbool cJSON_IsArray(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Array; +} + +extern cjbool cJSON_IsObject(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Object; +} + +extern cjbool cJSON_IsRaw(const cJSON * const item) +{ + if (item == NULL) + { + return false; + } + + return (item->type & 0xFF) == cJSON_Raw; +} diff --git a/cJSON.h b/cJSON.h index de1f69f..831df39 100644 --- a/cJSON.h +++ b/cJSON.h @@ -109,6 +109,18 @@ extern int cJSON_HasObjectItem(const cJSON *object, const char *string); /* For analysing failed parses. This returns a pointer to the parse error. You'll probably need to look a few chars back to make sense of it. Defined when cJSON_Parse() returns 0. 0 when cJSON_Parse() succeeds. */ extern const char *cJSON_GetErrorPtr(void); +/* These functions check the type of an item */ +extern int cJSON_IsInvalid(const cJSON * const item); +extern int cJSON_IsFalse(const cJSON * const item); +extern int cJSON_IsTrue(const cJSON * const item); +extern int cJSON_IsBool(const cJSON * const item); +extern int cJSON_IsNull(const cJSON * const item); +extern int cJSON_IsNumber(const cJSON * const item); +extern int cJSON_IsString(const cJSON * const item); +extern int cJSON_IsArray(const cJSON * const item); +extern int cJSON_IsObject(const cJSON * const item); +extern int cJSON_IsRaw(const cJSON * const item); + /* These calls create a cJSON item of the appropriate type. */ extern cJSON *cJSON_CreateNull(void); extern cJSON *cJSON_CreateTrue(void);