-
Notifications
You must be signed in to change notification settings - Fork 624
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
Unreliable serialization in output string (kotlinx date time types) #1626
Comments
I think i found the issue after reading serialization docs, it appears that this is default behaviour for default fields. However this feature does not make sense for every type of default field. |
For now as a workaround i initialize these fields in the init block, |
Had this issue when trying to assign a default long value and it seemed to randomly disappear. Initializing via the constructor works fine but only if i remove the default value as well. |
Hi!
Please note that the Also keep in mind that if the default value is calculated, then the property is serialized only if its actual value is different from that calculated at the time of checking during serialization. That is, if the default value depends on the time, then if enough time has passed between the creation of the object and the serialization, then the property will be serialized.
will unpredictably print an empty object or an object with an |
Ok all good for me, thank you |
Describe the bug
When I try to serialize an instance of this object with kotlinx serialization, the resulting string is inconsistent, sometimes (very rarely) it outputs test0 and test1 fields in the output, but more often than not those fields are not in the result as if they were null ?
Also while debugging these two fields are considered optional (elementsOptionality field) in the generated serializer class, so is id field but this is expected
To Reproduce
Simply deserialize this class
@Serializable data class TestX( val id: String? = null, val code: Int, val reason: String, val message: String?, val test0: LocalDateTime = LocalDateTime(2020, 5, 20, 17, 0, 0) ) { val test1 = Clock.System.now() }
and then
val jsonString = Json.encodeToString(test)
Expected behavior
Environment
The text was updated successfully, but these errors were encountered: