From 83beaa5396acd15874311bce4b01431e583a6113 Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 25 Oct 2022 14:46:26 +0200 Subject: [PATCH] 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. --- common/json_parse_simple.c | 13 +++++++++++++ common/json_parse_simple.h | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/common/json_parse_simple.c b/common/json_parse_simple.c index f7ce964ca..9fe94229c 100644 --- a/common/json_parse_simple.c +++ b/common/json_parse_simple.c @@ -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; diff --git a/common/json_parse_simple.h b/common/json_parse_simple.h index 4188a4eb4..4092dad75 100644 --- a/common/json_parse_simple.h +++ b/common/json_parse_simple.h @@ -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);