common/json: make json_scan return an error string.

This makes for more useful errors.  It prints where it was up to in
the guide, but doesn't print the entire JSON it's scanning.

Suggested-by: Christian Decker
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell
2021-01-07 16:04:43 +10:30
committed by Christian Decker
parent 53582a0f81
commit 3b7d0e7a62
9 changed files with 381 additions and 265 deletions

View File

@@ -152,15 +152,18 @@ jsmntok_t *json_tok_copy(const tal_t *ctx, const jsmntok_t *tok);
void json_tok_remove(jsmntok_t **tokens,
jsmntok_t *obj_or_array, const jsmntok_t *tok, size_t num);
/* Guide is % for a token: each must be followed by JSON_SCAN(). */
bool json_scan(const char *buffer,
const jsmntok_t *tok,
const char *guide,
...);
/* Guide is % for a token: each must be followed by JSON_SCAN().
* Returns NULL on error (asserts() on bad guide). */
const char *json_scan(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok,
const char *guide,
...);
/* eg. JSON_SCAN(json_to_bool, &boolvar) */
#define JSON_SCAN(fmt, var) \
json_scan, \
stringify(fmt), \
((var) + 0*sizeof(fmt((const char *)NULL, \
(const jsmntok_t *)NULL, var) == true)), \
(fmt)
@@ -168,16 +171,18 @@ bool json_scan(const char *buffer,
/* eg. JSON_SCAN_TAL(tmpctx, json_strdup, &charvar) */
#define JSON_SCAN_TAL(ctx, fmt, var) \
(ctx), \
stringify(fmt), \
((var) + 0*sizeof((*var) = fmt((ctx), \
(const char *)NULL, \
(const jsmntok_t *)NULL))), \
(fmt)
/* Already-have-varargs version */
bool json_scanv(const char *buffer,
const jsmntok_t *tok,
const char *guide,
va_list ap);
const char *json_scanv(const tal_t *ctx,
const char *buffer,
const jsmntok_t *tok,
const char *guide,
va_list ap);
/* Iterator macro for array: i is counter, t is token ptr, arr is JSMN_ARRAY */
#define json_for_each_arr(i, t, arr) \