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 Mar 30, 2021
1 parent 809df07 commit cd5db45
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
20 changes: 19 additions & 1 deletion cJSON.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
#include <stdlib.h>
#include <ctype.h>
#include <float.h>

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
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 cd5db45

Please sign in to comment.