Provide default value information in SerialDescriptor
See original GitHub issueWhat is your use-case and why do you need this feature?
It would be useful to retrieve the kotlin default value from the SerialDescriptor. Schema-based serialization formats that have the ability to specify a default value for a serialized field could use this information to enrich the generated schemas with informations about the default value.
Describe the solution you’d like
The SerialDescriptor should have an additional function that returns the “defaultValue” for an element.
Example:
@Serializable
data class Person(val name : String = "anonymous")
...
serialDescriptor.getElementDefaultValue(0) == "anonymous"
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (3 by maintainers)
Top Results From Across the Web
EncodeDefault - Kotlin
EncodeDefault ... Controls whether the target property is serialized when its value is equal to a default value, regardless of the format settings....
Read more >Jackson deserialization: How to get a default value even if the ...
If I have default values in A-class constructor, and have fields equal to null in JSON, all my default values are erased and...
Read more >Android Data Serialization Tutorial with the Kotlin Serialization ...
A transient property must have a default value. Consider the code example below: import kotlinx.serialization.Transient @Serializable data ...
Read more >kotlinx.serialization.SerialDescriptor
Serialization. Serial descriptor is used as bridge between decoders/encoders and serializers. When asking for a next element, the serializer provides an ...
Read more >Using the serialization plugin, there is an issue with code ...
String value="code" prefix: ERROR_EXPR 'Stub expression for default value of namespace' ... SerialDescriptor visibility:public [final]' type=kotlin.
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
It still is not really possible. We were thinking about it back in the day and weren’t able to come up with an elegant, complete and sound solution that works consistently over various serialization use-cases.
E.g. consider the following class:
@Serializable class Foo(val a: String, val b: String = a + " , but 'b'")
. What shouldgetElementDefaultValue(1)
return?And, as a library, we cannot provide an API in our very core that “works 50% of the time if the code is written in one way, but not in the other”, so, unfortunately, we cannot fix it.
It will be possible, but we are not going to do it for the same reasons: this is a half-baked solution that works 50% of the time. I realize that it solves your particular problem, but what the rest of the users should do?
What
getElementDefaultValue
should return? What type is it? How to distinguish defaultnull
from “this property actually doesn’t have a default value”? What should it return for complex default initializations? How to enforce this contract for authors of user-defined serializers and descriptors?