简体   繁体   中英

Deserialize JSON with large collection

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.

here is a JSON serializer from Microsoft

http://nuget.org/packages/System.Json

I have used it on several occasions and had no problems with it. Not as full featured as Newtonsoft but handles most scenarios

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM