From: Stephan Bosch Date: Wed, 7 Aug 2019 21:54:02 +0000 (+0200) Subject: lib-json: json-ostream - Add support for writing string values through an output... X-Git-Tag: 2.4.0~2384 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=566dd4fb030ce996909168385768892945936839;p=thirdparty%2Fdovecot%2Fcore.git lib-json: json-ostream - Add support for writing string values through an output stream --- diff --git a/src/lib-json/json-ostream.c b/src/lib-json/json-ostream.c index e21b6d41d8..c9a7844a33 100644 --- a/src/lib-json/json-ostream.c +++ b/src/lib-json/json-ostream.c @@ -1126,3 +1126,45 @@ void json_ostream_nwrite_node(struct json_ostream *stream, json_ostream_nwrite_value(stream, node->name, node->type, &node->value); } + +/* + * String output stream + */ + +int json_ostream_open_string_stream(struct json_ostream *stream, + const char *name, + struct ostream **ostream_r) +{ + int ret; + + ret = json_ostream_write_init(stream, name, JSON_TYPE_NONE); + if (ret <= 0) + return ret; + + *ostream_r = json_generate_string_open_stream(stream->generator); + return 1; +} + +struct ostream * +json_ostream_nopen_string_stream(struct json_ostream *stream, const char *name) +{ + struct ostream *ostream; + bool failed = FALSE; + int ret; + + if (!json_ostream_nwrite_pre(stream)) { + if (stream->output == NULL) + i_assert(!stream->nfailed); + else + failed = TRUE; + } + if (failed) { + int stream_errno = stream->output->stream_errno; + if (stream_errno == 0) + stream_errno = EIO; + return o_stream_create_error(stream_errno); + } + ret = json_ostream_open_string_stream(stream, name, &ostream); + json_ostream_nwrite_post(stream, ret); + return ostream; +} diff --git a/src/lib-json/json-ostream.h b/src/lib-json/json-ostream.h index 1c10bf75b5..fc865afe69 100644 --- a/src/lib-json/json-ostream.h +++ b/src/lib-json/json-ostream.h @@ -266,4 +266,18 @@ int json_ostream_write_text_stream(struct json_ostream *stream, void json_ostream_nwrite_text_stream(struct json_ostream *stream, const char *name, struct istream *input); +/* + * String output stream + */ + +/* Try to open an output stream for a writing a big string value. Returns 1 if + opened, 0 if the json output stream needs to be flushed more (tried + implicitly) before the stream can be opened, -1 if error. + */ +int json_ostream_open_string_stream(struct json_ostream *stream, + const char *name, + struct ostream **ostream_r); +struct ostream * +json_ostream_nopen_string_stream(struct json_ostream *stream, const char *name); + #endif