runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Serializing null recursive IEnumerable with JsonSerializer causes StackOverflowException

Open ttv86 opened this issue 7 years ago • 2 comments

When serializing an object that contains recursive IEnumerable member of itself, JsonSerializer.Serialize throws an StackOverflowException.

Error isn't thrown if using for example concrete List-class instead of IEnumerable.

.NET Core 3.0.100-preview8-013656 OS: Windows 10, 1903

Sample code:

namespace TestConsoleApp
{
    using System.Collections.Generic;
    using System.Text.Json;

    public static class Program
    {
        public static void Main()
        {
            var model = new Node();
            System.Console.WriteLine(JsonSerializer.Serialize(model));
        }
    }

    public sealed class Node
    {
        public IEnumerable<Node> Children { get; set; }
    }
}

Expected result: {"Children": null}

ttv86 avatar Aug 27 '19 05:08 ttv86

maybe @ahsonkhan can help on this?

dasMulli avatar Aug 27 '19 06:08 dasMulli

No repro for me in Preview 9 builds. Try the following:

  <ItemGroup>
    <PackageReference Include="System.Text.Json" Version="4.6.0-preview9.19421.4" />
  </ItemGroup>

Or install a preview9 build from https://github.com/dotnet/core-sdk

Believe this is a dup of https://github.com/dotnet/corefx/issues/40050 and https://github.com/dotnet/corefx/issues/39769 /cc @steveharter

Thanks for the report!

ericstj avatar Aug 27 '19 19:08 ericstj