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());
}
}
}
I believe this is fixed in https://www.nuget.org/packages/Newtonsoft.Json/12.0.3-beta2
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.
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.