Scala反序列化:找不到类 [英] Scala deserialization: class not found
问题描述
我试图理解在尝试序列化/反序列化一个非常简单的数据结构时出现的以下问题:
I'm trying to understand the following issue that occurs when trying to serialize/deserialize a very simple data structure:
case class SimpleClass(i: Int)
object SerializationDebug {
def main(args: Array[String]) {
val c = SimpleClass(0)
val l1 = List(c)
serializationSaveToFile("test", l1)
val l2 = serializationLoadFromFile("test") // .asInstanceOf ...
}
def serializationSaveToFile(fn: String, o: Any) {
val fos = new FileOutputStream(fn)
val oos = new ObjectOutputStream(fos)
oos.writeObject(o)
oos.close()
}
def serializationLoadFromFile(fn: String): Any = {
val fis = new FileInputStream(fn)
val ois = new ObjectInputStream(fis)
return ois.readObject()
}
}
尝试运行此代码时,我得到 java.lang.ClassNotFoundException:SimpleClass
在反序列化步骤中。我调查的当前结果是:
When trying to run this code I get java.lang.ClassNotFoundException: SimpleClass
in the deserialization step. The current results of my investigations are:
- 当我交换
SimpleClass
时,该示例起作用通过一些内置类型,即,我可以反序列化List [Int]
或List [(Int,Double)]
没有问题。再次将内置类型与我的SimpleClass
混合(即具有List [Any]
)会引发异常。 - 我试图在其他范围内定义
SimpleClass
(例如,嵌套在对象或本地范围内),但没有任何改变。另外,使用普通的(非大小写的)类扩展Serializable
也会得到相同的结果。 - 更令人困惑的是,使用
Array [SimpleClass]
代替List
确实有效!尝试其他容器可确认这种奇怪的不一致:在可变映射中将SimpleClass
作为类型参数作为不可变映射有效,
- The example works when I exchange
SimpleClass
by some built-in type, i.e., I can deserializeList[Int]
orList[(Int, Double)]
without a problem. Mixing built-in types with mySimpleClass
(i.e. having aList[Any]
) again throws an exception. - I tried to define
SimpleClass
in other scopes (for instance nested in the object or in the local scope even) but that did not change anything. Also, having a normal (non-case) class extendingSerializable
gives the same result. - Even more puzzling is that using an
Array[SimpleClass]
instead ofList
does work! Trying other containers confirms this strange inconsistency: havingSimpleClass
as type parameter within an immutable map works, in case of a mutable map I get the exception.
重要的是:我的Scala版本为2.10.0; JDK是1.7.0。
In case it matters: My Scala version is 2.10.0; JDK is 1.7.0.
这是怎么回事?这应该失败还是某种错误?我手头的实际问题涉及到更为复杂的数据结构(大量嵌套;内置类和自己的类的混合)。任何以最小干扰的简单方式(即不必查找容器类及其类型参数的有效组合)来序列化/反序列化此数据结构的建议也都欢迎!
What is going on here? Is this supposed to fail or is it some kind of bug? My actual problem at hand involves a much more complex data structure (a lot of nesting; mixture of built-in and own classes). Any suggestions to serializing/deserializing this data structure in a minimal-intrusive simple way (i.e. without having to find working combinations of container classes and their type parameters) are also welcome!
推荐答案
此解决方案对我来说效果很好:
This solution works fine for me:
val ois = new ObjectInputStream(new FileInputStream(fileName)) {
override def resolveClass(desc: java.io.ObjectStreamClass): Class[_] = {
try { Class.forName(desc.getName, false, getClass.getClassLoader) }
catch { case ex: ClassNotFoundException => super.resolveClass(desc) }
}
}
当然,当写入对象,我们使用相同的方式:
of course, when writing object, we use the same way:
val ois = new ObjectOutputStream(new FileOutputStream(path))
ois.writeObject(myobject)
这篇关于Scala反序列化:找不到类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!