I am writing a console app that parses JSON data from an outside source. This is in C# using .NET 4.0 (the full version, not the client profile).
I am attempting to use the System.Web.Script.Serialization.JavaScriptSerializer class to deserialize JSON data into an object, like this:
var targetObject = deserializer.Deserialize<TargetObjectType>(json);
Normally this works fine, but when the data contains a very large collection, I get this error: System.InvalidOperationException (“Operation is not valid due to the current state of the object.”) I did some research and found this: http://support.microsoft.com/kb/2661403. Basically, Microsoft limits the number of items as a security precaution.
The way to override this is to add a record to Web.config as described in the support article, but in my case it doesn’t work. I did some research and found someone else with this problem here: http://msdn.microsoft.com/en-us/library/system.web.script.serialization.javascriptserializer.aspx.
So my question is this: is there any way to use JavaScriptSerializer to deserialize a JSON object with a large collection in a console app?
PLEASE NOTE: My team lead would prefer to not use any library other than Microsoft’s. THIS MEANS I CANNOT USE JSON.NET (AKA NEWTONSOFT), SO PLEASE DO NOT SUGGEST IT.
I’d suggest you to opt for some other Json De/Serializer. For instance: http://james.newtonking.com/projects/json-net.aspx
It pretty much handled anything I threw at it yet; even TB-sized dumps from Couchbase server (as long you have proper RAM in the machine that is going to de/serialize).
/edit: I have read that you lead would prefer sticking to the vanilla Microsoft Webstack. However, referencing one more assembly is gonna save a ton of time and lot of headaches.