Skip to content

NodeSeq.Empty is not serializable #154

Closed
@joescii

Description

@joescii

It appears that the typical use case of NodeSeq serializes with no problems (I'm speaking of Node, which we can see here is marked as Serializable), but the NodeSeq.Empty surprisingly doesn't. I would expect it to behave like String where the empty string is trivially serializable.

Is there any reason that NodeSeq should not extend Serializable? The offender in the case of NodeSeq.Empty could trivially be marked to extend Serializable here

You can see the behavior below in this console session. Note that java version details are printed at the end. This was run on a MBP running Sierra 10.12.6.

[info] Starting scala interpreter...
[info] 
Welcome to Scala 2.12.3 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_131).
Type in expressions for evaluation. Or try :help.

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

scala> import scala.xml.NodeSeq
import scala.xml.NodeSeq

scala>       def serialize(in: Any): Array[Byte] = {
     |         val bos = new ByteArrayOutputStream()
     |         val oos = new ObjectOutputStream(bos)
     |         oos.writeObject(in)
     |         oos.flush()
     |         bos.toByteArray()
     |       }
serialize: (in: Any)Array[Byte]

scala> serialize("joescii")
res0: Array[Byte] = Array(-84, -19, 0, 5, 116, 0, 7, 106, 111, 101, 115, 99, 105, 105)

scala> serialize("")
res3: Array[Byte] = Array(-84, -19, 0, 5, 116, 0, 0)

scala> serialize(<joescii/>)
res1: Array[Byte] = Array(-84, -19, 0, 5, 115, 114, 0, 14, 115, 99, 97, 108, 97, 46, 120, 109, 108, 46, 69, 108, 101, 109, -121, -95, 0, -70, 78, 93, -40, -69, 2, 0, 6, 90, 0, 13, 109, 105, 110, 105, 109, 105, 122, 101, 69, 109, 112, 116, 121, 76, 0, 10, 97, 116, 116, 114, 105, 98, 117, 116, 101, 115, 116, 0, 20, 76, 115, 99, 97, 108, 97, 47, 120, 109, 108, 47, 77, 101, 116, 97, 68, 97, 116, 97, 59, 76, 0, 5, 99, 104, 105, 108, 100, 116, 0, 22, 76, 115, 99, 97, 108, 97, 47, 99, 111, 108, 108, 101, 99, 116, 105, 111, 110, 47, 83, 101, 113, 59, 76, 0, 5, 108, 97, 98, 101, 108, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 76, 0, 6, 112, 114, 101, 102, 105, 120, 113, 0, 126, 0, 3, 76, 0, 5, 115, 99, 111, 112, 101, 11...

scala> val xml = <joescii/>
xml: scala.xml.Elem = <joescii/>

scala> serialize(xml)
res4: Array[Byte] = Array(-84, -19, 0, 5, 115, 114, 0, 14, 115, 99, 97, 108, 97, 46, 120, 109, 108, 46, 69, 108, 101, 109, -121, -95, 0, -70, 78, 93, -40, -69, 2, 0, 6, 90, 0, 13, 109, 105, 110, 105, 109, 105, 122, 101, 69, 109, 112, 116, 121, 76, 0, 10, 97, 116, 116, 114, 105, 98, 117, 116, 101, 115, 116, 0, 20, 76, 115, 99, 97, 108, 97, 47, 120, 109, 108, 47, 77, 101, 116, 97, 68, 97, 116, 97, 59, 76, 0, 5, 99, 104, 105, 108, 100, 116, 0, 22, 76, 115, 99, 97, 108, 97, 47, 99, 111, 108, 108, 101, 99, 116, 105, 111, 110, 47, 83, 101, 113, 59, 76, 0, 5, 108, 97, 98, 101, 108, 116, 0, 18, 76, 106, 97, 118, 97, 47, 108, 97, 110, 103, 47, 83, 116, 114, 105, 110, 103, 59, 76, 0, 6, 112, 114, 101, 102, 105, 120, 113, 0, 126, 0, 3, 76, 0, 5, 115, 99, 111, 112, 101, 11...

scala> serialize(NodeSeq.Empty)
java.io.NotSerializableException: scala.xml.NodeSeq$$anon$1

  at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1182)
  at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
  at .serialize(<console>:18)
  ... 39 elided

scala> 
descartes:lift-framework joescii$ java -version
java version "1.8.0_131"
Java(TM) SE Runtime Environment (build 1.8.0_131-b11)
Java HotSpot(TM) 64-Bit Server VM (build 25.131-b11, mixed mode)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions