0

Here's my code. I'm getting an out of memory exception trying to deserialize. I've tried following other answers and can't get it to work. Any recommendations? The file is 121 MB.

using (StreamReader reader = new StreamReader(filePath))
{
  using (JsonReader jr = new JsonTextReader(reader))
  {
    JsonSerializer serializer = new JsonSerializer();
    data = serializer.Deserialize<MyObject>(jr);
  }
}

I've also tried this

using (StreamReader reader = new StreamReader(filePath))
{
    using (JsonReader jr = new JsonTextReader(reader))
    {
        while (jr.Read())
        {
            if (jr.TokenType == JsonToken.StartObject)
            {
                data = serializer.Deserialize<MyObject>(jr);
            }
        }
    }
}

MyObject is a List<SubObject>. SubObject contains four properties, one being Data. Data is a large string and where the majority of this large data is stored and likely where it's failing. There are only two or three SubObjects in the list

Carspn
  • 3
  • 3
  • Needs to see a [mcve], specifically `MyObject` and the JSON in question. Recommendations differ depending on whether you have a large number of tokens, a single very large string value. For instance if you have a single huge Base64 string see maybe [Json.Net deserialize out of memory issue](https://stackoverflow.com/q/33480477/3744182). – dbc Mar 11 '21 at 18:45
  • Apologies for that, I edited it. That does sound like my problem, though it's not encoded. I'll keep looking into that issue specifically. – Carspn Mar 11 '21 at 18:50
  • And be sure you're actually running in 64bit not 32. See [What is the purpose of the “Prefer 32-bit” setting in Visual Studio and how does it actually work?](https://stackoverflow.com/a/12066861/3744182) and https://learn.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2015/ide/how-to-configure-projects-to-target-platforms?view=vs-2015&redirectedfrom=MSDN – dbc Mar 11 '21 at 18:50
  • Json.NET always fully materializes every string property. If you have switched to 64 bit and are still running out of memory materializing single properties you may need to switch to a different serializer. E.g. `JsonReaderWriterFactory.CreateJsonReader` supports `ReadValueChunk()`, see [Parse huge OData JSON by streaming certain sections of the json to avoid LOH](https://stackoverflow.com/q/56038495/3744182). – dbc Mar 11 '21 at 18:54
  • *MyObject is a `List`* - Can you share a small JSON sample with comments showing where the large quantities of data are -- i.e. a [mcve]? – dbc Mar 11 '21 at 18:55

0 Answers0