System.OutOfMemoryException with JSON.NET with List<object>

后端 未结 1 1986
礼貌的吻别
礼貌的吻别 2020-12-20 08:06

I\'m doing a process where my server generates a file with JSON.Net with 25000 records and size of 85MB, then this file is imported by client.

The client downloads t

相关标签:
1条回答
  • 2020-12-20 08:47

    I was playing around a bit with the answer linked above: Deserialize json array stream one item at a time. The following slight modification might meet your needs. It reads and deserializes the JSON item-by-item rather than loading the entire list into memory:

    public static class JsonConvertExtensions
    {
        public static IEnumerable<T> DeserializeEnumerableFile<T>(string filename)
        {
            using (var stream = new StreamReader(filename))
                foreach (var item in DeserializeEnumerable<T>(stream))
                    yield return item;
        }
    
        public static IEnumerable<T> DeserializeEnumerableString<T>(string json)
        {
            using (var sr = new StringReader(json))
                foreach (var item in DeserializeEnumerable<T>(sr))
                    yield return item;
        }
    
        public static IEnumerable<T> DeserializeEnumerable<T>(TextReader textReader)
        {
            var serializer = JsonSerializer.CreateDefault();
    
            using (JsonTextReader reader = new JsonTextReader(textReader))
            {
                while (reader.Read())
                {
                    if (reader.TokenType == JsonToken.StartObject)
                    {
                        // Load each object from the stream and do something with it
                        yield return serializer.Deserialize<T>(reader);
                    }
                }
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题