From 80f5050b0a2c57fae4e38b533bcfbc8ab050c409 Mon Sep 17 00:00:00 2001 From: "Hossein.M" Date: Wed, 25 Oct 2023 12:24:56 +0330 Subject: [PATCH] feat: add new JsonType and function JSON_UINTEGER added to jsonType_t. json_getUinteger function added to get unsigned integer values. --- tiny-json.c | 6 ++++-- tiny-json.h | 13 +++++++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/tiny-json.c b/tiny-json.c index 795715c..088849a 100644 --- a/tiny-json.c +++ b/tiny-json.c @@ -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'; diff --git a/tiny-json.h b/tiny-json.h index 2b527e7..bb20911 100644 --- a/tiny-json.h +++ b/tiny-json.h @@ -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. */