title: JsonSerializer failing to write to a GZipStream
body: <p>I'm trying to serialize very large object directly to a zip stream. I manage to do this by serializing to a file stream in an intermediate step, loading it back and then compressing it.</p>
  
  <p>I've also tried compressing directly to a memory stream and it works. But when i use a GZipStream I'm always left with an "unfinished" object, the data is there it's correctly formatted up to the point where it ends unexpectedly.
    It's not for lack of flushing buffers since I've already tried flushing everything.</p>
  
  <p>Simplified sample code:</p>
  
  <pre><code>internal static byte[] SerializeAndCompress(object objectToSerialize)
  {
      using(var memStream = new MemoryStream())
      using (var zipStream = new GZipStream(memStream, CompressionMode.Compress, true))
      using (var streamWriter = new StreamWriter(zipStream))
      using (var jsonWriter = new JsonTextWriter(streamWriter))
      {
          var jsonSerializer = new JsonSerializer { ContractResolver = new CamelCasePropertyNamesContractResolver(), Formatting = Newtonsoft.Json.Formatting.None };
  
          jsonSerializer.Serialize(jsonWriter, objectToSerialize);
          jsonWriter.Flush();
  
          return memStream.ToArray();
      }
  }
  </code></pre>
  
  <p>Thanks.</p>