question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parse nullable properties with non-default values when the keys are not present in the Json

See original GitHub issue

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.

@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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
kingsleyadiocommented, Oct 27, 2020

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 smoother

1reaction
shanshincommented, Aug 31, 2021

see #1535 Added Json { explicitNulls = false } flag, target version is 1.3.0

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found