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 <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-09-12 09:42:29 +09:30
parent 5d25934865
commit 88354b79bd
2 changed files with 13 additions and 0 deletions

View File

@ -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
-----------------------------------------------------------------------------*/

View File

@ -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);