-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Serializer should throw NotSupportedException when collections can be instantiated but not populated #30571
Comments
Why do you think that's better? Isn't it clearer to convey that serializing a type like |
It's not the serializer that throws this exception, it's How can the serializer know which |
Oh, I think I get what you're saying now; you want to catch the |
The |
@martincostello That's a good point. Added to dotnet/corefx#40268. |
I checked semantics of Json.NET -- it ignores readonly collections (e.g. exception for StringValue). @JamesNK did "ignore" semantics for readonly collections pan out OK for Json.NET? Any regrets? Thanks |
Since this collection is readonly, it falls under the not-supported category, the serializer (not The other exception we throw on (de)serialization failure is In we throw a |
I think However, the other option is to ignore -- see previous comment and comparison with Json.Net. |
I'll wait for a decision on whether |
It isn't a common complaint that I know of so I guess so. Keep in mind that Json.NET has very good support for creating readonly collection's via their constructor (and various other customizations to support F# and immutable collections). I don't know exactly why StringValues doesn't throw an error when it is deserialized. It might be because it is a struct and a readonly collection. That's an odd type 🤷♂ |
@layomia and I talked offline. Conclusion:
|
What does this mean? You can't support As I see it, supporting
Also, this is about much more than Why rely on exceptions for flow control? If you ask me, that's more of an anti-pattern than wrapping execptions with a clear message and a documented exception type. |
This is understood. The point was we should throw the exception, not ignore (creating an empty instance like Json.NET does)
We should preemptively check if the instance is readonly and throw |
This is what the PR did, but as mentioned in dotnet/corefx#40268 (comment), it seemed like that wasn't desired. TBH, I'm a bit confused about what needs to be done here. If you don't want to special-case |
@khellang thanks for looking into this. Your PR was indeed the fix needed except for wrapping the exceptions. dotnet/corefx#40268 (comment) contained a typo at first: "Since we are now special casing for ReadOnly" (intended) read at first as "Since we are not special casing for ReadOnly" I'll create a PR closing this issue. |
Re-opening until fixed in 3.0. |
Fixed in release/3.0 by dotnet/corefx#40438. |
@ahsonkhan Filed dotnet/docs#14005. Let me know if there's anything you'd like to change/add 😄 |
Nope, it's perfect :) |
Consider (head at dotnet/corefx@4fbef81)
Output
dotnet/corefx#39001 adds support for types that implement BCL enumerables that are natively supported in the serializer.
For cases likeStringValue
where we cannot invoke the add method of the implemented type (ICollection<string>.Add
in this case), we should throw aJsonException
instead of aNotSupportedException
:For enumerables that are not supported: readonly collections like
StringValues
(implementsICollection<string>
), and collections that do not implement any of:IList
ICollection<T>
Stack<T>
Queue<T>
IDictionary
IDictionary<string, TValue>
,the serializer should throw a
NotSupportedException
.These collections can be supported in the future.
The text was updated successfully, but these errors were encountered: