feat: add new JsonType and function

JSON_UINTEGER added to jsonType_t.
json_getUinteger function added to get unsigned integer values.
This commit is contained in:
Hossein.M 2023-10-25 12:24:56 +03:30
parent 025cdde5f2
commit 80f5050b0a
2 changed files with 15 additions and 4 deletions

View File

@ -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 )
property->type = JSON_UINTEGER;
if ( len == maxdigits ) {
char const tmp = *ptr;
*ptr = '\0';

View File

@ -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. */
@ -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. */