简体   繁体   English

Newtonsoft.Json-反序列化大对象时出现内存不足异常

[英]Newtonsoft.Json - Out of memory exception while deserializing big object

I have a problem deserializing a JSON file of about 1GB. 我在反序列化大约1GB的JSON文件时遇到问题。 When I run the following code I get an out of memory exception: 当我运行以下代码时,出现内存不足异常:

using (FileStream sr = new FileStream("myFile.json", FileMode.Open, FileAccess.Read))
{
  using (StreamReader reader = new StreamReader(sr))
  {
    using (JsonReader jsReader = new JsonTextReader(reader))
    {
      JsonSerializer serializer = new JsonSerializer();
      dataObject = serializer.Deserialize<T>(jsReader);
    }
  }
}

the exception is thrown by 由抛出异常

Newtonsoft.Json.Linq.JTokenWriter.WriteValue(Int64 value)

The serialization works well, here is the code I'm using 序列化效果很好,这是我正在使用的代码

using (StreamWriter reader = new StreamWriter("myFile.json"))
{
   using (JsonReader jsWriter = new JsonWriter(reader))
   {
      JsonTextWriter jsonWriter = new JsonTextWriter(jsWriter) { Formatting = Formatting.Indented };
      JsonSerializer ser = new JsonSerializer();
      ser.Serialize(jsonWriter, dataObject, dataObject.GetType());
      jsonWriter.Flush();
    }
}}

Am I doing something wrong in the deserialization? 我在反序列化过程中做错了什么吗? Can you help suggesting a way to deserialize big json object? 您能帮忙建议反序列化大json对象的方法吗?

Thanks 谢谢

According to Newtonsoft.Json Performance Tips your approach has to work (because you read via stream and it should make portion from your file). 根据Newtonsoft.Json Performance Tips的介绍,您的方法必须有效(因为您通过流读取,并且应该从文件中提取一部分)。 I can't figure out why your code doesn't work. 我不知道为什么您的代码不起作用。

But you can try another approach, that was described in the next article - Parsing Big Records with Json.NET 但是您可以尝试另一种方法,该方法在下一篇文章中进行了介绍- 使用Json.NET解析大记录

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM