Browse Source

common/json: json_add_stringn helper.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
fix-mocks
Rusty Russell 4 years ago
parent
commit
c29e290ee6
  1. 10
      common/json.c
  2. 7
      common/json.h

10
common/json.c

@ -784,13 +784,19 @@ void json_add_literal(struct json_stream *result, const char *fieldname,
memcpy(dest, literal, strlen(literal)); 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)) if (taken(value))
tal_free(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) void json_add_bool(struct json_stream *result, const char *fieldname, bool value)
{ {
json_add_member(result, fieldname, false, value ? "true" : "false"); json_add_member(result, fieldname, false, value ? "true" : "false");

7
common/json.h

@ -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); 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 /* '"fieldname" : "value"' or '"value"' if fieldname is NULL. String must
* already be JSON escaped as necessary. */ * already be JSON escaped as necessary. */
void json_add_escaped_string(struct json_stream *result, void json_add_escaped_string(struct json_stream *result,

Loading…
Cancel
Save