Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tiny-json.c
Original file line number Diff line number Diff line change
Expand Up @@ -287,11 +287,13 @@ static char* numValue( char* ptr, json_t* property ) {
if ( JSON_INTEGER == property->type ) {
char const* value = property->u.value;
bool const negative = *value == '-';
static char const min[] = "-9223372036854775808";
static char const max[] = "9223372036854775807";
static char const min[] = "-9223372036854775808"; // min int64_t
static char const max[] = "18446744073709551615"; // max uint64_t
unsigned int const maxdigits = ( negative? sizeof min: sizeof max ) - 1;
unsigned int const len = ( unsigned int const ) ( ptr - value );
if ( len > maxdigits ) return 0;
if ( negative == 0 )
Comment thread
Hossein-M98 marked this conversation as resolved.
Outdated
property->type = JSON_UINTEGER;
if ( len == maxdigits ) {
char const tmp = *ptr;
*ptr = '\0';
Expand Down
13 changes: 11 additions & 2 deletions tiny-json.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extern "C" {
/** Enumeration of codes of supported JSON properties types. */
typedef enum {
JSON_OBJ, JSON_ARRAY, JSON_TEXT, JSON_BOOLEAN,
JSON_INTEGER, JSON_REAL, JSON_NULL
JSON_INTEGER, JSON_UINTEGER, JSON_REAL, JSON_NULL
} jsonType_t;

/** Structure to handle JSON properties. */
Expand Down Expand Up @@ -137,12 +137,21 @@ static inline bool json_getBoolean( json_t const* property ) {
}

/** Get the value of a json integer property.
* @param property A valid handler of a json object. Its type must be JSON_INTEGER.
* @param property A valid handler of a json object. Its type must be JSON_INTEGER or JSON_UINTEGER.
* @warning If the data type is JSON_UINTEGER and the value is greater than INT64_MAX,
* this function will not generate the correct value.
* @return The value stdint. */
static inline int64_t json_getInteger( json_t const* property ) {
return strtoll( property->u.value,(char**)NULL, 10);
}

/** Get the value of a json unsigned integer property.
* @param property A valid handler of a json object. Its type must be JSON_UINTEGER.
* @return The value stdint. */
static inline uint64_t json_getUinteger( json_t const* property ) {
return strtoull( property->u.value,(char**)NULL, 10);
}

/** Get the value of a json real property.
* @param property A valid handler of a json object. Its type must be JSON_REAL.
* @return The value. */
Expand Down