SerializeObject throws a System.OutOfMemoryException

I have a serious problem with "JsonConvert.SerializeObject". I need to serialize over 500,000 dictionary entries, to do serialization, it produces the following error; System.OutOfMemoryException. I tried serializing each pair, a pair of values ​​separately in foreach, but it is locked. Apparently, this is an optimization problem, but I don’t know where to start, are streams serialized in parts? These functions work fine with multiple values. My code is:

string json = JsonConvert.SerializeObject(DatatableToDictionary(dt), Newtonsoft.Json.Formatting.Indented);

public List<Dictionary<string, object>> DatatableToDictionary(DataTable dt, List<DataColumn> columns)
{
    return dt.Rows.Cast<DataRow>().Select(
         r => columns.ToDictionary(c => c.ColumnName, c => r[c.ColumnName])).ToList();
}
+4
source share
1 answer

When you are dealing with a lot of data, you can transfer it to a file to avoid all of it in memory at once.

var filePath = @"C:\somewhere.json";

using (var fs = File.Open(filePath, FileMode.CreateNew))
using (var sw = new StreamWriter(fs))
using (var jw = new JsonTextWriter(sw))
{
    var serializer = new JsonSerializer();
    serializer.Serialize(jw, dictionary);
}

.

+4

Source: https://habr.com/ru/post/1655210/


All Articles