Issues parsing a 1GB json file using JSON.NET

前端 未结 2 640
闹比i
闹比i 2021-01-28 02:29

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

2条回答
  •  一向
    一向 (楼主)
    2021-01-28 03:28

    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(reader) 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/.

提交回复
热议问题