Feature Request: Obey MaxDepth in SerializeObject()
See original GitHub issueIt would be helpful if MaxDepth worked on SerializeObject() as well, not just Deserialize().
Currently, you can use MaxDepth to stop the serializer from deserializing anything past a given depth on the initial object(s). But, you can’t currently tell it to only serialize down to a certain depth.
Additionally, it appears that the normal behavior of MaxDepth is to throw an exception, which is not desirable. It would help if there was a way to tell it to simply serialize/deserialize down to MaxDepth and stop. An exception is undesirable behavior in many circumstances.
A good example of where this is useful is on an Entity Framework database with LazyLoading enabled and virtual properties throughout. SerializeObject(user, new JsonSerializerSettings { MaxDepth = 0 });
would be useful for containing JSON serialization to just the user and their FK Ids. Without MaxDepth, JsonConvert is going to dutifully serialize most or all of the entire database.
I’m happy to write this feature if people are interested in it.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:24
- Comments:7
How is this not fixed? Everyone who has set this expects a certain behavior and isn’t getting it.
Yes but it’s not pretty. I had to create my own JsonConverter and pass it to
JsonConvert.SerializeObject
.For my use case, I wanted to log the contents of an object that contained data for executing a command or query, which I called the “Request” object:
And this is my
RequestJsonConverter
class: