4

I have a program that deserializes large objects from a web service. After a webservice call and a 200, the code looks like this.

JsonConvert.DeserializeObject<List<T>>(resp.Content.ReadAsStringAsync().Result).ToList()

Sometimes while running this process I will get an aggregate exception which shows an inner exception as out of memory. I can't determine if it is the process of reading in the string of JSON data (which is probably awfully large) or the Deserializing that is causing this issue. What I would like to do is break out the string and pull each JSON object back individually from the response and then deserialize it. I am just having trouble finding a way to only bring out one JSON object at a time from the response. Any suggestions are greatly appreciated!

user3170736
  • 511
  • 5
  • 24

2 Answers2

4
HttpClient client = new HttpClient();

using (Stream s = client.GetStreamAsync("http://www.test.com/large.json").Result)
using (StreamReader sr = new StreamReader(s))
using (JsonReader reader = new JsonTextReader(sr))
{
    JsonSerializer serializer = new JsonSerializer();

    // read the json from a stream
    // json size doesn't matter because only a small piece is read at a time from the HTTP request
    Person p = serializer.Deserialize<Person>(reader);
}
user3170736
  • 511
  • 5
  • 24
  • 1
    Why use `GetStreamAsync(...).Result`? Waiting for an uncompleted task result blocks, so you might as well use the synchronous version. – spender Jan 16 '14 at 01:47
  • 2
    You should probably reference where you got the code from as well. – nick_w Jan 16 '14 at 02:50
  • Code came from http://james.newtonking.com/json/help/index.html?topic=html/Performance.htm. As for the async versus sync versions the site says to use async. – user3170736 Jan 16 '14 at 14:41
  • The user example returns a List. If the list is too large, this won't help. See this answer: http://stackoverflow.com/a/24115672/475876 – Akira Yamamoto Jun 21 '16 at 15:17
0

https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/web-services/rest contains a warning:

Using the ReadAsStringAsync method to retrieve a large response can have a negative performance impact. In such circumstances the response should be directly deserialized to avoid having to fully buffer it.

Sjors Miltenburg
  • 2,540
  • 4
  • 33
  • 60