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();
}
source
share