-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Unable to deserialize ienumerables that is not a list #61993
Comments
Tagging subscribers to this area: @dotnet/area-system-linq Issue DetailsDescriptionIf I have a custom list like this
} then when i try to deserialize it, then it gives me a runtime error saying "The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'" that is obviously incorrect though. I am able to deserialize no problem with newtonsofts. I need to transfer to system.json for better performance. Reproduction Stepsin a global class, use something like this then in main, use code like this SecondList list = new() text = jj.JsonSerializer.Serialize(list); SecondList? list2 = jj.JsonSerializer.Deserialize<SecondList>(text); Expected behaviorThe second set of code shows the deserialized and the proper numbers print. Actual behaviorThe runtime error with this message There needs to be a way to deserialize even custom classes that implement ienumerable like newtonsoft has. Regression?No response Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
Tagging subscribers to this area: @dotnet/area-system-text-json Issue DetailsDescriptionIf I have a custom list like this
} then when i try to deserialize it, then it gives me a runtime error saying "The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'" that is obviously incorrect though. I am able to deserialize no problem with newtonsofts. I need to transfer to system.json for better performance. Reproduction Stepsin a global class, use something like this then in main, use code like this SecondList list = new() text = jj.JsonSerializer.Serialize(list); SecondList? list2 = jj.JsonSerializer.Deserialize<SecondList>(text); Expected behaviorThe second set of code shows the deserialized and the proper numbers print. Actual behaviorThe runtime error with this message There needs to be a way to deserialize even custom classes that implement ienumerable like newtonsoft has. Regression?No response Known WorkaroundsNo response ConfigurationNo response Other informationNo response
|
Duplicate of #38514. One workaround is to implement a custom converter for your collection. |
Description
If I have a custom list like this
then when i try to deserialize it, then it gives me a runtime error saying "The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'"
that is obviously incorrect though.
I am able to deserialize no problem with newtonsofts. I need to transfer to system.json for better performance.
Is there any way to make it work with custom classes that implement ienumerable that behind the scenes use the standard list?
Reproduction Steps
in a global class, use something like this
global using jj = System.Text.Json;
then in main, use code like this
SecondList list = new()
{
1,
2,
3,
4
};
text = jj.JsonSerializer.Serialize(list);
Console.WriteLine(text);
SecondList? list2 = jj.JsonSerializer.Deserialize<SecondList>(text);
foreach (var item in list2!)
{
Console.WriteLine(item);
}
Expected behavior
The second set of code shows the deserialized and the proper numbers print.
Actual behavior
The runtime error with this message
The collection type 'RethinkList.SecondList`1[System.Int32]' is abstract, an interface, or is read only, and could not be instantiated and populated. Path: $ | LineNumber: 0 | BytePositionInLine: 1.'
There needs to be a way to deserialize even custom classes that implement ienumerable like newtonsoft has.
Regression?
No response
Known Workarounds
No response
Configuration
No response
Other information
No response
The text was updated successfully, but these errors were encountered: