From 88354b79bd5bd067abea32781865a2644cc74bcd Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 12 Sep 2022 09:42:29 +0930 Subject: [PATCH] common: helper to get id field as a string. We'll be doing this quite a bit, so provide common helper. Signed-off-by: Rusty Russell --- common/json_parse_simple.c | 9 +++++++++ common/json_parse_simple.h | 4 ++++ 2 files changed, 13 insertions(+) diff --git a/common/json_parse_simple.c b/common/json_parse_simple.c index cf781611e..f7ce964ca 100644 --- a/common/json_parse_simple.c +++ b/common/json_parse_simple.c @@ -187,6 +187,15 @@ const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index) return NULL; } +const char *json_get_id(const tal_t *ctx, + const char *buffer, const jsmntok_t *obj) +{ + const jsmntok_t *idtok = json_get_member(buffer, obj, "id"); + if (!idtok) + return NULL; + return json_strdup(ctx, buffer, idtok); +} + /*----------------------------------------------------------------------------- JSMN Result Validation Starts -----------------------------------------------------------------------------*/ diff --git a/common/json_parse_simple.h b/common/json_parse_simple.h index d0670b013..4188a4eb4 100644 --- a/common/json_parse_simple.h +++ b/common/json_parse_simple.h @@ -62,6 +62,10 @@ const jsmntok_t *json_get_member(const char *buffer, const jsmntok_t tok[], /* Get index'th array member. */ const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index); +/* Helper to get "id" field from object. */ +const char *json_get_id(const tal_t *ctx, + const char *buffer, const jsmntok_t *obj); + /* Allocate a starter array of tokens for json_parse_input */ jsmntok_t *toks_alloc(const tal_t *ctx);