-
Notifications
You must be signed in to change notification settings - Fork 102
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 on the JVM #143
Comments
Hi! Could you please share the reasons why you can't use |
Hi @dkhalanskyjb, The proposed solution with We will then use However I am not sure if |
Not at all. We don't currently give any guarantees about the internal representation of our classes; just annotating Java with So, we would need to have a good, solid reason to implement |
Not related to android. |
What do you think about writing An overly cautious example that can be slightly simplified if you know what you're doing: public data class LocalDateModel(@Transient var value: LocalDate): java.io.Serializable {
private fun writeObject(oStream: ObjectOutputStream) {
oStream.defaultWriteObject()
oStream.writeObject(value.toString())
}
private fun readObject(iStream: ObjectInputStream) {
iStream.defaultReadObject()
value = LocalDate.parse(iStream.readObject() as String)
}
private fun readObjectNoData() {
throw InvalidObjectException("Stream data required")
}
} (be careful to check out |
Hi @dkhalanskyjb, |
I just hit this issue.
|
@4brunu, does the adapter pattern, shown above, not solve the issue? |
It was not strait forward, I needed to create type alias to a lot of abstractions, but in the end, it worked. In case it helps anyone, I leave here the url to a sample project that I created to show how to use kotlinx-datetime with parcelable. https://github.com/4brunu/kotlinx-datetime-parcelize-issue |
Maybe including the adapters on the android target was already a step to make the integration with Parcelize/Serialization easier. |
I just ran into the same problem on android. In my case it is about kotlin.time.Duration, which is inline class represented by Long. |
|
Ah 🤦 |
forces to same issue when trying to use Android Navigation component with arguments - that require parcelable or serializable.
|
@mtrakal, did you try this #143 (comment) ? |
@dkhalanskyjb not so easy to wrap every instance of I understand that you don't want to be dependant on java classes, on second side, it's still there edit: import android.os.Parcel
import kotlinx.parcelize.Parceler
import kotlin.time.Duration
/**
* Usage:
* For whole class:
* @Parcelize
* @TypeParceler<Duration, DurationClassParceler>()
* class MyClass(val instant: Duration) : Parcelable
*
* For Property-local parceler
* @Parcelize
* class MyClass(@TypeParceler<Duration, DurationClassParceler>() val external: Duration) : Parcelable
*
* For Type-local parceler
* @Parcelize
* class MyClass(val external: @WriteWith<DurationClassParceler>() Duration) : Parcelable
*/
object DurationClassParceler : Parceler<Duration> {
override fun create(parcel: Parcel) = Duration.parse(parcel.readString()!!)
override fun Duration.write(parcel: Parcel, flags: Int) {
parcel.writeString(this.toString())
}
} and import android.os.Parcel
import kotlinx.datetime.Instant
import kotlinx.parcelize.Parceler
/**
* Usage:
* For whole class:
* @Parcelize
* @TypeParceler<Instant?, InstantClassParceler>()
* class MyClass(val instant: Instant?) : Parcelable
*
* For Property-local parceler
* @Parcelize
* class MyClass(@TypeParceler<Instant?, InstantClassParceler>() val external: Instant?) : Parcelable
*
* For Type-local parceler
* @Parcelize
* class MyClass(val external: @WriteWith<InstantClassParceler>() Instant?) : Parcelable
*/
object InstantClassParceler : Parceler<Instant?> {
override fun create(parcel: Parcel) = Instant.parse(parcel.readString()!!)
override fun Instant?.write(parcel: Parcel, flags: Int) {
parcel.writeString(this.toString())
}
} |
Maybe something like this https://github.com/chRyNaN/serialization-parcelable or https://github.com/AhmedMourad0/bundlizer would work, then: I don't understand the reason to write separate |
@mtrakal I think the nulls on your Instant aren't handled correctly. Perhaps this?
|
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
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
With some expect/actual code its also possible to only add |
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
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
This is a real pain and should be easy to solve. We have to wrap |
Hello,
Please make the Android
actual
types implement alsojava.io.Serializable
(not Kotlin@Serializable
as described in #37).This will allow us to make the complex types, which depends on
kotlinx.datetime.*
types eitherSerializable
orParcelable
in Android app.For instance,
java.time.Instant
ISSerializable
, however theactual
Android classkotlinx.datetime.Instant
is NOT.The text was updated successfully, but these errors were encountered: