common/json_stream: make json_add_jsonstr take a length.

This is useful when have have a jsmntok_t.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-07-16 14:28:58 +09:30
parent f65d3bb1fc
commit d0a55a62b3
6 changed files with 23 additions and 15 deletions

View file

@ -199,13 +199,13 @@ static char *json_member_direct(struct json_stream *js,
void json_add_jsonstr(struct json_stream *js,
const char *fieldname,
const char *jsonstr)
const char *jsonstr,
size_t jsonstrlen)
{
char *p;
size_t len = strlen(jsonstr);
p = json_member_direct(js, fieldname, len);
memcpy(p, jsonstr, len);
p = json_member_direct(js, fieldname, jsonstrlen);
memcpy(p, jsonstr, jsonstrlen);
}
/* This is where we read the json_stream and write it to conn */

View file

@ -157,12 +157,13 @@ void json_add_escaped_string(struct json_stream *result,
* 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.
* @jsonstr: the JSON entity
* @jsonstrlen: the length of @jsonstr
*/
void json_add_jsonstr(struct json_stream *js,
const char *fieldname,
const char *jsonstr);
const char *jsonstr,
size_t jsonstrlen);
/**
* json_stream_output - start writing out a json_stream to this conn.

View file

@ -578,7 +578,7 @@ static struct json_stream *json_start(struct command *cmd)
json_object_start(js, NULL);
json_add_string(js, "jsonrpc", "2.0");
json_add_jsonstr(js, "id", cmd->id);
json_add_jsonstr(js, "id", cmd->id, strlen(cmd->id));
return js;
}
@ -742,13 +742,15 @@ static void rpc_command_hook_final(struct rpc_command_hook_payload *p STEALS)
if (p->custom_result != NULL) {
struct json_stream *s = json_start(p->cmd);
json_add_jsonstr(s, "result", p->custom_result);
json_add_jsonstr(s, "result",
p->custom_result, strlen(p->custom_result));
json_object_end(s);
return was_pending(command_raw_complete(p->cmd, s));
}
if (p->custom_error != NULL) {
struct json_stream *s = json_start(p->cmd);
json_add_jsonstr(s, "error", p->custom_error);
json_add_jsonstr(s, "error",
p->custom_error, strlen(p->custom_error));
json_object_end(s);
return was_pending(command_raw_complete(p->cmd, s));
}

View file

@ -499,7 +499,8 @@ multifundchannel_finished(struct multifundchannel_command *mfc)
mfc->removeds[i].error_message);
if (mfc->removeds[i].error_data)
json_add_jsonstr(out, "data",
mfc->removeds[i].error_data);
mfc->removeds[i].error_data,
strlen(mfc->removeds[i].error_data));
json_object_end(out); /* End error object */
json_object_end(out);
}
@ -1382,7 +1383,8 @@ perform_fundpsbt(struct multifundchannel_command *mfc, u32 feerate)
&after_fundpsbt,
&mfc_forward_error,
mfc);
json_add_jsonstr(req->js, "utxos", mfc->utxos_str);
json_add_jsonstr(req->js, "utxos",
mfc->utxos_str, strlen(mfc->utxos_str));
json_add_bool(req->js, "reservedok", false);
} else {
plugin_log(mfc->cmd->plugin, LOG_DBG,
@ -1814,7 +1816,8 @@ post_cleanup_redo_multifundchannel(struct multifundchannel_redo *redo)
json_add_string(out, "method", failing_method);
if (mfc->removeds[i].error_data)
json_add_jsonstr(out, "data",
mfc->removeds[i].error_data);
mfc->removeds[i].error_data,
strlen(mfc->removeds[i].error_data));
/* Close 'data'. */
json_object_end(out);

View file

@ -356,7 +356,7 @@ static struct command_result *start_mw(struct multiwithdraw_command *mw)
&mw_forward_error,
mw);
json_add_bool(req->js, "reservedok", false);
json_add_jsonstr(req->js, "utxos", mw->utxos);
json_add_jsonstr(req->js, "utxos", mw->utxos, strlen(mw->utxos));
} else {
plugin_log(mw->cmd->plugin, LOG_DBG,
"multiwithdraw %"PRIu64": fundpsbt.",

View file

@ -346,7 +346,9 @@ openchannel_finished(struct multifundchannel_command *mfc)
json_add_node_id(out, "id", &dest->id);
json_add_string(out, "method", "openchannel_signed");
if (dest->error_data)
json_add_jsonstr(out, "data", dest->error_data);
json_add_jsonstr(out, "data",
dest->error_data,
strlen(dest->error_data));
json_object_end(out);
return mfc_finished(mfc, out);