Parse nullable properties with non-default values when the keys are not present in the Json
See original GitHub issueWhat 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.
@kotlinx.serialization.Serializable
data class Foo(val foo: String?)
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 produce
Foo(foo = null)
when {}
this is provided.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Handling nonexistent, null and multi value type properties in ...
In Power Automate cloud flow, Parse JSON action is used to access ... is the property values will not show in dynamic content...
Read more >Representing null in JSON - Stack Overflow
I would use null to show that there is no value for that particular key. For example, use null to represent that "number...
Read more >3 ways to ignore null fields while converting Java object to ...
You can ignore null fields at the class level by using @JsonInclude(Include.NON_NULL) to only include non-null fields, thus excluding any attribute whose value ......
Read more >JSON.parse() - JavaScript - MDN Web Docs
The JSON.parse() method parses a JSON string, constructing the JavaScript value or object described by the string.
Read more >Value Conversions - EF Core - Microsoft Learn
Value converters allow property values to be converted when reading from ... them to be shared amongst nullable and non-nullable properties.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Resurfacing this issue again I also just got bit by the same problem while trying to migrate from Moshi to kotlinx.serialization I had nullable properties that I expected to be initialized with
null
if the value is ever missing from the API, but instead, the app just crashed I believe it would be really nice to provide this option as an opt-in configuration, so whoever uses it is fully aware and there won’t be any confusion. This will also make transition to kotlinx.serialization from other libraries much smoothersee #1535 Added
Json { explicitNulls = false }
flag, target version is1.3.0