mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-18 22:54:25 +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:
@@ -585,6 +585,17 @@ again:
|
|||||||
return toks;
|
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)
|
const char *jsmntype_to_string(jsmntype_t t)
|
||||||
{
|
{
|
||||||
switch (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,
|
jsmntok_t *json_parse_input(const tal_t *ctx,
|
||||||
const char *input, int len, bool *valid);
|
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. */
|
/* Convert a jsmntype_t enum to a human readable string. */
|
||||||
const char *jsmntype_to_string(jsmntype_t t);
|
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!) */
|
/* This should *not* parse! (used to give toks[0]->size == 2!) */
|
||||||
buf = "{ 'satoshi', '546' }";
|
buf = "{ 'satoshi', '546' }";
|
||||||
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
|
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
||||||
assert(!ok);
|
assert(!toks);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void test_json_delve(void)
|
static void test_json_delve(void)
|
||||||
{
|
{
|
||||||
const jsmntok_t *toks, *t;
|
const jsmntok_t *toks, *t;
|
||||||
char *buf;
|
char *buf;
|
||||||
bool ok;
|
|
||||||
|
|
||||||
buf = "{\"1\":\"one\", \"2\":\"two\", \"3\":[\"three\", {\"deeper\": 17}]}";
|
buf = "{\"1\":\"one\", \"2\":\"two\", \"3\":[\"three\", {\"deeper\": 17}]}";
|
||||||
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
|
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
||||||
assert(ok);
|
assert(toks);
|
||||||
assert(toks->size == 3);
|
assert(toks->size == 3);
|
||||||
|
|
||||||
t = json_delve(buf, toks, ".1");
|
t = json_delve(buf, toks, ".1");
|
||||||
@@ -227,8 +226,8 @@ static void test_json_delve(void)
|
|||||||
" }\n"
|
" }\n"
|
||||||
"}\n"
|
"}\n"
|
||||||
"\n";
|
"\n";
|
||||||
toks = json_parse_input(tmpctx, buf, strlen(buf), &ok);
|
toks = json_parse_simple(tmpctx, buf, strlen(buf));
|
||||||
assert(ok);
|
assert(toks);
|
||||||
assert(toks->size == 4);
|
assert(toks->size == 4);
|
||||||
|
|
||||||
t = json_delve(buf, toks, ".rpcfile");
|
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);
|
struct json *j = tal(ctx, struct json);
|
||||||
j->buffer = tal_strdup(j, str);
|
j->buffer = tal_strdup(j, str);
|
||||||
convert_quotes(j->buffer);
|
convert_quotes(j->buffer);
|
||||||
bool ok;
|
j->toks = json_parse_simple(j, j->buffer, strlen(j->buffer));
|
||||||
j->toks = json_parse_input(j, j->buffer, strlen(j->buffer), &ok);
|
assert(j->toks);
|
||||||
assert(ok);
|
|
||||||
j->toks = json_tok_copy(j, j->toks);
|
j->toks = json_tok_copy(j, j->toks);
|
||||||
return j;
|
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)
|
static void runtest(const char *filename)
|
||||||
{
|
{
|
||||||
const tal_t *ctx = tal(NULL, u8);
|
const tal_t *ctx = tal(NULL, u8);
|
||||||
bool valid;
|
|
||||||
char *buffer = grab_file(ctx, filename);
|
char *buffer = grab_file(ctx, filename);
|
||||||
const jsmntok_t *toks, *session_key_tok, *associated_data_tok, *gentok,
|
const jsmntok_t *toks, *session_key_tok, *associated_data_tok, *gentok,
|
||||||
*hopstok, *hop, *payloadtok, *pubkeytok, *typetok, *oniontok, *decodetok;
|
*hopstok, *hop, *payloadtok, *pubkeytok, *typetok, *oniontok, *decodetok;
|
||||||
@@ -217,8 +216,8 @@ static void runtest(const char *filename)
|
|||||||
struct route_step *step;
|
struct route_step *step;
|
||||||
char *hexprivkey;
|
char *hexprivkey;
|
||||||
|
|
||||||
toks = json_parse_input(ctx, buffer, strlen(buffer), &valid);
|
toks = json_parse_simple(ctx, buffer, strlen(buffer));
|
||||||
if (!valid)
|
if (!toks)
|
||||||
errx(1, "File is not a valid JSON file.");
|
errx(1, "File is not a valid JSON file.");
|
||||||
|
|
||||||
gentok = json_get_member(buffer, toks, "generate");
|
gentok = json_get_member(buffer, toks, "generate");
|
||||||
|
|||||||
@@ -442,12 +442,12 @@ static const jsmntok_t *read_rpc_reply(const tal_t *ctx,
|
|||||||
int *reqlen)
|
int *reqlen)
|
||||||
{
|
{
|
||||||
const jsmntok_t *toks;
|
const jsmntok_t *toks;
|
||||||
bool valid;
|
|
||||||
|
|
||||||
*reqlen = read_json_from_rpc(plugin);
|
*reqlen = read_json_from_rpc(plugin);
|
||||||
|
|
||||||
toks = json_parse_input(ctx, membuf_elems(&plugin->rpc_conn->mb), *reqlen, &valid);
|
toks = json_parse_simple(ctx,
|
||||||
if (!valid)
|
membuf_elems(&plugin->rpc_conn->mb), *reqlen);
|
||||||
|
if (!toks)
|
||||||
plugin_err(plugin, "Malformed JSON reply '%.*s'",
|
plugin_err(plugin, "Malformed JSON reply '%.*s'",
|
||||||
*reqlen, membuf_elems(&plugin->rpc_conn->mb));
|
*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)
|
static bool rpc_read_response_one(struct plugin *plugin)
|
||||||
{
|
{
|
||||||
bool valid;
|
|
||||||
const jsmntok_t *toks, *jrtok;
|
const jsmntok_t *toks, *jrtok;
|
||||||
|
|
||||||
/* For our convenience, lightningd always ends JSON requests with
|
/* 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))
|
if (!memmem(plugin->rpc_buffer, plugin->rpc_used, "\n\n", 2))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
toks = json_parse_input(NULL, plugin->rpc_buffer, plugin->rpc_used,
|
toks = json_parse_simple(NULL, plugin->rpc_buffer, plugin->rpc_used);
|
||||||
&valid);
|
|
||||||
if (!toks) {
|
if (!toks) {
|
||||||
if (!valid) {
|
plugin_err(plugin, "Failed to parse RPC JSON response '%.*s'",
|
||||||
plugin_err(plugin, "Failed to parse RPC JSON response '%.*s'",
|
(int)plugin->rpc_used, plugin->rpc_buffer);
|
||||||
(int)plugin->rpc_used, plugin->rpc_buffer);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
/* We need more. */
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user