]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-json: json-ostream - Add support for writing string values through an output...
authorStephan Bosch <stephan.bosch@open-xchange.com>
Wed, 7 Aug 2019 21:54:02 +0000 (23:54 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Sat, 18 Nov 2023 18:58:04 +0000 (18:58 +0000)
src/lib-json/json-ostream.c
src/lib-json/json-ostream.h

index e21b6d41d8e501be702abc04facdb7714ce876ec..c9a7844a33d9310920ac4e43b8948c4f6dc77831 100644 (file)
@@ -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;
+}
index 1c10bf75b5a5cb412830f52065dac85ab2bf479c..fc865afe696c11a6626b37b34333171a0aa9cc3e 100644 (file)
@@ -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