You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
JsonConstrutor different behavior between Newtonsoft.Json and System.Text.Json #46480
It is deserialized fine with Newtonsoft, but not with System.Text.
The reason is that constructor parameter type is int? while the property has type of int.
Newtonsoft doesn't validate it, giving an option to do anything in the constructor.
There are possible workarounds but they are not as simple as the original code:
Make property private and add a public non nullable:
Activity
Clockwork-Muse commentedon Dec 31, 2020
Um, if you throw an exception like that, why not just make the constructor take a non-nullable?
NN--- commentedon Dec 31, 2020
It is a general question.
The argument validation can be more complicated than this.
layomia commentedon Jan 25, 2021
Dup of #44428.