(reload) http://www.jianshu.com/p/e4bb5e23d9f5
All the articles in this series are my learning notes. If there are any inappropriate points, you are always welcome to make corrections.If you find my articles useful to you, please follow me and mine Kotlin Theme Let's learn and progress together!
Finally, there's a Kotlin article that doesn't follow the previous series of notes. This is the first question you'll encounter when trying to use Kotlin in your project: How do you serialize generics in Kotlin?We will solve this problem in two steps:
- Serialization of general data class es
- Serialization of generics
1. Serialization of general data class es
This question came up in my previous article, " Kotlin Learning Notes (9) - Data Class As mentioned in the book, here again, serialization in kotlin is essentially the same as manual implementation in java
data class PersonData(var name : String, var age : Int, val sex : String) : Parcelable{ override fun writeToParcel(p0: Parcel?, p1: Int) { p0?.writeString(this.name) p0?.writeInt(this.age) p0?.writeString(this.sex) } override fun describeContents(): Int { return 0 } constructor(source: Parcel) : this(source.readString(), source.readInt(), source.readString()) companion object { @JvmField val CREATOR: Parcelable.Creator<PersonData> = object : Parcelable.Creator<PersonData> { override fun createFromParcel(source: Parcel): PersonData { return PersonData(source) } override fun newArray(size: Int): Array<PersonData?> { return arrayOfNulls(size) } } } }
It's worth mentioning that when I write " Kotlin Learning Notes (9) - Data Class "When, it can only be written like this.But now there are plug-ins for automatic serialization on AS: select Plugins in the Settings of AS and search for "Parcelable Code Generator(for kotlin)" to install.I believe that many friends have used its Andorid version, using the same basic method, using the effect as shown in the figure:

2. Serialization of Generics
Generics don't have to be said much, I'm in Kotlin Learning Notes (10) - Generics "Is also described in detail (this advertisement is too natural, wow click), personal use of the scene should be to process the results returned by the server.If it happens that this result model needs to be passed between activities, is it necessary to consider generic serialization?First, see how I did it:
data class Resp<T: Parcelable>(var code: Int = 0, var msg: String = "", var data: T? = null) : Parcelable { val EMPTY = "empty" constructor(source: Parcel) : this() { code = source.readInt() msg = source.readString() val className = source.readString() data = if(className == EMPTY) null else source.readParcelable(Class.forName(className).getClassLoader()) } fun classLoader() : ClassLoader { val type = javaClass.genericSuperclass val params = (type as ParameterizedType).actualTypeArguments return (params[0] as Class<T>).classLoader } override fun describeContents(): Int { return 0 } override fun writeToParcel(dest: Parcel, flags: Int) { dest.writeInt(this.code) dest.writeString(this.msg) dest.writeString(if(this.data==null) EMPTY else this.data!!::class.java.name) dest.writeParcelable(this.data, flags) } companion object { @JvmField val CREATOR: Parcelable.Creator<Resp<Parcelable>> = object : Parcelable.Creator<Resp<Parcelable>> { override fun createFromParcel(source: Parcel): Resp<Parcelable> { return Resp<Parcelable>(source) } override fun newArray(size: Int): Array<Resp<Parcelable>?> { return arrayOfNulls(size) } } } }
The following points should be noted:
-
Generic T must explicitly declare serializable, that is, my generic type <T: Parcelable>.
-
In declarative parameters of data streams, generic parameters must have default values, usually null.My habit is to give default values to all parameters so that I can have a parameterless constructor.
-
By saving generic types, we reach the problem of not getting a generic ClassLoader.That is the most important point of this article:
constructor(source: Parcel) : this() { ... val className = source.readString() data = if(className == EMPTY) null else source.readParcelable(Class.forName(className).getClassLoader()) } override fun writeToParcel(dest: Parcel, flags: Int) { ... dest.writeString(if(this.data==null) EMPTY else this.data!!::class.java.name) dest.writeParcelable(this.data, flags) }
Author: I love chestnuts
Link: http://www.jianshu.com/p/e4bb5e23d9f5
Source: Short Book
Copyright belongs to the author.For commercial reprinting, please contact the author for authorization. For non-commercial reprinting, please indicate the source.