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;