From: Stephan Bosch Date: Wed, 7 Aug 2019 22:28:05 +0000 (+0200) Subject: lib-json: json-ostream - Add support for writing literal JSON text from an input... X-Git-Tag: 2.4.0~2385 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a0eff0c9b50ee0fb303ac8aa350ba5f353f48718;p=thirdparty%2Fdovecot%2Fcore.git lib-json: json-ostream - Add support for writing literal JSON text from an input stream --- diff --git a/src/lib-json/json-ostream.c b/src/lib-json/json-ostream.c index 587185b3eb..e21b6d41d8 100644 --- a/src/lib-json/json-ostream.c +++ b/src/lib-json/json-ostream.c @@ -958,6 +958,33 @@ void json_ostream_nwrite_text(struct json_ostream *stream, json_ostream_nwrite_value(stream, name, JSON_TYPE_TEXT, &jvalue); } +int json_ostream_write_text_stream(struct json_ostream *stream, + const char *name, struct istream *input) +{ + struct json_value jvalue; + + i_zero(&jvalue); + jvalue.content_type = JSON_CONTENT_TYPE_STREAM; + jvalue.content.stream = input; + + return json_ostream_write_value(stream, name, JSON_TYPE_TEXT, + &jvalue); +} + +void json_ostream_nwrite_text_stream(struct json_ostream *stream, + const char *name, struct istream *input) +{ + struct json_value jvalue; + + i_zero(&jvalue); + jvalue.content_type = JSON_CONTENT_TYPE_STREAM; + jvalue.content.stream = input; + + json_ostream_nwrite_value(stream, name, JSON_TYPE_TEXT, &jvalue); + if (input->stream_errno != 0) + stream->nfailed = TRUE; +} + /* * Nodes */ diff --git a/src/lib-json/json-ostream.h b/src/lib-json/json-ostream.h index f2867fe406..1c10bf75b5 100644 --- a/src/lib-json/json-ostream.h +++ b/src/lib-json/json-ostream.h @@ -259,4 +259,11 @@ int json_ostream_write_text(struct json_ostream *stream, void json_ostream_nwrite_text(struct json_ostream *stream, const char *name, const char *str); +/* Try to write the stream to the output stream directly (JSON-text, not as + a string). Returns 1 if buffered, 0 if not, -1 if error. */ +int json_ostream_write_text_stream(struct json_ostream *stream, + const char *name, struct istream *input); +void json_ostream_nwrite_text_stream(struct json_ostream *stream, + const char *name, struct istream *input); + #endif