-
Notifications
You must be signed in to change notification settings - Fork 626
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
Parse nullable properties with non-default values when the keys are not present in the Json #1024
Comments
Adding a default value is not a workaround, it is a way to explicitly configure what to do when the property is missing (what default value to use) and to ensure that its usage from code is as close as possible from deserialization to avoid any confusion. For a case of SqlDelight auto-generated entities, IMHO, a better fix would be to make SqlDelight configurable so that it generates entities with appropriate default values and also marks them as |
I totally understand that this request might make things less clear but isn't this feature supported on other platforms? Regarding the SqlDelight use case, unfortunately it is not that straightforward when using column adapters with default values. |
Resurfacing this issue again |
I think giving null to nullable property is more reasonable than crashing app due to missing property.
|
see #1535 |
explicitNulls looks like not the right solution because it affects both encoding and decoding. The question was like: "don't make my code crash where libraries like Gson work well". |
@Andrew0000 You don't need to use the same configuration for encoding/decoding, so you can flip this option for each. |
@pdvrieze Well, this is true. But keeping 2 Json instances for encoding/decoding is +1 point to keep in mind when to use which (complexity). |
We try to make encoding and decoding symmetrical when possible, i.e. |
I totally understand this paradigm. While you could make 2 underlying fields (something like explicitEncodingNulls and explicitDecodingNulls) behind the explicitNulls property with a custom setter for those who needs that consistency. And ability to set that 2 fields independently for those who needs inconsistency. |
What is your use-case and why do you need this feature?
Ability to parse nullable properties when the keys are not present in the JSON.
Trying to parse this JSON string
{}
fails.The current workaround would be to add a default value
val foo: String? = null
. However, we don't always have access to the entities in order to apply the fix. One example is when trying to create external serializers for the SqlDelight autogenerated entities.Describe the solution you'd like
Add a configuration
acceptNonPresentKeys
which when set to true, the parser would automatically produceFoo(foo = null)
when{}
this is provided.The text was updated successfully, but these errors were encountered: