common: add routine to get double from JSON.

I don't like it, but we do expose some times like this :(

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2023-01-30 16:54:16 +10:30 committed by Alex Myers
parent eb6b8551d4
commit 9589ea0240
2 changed files with 19 additions and 0 deletions

View file

@ -101,6 +101,22 @@ bool json_str_to_u64(const char *buffer, const jsmntok_t *tok, u64 *num)
return json_to_u64(buffer, &temp, num);
}
bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num)
{
char *end;
errno = 0;
*num = strtod(buffer + tok->start, &end);
if (end != buffer + tok->end)
return false;
/* Check for overflow */
if (errno == ERANGE)
return false;
return true;
}
bool json_to_u32(const char *buffer, const jsmntok_t *tok, u32 *num)
{
uint64_t u64;

View file

@ -42,6 +42,9 @@ 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);
/* Extract double from this (generally a bad idea!) */
bool json_to_double(const char *buffer, const jsmntok_t *tok, double *num);
/* Extract boolean from this */
bool json_to_bool(const char *buffer, const jsmntok_t *tok, bool *b);