-
Notifications
You must be signed in to change notification settings - Fork 101
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
Implement java.io.Serializable for some of the classes #373
base: master
Are you sure you want to change the base?
Conversation
e803d80
to
75a14a6
Compare
Can you please merge this commit if possible? @ilya-g |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better control over serialized representation I would suggest using writeReplace/readResolve with an Externalizable container.
core/jvm/src/Instant.kt
Outdated
@@ -111,6 +113,25 @@ public actual class Instant internal constructor(internal val value: jtInstant) | |||
|
|||
internal actual val MIN: Instant = Instant(jtInstant.MIN) | |||
internal actual val MAX: Instant = Instant(jtInstant.MAX) | |||
|
|||
@JvmStatic | |||
private val serialVersionUID: Long = 1L |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually use const val
for that.
I don't understand the idea. Do you mean that From the docs (https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/io/Externalizable.html):
But we don't have public no-arg constructors. I guess we could do something like |
A very necessary implementation on Android to be able to pass |
@akhbulatov, it's not "necessary", just convenient: see #143 (comment) |
75a14a6
to
9fc52b6
Compare
Implement java.io.Serializable for * Instant * LocalDate * LocalTime * LocalDateTime * UtcOffset TimeZone is not `Serializable` because its behavior is system-dependent. We can make it `java.io.Serializable` later if there is demand. We are using string representations instead of relying on Java's entities being `java.io.Serializable` so that we have more freedom to change our implementation later. Fixes #143
9fc52b6
to
32c9cc0
Compare
Do you have an estimate when this will get merged? It's a pretty huge inconvenience for us to wrap Instant because we want to export an SDK and the wrapping shouldn't be exposed to our users. It seems to be impossible without writing huge amounts of boilerplate. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take a look at how the list and set builders are serialized in stdlib. I believe we'd better have something similar here.
If you want, I can take over this branch and implement it accordingly.
core/jvm/src/Instant.kt
Outdated
} | ||
|
||
private fun writeObject(oStream: java.io.ObjectOutputStream) { | ||
oStream.defaultWriteObject() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe this would write all object fields first.
Do you mean https://github.com/JetBrains/kotlin/blob/3be0aa15f714726e4872c09b35f789652c46876d/libraries/stdlib/jvm/src/kotlin/collections/builders/ListBuilder.kt#L674? Ok, I think I got the idea, thanks. This does look better than reflection. It's not important to me who does this. |
import kotlinx.datetime.* | ||
import java.io.* | ||
|
||
internal class SerializedValue(var typeTag: Int, var value: Any?) : Externalizable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, we can place it in kotlinx.datetime
package and name it shortly, e.g. Ser
, to shave off some bytes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean, shave off some bytes from the serialized representation? I'm okay with that, as long as it still stays in the internal/
directory.
Does it make sense to mark this with @PublishedApi
to signal that it's an incompatible change to move/rename this class?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to mark this with @PublishedApi to signal that it's an incompatible change to move/rename this class?
Yes, I can do that
ce1dc4a
to
42fa2a5
Compare
import kotlinx.datetime.* | ||
import java.io.* | ||
|
||
internal class SerializedValue(var typeTag: Int, var value: Any?) : Externalizable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these values be private
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They can, but does it makes a difference for an internal class? Or you mean in order not to generate getters/setters?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, for the latter.
import kotlinx.datetime.* | ||
import java.io.* | ||
|
||
internal class SerializedValue(var typeTag: Int, var value: Any?) : Externalizable { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean, shave off some bytes from the serialized representation? I'm okay with that, as long as it still stays in the internal/
directory.
Does it make sense to mark this with @PublishedApi
to signal that it's an incompatible change to move/rename this class?
I think it's good to go, but can't approve a pull request that I myself opened. |
The reason I'm not clicking the "merge" button is that we are going to move |
Does this mean the stdlib will get a nice solution for adding Serializable support from within KMP code? I've contributed something to Ktor which could possibly serve as a starting point for a simple helper API in the stdlib: ktorio/ktor#4421 |
Implement java.io.Serializable for
TimeZone is not
Serializable
because its behavior is system-dependent. We can make itjava.io.Serializable
later if there is demand.We are using string representations instead of relying on Java's entities being
java.io.Serializable
so that we have more freedom to change our implementation later.Fixes #143