<?xml version="1.0"?>
<page id="bson_writer_t"
      type="guide"
      style="class"
      xmlns="http://projectmallard.org/1.0/"
      xmlns:api="http://projectmallard.org/experimental/api/"
      xmlns:ui="http://projectmallard.org/experimental/ui/">

  <info>
    <link type="guide" xref="index#api-reference" />
  </info>

  <title>bson_writer_t</title>
  <subtitle>Bulk BSON serialization Abstraction</subtitle>

  <section id="description">
    <title>Synopsis</title>
    <synopsis><code mime="text/x-csrc"><![CDATA[#include <bson.h>

typedef struct _bson_writer_t bson_writer_t;

bson_writer_t *bson_writer_new     (uint8_t           **buf,
                                    size_t             *buflen,
                                    size_t              offset,
                                    bson_realloc_func   realloc_func,
                                    void               *realloc_func_ctx);
void           bson_writer_destroy (bson_writer_t      *writer);]]></code></synopsis>
  </section>

  <section id="description">
    <title>Description</title>
    <p>The <code xref="bson_writer_t">bson_writer_t</code> API provides an abstraction for serializing many BSON documents to a single memory region. The memory region may be dynamically allocated and re-allocated as more memory is demanded. This can be useful when building network packets from a high-level language. For example, you can serialize a Python Dictionary directly to a single buffer destined for a TCP packet.</p>
  </section>

  <links type="topic" groups="function" style="2column">
    <title>Functions</title>
  </links>

  <section id="examples">
    <title>Example</title>
    <listing>
      <title>Bulk Serializer</title>
      <synopsis><code mime="text/x-csrc"><![CDATA[#include <bson.h>

int main (int argc, char *argv[])
{
   bson_writer_t *writer;
   uint8_t *buf = NULL;
   size_t buflen = 0;
   bson_t *doc;

   writer = bson_writer_new (&buf, &buflen, 0, bson_realloc_ctx, NULL);

   for (i = 0; i < 1000; i++) {
      bson_writer_begin (writer, &doc);
      BSON_APPEND_INT32 (&doc, "i", i);
      bson_writer_end (writer);
   }

   bson_writer_destroy (writer);

   bson_free (buf);

   return 0;
}]]></code></synopsis>
    </listing>
  </section>
</page>
