aboutsummaryrefslogtreecommitdiff
path: root/doc
diff options
context:
space:
mode:
authorNathaniel McCallum <npmccallum@redhat.com>2017-01-26 09:46:48 +0100
committerNathaniel McCallum <npmccallum@redhat.com>2017-01-26 16:16:24 +0100
commitb900967f6fbfae098ced9dfeab7b2b51e1a22c0a (patch)
treec58c08d1d918756cfb57b5fd8d20345eeb410fa6 /doc
parent746c2c3a996a7f6e80bd19fe3de438637e6363e1 (diff)
downloadjansson-b900967f6fbfae098ced9dfeab7b2b51e1a22c0a.zip
jansson-b900967f6fbfae098ced9dfeab7b2b51e1a22c0a.tar.gz
jansson-b900967f6fbfae098ced9dfeab7b2b51e1a22c0a.tar.bz2
Implement json_dumpb()
This function encodes the json_t object to a pre-allocated buffer. It compliments the already existing json_loadb() function and is useful for parsing JSON-RPC (among other protocols) when sent over datagram sockets. Signed-off-by: Nathaniel McCallum <npmccallum@redhat.com>
Diffstat (limited to 'doc')
-rw-r--r--doc/apiref.rst22
1 files changed, 22 insertions, 0 deletions
diff --git a/doc/apiref.rst b/doc/apiref.rst
index ca38bbf..839bb68 100644
--- a/doc/apiref.rst
+++ b/doc/apiref.rst
@@ -948,6 +948,28 @@ These functions output UTF-8:
error. *flags* is described above. The return value must be freed
by the caller using :func:`free()`.
+.. function:: size_t json_dumpb(const json_t *json, char *buffer, size_t size, size_t flags)
+
+ Writes the JSON representation of *json* to the *buffer* of
+ *size* bytes. Returns the number of bytes that would be written
+ or 0 on error. *flags* is described above. *buffer* is not
+ null-terminated.
+
+ This function never writes more than *size* bytes. If the return
+ value is greater than *size*, the contents of the *buffer* are
+ undefined. This behavior enables you to specify a NULL *buffer*
+ to determine the length of the encoding. For example::
+
+ size_t size = json_dumpb(json, NULL, 0, 0);
+ if (size == 0)
+ return -1;
+
+ char *buf = alloca(size);
+
+ size = json_dumpb(json, buf, size, 0);
+
+ .. versionadded:: 2.10
+
.. function:: int json_dumpf(const json_t *json, FILE *output, size_t flags)
Write the JSON representation of *json* to the stream *output*.