0

I am retrieving data from the database and serializing it into a JSON string. The following has worked for me:

List<MyClass> myList= ...;
string strJSON = new JavaScriptSerializer().Serialize(myList);

But it only works for small chunks of data. When I attempt to use it to retrieve large amount of data, I get the following error "The length of the string exceeds the value set on the maxJsonLength property”.

Changing the value of maxJsonLength does not work since the amount of data is too large.

I need another way of serializing my data into JSON string and writing it to a .json file. Can anyone help?

Coding Duchess
  • 6,445
  • 20
  • 113
  • 209
  • Read https://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.maxjsonlength(v=vs.110).aspx – L.B Aug 13 '15 at 20:40
  • It is not the same question - the question you linked to is whether it is possible to set maxJsonLength to unlimited. That's not my question. I already know that it is not possible. I am looking for an alternative solution – Coding Duchess Aug 13 '15 at 20:46
  • @ElenaDBA What have you tried? Reading the top 2 answers and the link that L.B linked, you can basically have up to ~4 GB of Unicode string data using the JavaScriptSerializer. Does your data size surpass this? – Lunyx Aug 13 '15 at 20:49
  • yes, it is larger. I have tried: MemoryStream ms = new MemoryStream(); /DataContractJsonSerializer dcjs =new DataContractJsonSerializer(typeof(MyClass)); dcjs.WriteObject(ms, myList); but it didn't work – Coding Duchess Aug 13 '15 at 20:52
  • @ElenaDBA You're not going to be able to serialize it to a single JSON string then. The max string size for C# is 2,147,483,647. – Lunyx Aug 13 '15 at 21:07
  • Lunyx, I figured that out already, hence my question about a different way to do that... – Coding Duchess Aug 13 '15 at 21:09
  • You will need to serialize directly to a file using a stream -- but `JavaScriptSerializer` does not have a method for that. You will need to switch to Json.NET or `DataContactJsonSerializer`. – dbc Aug 14 '15 at 15:13
  • dbc, I tried but ran into problems – Coding Duchess Aug 17 '15 at 13:00

0 Answers0