Skip to content

Commit

Permalink
common/json: json_add_stringn helper.
Browse files Browse the repository at this point in the history
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
  • Loading branch information
rustyrussell committed Dec 2, 2020
1 parent 9361b62 commit c29e290
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
10 changes: 8 additions & 2 deletions common/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,13 +784,19 @@ void json_add_literal(struct json_stream *result, const char *fieldname,
memcpy(dest, literal, strlen(literal));
}

void json_add_string(struct json_stream *result, const char *fieldname, const char *value TAKES)
void json_add_stringn(struct json_stream *result, const char *fieldname,
const char *value TAKES, size_t value_len)
{
json_add_member(result, fieldname, true, "%s", value);
json_add_member(result, fieldname, true, "%.*s", (int)value_len, value);
if (taken(value))
tal_free(value);
}

void json_add_string(struct json_stream *result, const char *fieldname, const char *value TAKES)
{
json_add_stringn(result, fieldname, value, strlen(value));
}

void json_add_bool(struct json_stream *result, const char *fieldname, bool value)
{
json_add_member(result, fieldname, false, value ? "true" : "false");
Expand Down
7 changes: 7 additions & 0 deletions common/json.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ const jsmntok_t *json_delve(const char *buffer,
*/
void json_add_string(struct json_stream *result, const char *fieldname, const char *value);

/* '"fieldname" : "value[:value_len]"' or '"value[:value_len]"' if
* fieldname is NULL. Turns any non-printable chars into JSON
* escapes, but leaves existing escapes alone.
*/
void json_add_stringn(struct json_stream *result, const char *fieldname,
const char *value TAKES, size_t value_len);

/* '"fieldname" : "value"' or '"value"' if fieldname is NULL. String must
* already be JSON escaped as necessary. */
void json_add_escaped_string(struct json_stream *result,
Expand Down

0 comments on commit c29e290

Please sign in to comment.