Last active
December 3, 2018 16:12
-
-
Save uzimith/da102976c4b4537f53ff4c6fe1499379 to your computer and use it in GitHub Desktop.
how to use msgpack4z
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
lazy val root = (project in file(".")). | |
settings( | |
libraryDependencies ++= Seq( | |
"com.github.xuwei-k" %% "msgpack4z-core" % "0.3.9", | |
"com.github.xuwei-k" %% "msgpack4z-native" % "0.3.5" | |
) | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import msgpack4z.{CaseCodec, MsgInBuffer, MsgOutBuffer, MsgpackCodec} | |
// 基本型の型クラスの読み込み | |
import msgpack4z.CodecInstances.all._ | |
// Eitherのcase class | |
import scalaz.{-\/, \/-} | |
object API { | |
case class UserRequest(foo: Int, bar: String, b: Boolean) | |
// 型クラスを定義 | |
implicit val instance: MsgpackCodec[UserRequest] = | |
// massage packがarrayのときCaseCodecを使う | |
// `packがシリアライズ、unpackがデシリアライズのメソッドです。ここで、あえてシリアライズとデシリアライズの型クラスを分けずに、必ず両方を定義しないといけない仕組みにしました。` | |
// https://xuwei-k.hatenablog.com/entry/20150106/1420506917 | |
// ということでserializeとdeserializeの関数を定義する | |
CaseCodec.codec(UserRequest.apply _, UserRequest.unapply _) | |
} | |
object Main extends App { | |
import API._ | |
val request = UserRequest(10, "hoge", true) | |
// deserialize | |
val deserialized = | |
MsgpackCodec[UserRequest].toBytes(request, MsgOutBuffer.create()) | |
println(deserialized) | |
// serialize | |
val serialized = | |
MsgpackCodec[UserRequest].unpackAndClose(MsgInBuffer(deserialized)) | |
serialized match { | |
// Right | |
case \/-(value) => | |
println(value) | |
// Left | |
case -\/(error) => | |
println(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment