-
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
Json.Deserialize System.Net.Mail; #76798
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionDeserialize a MailAddress is not possible. Reproduction StepsJust run this simple code in .Net 6.0
Expected behaviorSerialize is OK. But not the Deserialize Actual behaviorSystem.NotSupportedException: 'Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'System.Net.Mail.MailAddress'. Regression?No response Known WorkaroundsNo response Configuration
Other informationNo response
|
Hello, did you try writing a converter? |
This is by design -- types containing multiple public constructors without a JsonConstructor attribute are treated as ambiguous and are not supported for deserialization. I would recommend authoring a custom converter for the type. |
@RallyTuning would it have helped you discover this if the error message had suggested a custom converter? Or perhaps you were already aware of them and were hoping for another solution? |
@danmoseley no, i didn't have tried a custom converter, i just created a custom MailAddress class to "solve" the problem. But it is strange anyway that a built-in function to deserialize a class, didn't deserialize a simple built-in class (System.Net.Mail.MailAddress). For this reason i reported it. @eiriktsarpalis yes understood, but it's weird that this applies also with standard .Net classes. |
Thanks for the info. This isn't may area, so I defer to @eiriktsarpalis for his thoughts on the exception messages. |
Most types aren't designed with serialization in mind, and this applies to the vast majority of types shipped with the BCL. System.Text.Json tries to provide out-of-the-box support for a small number of frequently used types, but ultimately there are limits to how many can be supported: code size concerns, trimability concerns and the general impracticality of taking a dependency of every other library in the stack. For most types, the serializer will make a best effort to derive a JSON contract but whenever this fails, using either a custom converter or a custom contract is your best way forward. This suggestion applies to both BCL and third-party classes.
It's something we could consider for all error modes deriving from contract resolution, i.e. a sentence pointing users to either writing custom converters or contract customizers. Is adding links something that we do in exception messages? |
We have some exception messages and host/runtime messages that contain links. It's not pretty but if it really helps, it's not bad. They should be aka.ms links though to be stable. |
But in this case maybe key words to search for might be enough. |
A very long message with tons of information will just confuse the majority of users. The actual exception message is "ok", it just little bit ambiguous.
I would ADD this message to the existing one, then it will contains the exception and a workaround. |
Description
Deserialize a MailAddress is not possible.
Reproduction Steps
Just run this simple code in .Net 6.0
Expected behavior
Serialize is OK. But not the Deserialize
Actual behavior
System.NotSupportedException: 'Deserialization of types without a parameterless constructor, a singular parameterized constructor, or a parameterized constructor annotated with 'JsonConstructorAttribute' is not supported. Type 'System.Net.Mail.MailAddress'.
Regression?
No response
Known Workarounds
No response
Configuration
Other information
No response
The text was updated successfully, but these errors were encountered: