-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
JsonConverter for struct not used for nullable fields when passed through JsonSerializerSettings #2939
Comments
Related to #2245. As commented on in that issue, the workaround for this limitation is using the non-generic JsonConverter base class and overriding the Also keep in mind that WriteJson method of your converter would need to deal with Nullable<T> instances representing null in whatever way is desired, with the ReadJson method likely also needing to deal with the json input possibly containing null values instead of data for a Test instance. (P.S. I am just a user and not associated with the Newtonsoft.Json project) |
Yes and no. (I actually read this issue before posting.) Yes, in the responses the OP seems to basically suggest the desired behavior I outline here. The behavior which works, but only in case the converter is registered through an attribute. So maybe this has been implemented since, or the OP was unaware. No, in that the OP started from the outset with a broader (non-generic) converter, and incorrectly implemented The discrepancy in behavior between the two ways of registering is what I primarily want to highlight here. Documentation-wise (or the limited source code I browsed), I see no indication this is intentional. But, it strikes me as extremely odd (and even unlikely) such an old established, widely used, library would contain a bug in such a core feature. Out-of-the-box support for things like this is why serialization frameworks exist. 🤔 So I still feel I may be overlooking something. |
Source/destination types
Source/destination JSON
Expected behavior
When passing
TestConverter
throughJsonSerializersSettings
, as per the code in "steps to reproduce", for theserialized
value to show up as:Things work as expected when applying
[JsonConverter(typeof(TestConverter))]
tostruct Test
, but that is not an option for external types.Actual behavior
The custom
JsonConverter
goes unused. This line in the library seems to returnnull
, which is where I would expect the registered converter to be used.Steps to reproduce
The text was updated successfully, but these errors were encountered: