Skip to content

Commit

Permalink
Support 64bit integer if the compiler support it
Browse files Browse the repository at this point in the history
Change-Id: I95458471970a78448f484c8de02ac79632dd229f
Signed-off-by: Xiang Xiao <[email protected]>
  • Loading branch information
xiaoxiang781216 committed Apr 8, 2021
1 parent 69adcc3 commit f3e178f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
29 changes: 28 additions & 1 deletion cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,19 @@
#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)
#define intfmt "%ll"
#else
#define CJSON_INT_MAX INT_MAX
#define CJSON_INT_MIN INT_MIN
#define strtoint(s) atoi((const char*)(s))
#define intfmt "%d"
#endif

#define isint(d) (d == floor(d))

/* define isnan and isinf for ANSI C, if in C99 or above, isnan and isinf has been defined in math.h */
#ifndef isinf
Expand Down Expand Up @@ -309,6 +320,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();
Expand Down Expand Up @@ -338,12 +350,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;

Expand Down Expand Up @@ -374,6 +391,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;
Expand Down Expand Up @@ -566,6 +588,11 @@ static cJSON_bool print_number(const cJSON * const item, printbuffer * const out
{
length = sprintf((char*)number_buffer, "null");
}
else if (isint(d) && ((item->valueint != CJSON_INT_MAX &&
item->valueint != CJSON_INT_MIN) || d == item->valueint))
{
length = sprintf((char*)number_buffer, intfmt, item->valueint);
}
else
{
/* Try 15 decimal places of precision to avoid nonsignificant nonzero digits */
Expand Down Expand Up @@ -3015,7 +3042,7 @@ CJSON_PUBLIC(cJSON_bool) cJSON_Compare(const cJSON * const a, const cJSON * cons
return true;

case cJSON_Number:
if (compare_double(a->valuedouble, b->valuedouble))
if ((a->valueint == b->valueint) && compare_double(a->valuedouble, b->valuedouble))
{
return true;
}
Expand Down
6 changes: 5 additions & 1 deletion cJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down

0 comments on commit f3e178f

Please sign in to comment.