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!

13
  • Sounds more like a type issue Commented Jan 16, 2014 at 0:26
  • 4
    Not sure I fully understand your comment. Commented Jan 16, 2014 at 0:28
  • Serialization is failing because, and this an example, you try to assign a string to a number. It happen with booleans or any other type Commented Jan 16, 2014 at 0:31
  • Double check if you JSON does not have and invalid type Commented Jan 16, 2014 at 0:31
  • The serialization only fails sometimes and it throws an out of memory exception. Since it it intermittent with the same JSON data I wouldn't believe this is anything to do with type. Commented Jan 16, 2014 at 0:33

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Why use GetStreamAsync(...).Result? Waiting for an uncompleted task result blocks, so you might as well use the synchronous version.
You should probably reference where you got the code from as well.
Code came from james.newtonking.com/json/help/index.html?topic=html/…. As for the async versus sync versions the site says to use async.
The user example returns a List. If the list is too large, this won't help. See this answer: stackoverflow.com/a/24115672/475876
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.