I'm receiving a large JSON string from a WebService and I'm looking for the best memory optimized way of deserializing it with C#.
JSON structure:
{
"d": {
"results": [
{
"metadata": {
"id": "",
"uri": "",
"type": ""
},
"ID": "",
"Value1": "",
"Value2": "",
"Value3": ""
},
{
"metadata": {
"id": "",
"uri": "",
"type": ""
},
"ID": "",
"Value1": "",
"Value2": "",
"Value3": ""
},
]
}
}
I want to get all the object inside the "result" array but only one object after another and not like right now the complete list. I'm already using a StreamReader to avoid loading the complete json string into memory. Is there any option to read only one object, do some processing and then read the next one to avoid "OutOfMemoryExceptions"?
WebResponse response = r.GetResponse();
using (Stream dataStream = response.GetResponseStream())
{
var serializer = new JsonSerializer();
using (var sr = new StreamReader(dataStream))
using (var jsonTextReader = new JsonTextReader(sr))
{
return ((RootObject)serializer.Deserialize<RootObject>(jsonTextReader)).RootObject2.Results;
}