Newtonsoft.Json - memory exception when deserializing a large object

I have a problem deserializing a JSON file of about 1 GB in size. When I run the following code, I get an exception in memory:

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); } } } 

exception thrown

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

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 deserialization? Can you help suggest a way to deserialize a large json object?

thanks

+6
source share
1 answer

According to Newtonsoft.Json Performance Tips, your approach should work (because you are reading through a stream, and it should make part of your file). I cannot understand why your code is not working.

But you can try a different approach, described in the next article - Parse large records using Json.NET

+4
source

All Articles