Serialization depth

Jan 2, 2009 at 2:27 PM
Hi, first, sorry for my english =P.

I'm new to Json.Net and I've been playing with it a little, well here is my case and my question:

I want to serialize an object but this object has many objects associated with it, and those objects have associations too, wich gives me a huge Json as result , the problem is that i want only a small portion of that data, say only the root object and the next level of depth. I can't use the JsonIgnore attribute because this will cause to be permanently ignored.

It s possible to specify the depth of the serialization process?

Jan 3, 2009 at 12:56 PM
No. You will have to write a JsonConverter for that type to get the custom serialization behavior you want.
Jan 3, 2009 at 3:28 PM
Hi James, 

Thank you for your early answer, I'd really apreciate that. I need to do this for almost every domain object (it is an application that starts and already have more than 20 domain objects and it will be much more) in my application, what do you recommend me, the JsonConverter or inherit the JsonTextWriter to implement this behaviour?

Thank You
Jan 3, 2009 at 11:44 PM

I have modified the JsonSerializer to only serialize a graph to the N-th level if someone wants the code please let me know.

Jan 16, 2009 at 7:05 AM

I am facing the same issue, and due to the depth of levels I get a stackoverflow error, I not sure whether depth would be the reason, however it would be nice if you could share the code.

Jan 17, 2009 at 4:25 PM
Sure Mustafa, only changes the JsonSerializerSettings and the JsonSerializer, i sent you an email with the classes. =)


Mar 24, 2011 at 10:43 PM

Hi TrentCioran, would you be able to share this change with me ? I am trying to find if there is a simpler way to restrict the recursion depth rather than weriting a converter.

Mar 29, 2011 at 7:15 AM

Hi TrentCioran:

I get the same problem, can you share the code with me? 

email address :


May 10, 2011 at 2:53 PM

Hi trentcioran,

I also got the same problem, could you please send me the code?

email address: chin[at]


May 12, 2011 at 3:04 PM

I'm interested as well.  Perhaps you could just post the code here for everyone?

Otherwise, my username at gmail dot com

May 12, 2011 at 3:15 PM

The code is hardly outdated (i made a fork almost 2 years ago) this weekend I have time, I will update the sources and post the code. Sorry for the dealyed response guys :)


May 19, 2011 at 3:47 AM

Sorry guys, I've been a little busy here is the code, i couldn't update it but well, better than nothing isn't it? as soon i get some time i'll come back with an updated version.

shared/6n3jt0e60t  this is in box net




Oct 1, 2014 at 8:47 PM
So, has anyone been able to do this?

I need to set a max depth on serializing an object. I never plan on deserializing it.