How write this in Kotlin Class MyClass implement ...
# android
h
How write this in Kotlin Class MyClass implement Serializable { Int id; String name; // getters & setters }
c
Copy code
@Parcelize
data class MyClass(
  val id: Int,
  val name: String
) : Parcelable
h
Thanks my friend
g
Wait, why does serializable replaced with parcelable? Parcelable is not a requirement, Kotlin supports standard Java Serialization too, but you should be careful with null ability Also it's not a requirement to make this class data and even move properties to constructor If you show full example of would be possible to suggest what is direct translation to Kotlin. Also java to Kotlin converter can do this automatically
c
They can annotate it with
@Serializable
as well
g
@Serializable is also not the same! It will not make class to work with Java Serialization
I do not say that
<http://java.io|java.io>.Serializable
is better than Parcelable on Android or usage of Kotlinx Serialization My point is that question was not “what is best way to serialize class in Kotlin”, it was about how to convert Java class to Kotlin. And answer is that just implement Serializable interface, same as in java, and yes, use Kotlin properties and constructor initialiser as you suggested Parcelable or kotlinx,serialization is much wider topic, you cannot use any of them and keep old code working, it requires migration of existing code