I have gotten an application where the input has been scaled up from 50K location records to 1.1 Million location records. This has caused serious issues as the entire file was
When the reader is positioned at the beginning of the object you want to deserialize (an entry in the Locations
array in your case), you can just call ser.Deserialize
and it will work, advancing to the end of the object at that level, and no further. Thus the following should iterate through the Location
objects in your file, loading each one separately:
public static IEnumerable DeserializeNestedItems(TextReader textReader)
{
var ser = new JsonSerializer();
using (var reader = new JsonTextReader(textReader))
{
reader.SupportMultipleContent = true;
while (reader.Read())
{
if (reader.TokenType == JsonToken.StartObject && reader.Depth == 2)
{
var item = ser.Deserialize(reader);
yield return item;
}
}
}
}
And an example of use using your test string:
Debug.Assert(DeserializeNestedItems(new StringReader(json)).Count() == 2); // No assert.
var list = DeserializeNestedItems(new StringReader(json)).SelectMany(l => l.AccessPoints).Select(a => new { a.Latitude, a.Longitude }).ToList();
Debug.WriteLine(JsonConvert.SerializeObject(list, Formatting.Indented));
Which outputs:
[ { "Latitude": 40.59485, "Longitude": -73.96174 }, { "Latitude": 40.59485, "Longitude": -73.96174 } ]
Note - the Location
class comes from posting your JSON to http://json2csharp.com/.