Newtonsoft.json: DeserializeObject fails in .NET Core 3 on collection initialized with Enumerable.Empty

Created on 15 Oct 2019  路  4Comments  路  Source: JamesNK/Newtonsoft.Json

When I have a collection initialized with an Enumerable.Empty then DeserializeObject throws:

Unhandled exception. Newtonsoft.Json.JsonSerializationException: Cannot create and populate list type System.Linq.EmptyPartition`1[System.String]. Path 'Values', line 1, position 11.

If I change the initialization to something else (such as new List<string>()) then it works. This worked in prior versions of .NET, but now seems to fail in .NET Core 3.

Full repo:

namespace ConsoleApp1
{
    public class Foo
    {
        public IEnumerable<string> Values { get; set; } = Enumerable.Empty<string>();
    }

    class Program
    {
        static void Main(string[] args)
        {
            var f1 = new Foo { Values = new[] { "apple", "banana", "carrot" } };
            var json = JsonConvert.SerializeObject(f1);
            Console.WriteLine(json);

            var f2 = JsonConvert.DeserializeObject<Foo>(json);
            Console.WriteLine(f2.Values.Count());
        }
    }
}

Most helpful comment

We tested and it seems it's working with the patch. Of course, the next question is what do you envision a release? Given that it breaks anyone using .NET Core 3 it seems an important patch to get out.

Thanks.

All 4 comments

We tested and it seems it's working with the patch. Of course, the next question is what do you envision a release? Given that it breaks anyone using .NET Core 3 it seems an important patch to get out.

Thanks.

Any update? I don't see milestones, so I'm unsure what the plan is.

This still appears to be an issue, so same question as above re. an update. Our codebase makes extensive use of Json.NET functionality so switching to System.Text.Json isn't an appealing option.

Was this page helpful?
0 / 5 - 0 ratings