Skip to content

Instantly share code, notes, and snippets.

@MarioAriasC
Created September 3, 2015 09:00
Show Gist options
  • Save MarioAriasC/de3676ac18e84e1c2703 to your computer and use it in GitHub Desktop.
Save MarioAriasC/de3676ac18e84e1c2703 to your computer and use it in GitHub Desktop.
Kryo serialization works out-of-the-box with Kotlin
package org.cakesolutions.spark.kryo
import com.esotericsoftware.kryo.Kryo
import com.esotericsoftware.kryo.io.Input
import com.esotericsoftware.kryo.io.Output
import java.io.FileInputStream
import java.io.FileOutputStream
fun main(args: Array<String>) {
val kryo = Kryo()
val output = Output(FileOutputStream("file.bin"))
val user = User("Mario", Address("Manchester", "Flat 86", "M###XX"))
kryo.writeObject(output, user)
output.close()
val input = Input(FileInputStream("file.bin"))
val u = kryo.readObject(input, javaClass<User>())
input.close()
println("u = ${u}")
}
data class Address(val city: String? = null, val line: String? = null, val postCode: String? = null)
data class User(val name: String? = null,
val address: Address? = null)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment