MyNixOS website logo
Description

MessagePack Serialization an Deserialization for Packer.

This package implements MessagePack on top of the Packer package.

packer-messagepack Hackage version Build Status

About

This package provides MessagePack serialization / deserialization built on top of Packer.

More precisely, this package exposes the following:

  • the type class ToMsgPack:
class ToMsgPack a where
  toMsgPack :: a -> Packing ()
  msgPackSize :: MonadThrow m => a -> m Int64
  • the type class FromMsgPack:
class FromMsgPack a where
  fromMsgPack :: Unpacking a
  • the type Object:
data Object = ObjectString Text
            | ObjectBinary ByteString
            | ObjectUInt Word64
            | ObjectInt Int64
            | ObjectBool Bool
            | ObjectFloat32 Float
            | ObjectFloat64 Double
            | ObjectArray [Object]
            | ObjectMap (Map Object Object)
            | ObjectNil
  • Instances for the following types:

    • Bool

    • Int

    • Word8

    • Word16

    • Word32

    • Word64

    • Int8

    • Int16

    • Int32

    • Int64

    • Float

    • Double

    • ByteString

    • Text

    • Object

    • Furthermore there are instances for

      • lists [a], if the type a is an instance of FromMsgPack resp. ToMsgPack.

      • maps Map k v if the types k and v are instances of FromMsgPack resp. ToMsgPack.

Usage

For example, to serialize a number into a MessagePack encoded ByteString, use:

let n = 2342 :: Int
size <- msgPackSize n
let bytes = runPacking size (toMsgPack n)

To deserialize a ByteString you can use fromMsgPack specialized to fromMsgPack :: Unpacking Object in case the type of the next MessagePack object is not known. For example:

let obj = runUnpacking fromMsgPack bytes :: Object

On the other hand, if a specific type is expected, fromMsgPack can be used specialized to the respective type as follows:

let n' = runUnpacking fromMsgPack bytes :: Int

Note that a MessagePack signed (resp. unsigned) integer can be as big as an Int64 (resp. Word64). Therefore, if you want to make sure that there are no overflow problems, use Int64 (resp. Word64) during deserialization. In case of overflows exceptions will be thrown. For example:

let n = (2^62) :: Int64
size <- msgPackSize n
let bytes = runPacking size (toMsgPack n)
    n' = runUnpacking fromMsgPack bytes :: Int32

Because the number 2^62 exceeds the boundaries of Int32, n' will denote a pure exception:

MsgPackDeserializationFailure "Integer Overflow"

Stackage

Currently, Packer is not included in Stackage yet. Therefore, if you would like to use this package together with Stackage, you could pull them in via extra-deps. For example:

extra-deps: [packer-VERSION, packer-messagepack-VERSION]
Metadata

Version

0.2.0.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows