Abstract
Hi, I'm working on a project where it is needed to send potentially huge json of some object via HttpClient, a 10-20 mb of JSON is a typical size. In order do that efficiently I want to use streams, both with Json.Net to serialize an object plus streams for posting data with HttpClient.
Problem
Here is the snippet for serialization with Json.net, in order to work with streams, Json.net expects a stream that it will write into:
public static void Serialize( object value, Stream writeOnlyStream )
{
StreamWriter writer = new StreamWriter(writeOnlyStream); <-- Here Json.net expects the stream to be already created
JsonTextWriter jsonWriter = new JsonTextWriter(writer);
JsonSerializer ser = new JsonSerializer();
ser.Serialize(jsonWriter, value );
jsonWriter.Flush();
}
While HttpClient expects a stream that it will read from:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:54359/");
var response = await client.PostAsync("/api/snapshot", new StreamContent(readOnlyStream)); <-- The same thing here, HttpClient expects the stream already to exist
...
}
So eventually this means that both classes expecting the Stream to be created by someone else, but there are no streams both for Json.Net, neither for HttpClient. So the problem seems that can be solved by implementing a stream that would intercept a read requests made to read-only stream, and issue writes upon request from write-only stream.
Question
Maybe someone has stumbled on such situation already, and probably found already implemented solution to this problem. If so, please share it with me,
Thank you in advance!
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…