scala-zero-formatter

March 18, 2017 · View on GitHub

Build Status Scala.js

Implementation of ZeroFormatter in Scala.

Latest stable version

libraryDependencies += "com.github.pocketberserker" %% "zero-formatter" % "0.7.0"
// JVM only
libraryDependencies += "com.github.pocketberserker" %% "zero-formatter-unsafe" % "0.7.0"
libraryDependencies += "com.github.pocketberserker" %% "zero-formatter-scalaz" % "0.7.0"
libraryDependencies += "com.github.pocketberserker" %% "zero-formatter-cats-core" % "0.7.0"
// JVM only
libraryDependencies += "com.github.pocketberserker" %% "akka-http-zero-formatter" % "0.7.0"
// JVM only
libraryDependencies += "com.github.pocketberserker" %% "zero-formatter-lz4" % "0.7.0"
// JVM only
libraryDependencies += "com.github.pocketberserker" %% "zero-formatter-zstd" % "0.7.0"

Usage

Define case class and fields mark as @Index, call ZeroFormatter.serialize[T]/deserialize[T}

import zeroformatter._

@ZeroFormattable
case class MyClass(
  @Index(0) age: Int,
  @Index(1) firstName: String,
  @Index(2) lastName: String,
  @Index(3) list: Vector[Int]
)

val mc = MyClass(99, "hoge", "fuga", Vector(1, 10, 100))

val bytes = ZeroFormatter.serialize(mc)
val mc2 = ZeroFormatter.deserialize[MyClass](bytes)

lazy-evaluation

If you use cats.Eval, case class deserialization is lazy-evaluation.

import cats.Eval
import zeroformatter._
import zeroformatter.cats._

@ZeroFormattable
case class LazyClass(
  @Index(0) age: Eval[Int],
  @Index(1) firstName: Eval[String],
  @Index(2) lastName: Eval[String],
  @Index(3) list: Eval[Vector[Int]]
)

val lc = ZeroFormatter.deserialize[LazyClass](bytes)

Caution

lazy-evaluation deserialization is supported only Object and LazyList.

Supported types

see also WireFormat Specification.

Primitive Format

ScalaC#Note
ShortInt16
IntInt32
LongInt64
spire.math.UShortUInt16
spire.math.UIntUInt32
spire.math.ULongUInt64
FloatSingle
Double
Boolean
spire.math.UByteByte
ByteSByte
Char
DurationTimeSpan
LocalDateTimeDateTimeJVM only.
OffsetDateTimeDateTimeOffsetJVM only.
StringScala and C# string is always nullable currently.
Option[Short]Int16?
Option[Int]Int32?
Option[Long]Int64?
Option[spire.math.UShort]UInt16?
Option[spire.math.UInt]UInt32?
Option[spire.math.ULong]UInt64?
Option[Float]Single?
Option[Double]Double?
Option[Boolean]Boolean?
Option[spire.math.UByte]Byte?
Option[Byte]SByte?
Option[Duration]TimeSpan?
Option[LocalDateTime]DateTime?JVM only.
Option[OffsetDateTime]DateTimeOffset?JVM only.
Option[String]

Sequence Format

ScalaC#Note
Array[T]Sequence<T>if length = -1, indicates null
Option[Array[T]]Sequence<T>if length = -1, indicates None

List Format

ScalaC#Note
LazyListFixedSizeListStage2 support only zeroformatter-cats-core module
LazyListVariableSizeListStage2 support only zeroformatter-cats-core module

Object Format

ScalaC#Note
ObjectObjectif byteSize = -1, indicates null
Option[Object]Objectif byteSize = -1, indicates None
StructStruct
Option[Struct]Struct?
Option[(A1, A2)]Tuple<A1, A2>

Union Format

ScalaC#Note
UnionUnion