mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-02-20 13:54:36 +01:00
common: add simple json parse wrapper for the complete cases.
We're going to change the API on the more complete JSON parser, so make and use a simple API for the easy cases. Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
parent
40f17d994c
commit
80b2e1e298
6 changed files with 31 additions and 25 deletions
|
@ -585,6 +585,17 @@ again:
|
|||
return toks;
|
||||
}
|
||||
|
||||
jsmntok_t *json_parse_simple(const tal_t *ctx, const char *input, int len)
|
||||
{
|
||||
bool valid;
|
||||
jsmntok_t *toks;
|
||||
|
||||
toks = json_parse_input(ctx, input, len, &valid);
|
||||
if (toks && !valid)
|
||||
toks = tal_free(toks);
|
||||
return toks;
|
||||
}
|
||||
|
||||
const char *jsmntype_to_string(jsmntype_t t)
|
||||
{
|
||||
switch (t) {
|
||||
|
|
|
@ -89,6 +89,10 @@ const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index);
|
|||
jsmntok_t *json_parse_input(const tal_t *ctx,
|
||||
const char *input, int len, bool *valid);
|
||||
|
||||
/* Simplified version of above which parses only a complete, valid
|
||||
* JSON string */
|
||||
jsmntok_t *json_parse_simple(const tal_t *ctx, const char *input, int len);
|
||||
|
||||
/* Convert a jsmntype_t enum to a human readable string. */
|
||||
const char *jsmntype_to_string(jsmntype_t t);
|
||||
|
||||
|
|
|
@ -144,19 +144,18 @@ static void test_json_tok_size(void)
|
|||
|
||||
/* This should *not* parse! (used to give toks[0]->size == 2!) */
|
||||
buf = "{ 'satoshi', '546' }";
|
||||
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
|
||||
assert(!ok);
|
||||
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
||||
assert(!toks);
|
||||
}
|
||||
|
||||
static void test_json_delve(void)
|
||||
{
|
||||
const jsmntok_t *toks, *t;
|
||||
char *buf;
|
||||
bool ok;
|
||||
|
||||
buf = "{\"1\":\"one\", \"2\":\"two\", \"3\":[\"three\", {\"deeper\": 17}]}";
|
||||
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
|
||||
assert(ok);
|
||||
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
||||
assert(toks);
|
||||
assert(toks->size == 3);
|
||||
|
||||
t = json_delve(buf, toks, ".1");
|
||||
|
@ -227,8 +226,8 @@ static void test_json_delve(void)
|
|||
" }\n"
|
||||
"}\n"
|
||||
"\n";
|
||||
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
|
||||
assert(ok);
|
||||
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
||||
assert(toks);
|
||||
assert(toks->size == 4);
|
||||
|
||||
t = json_delve(buf, toks, ".rpcfile");
|
||||
|
|
|
@ -148,9 +148,8 @@ static struct json *json_parse(const tal_t * ctx, const char *str)
|
|||
struct json *j = tal(ctx, struct json);
|
||||
j->buffer = tal_strdup(j, str);
|
||||
convert_quotes(j->buffer);
|
||||
bool ok;
|
||||
j->toks = json_parse_input(j, j->buffer, strlen(j->buffer), &ok);
|
||||
assert(ok);
|
||||
j->toks = json_parse_simple(j, j->buffer, strlen(j->buffer));
|
||||
assert(j->toks);
|
||||
j->toks = json_tok_copy(j, j->toks);
|
||||
return j;
|
||||
}
|
||||
|
|
|
@ -204,7 +204,6 @@ static char *opt_set_node_id(const char *arg, struct node_id *node_id)
|
|||
static void runtest(const char *filename)
|
||||
{
|
||||
const tal_t *ctx = tal(NULL, u8);
|
||||
bool valid;
|
||||
char *buffer = grab_file(ctx, filename);
|
||||
const jsmntok_t *toks, *session_key_tok, *associated_data_tok, *gentok,
|
||||
*hopstok, *hop, *payloadtok, *pubkeytok, *typetok, *oniontok, *decodetok;
|
||||
|
@ -217,8 +216,8 @@ static void runtest(const char *filename)
|
|||
struct route_step *step;
|
||||
char *hexprivkey;
|
||||
|
||||
toks = json_parse_input(ctx, buffer, strlen(buffer), &valid);
|
||||
if (!valid)
|
||||
toks = json_parse_simple(ctx, buffer, strlen(buffer));
|
||||
if (!toks)
|
||||
errx(1, "File is not a valid JSON file.");
|
||||
|
||||
gentok = json_get_member(buffer, toks, "generate");
|
||||
|
|
|
@ -442,12 +442,12 @@ static const jsmntok_t *read_rpc_reply(const tal_t *ctx,
|
|||
int *reqlen)
|
||||
{
|
||||
const jsmntok_t *toks;
|
||||
bool valid;
|
||||
|
||||
*reqlen = read_json_from_rpc(plugin);
|
||||
|
||||
toks = json_parse_input(ctx, membuf_elems(&plugin->rpc_conn->mb), *reqlen, &valid);
|
||||
if (!valid)
|
||||
toks = json_parse_simple(ctx,
|
||||
membuf_elems(&plugin->rpc_conn->mb), *reqlen);
|
||||
if (!toks)
|
||||
plugin_err(plugin, "Malformed JSON reply '%.*s'",
|
||||
*reqlen, membuf_elems(&plugin->rpc_conn->mb));
|
||||
|
||||
|
@ -648,7 +648,6 @@ static void rpc_conn_finished(struct io_conn *conn,
|
|||
|
||||
static bool rpc_read_response_one(struct plugin *plugin)
|
||||
{
|
||||
bool valid;
|
||||
const jsmntok_t *toks, *jrtok;
|
||||
|
||||
/* For our convenience, lightningd always ends JSON requests with
|
||||
|
@ -656,15 +655,10 @@ static bool rpc_read_response_one(struct plugin *plugin)
|
|||
if (!memmem(plugin->rpc_buffer, plugin->rpc_used, "\n\n", 2))
|
||||
return false;
|
||||
|
||||
toks = json_parse_input(NULL, plugin->rpc_buffer, plugin->rpc_used,
|
||||
&valid);
|
||||
toks = json_parse_simple(NULL, plugin->rpc_buffer, plugin->rpc_used);
|
||||
if (!toks) {
|
||||
if (!valid) {
|
||||
plugin_err(plugin, "Failed to parse RPC JSON response '%.*s'",
|
||||
(int)plugin->rpc_used, plugin->rpc_buffer);
|
||||
return false;
|
||||
}
|
||||
/* We need more. */
|
||||
plugin_err(plugin, "Failed to parse RPC JSON response '%.*s'",
|
||||
(int)plugin->rpc_used, plugin->rpc_buffer);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue