Browse Source

common/json_stream.c: Implement a `json_add_jsonstr` to add already-JSON strings to `json_stream` objects.

Also incidentally fixes #3613

ChangeLog-none
travis-debug
ZmnSCPxj jxPCSnmZ 5 years ago
committed by Rusty Russell
parent
commit
919d371fe8
  1. 12
      common/json_stream.c
  2. 12
      common/json_stream.h
  3. 14
      plugins/fundchannel.c
  4. 22
      plugins/pay.c

12
common/json_stream.c

@ -162,6 +162,18 @@ void json_add_member(struct json_stream *js,
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 */
static struct io_plan *json_stream_output_write(struct io_conn *conn,
struct json_stream *js)

12
common/json_stream.h

@ -114,6 +114,18 @@ void json_add_member(struct json_stream *js,
bool quote,
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.
* @js: the json_stream.

14
plugins/fundchannel.c

@ -44,17 +44,6 @@ struct funding_req {
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,
const char *buf,
const jsmntok_t *error,
@ -220,8 +209,7 @@ static void txprepare(struct json_stream *js,
if (fr->minconf)
json_add_u32(js, "minconf", *fr->minconf);
if (fr->utxo_str)
json_out_add_raw_len(js->jout, "utxos", fr->utxo_str,
strlen(fr->utxo_str));
json_add_jsonstr(js, "utxos", fr->utxo_str);
}
static struct command_result *prepare_actual(struct command *cmd,

22
plugins/pay.c

@ -147,13 +147,6 @@ static void json_out_add_raw_len(struct json_out *jout,
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)
{
struct pay_attempt *attempt = current_attempt(pc);
@ -179,6 +172,15 @@ static const jsmntok_t *copy_member(struct json_out *ret,
if (!m)
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. */
json_out_add_raw_len(ret, membername,
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++) {
json_object_start(data, NULL);
if (pc->ps->attempts[i].route)
json_add_member(data, "route", true, "%s",
json_add_jsonstr(data, "route",
pc->ps->attempts[i].route);
json_out_add_splice(data->jout, "failure",
pc->ps->attempts[i].failure);
@ -835,7 +837,7 @@ static struct command_result *getroute_done(struct command *cmd,
attempt->sendpay = true;
req = jsonrpc_request_start(cmd->plugin, cmd, "sendpay",
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, "bolt11", pc->ps->bolt11);
if (pc->label)
@ -1420,7 +1422,7 @@ static void add_attempt(struct json_stream *ret,
}
if (attempt->route)
json_add_member(ret, "route", true, "%s", attempt->route);
json_add_jsonstr(ret, "route", attempt->route);
if (attempt->failure)
json_out_add_splice(ret->jout, "failure", attempt->failure);

Loading…
Cancel
Save