kotlinx-datetime icon indicating copy to clipboard operation
kotlinx-datetime copied to clipboard

Implement java.io.Serializable for some of the classes

Open dkhalanskyjb opened this issue 1 year ago • 7 comments

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

dkhalanskyjb avatar Mar 22 '24 14:03 dkhalanskyjb

Can you please merge this commit if possible? @ilya-g

Moozart avatar Apr 09 '24 14:04 Moozart

I don't understand the idea. Do you mean that LocalTime should be Externalizable?

From the docs (https://docs.oracle.com/javase%2F7%2Fdocs%2Fapi%2F%2F/java/io/Externalizable.html):

When an Externalizable object is reconstructed, an instance is created using the public no-arg constructor

But we don't have public no-arg constructors. I guess we could do something like class LocalTime { @PublishedApi internal constructor() }, which would make it a public constructor for the JVM, and trick Kotlin somehow to avoid initializing val value: java.time.LocalTime in that constructor, but is it worth it to make such intrusive changes just to support Java serialization?

dkhalanskyjb avatar Apr 22 '24 09:04 dkhalanskyjb

A very necessary implementation on Android to be able to pass LocalDate/LocalDateTime objects between screens

akhbulatov avatar Apr 26 '24 11:04 akhbulatov

@akhbulatov, it's not "necessary", just convenient: see https://github.com/Kotlin/kotlinx-datetime/issues/143#issuecomment-975380754

dkhalanskyjb avatar Apr 26 '24 12:04 dkhalanskyjb

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.

wkornewald avatar Jun 20 '24 06:06 wkornewald

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.

dkhalanskyjb avatar Aug 05 '24 09:08 dkhalanskyjb

I think it's good to go, but can't approve a pull request that I myself opened.

dkhalanskyjb avatar Aug 19 '24 14:08 dkhalanskyjb