Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have a serious problem with "JsonConvert.SerializeObject" I need to serialize more than 500,000 dictionary records to make serialize throws the following error; System.OutOfMemoryException. I tried to serialize each key, value pair individually in a foreach but it's locked. Apparently it is an optimization problem, but I do not know where to start, threads to serialize in parts?
These functions works fine with few values.
My code:
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();
–
When you're dealing with a large amount of data, you can stream it to a file to avoid having it all 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);
This will serialize a bit at a time and avoid having a giant string in memory.
–
–
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.