Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DeserializeObject fails in .NET Core 3 on collection initialized with Enumerable.Empty #2194

Closed
brockallen opened this issue Oct 15, 2019 · 4 comments

Comments

@brockallen
Copy link

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());
        }
    }
}
@JamesNK
Copy link
Owner

JamesNK commented Oct 15, 2019

I believe this is fixed in https://www.nuget.org/packages/Newtonsoft.Json/12.0.3-beta2

@JamesNK JamesNK closed this as completed Oct 15, 2019
@brockallen
Copy link
Author

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.

@brockallen
Copy link
Author

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

@akidd40
Copy link

akidd40 commented Aug 14, 2020

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants