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

雨燕双飞 提交于 2019-12-04 03:16:50

问题


I have a problem deserializing a JSON file of about 1GB. 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?

Thanks


回答1:


According to Newtonsoft.Json Performance Tips your approach has to work (because you read via stream and it should make portion from your file). 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



来源:https://stackoverflow.com/questions/33868388/newtonsoft-json-out-of-memory-exception-while-deserializing-big-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!