File tree Expand file tree Collapse file tree 1 file changed +34
-3
lines changed Expand file tree Collapse file tree 1 file changed +34
-3
lines changed Original file line number Diff line number Diff line change 1
- case class Foo ()
1
+ case class Foo (s : String )
2
2
3
3
object Test {
4
- def main (args : Array [String ]): Unit =
5
- assert(Foo ().isInstanceOf [Serializable ])
4
+ def main (args : Array [String ]): Unit = {
5
+ val foo = Foo (" bar" )
6
+ assert(foo.isInstanceOf [Serializable ])
7
+ assert(SerDes .serializeDeserialize[Foo ](foo) == foo)
8
+ }
9
+ }
10
+
11
+ // From https://github.com/scala/scala/pull/5278
12
+ object SerDes {
13
+ import java .io ._
14
+
15
+ def assertNotSerializable (a : AnyRef ): Unit = {
16
+ try {
17
+ serialize(a)
18
+ assert(false )
19
+ } catch {
20
+ case _ : NotSerializableException => // okay
21
+ }
22
+ }
23
+
24
+ def serialize (obj : AnyRef ): Array [Byte ] = {
25
+ val buffer = new ByteArrayOutputStream
26
+ val out = new ObjectOutputStream (buffer)
27
+ out.writeObject(obj)
28
+ buffer.toByteArray
29
+ }
30
+
31
+ def deserialize (a : Array [Byte ]): AnyRef = {
32
+ val in = new ObjectInputStream (new ByteArrayInputStream (a))
33
+ in.readObject
34
+ }
35
+
36
+ def serializeDeserialize [T <: AnyRef ](obj : T ) = deserialize(serialize(obj)).asInstanceOf [T ]
6
37
}
You can’t perform that action at this time.
0 commit comments