sciss / serial

Contributors Wanted GNU Library General Public License v2.1 only GitHub

A simple serialization layer for the Scala programming language. Mirror of https://codeberg.org/sciss/Serial

Serial

Build Status Maven Central

statement

Serial provides a simple serialization layer for the Scala programming language. It is based on readers and writers of byte array or file backed up data streams.

Serial is (C)opyright 2011–2021 by Hanns Holger Rutz. All rights reserved. It is released under the GNU Lesser General Public License and comes with absolutely no warranties. To contact the author, send an e-mail to contact at sciss.de.

requirements / installation

The project builds with sbt against Scala 2.13, 2.12, Dotty (JVM) and Scala 2.13 (JS). The last version to support Scala 2.11 was v1.1.1.

linking

The following dependency is necessary:

"de.sciss" %% "serial" % v

The current version v is "2.0.1".

example

In most cases, you want to serialize immutable objects such as instances of case classes. For those, the ConstFormat is used.

import de.sciss.serial._

case class Person(name: String, age: Int)

implicit object PersonSerializer extends ConstFormat[Person] {
  def write(v: Person, out: DataOutput): Unit = {
    out.writeUTF(v.name)
    out.writeInt(v.age)
  }

  def read(in: DataInput): Person = {
    val name  = in.readUTF()
    val age   = in.readInt()
    Person(name, age)
  }
}

val p   = Person("Nelson", 94)
val out = DataOutput()
val ser = implicitly[ConstFormat[Person]]
ser.write(p, out)
val bin = out.toByteArray

val in  = DataInput(bin)
val q   = ser.read(in)
println(q)
assert(p == q)

There are serializers included for the standard primitive types and common extensions such as Option, Either, Tuple2, List etc.

Transactional provision

The library is mainly used in conjunction with the Lucre framework, where transactional context is needed. Here the types are TReader, TWriter and TFormat which provide an additional type parameter for the implicitly passed transaction. Since ConstReader[A] and ConstFormat[A] extend TReader[Any A] and TFormat[Any, A], the latter can be used throughout.