mirror of
https://github.com/ElementsProject/lightning.git
synced 2025-03-01 09:40:19 +01:00
common/json_stream.c: Implement a json_add_jsonstr
to add already-JSON strings to json_stream
objects.
Also incidentally fixes #3613 ChangeLog-none
This commit is contained in:
parent
b51772ff68
commit
919d371fe8
4 changed files with 37 additions and 23 deletions
|
@ -162,6 +162,18 @@ void json_add_member(struct json_stream *js,
|
||||||
va_end(ap);
|
va_end(ap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void json_add_jsonstr(struct json_stream *js,
|
||||||
|
const char *fieldname,
|
||||||
|
const char *jsonstr)
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
size_t len = strlen(jsonstr);
|
||||||
|
|
||||||
|
p = json_member_direct(js, fieldname, len);
|
||||||
|
|
||||||
|
memcpy(p, jsonstr, len);
|
||||||
|
}
|
||||||
|
|
||||||
/* This is where we read the json_stream and write it to conn */
|
/* This is where we read the json_stream and write it to conn */
|
||||||
static struct io_plan *json_stream_output_write(struct io_conn *conn,
|
static struct io_plan *json_stream_output_write(struct io_conn *conn,
|
||||||
struct json_stream *js)
|
struct json_stream *js)
|
||||||
|
|
|
@ -114,6 +114,18 @@ void json_add_member(struct json_stream *js,
|
||||||
bool quote,
|
bool quote,
|
||||||
const char *fmt, ...) PRINTF_FMT(4,5);
|
const char *fmt, ...) PRINTF_FMT(4,5);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* json_add_jsonstr - add a JSON entity in a string that is already
|
||||||
|
* JSON-formatted.
|
||||||
|
* @js: the json_stream.
|
||||||
|
* @fieldname: fieldname (if in object), otherwise must be NULL.
|
||||||
|
* @jsonstr: the JSON entity, must be non-NULL, a null-terminated
|
||||||
|
* string that is already formatted in JSON.
|
||||||
|
*/
|
||||||
|
void json_add_jsonstr(struct json_stream *js,
|
||||||
|
const char *fieldname,
|
||||||
|
const char *jsonstr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* json_member_direct - start a generic member.
|
* json_member_direct - start a generic member.
|
||||||
* @js: the json_stream.
|
* @js: the json_stream.
|
||||||
|
|
|
@ -44,17 +44,6 @@ struct funding_req {
|
||||||
const char *error;
|
const char *error;
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Helper to copy JSON object directly into a json_out */
|
|
||||||
static void json_out_add_raw_len(struct json_out *jout,
|
|
||||||
const char *fieldname,
|
|
||||||
const char *jsonstr, size_t len)
|
|
||||||
{
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
p = json_out_member_direct(jout, fieldname, len);
|
|
||||||
memcpy(p, jsonstr, len);
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct command_result *send_prior(struct command *cmd,
|
static struct command_result *send_prior(struct command *cmd,
|
||||||
const char *buf,
|
const char *buf,
|
||||||
const jsmntok_t *error,
|
const jsmntok_t *error,
|
||||||
|
@ -220,8 +209,7 @@ static void txprepare(struct json_stream *js,
|
||||||
if (fr->minconf)
|
if (fr->minconf)
|
||||||
json_add_u32(js, "minconf", *fr->minconf);
|
json_add_u32(js, "minconf", *fr->minconf);
|
||||||
if (fr->utxo_str)
|
if (fr->utxo_str)
|
||||||
json_out_add_raw_len(js->jout, "utxos", fr->utxo_str,
|
json_add_jsonstr(js, "utxos", fr->utxo_str);
|
||||||
strlen(fr->utxo_str));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct command_result *prepare_actual(struct command *cmd,
|
static struct command_result *prepare_actual(struct command *cmd,
|
||||||
|
|
|
@ -147,13 +147,6 @@ static void json_out_add_raw_len(struct json_out *jout,
|
||||||
memcpy(p, jsonstr, len);
|
memcpy(p, jsonstr, len);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void json_out_add_raw(struct json_out *jout,
|
|
||||||
const char *fieldname,
|
|
||||||
const char *jsonstr)
|
|
||||||
{
|
|
||||||
json_out_add_raw_len(jout, fieldname, jsonstr, strlen(jsonstr));
|
|
||||||
}
|
|
||||||
|
|
||||||
static struct json_out *failed_start(struct pay_command *pc)
|
static struct json_out *failed_start(struct pay_command *pc)
|
||||||
{
|
{
|
||||||
struct pay_attempt *attempt = current_attempt(pc);
|
struct pay_attempt *attempt = current_attempt(pc);
|
||||||
|
@ -179,6 +172,15 @@ static const jsmntok_t *copy_member(struct json_out *ret,
|
||||||
if (!m)
|
if (!m)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
|
/* FIXME: The fact it is a string is probably the wrong thing
|
||||||
|
* to handle: if it *is* a string we should probably copy
|
||||||
|
* the quote marks, but json_tok_full/json_tok_full_len
|
||||||
|
* specifically remove those.
|
||||||
|
* It works *now* because it is only used in "code" and
|
||||||
|
* "data": "code" is always numeric, and "data" is usually
|
||||||
|
* a JSON object/key-value table, but pure stromgs will
|
||||||
|
* probably result in invalid JSON.
|
||||||
|
*/
|
||||||
/* Literal copy: it's already JSON escaped, and may be a string. */
|
/* Literal copy: it's already JSON escaped, and may be a string. */
|
||||||
json_out_add_raw_len(ret, membername,
|
json_out_add_raw_len(ret, membername,
|
||||||
json_tok_full(buf, m), json_tok_full_len(m));
|
json_tok_full(buf, m), json_tok_full_len(m));
|
||||||
|
@ -262,7 +264,7 @@ static struct command_result *waitsendpay_expired(struct command *cmd,
|
||||||
for (size_t i = 0; i < tal_count(pc->ps->attempts); i++) {
|
for (size_t i = 0; i < tal_count(pc->ps->attempts); i++) {
|
||||||
json_object_start(data, NULL);
|
json_object_start(data, NULL);
|
||||||
if (pc->ps->attempts[i].route)
|
if (pc->ps->attempts[i].route)
|
||||||
json_add_member(data, "route", true, "%s",
|
json_add_jsonstr(data, "route",
|
||||||
pc->ps->attempts[i].route);
|
pc->ps->attempts[i].route);
|
||||||
json_out_add_splice(data->jout, "failure",
|
json_out_add_splice(data->jout, "failure",
|
||||||
pc->ps->attempts[i].failure);
|
pc->ps->attempts[i].failure);
|
||||||
|
@ -835,7 +837,7 @@ static struct command_result *getroute_done(struct command *cmd,
|
||||||
attempt->sendpay = true;
|
attempt->sendpay = true;
|
||||||
req = jsonrpc_request_start(cmd->plugin, cmd, "sendpay",
|
req = jsonrpc_request_start(cmd->plugin, cmd, "sendpay",
|
||||||
sendpay_done, sendpay_error, pc);
|
sendpay_done, sendpay_error, pc);
|
||||||
json_out_add_raw(req->js->jout, "route", attempt->route);
|
json_add_jsonstr(req->js, "route", attempt->route);
|
||||||
json_add_string(req->js, "payment_hash", pc->payment_hash);
|
json_add_string(req->js, "payment_hash", pc->payment_hash);
|
||||||
json_add_string(req->js, "bolt11", pc->ps->bolt11);
|
json_add_string(req->js, "bolt11", pc->ps->bolt11);
|
||||||
if (pc->label)
|
if (pc->label)
|
||||||
|
@ -1420,7 +1422,7 @@ static void add_attempt(struct json_stream *ret,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (attempt->route)
|
if (attempt->route)
|
||||||
json_add_member(ret, "route", true, "%s", attempt->route);
|
json_add_jsonstr(ret, "route", attempt->route);
|
||||||
|
|
||||||
if (attempt->failure)
|
if (attempt->failure)
|
||||||
json_out_add_splice(ret->jout, "failure", attempt->failure);
|
json_out_add_splice(ret->jout, "failure", attempt->failure);
|
||||||
|
|
Loading…
Add table
Reference in a new issue