Reputation: 101
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:
Any help would be greatly appreciated as I have tried everything I can think of to solve the problem.
Upvotes: 3
Views: 1817
Reputation: 101
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();
Upvotes: 4