java - De-/Serializing with readObject/writeObject -
i'm having bit of struggle these writeobject/readobject methods.
let's have a
trait abstractposition{ def file:path def start:string def end:string }
with
class sourceposition(val: path, val start:string, val end:string) extends abstractposition object sourceposition { def apply(file: path, start: string, end: string) = new sourceposition(file, start, some(end)) def unapply(sp: sourceposition) = some((sp.file, sp.start, sp.end)) }
and have store such positions file. naive attempt fails because path objects not serializable:
java.io.notserializableexception: ... .sourceposition
so rewrite:
trait abstractposition extends serializable{ def file:path def start:string def end:string } class sourceposition(@transient var filearg: path, val start:string, val end:string) extends abstractposition{ private var filestring :string = null override def file: path = this.filearg @throws(classof[ioexception]) private def writeobject(out: objectoutputstream): unit = { filestring = file.tostring out.defaultwriteobject() } @throws(classof[ioexception]) private def readobject(in: objectinputstream): unit = { in.defaultreadobject() filearg = paths.get(filestring) } object sourceposition { def apply(file: path, start: string, end: string) = new sourceposition(file, start, some(end)) def unapply(sp: sourceposition) = some((sp.file, sp.start, sp.end)) }
but no avail:
java.io.notserializableexception: sun.nio.fs.windowspath$windowspathwithattributes
what doing wrong?
and how can achieve i'm trying do?
make sourceposition
case class: it's perfect candidate it's immutable. case classes serializable default without writeobject
/readobject
stuff. bonus apply
/unapply
methods generated automatically scalac.
Comments
Post a Comment