-
Notifications
You must be signed in to change notification settings - Fork 635
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
@Optional annotation seems excessive #19
Comments
We've decided to require it there to be explicit about optionallity (at least initially). It also helps when you start moving your properties between primary constructor and class body. The parameters in the class body must always have an initialization expression, but it may not be conceptually appropriate to tread all of them as implicitly optional. Or maybe it is appropriate. We can reconsider this, but it really needed a larger design discussion. |
I prefer we infer |
In the current state where optional values are always written anyway this may be an option, but if we have optional mean that writing may be omitted by the format(encoder/decoder) a default optionality can create surprising effects. |
I second the idea of removing Optional and replacing it with Required. At the very least Optional should be implied for nullable types. |
+1 to @rawnsley 's If you have strong reasons to keep the existing functionality by default, another way is to make a global strategy and one to chose for himself: @Serializable
data class Foo(
val name: String?
)
Json(implyNullOptionals = true).parse(Foo.serializer(), "{ }") // this should print Foo(name=null) |
@elizarov @pdvrieze @sandwwraith I think that every field can be optional by default (or this option can be specified via Json constructor) and doesn't throw exceptions if it has default value. What do you think now after year while this issue exist? |
The way the system works is that missing values are actually verified by the (generated) (de)serializationstrategy. Any format would struggle supporting this as it would need to keep its own account of missing non-read values and implicitly read nulls. This is actually what my XML library does - it treats missing values as null (for now it doesn't yet look at optionality as that was not visible in older versions). It may be possible to create proxy strategies that make this work with default formats. I think that would probably be the way to go. |
Seems there are no cases when this feature would do any harm, so starting from Please note that you have to use Kotlin |
@sandwwraith What is alternative when kotlin version is < 1.3.30. |
@aayush26 Why don't you update to Kotlin 1.3.61? |
@aayush26 Specify |
For
@Optional
annotation default constructor parameter value is required. I guess we could consider all of parameter with default values as optionalThe text was updated successfully, but these errors were encountered: