由于 scala.util.Marshal 已弃用,如何将旧代码迁移到 scala 2.10?
object Serilaizer{
def objectToBytes[T](foo: T)(implicit expected: ClassManifest[T]): Array[Byte] = {
Marshal.dump(foo)
}
def bytesToObject[T](fooBytes: Array[Byte])(implicit expected: ClassManifest[T]): Option[T] = {
Some(Marshal.load[T](fooBytes))
}
}
I saw SBinary https://github.com/harrah/sbinary但尚未发布。
这是到目前为止所提出的,实施原始 Marshal 转储/装载 http://harrah.github.io/browse/samples/library/scala/util/Marshal.scala.html代码(如scala 2.9),不确定这是最好的方法,但它似乎有效
object Serilaizer {
def objectToBytes[T](foo: T): Array[Byte] = { // replace Marshal.dump(foo)
val ba = new ByteArrayOutputStream()
val out = new ObjectOutputStream(ba)
out.writeObject(foo)
out.close()
ba.toByteArray
}
def bytesToObject[T](fooBytes: Array[Byte]): Option[T] = { // replace Marshal.load[T](foo)
if (userDataBytes != null) {
try {
val in = new ObjectInputStream(new ByteArrayInputStream(fooBytes))
val o = in.readObject.asInstanceOf[T]
in.close()
Some(o)
}
catch {
case e: Exception => {
throw new ClassCastException ("Serialization Problem", e)
None
}
}
}
else {
None
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)