如何使用@serializable scala对象?

时间:2010-08-09 16:40:05

标签: scala serialization

我知道您可以将scala对象标记为@serializable,但我不明白之后如何处理该对象。我只是将其视为Java Serializable对象吗?

我想将对象序列化为字节流。有人能告诉我将序列化对象转换为字节数组或字符串的代码吗?

(谷歌对这个问题没有帮助)

关注:谢谢。我现在明白我可以像Java Serializable对象一样使用它。有时,明显的答案让我感到不安。

2 个答案:

答案 0 :(得分:13)

回答您的第一个问题:是的,您可以将其视为Java Serializable对象:


scala> @serializable object A
defined module A

scala> import java.io.ByteArrayOutputStream;
import java.io.ByteArrayOutputStream

scala> import java.io.ObjectOutputStream;
import java.io.ObjectOutputStream

scala> val baos = new ByteArrayOutputStream(1024)
baos: java.io.ByteArrayOutputStream = 

scala> val o = new ObjectOutputStream(baos)
o: java.io.ObjectOutputStream = java.io.ObjectOutputStream@3d689405

scala> o.writeObject(A)

scala> baos.toByteArray
res4: Array[Byte] = Array(-84, -19, 0, 5, 115, 114, 0, 24, 108, 105, 110, 101, 49, 51, 36, 111, 98, 106, 101, 99, 116, 36, 36, 105, 119, 36, 36, 105, 119, 36, 65, 36, 110, -104, -28, -53, -123, -97, -118, -36, 2, 0, 0, 120, 112)

scala> object B
defined module B

scala> o.writeObject(B)
java.io.NotSerializableException: B$
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    at .(:13)
    at .()
    at RequestResult$.(:9)
    at RequestResult$.()
    at RequestResult$scala_repl_result()
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at scala.tools.nsc.Interpreter$Request$$anonfun$loadAndRun$1$$anonfun$apply$18.apply(Interpreter.scala:981)
    at scala.tools.nsc.Interpreter$Request$...

如果要序列化为某种字符串格式,可能this library可能很有用,它将scala对象序列化为JSON。

答案 1 :(得分:9)

一般情况下,我不建议序列化对象,但它可能是一种通过网络发送“全局状态”的方法。

关于如何发送/接收数据,请查看java.io.ObjectOutputStream和java.io.ObjectInputStream。

scala> trait Test { def x : Int }
defined trait Test

scala> @serializable object Foo { var x = 5 }
defined module Foo


scala> import java.io._                                                     
import java.io._

scala> def write() {
     | val output = new ObjectOutputStream(new FileOutputStream("test.obj"))
     | output.writeObject(Foo)
     | output.close()
     | }
write: ()Unit


scala> write()

scala> def read() = {
     | val input = new ObjectInputStream(new FileInputStream("test.obj"))
     | val obj = input.readObject()
     | input.close()
     | obj
     | }
read: ()java.lang.Object

scala> Foo.x = 7

scala> val r = read()
r: Test = Foo$@2855e552

scala> r.x
res39: Int = 7

您看到......顶级对象在序列化方面效果不佳。但是,可以序列化嵌套对象。例如:

scala> @serializable
     | class SomeClass(var y : Int) {
     |   @serializable object X extends Test {  def x  = y }
     | }
defined class SomeClass


scala> def write(x : AnyRef) {
     | val output = new ObjectOutputStream(new FileOutputStream("test.obj"))
     | output.writeObject(x)
     | output.close()
     | }
write: (x : AnyRef)Unit

scala> def read[A] = {
     | val input = new ObjectInputStream(new FileInputStream("test.obj"))
     | val obj = input.readObject()
     | input.close()
     | obj.asInstanceOf[A]
     | }
read: [A]A

scala> write(x.X)

scala> val y = read[Test]
y: Test = SomeClass$X$@58e39f23

scala> y.x
res51: Int = 10

scala> x.y = 20  

scala> x.X.x
res52: Int = 20

scala> y.x
res53: Int = 10

希望有所帮助!