-
Notifications
You must be signed in to change notification settings - Fork 644
Support nullable types in JsonTransformingSerializer #1927
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
Comments
My usage the same. I'm using 3rd party API and at deserialization process I'm not sure that there is expected value at my property, then I want to provide default null value |
Well, as I said fix is quiet simple at the source level, but I'm not sure that is appropriate way to change It's gross, because it wasn't well-tested. I just checked my needs and it's working. There is nothing extra added, I just replaced upper bound to nullable Any and transformed all non-null type to nullable, also moved 2 internal cast methods to be accessed.
|
Or even it could be just |
I think that the solution is simpler than that. We don't need any extra class. Just change the definition of There is no need to force T to be of type "Any". T type includes nullable and non nullable types |
Yep. I am not suppose to do extra class. It's solution for now without any PRs. Also, T : Any? is the same as T as I know. |
Any movement on this issue? |
This will allow for more flexible Json transformations, replacing invalid/unexpected input data with `null`s. Fixes #1927
What is your use-case and why do you need this feature?
Based on this comment, a custom
JsonTransformingSerializer
doesn't support nullable types.There is currently no straightforward way (except cumbersome usage of null "marker objects" in data) to handle custom deserialization of a
null
JSON field when one needs to directly read from aJsonElement
s to create a decoded model.A pretty common use case would be setting a property to
null
or to a default type in a model when an unexpected value (or set of values) is encountered while reading the JSON element in a legacy API.Describe the solution you'd like
As suggested in the comment above, it would be enough for
JsonTransformingSerializer
to support nullable types:Right now, if I attempt that the deserialization fails with:
The text was updated successfully, but these errors were encountered: