Removing deserialization of large JSON objects from a web service (from memory)

I have a program that deserializes large objects from a web service. After calling webservice and 200, the code is as follows.

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

Sometimes during the execution of this process I get a generic exception that shows the internal exception as out of memory. I cannot determine if this is the reading process in the JSON data string (which is probably terribly large), or Deserializing, which causes this problem. What I would like to do is tear a line and pull out each JSON object separately from the response, and then deserialize it. I just find it difficult to find a way to output only one JSON object at a time from the response. Any suggestions are welcome!

+4
source share
1 answer
 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); } 
+3
source

Source: https://habr.com/ru/post/1235024/


All Articles