From cd5db451638c7152895d08cc786971570336af98 Mon Sep 17 00:00:00 2001 From: Xiang Xiao Date: Wed, 31 Mar 2021 02:41:04 +0800 Subject: [PATCH] Support 64bit integer if the compiler support it Change-Id: I95458471970a78448f484c8de02ac79632dd229f Signed-off-by: Xiang Xiao --- cJSON.c | 20 +++++++++++++++++++- cJSON.h | 6 +++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/cJSON.c b/cJSON.c index 397c2f10..e9d9197f 100644 --- a/cJSON.c +++ b/cJSON.c @@ -40,8 +40,8 @@ #include #include #include -#include #include +#include #include #include @@ -70,8 +70,15 @@ #define false ((cJSON_bool)0) /* define our own int max and min */ +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +#define CJSON_INT_MAX LLONG_MAX +#define CJSON_INT_MIN LLONG_MIN +#define strtoint(s) strtoll((const char*)(s), NULL, 0) +#else #define CJSON_INT_MAX INT_MAX #define CJSON_INT_MIN INT_MIN +#define strtoint(s) atoi((const char*)(s)) +#endif /* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */ #ifndef isinf @@ -309,6 +316,7 @@ typedef struct static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_buffer) { double number = 0; + cJSON_bool integer = cJSON_True; unsigned char *after_end = NULL; unsigned char number_c_string[64]; unsigned char decimal_point = get_decimal_point(); @@ -338,12 +346,17 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu case '9': case '+': case '-': + number_c_string[i] = buffer_at_offset(input_buffer)[i]; + break; + case 'e': case 'E': + integer = cJSON_False; number_c_string[i] = buffer_at_offset(input_buffer)[i]; break; case '.': + integer = cJSON_False; number_c_string[i] = decimal_point; break; @@ -374,6 +387,11 @@ static cJSON_bool parse_number(cJSON * const item, parse_buffer * const input_bu else { item->valueint = (cJSON_int)number; + if (integer == cJSON_True && item->valueint != number) + { + /* convert again in caset of losing the precision */ + item->valueint = strtoint(number_c_string); + } } item->type = cJSON_Number; diff --git a/cJSON.h b/cJSON.h index 6f0a9385..103ece2c 100644 --- a/cJSON.h +++ b/cJSON.h @@ -99,7 +99,11 @@ then using the CJSON_API_VISIBILITY flag to "export" the same symbols the way CJ #define cJSON_IsReference 256 #define cJSON_StringIsConst 512 +#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L +typedef long long cJSON_int; +#else typedef int cJSON_int; +#endif /* The cJSON structure: */ typedef struct cJSON @@ -275,7 +279,7 @@ CJSON_PUBLIC(cJSON*) cJSON_AddObjectToObject(cJSON * const object, const char * CJSON_PUBLIC(cJSON*) cJSON_AddArrayToObject(cJSON * const object, const char * const name); /* When assigning an integer value, it needs to be propagated to valuedouble too. */ -#define cJSON_SetIntValue(object, number) ((object) ? (object)->valueint = (object)->valuedouble = (number) : (number)) +#define cJSON_SetIntValue(object, number) ((object) ? ((object)->valuedouble = (number), (object)->valueint = (number)) : (number)) /* helper for the cJSON_SetNumberValue macro */ CJSON_PUBLIC(double) cJSON_SetNumberHelper(cJSON *object, double number); #define cJSON_SetNumberValue(object, number) ((object != NULL) ? cJSON_SetNumberHelper(object, (double)number) : (number))