diff --git a/common/json.c b/common/json.c index 8cd37cdd0..af99b2e82 100644 --- a/common/json.c +++ b/common/json.c @@ -105,6 +105,21 @@ bool json_to_u16(const char *buffer, const jsmntok_t *tok, return true; } +bool json_to_u32(const char *buffer, const jsmntok_t *tok, + uint32_t *num) +{ + uint64_t u64; + + if (!json_to_u64(buffer, tok, &u64)) + return false; + *num = u64; + + /* Just in case it doesn't fit. */ + if (*num != u64) + return false; + return true; +} + bool json_to_int(const char *buffer, const jsmntok_t *tok, int *num) { char *end; diff --git a/common/json.h b/common/json.h index 355af0103..80548671a 100644 --- a/common/json.h +++ b/common/json.h @@ -37,6 +37,10 @@ bool json_to_number(const char *buffer, const jsmntok_t *tok, bool json_to_u64(const char *buffer, const jsmntok_t *tok, uint64_t *num); +/* Extract number from this (may be a string, or a number literal) */ +bool json_to_u32(const char *buffer, const jsmntok_t *tok, + uint32_t *num); + /* Extract number from this (may be a string, or a number literal) */ bool json_to_u16(const char *buffer, const jsmntok_t *tok, uint16_t *num);