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.

Provide default value information in SerialDescriptor

See original GitHub issue

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

github_iconTop GitHub Comments

2reactions
qwwdfsadcommented, Nov 13, 2020

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 should getElementDefaultValue(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.

0reactions
qwwdfsadcommented, Nov 18, 2020

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 default null 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?

Read more comments on GitHub >

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

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