Skip to content

StackOverflowException when serializing a deep tree (~750 levels) #662

@warquys

Description

@warquys

I am using a tree-like structure in my application.
If find out the issue what the deeps, i relise a simple snipet for testing it.
When the depth reaches around 750 nested nodes, the serialization fails with a StackOverflowException.

Here is a minimal repro:

IExtendedXmlSerializer serializer = new ConfigurationContainer()
            .EnableAllConstructors()
            .UseAutoFormatting()
            .EnableClassicListNaming()
            //.WithUnknownContent()
            .UseOptimizedNamespaces()
            .AllowMultipleReferences()
            // Additional configurations...
            .EnableXmlText()
            .Create();

int targget = 750;

Node head = new Node();
Node current = head;
for (int i = 0; i < targget; i++)
{
    current.Childes.Add(current = new Node() { Value = i });
}

// stack overflow
string result = serializer.Serialize(head);

public class Node
{
    public List<Node> Childes { get; set; } = new List<Node>();

    public int Value { get; set; }
}

The issue seems to be caused by deep recursive traversal during serialization.

Currently, my workaround is to split the serialization into multiple steps to limit recursion depth, but this is not ideal.

Questions

Is there a way to make ExtendedXmlSerializer use heap-based iteration instead of recursive stack calls during serialization?

Is it possible to configure or limit recursion depth, or otherwise avoid stack usage?

Are there recommended patterns for serializing very deep object graphs with this library?

Any guidance or design recommendations would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions