import java.io. {FileInputStream, FileOutputStream, ObjectInputStream, ObjectOutputStream}
object SymbolSerializeDemo {
def main(args: Array[String]): Unit = {
val fileName = "file.ser"
val symbolCheck: Symbol = Symbol("someSymbol")
//serializeToFile(symbolCheck, fileName)
deserializeFromFile(fileName)
}
private def serializeToFile(input: Symbol, fileName: String): Unit = {
try {
val file: FileOutputStream = new FileOutputStream(fileName)
val out: ObjectOutputStream = new ObjectOutputStream(file)
out.writeObject(input)
}
}
private def deserializeFromFile(fileName: String): Unit = {
try {
val file: FileInputStream = new FileInputStream(fileName)
val input: ObjectInputStream = new ObjectInputStream(file)
val output = input.readObject.asInstanceOf[Symbol]
println("Symbol after deseralization " output.name)
}
}
}
I am trying to deserialized scala symbol, serialized in scala 2.11 but I am getting error as java.io.InvalidClassException: scala.Symbol; local class incompatible: stream classdesc serialVersionUID = 2966401305346518859, local class serialVersionUID = 6865603221856321286
Can we write custom serialization for this or any other option?
I tried adding serialVersionUID for class as well as for Symbol
CodePudding user response:
Can you run Scala 2.11 from Scala 2.12 for deserialization or will it be too slow?
// Scala 2.12
import sys.process._
Symbol("scala-2.11.12/bin/scala path_to_deserializer/Deserializer.scala path_to_file/file.ser".!!)
// 'someSymbol
// Deserializer.scala
import java.io.{FileInputStream, ObjectInputStream}
object Deserializer {
def main(args: Array[String]): Unit = {
deserializeFromFile(args(0))
}
private def deserializeFromFile(fileName: String): Unit = {
val file: FileInputStream = new FileInputStream(fileName)
val input: ObjectInputStream = new ObjectInputStream(file)
val output = input.readObject.asInstanceOf[Symbol]
println(output.name)
}
}
CodePudding user response:
I have fixed the issue by downgrading scala version from 2.12.17 to 2.12.6.