Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pocketberserker/scala-zero-formatter
https://github.com/pocketberserker/scala-zero-formatter
scala scalajs serialization
Last synced: 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/pocketberserker/scala-zero-formatter
- Owner: pocketberserker
- License: mit
- Created: 2016-12-28T01:52:25.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2017-05-16T23:52:26.000Z (over 7 years ago)
- Last Synced: 2024-11-30T14:46:53.584Z (2 months ago)
- Topics: scala, scalajs, serialization
- Language: Scala
- Size: 205 KB
- Stars: 7
- Watchers: 3
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# scala-zero-formatter
[![Build Status](https://travis-ci.org/pocketberserker/scala-zero-formatter.svg?branch=master)](https://travis-ci.org/pocketberserker/scala-zero-formatter)
[![Scala.js](https://www.scala-js.org/assets/badges/scalajs-0.6.14.svg)](https://www.scala-js.org)Implementation of [ZeroFormatter](https://github.com/neuecc/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}`
```scala
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.
```scala
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](https://github.com/neuecc/ZeroFormatter/tree/1.5.7#wireformat-specification).
### Primitive Format
| Scala | C# | Note |
| ---- | ---- | --- |
| `Short` | `Int16` | |
| `Int` | `Int32`| |
| `Long` | `Int64` | |
| `spire.math.UShort` | `UInt16` | |
| `spire.math.UInt` | `UInt32` | |
| `spire.math.ULong` | `UInt64` | |
| `Float` | `Single` | |
| `Double` | ← | |
| `Boolean` | ← | |
| `spire.math.UByte` | `Byte` | |
| `Byte` | `SByte` | |
| `Char` | ← | |
| `Duration` | `TimeSpan` | |
| `LocalDateTime` | `DateTime` | JVM only. |
| `OffsetDateTime` | `DateTimeOffset` | JVM only. |
| `String` | ← | Scala 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
| Scala | C# | Note |
| ---- | ---- | --- |
| `Array[T]` | `Sequence` | if length = -1, indicates null |
| `Option[Array[T]]` | `Sequence` | if length = -1, indicates `None` |### List Format
| Scala | C# | Note |
| ---- | ---- | --- |
| `LazyList` | FixedSizeList | Stage2 support only `zeroformatter-cats-core` module |
| `LazyList` | VariableSizeList | Stage2 support only `zeroformatter-cats-core` module |### Object Format
| Scala | C# | Note |
| ---- | ---- | --- |
| Object | Object | if byteSize = -1, indicates null |
| Option[Object] | Object | if byteSize = -1, indicates `None` |
| `Struct` | Struct | |
| `Option[Struct]` | Struct? | |
| `Option[(A1, A2)]` | Tuple | |### Union Format
| Scala | C# | Note |
| ---- | ---- | --- |
| `Union` | Union | |