Comments (11)
I prefer we infer Optional
and you override with Required
in the cases where you don't like the default behaviour. This would also match some other implementations of data binding that have been around a bit setting expectations :-)
from kotlinx.serialization.
@elizarov @pdvrieze @sandwwraith
Can you take look at this issue once more?
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.
In my situation I have client-server architecture. Configuration files can be edited by a user. He can make a mistake and then default value should be used since I don't want to give up and allow to server to die. Default values exist for every field and they are safe for every user.
What do you think now after year while this issue exist?
from kotlinx.serialization.
I second the idea of removing Optional and replacing it with Required.
At the very least Optional should be implied for nullable types.
from kotlinx.serialization.
+1 to @rawnsley 's At the very least Optional should be implied for nullable types
.
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)
from kotlinx.serialization.
Seems there are no cases when this feature would do any harm, so starting from 0.11.0
, optional annotation is deprecated and no longer required. Any properties with default values are considered optional.
Please note that you have to use Kotlin 1.3.30
or higher.
from kotlinx.serialization.
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.
from kotlinx.serialization.
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.
from kotlinx.serialization.
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.
from kotlinx.serialization.
@sandwwraith What is alternative when kotlin version is < 1.3.30.
Mine is 1.3.21 and not able to find any documentation for this.
from kotlinx.serialization.
@aayush26 Why don't you update to Kotlin 1.3.61?
from kotlinx.serialization.
@aayush26 Specify @Optional
explicitly. New library versions do not work with old Kotlin versions anyway.
from kotlinx.serialization.
Related Issues (20)
- kotlinx.serialization.SerializationException: Enum class Country does not contain element with name 'MISSING' at path $[2] HOT 1
- [Possible duplicate] Small inconsistency in the logic of encoding of fields with defaults HOT 1
- Didn't find class "kotlinx.serialization.json.Json" on path: DexPathList HOT 1
- typealiased @Serializable cannot be used as type hints HOT 1
- Value classes not using encodeInlineElement and inefficient boxing of primitive values
- Errors revealed only on run-time HOT 1
- ERROR: org.jetbrains.kotlinx.serialization.compiler.extensions.SerializationResolveExtension caused LinkageError HOT 3
- Allow annotation meta-annotations with Serializable
- Streaming performance: kotlinx-io & Okio integrations
- kotlinx.serialization.json deserialize error HOT 1
- Quoted booleans are being allowed even with `isLenient = false` HOT 6
- very strange serialization HOT 2
- The built-in String type (JsonPrimitive/JsonLiteral) puts double quotes around the string value HOT 8
- Don't check kind if needn't type discriminator for enum and primitive HOT 5
- 2.0.20 with generatedSerializer and reference to interface: SealedClassSerializer init: subclassSerializers contains null HOT 2
- [protobuf] How to encode/decode map with null value?
- Option to serialize enums as lowercase/snake_case HOT 4
- hande single quotes when `isLenient=true` HOT 3
- Support for `serializer<@Contextual T>` HOT 3
- Not a Bug But missing step in the Documentation when working with KMP HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from kotlinx.serialization.