I don't like using MemoryStream objects inbetween stream interfaces. They are awkward, requiring you to re-seek to the start, and will also peak memory usage in demanding situations.
Sometimes a utility will only work a certain way. Perhaps it will output byte[]s, or write to a stream, or is a stream in a pipeline that you read from, pulling the data through.
This Newtonsoft JSON serializer is a utility which can only write to a stream.
var js = new Newtonsoft.Json.JsonSerializer();
var sw = new StreamWriter(ps);
js.Serialize(sw, o);
This is a problem for me, because I want to chain:
- IEnumerable
- JSON serialization
- GZIP compression
- HTTP to client
- (Network)
- HTTP from Server
- GZIP decompression
- JSON deserialization
- IEnumerable
Apart from the difficulties getting the JSON deserializer to present a nice IEnumerable interface, the rest of the parts don't provide an interface suitable for pipelining. Even the GZIP compression side is the wrong way around.
Ideally, on the server-side I would be able to do:
IEnumerable<object> o = GetData();
var js = new Newtonsoft.Json.JsonSerialization(o);
var gz = new System.IO.Compression.GZipStream(js, System.IO.Compression.CompressionMode.Compress, true);
return new FileStreamResult(gz, "application/x-gzip");
I could possibly extend the Newtonsoft project to provide a pipeline implementation, and I may do so. But until then I need a solution, and I believe one is required for other utilities (including the BCL GZipStream).
- Are there any solutions which allow one to join such utilities more efficiently?
- Is there a library which contains an adapter for such situations?
I am working on such a library, not expecting there to be such a library.