title: Writing JSON to a stream without buffering the string in memory
body: <p>I would like to write JSON to a <code>Stream</code> by building the document up explicitly. For example:</p>
  
  <pre><code>var stream = ...;
  var writer = new JsonWriter(stream);
  
  writer.BeginArray();
  {
    writer.BeginObject();
    {
      writer.String("foo");
      writer.Number(1);
      writer.String("bar");
      writer.Number(2.3);
    }
    writer.EndObject();
  }
  writer.EndArray();
  </code></pre>
  
  <p>This would produce:</p>
  
  <pre><code>[
    {
      "foo": 1,
      "bar": 2.3
    }
  ]
  </code></pre>
  
  <p>The benefit of this approach is that nothing needs to be buffered in memory. In my situation, I'm writing quite a lot of JSON to the stream. Solutions such as <a href="https://stackoverflow.com/a/16921677/24874">this one</a> involve creating all your objects in memory, then serialising them to a large string in memory, then finally writing this string to the stream and garbage collecting, probably from the <a href="http://msdn.microsoft.com/en-us/magazine/cc534993.aspx" rel="noreferrer">LOH</a>. I want to keep my memory use low, writing out elements while reading data from another file/DB/etc stream.</p>
  
  <p>This kind of approach is available in C++ via the <a href="https://code.google.com/p/rapidjson/" rel="noreferrer">rapidjson library</a>.</p>
  
  <p>I've searched around a fair bit for this and haven't found a solution.</p>