I have a case class User
. I have to serialize it using kryo. here is my class.
while implementing read() of Kryo I'm having issue. my variables has val type. as we know we can't change value of val's.
case class User(name : String, age : int) extends KryoSerializable {
def this()={
this("",0)
}
//
override def read(kryo : Kryo, input : Input) {
// here i'm getting error. i can't override name because its val
name = input.readString()
age = input.readInt()
println("-----------Read method of Kryo")
}
override def write(kryo : Kryo, output : Output) {
output.writeString(name)
output.writeInt(age)
println("--------Write method of Kryo")
}
}
please guide me, how can I do that?
If you do not have any pressing reasons to use a case class
for user, I would recommend to not use a case class
. Because you have to declare name and age as variables. And a case class
is not supposed to be mutable.
Here is something, that might accomplish, what you want without making User
a case class
:
class User (private var _name : String, private var _age : Int) extends KryoSerializable {
def name = _name
def age = _age
override def read(kryo : Kryo, input : Input) {
// here i'm getting error. i can't override name because its val
_name = input.readString()
_age = input.readInt()
println("-----------Read method of Kryo")
}
override def write(kryo : Kryo, output : Output) {
output.writeString(_name)
output.writeInt(_age)
println("--------Write method of Kryo")
}
}
object User {
def apply(name: String, age: Int = 0): User = new User(name, age)
}