I retrieve a huge JSON
data from the server
The problem is that, the serialization process have performance cost, in order to minimize this cost I need to retrieve this data as a stream first and then, with the help of Newtonsoft's Json.net
library serialize the data as a stream to the required type.
Now I'm doing this:
using (var stream = await httpClient.GetStreamAsync(requestUrl))
{
using(var streamReader = new StreamReader(stream))
{
using(var jsonReader = new JsonTextReader(streamReader))
{
var items = jsonSerializer.Deserialize<TEntity[]>(jsonReader);
//Some logic
}
}
}
The above code is good, but I need to get the HttpStatusCode
from the response object, but I cannot get the response object while working with streams. I searched for a solution and found this question.
Now, my question is: Is there any difference between the two approaches?
Calling GetAsync
to get the response object and then calling response.Content.ReadAsStreamAsync
will it have performance overhead?
I mean how does HttpClient
do it?
Does it load the data into the memory and then when I call response.Content.ReadAsStreamAsync
I read the data from the memory as stream?
Or, when I call response.Content.ReadAsStreamAsync
I'm getting the data as stream directly from the server?