2018-11-20 02:46:32 +01:00
|
|
|
/* lightningd/json_stream.h
|
|
|
|
* Helpers for outputting JSON results into a membuf.
|
|
|
|
*/
|
2020-01-20 10:23:55 +01:00
|
|
|
#ifndef LIGHTNING_COMMON_JSON_STREAM_H
|
|
|
|
#define LIGHTNING_COMMON_JSON_STREAM_H
|
2018-11-20 02:46:32 +01:00
|
|
|
#include "config.h"
|
|
|
|
#include <ccan/tal/tal.h>
|
|
|
|
|
|
|
|
struct command;
|
|
|
|
struct io_conn;
|
2019-02-18 03:44:29 +01:00
|
|
|
struct log;
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2020-01-27 00:43:01 +01:00
|
|
|
struct json_stream {
|
|
|
|
/* NULL if we ran OOM! */
|
|
|
|
struct json_out *jout;
|
|
|
|
|
|
|
|
/* Who is writing to this buffer now; NULL if nobody is. */
|
|
|
|
struct command *writer;
|
|
|
|
|
|
|
|
/* Who is io_writing from this buffer now: NULL if nobody is. */
|
|
|
|
struct io_conn *reader;
|
|
|
|
struct io_plan *(*reader_cb)(struct io_conn *conn,
|
|
|
|
struct json_stream *js,
|
|
|
|
void *arg);
|
|
|
|
void *reader_arg;
|
|
|
|
size_t len_read;
|
|
|
|
|
|
|
|
/* Where to log I/O */
|
|
|
|
struct log *log;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/**
|
|
|
|
* new_json_stream - create a new JSON stream.
|
|
|
|
* @ctx: tal context for allocation.
|
|
|
|
* @writer: object responsible for writing to this stream.
|
2019-02-18 03:44:29 +01:00
|
|
|
* @log: where to log the IO
|
2018-11-20 02:46:32 +01:00
|
|
|
*/
|
2019-02-18 03:44:29 +01:00
|
|
|
struct json_stream *new_json_stream(const tal_t *ctx, struct command *writer,
|
|
|
|
struct log *log);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2018-12-12 19:09:06 +01:00
|
|
|
/**
|
|
|
|
* Duplicate an existing stream.
|
|
|
|
*
|
|
|
|
* Mostly useful when we want to send copies of a given stream to
|
|
|
|
* multiple recipients, that might read at different speeds from the
|
|
|
|
* stream. For example this is used when construcing a single
|
|
|
|
* notification and then duplicating it for the fanout.
|
|
|
|
*
|
|
|
|
* @ctx: tal context for allocation.
|
|
|
|
* @original: the stream to duplicate.
|
2019-05-31 04:01:07 +02:00
|
|
|
* @log: log for new stream.
|
2018-12-12 19:09:06 +01:00
|
|
|
*/
|
2019-05-31 04:01:07 +02:00
|
|
|
struct json_stream *json_stream_dup(const tal_t *ctx,
|
|
|
|
struct json_stream *original,
|
|
|
|
struct log *log);
|
2018-12-12 19:09:06 +01:00
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/**
|
|
|
|
* json_stream_close - finished writing to a JSON stream.
|
|
|
|
* @js: the json_stream.
|
|
|
|
* @writer: object responsible for writing to this stream.
|
|
|
|
*/
|
|
|
|
void json_stream_close(struct json_stream *js, struct command *writer);
|
|
|
|
|
2019-05-23 12:09:17 +02:00
|
|
|
/* For low-level JSON stream access: */
|
|
|
|
void json_stream_log_suppress(struct json_stream *js, const char *cmd_name);
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/* '"fieldname" : [ ' or '[ ' if fieldname is NULL */
|
|
|
|
void json_array_start(struct json_stream *js, const char *fieldname);
|
|
|
|
/* '"fieldname" : { ' or '{ ' if fieldname is NULL */
|
|
|
|
void json_object_start(struct json_stream *ks, const char *fieldname);
|
2019-06-17 07:52:26 +02:00
|
|
|
/* '],' */
|
2018-11-20 02:46:32 +01:00
|
|
|
void json_array_end(struct json_stream *js);
|
2019-06-17 07:52:26 +02:00
|
|
|
/* '},' */
|
2018-11-20 02:46:32 +01:00
|
|
|
void json_object_end(struct json_stream *js);
|
2019-06-17 07:52:26 +02:00
|
|
|
/* ' },' */
|
|
|
|
void json_object_compat_end(struct json_stream *js);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* json_stream_append - literally insert this string into the json_stream.
|
|
|
|
* @js: the json_stream.
|
|
|
|
* @str: the string.
|
2018-12-08 01:35:56 +01:00
|
|
|
* @len: the length to append (<= strlen(str)).
|
|
|
|
*/
|
2019-06-12 02:38:55 +02:00
|
|
|
void json_stream_append(struct json_stream *js, const char *str, size_t len);
|
2018-12-08 01:35:56 +01:00
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/**
|
2019-06-12 02:38:55 +02:00
|
|
|
* json_add_member - add a generic member.
|
2018-11-20 02:46:32 +01:00
|
|
|
* @js: the json_stream.
|
2019-06-12 02:38:55 +02:00
|
|
|
* @fieldname: fieldname (if in object), otherwise must be NULL.
|
|
|
|
* @quote: true if should be escaped and wrapped in "".
|
2018-11-20 02:46:32 +01:00
|
|
|
* @fmt...: the printf-style format
|
2019-06-12 02:38:55 +02:00
|
|
|
*
|
|
|
|
* The resulting string from @fmt is escaped if quote is true:
|
|
|
|
* see json_member_direct to avoid quoting.
|
2018-11-20 02:46:32 +01:00
|
|
|
*/
|
2019-06-12 02:38:55 +02:00
|
|
|
void json_add_member(struct json_stream *js,
|
|
|
|
const char *fieldname,
|
|
|
|
bool quote,
|
2019-11-01 00:38:00 +01:00
|
|
|
const char *fmt, ...) PRINTF_FMT(4,5);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
2020-04-07 20:01:36 +02:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
|
2018-11-20 02:46:32 +01:00
|
|
|
/**
|
2019-06-12 02:38:55 +02:00
|
|
|
* json_member_direct - start a generic member.
|
2018-11-20 02:46:32 +01:00
|
|
|
* @js: the json_stream.
|
2019-06-12 02:38:55 +02:00
|
|
|
* @fieldname: fieldname (if in object), otherwise must be NULL.
|
|
|
|
* @extra: the space to reserve.
|
|
|
|
*
|
|
|
|
* Returns NULL if oom, otherwise returns a ptr to @extra bytes.
|
2018-11-20 02:46:32 +01:00
|
|
|
*/
|
2019-06-12 02:38:55 +02:00
|
|
|
char *json_member_direct(struct json_stream *js,
|
|
|
|
const char *fieldname, size_t extra);
|
2018-11-20 02:46:32 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* json_stream_output - start writing out a json_stream to this conn.
|
|
|
|
* @js: the json_stream
|
|
|
|
* @conn: the io_conn to write out to.
|
|
|
|
* @cb: the callback to call once it's all written.
|
|
|
|
* @arg: the argument to @cb
|
|
|
|
*/
|
|
|
|
#define json_stream_output(js, conn, cb, arg) \
|
|
|
|
json_stream_output_((js), (conn), \
|
|
|
|
typesafe_cb_preargs(struct io_plan *, \
|
|
|
|
void *, \
|
|
|
|
(cb), (arg), \
|
2018-11-20 02:46:32 +01:00
|
|
|
struct io_conn *, \
|
|
|
|
struct json_stream *), \
|
2018-11-20 02:46:32 +01:00
|
|
|
(arg))
|
|
|
|
|
|
|
|
struct io_plan *json_stream_output_(struct json_stream *js,
|
|
|
|
struct io_conn *conn,
|
|
|
|
struct io_plan *(*cb)(struct io_conn *conn,
|
2018-11-20 02:46:32 +01:00
|
|
|
struct json_stream *js,
|
2018-11-20 02:46:32 +01:00
|
|
|
void *arg),
|
|
|
|
void *arg);
|
|
|
|
|
2020-10-12 07:33:49 +02:00
|
|
|
/* Ensure there's a double \n after a JSON response. */
|
|
|
|
void json_stream_double_cr(struct json_stream *js);
|
2019-06-12 02:38:55 +02:00
|
|
|
void json_stream_flush(struct json_stream *js);
|
|
|
|
|
2020-01-20 10:23:55 +01:00
|
|
|
#endif /* LIGHTNING_COMMON_JSON_STREAM_H */
|