简体   繁体   中英

JsonConvert.DeserializeObject() throws StackOverflow exception

I am working on a WebAPI project that is hosted on Azure and am running into some issues deserializing an object that I serialized myself. What I am doing is storing the serialized Json in a DB and on the service reading that string from the DB and trying to deserialize it. The exact exception I am getting is as follows:

An unhandled exception of type 'System.StackOverflowException' occurred in Newtonsoft.Json.dll

The object that I am trying to deserialize is a Graph data structure that has undirected edges and using a base class in its node type definition but accepts nodes of any derived class. This means that I did have to change the default Json serializer settings to the following:

new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects, TypeNameHandling = TypeNameHandling.Auto }

This enables it so it can handle the circular references that are created by undirected edges and so it can handle the Graph being of type

Graph<Intersection>

Accepting any nodes that derive their type from Intersection. Intersection is just a simple abstract class that has a couple of base type properties and any derived class only adds a few more base type properties.

The debugging steps I have taken so far are as follows:

  1. Tried deserializing the Json in the same application that serialized it. It was successfully able to deserialize the Json. (The graph was created in a WPF application)
  2. Created a console app that did the exact same process for deserializing the json as in the WebAPI project, thinking it might be an issue with reading the Json from the DB or something else. But it was able to deserialize no problem.
  3. I then tried creating a class library in the WebAPI solution and tried having it deserialize the Json but it failed with the same StackOverflow exception.
  4. I have tried adding in the Json.Net binaries to my project to debug through it but the StackOverflow exception is being thrown all over the code and I can't seem to narrow down where it is happening.
  5. I thought it might be an issue with Azure and the Azure emulator so I ran the WebAPI project on the local IIS but it still had the same issue deserializing.

Any help would be greatly appreciated as I have tried everything I can think of to solve the problem.

The issue appears to be around the size of the default stack that IIS has, which is less than the size of my Json file. This is the reason I don't get the stackoverflow exception in any of the other applications given they have much larger stacks.

The solution is to run the code in the following line, which runs the Json deserialization on a new thread with a much larger stack.

Thread thread = new Thread(new ThreadStart(() => graph = JsonConvert.DeserializeObject<Graph<Intersection>>(json, GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings)), 1000000);
thread.Start();
thread.Join();

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