json: Add helper for quoted numbers

The JSON specification technically disallows maps with numeric keys,
so we'll want to slowly migrate away from using them. This helper
extracts the numeric value from a quoted number, which is a legal
representation of the same in JSON.
This commit is contained in:
Christian Decker 2022-10-25 14:46:26 +02:00
parent f158b529d3
commit 83beaa5396
2 changed files with 17 additions and 0 deletions

View File

@ -88,6 +88,19 @@ bool json_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num)
return true;
}
bool json_str_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num)
{
jsmntok_t temp;
if (tok->type != JSMN_STRING)
return false;
temp = *tok;
temp.start += 1;
temp.end -= 1;
return json_to_u64(buffer, &temp, num);
}
bool json_to_u32(const char *buffer, const jsmntok_t *tok, u32 *num)
{
uint64_t u64;

View File

@ -35,6 +35,10 @@ char *json_strdup(const tal_t *ctx, const char *buffer, const jsmntok_t *tok);
/* Extract number from this (may be a string, or a number literal) */
bool json_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num);
/* Extract number from string. The number must be the entirety of the
* string between the '"' */
bool json_str_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num);
/* Extract number from this (may be a string, or a number literal) */
bool json_to_u32(const char *buffer, const jsmntok_t *tok, u32 *num);