Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.OutOfMemoryException with JSON.NET

Tags:

json.net

Tried this with JSON.NET 6.0

DataSet ds = Newtonsoft.Json.JsonConvert.DeserializeObject<DataSet>
("{\"tab1\":[{\"col1\":\"val1\"}]}"); // OK

DataTable dt = Newtonsoft.Json.JsonConvert.DeserializeObject<DataTable>
("{\"col1\":\"val1\"}"); // System.OutOfMemoryException

why?

thank you

like image 858
DanCZ Avatar asked Jan 24 '26 01:01

DanCZ


2 Answers

IMO it's a bug, But if you want to deserialize DataSet or DataTable this might help you.

like image 131
deKajoo Avatar answered Jan 27 '26 00:01

deKajoo


The JSON in your second example represents a only a single row of data, not a DataTable. A DataTable is an ordered collection of DataRows, therefore it needs to have square brackets in the JSON. Try it like this instead:

string json = "[{\"col1\":\"val1\"}]";
DataTable dt = JsonConvert.DeserializeObject<DataTable>(json);

I am not sure why you are getting an OutOfMemoryException; I get a JsonSerializationException when I try it, as I would expect. Perhaps this was a bug that was fixed in the most recent version of Json.Net.

like image 21
Brian Rogers Avatar answered Jan 26 '26 23:01

Brian Rogers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!