packer-messagepack: MessagePack Serialization an Deserialization for Packer

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain]

This package implements MessagePack on top of the Packer package.


[Skip to ReadMe]

Properties

Versions0.1.0.0, 0.1.0.0, 0.2.0.0
Change logNone available
Dependenciesbase (>=4.7 && <5), bytestring, containers, packer (>=0.1.9 && <0.2), safe-exceptions, text, unliftio [details]
LicenseBSD-3-Clause
Copyright(c) 2017 Moritz Schulte
AuthorMoritz Schulte
Maintainermtesseract@silverratio.net
CategoryData
Home pagehttps://github.com/mtesseract/packer-msgpack#readme
UploadedSun Oct 22 15:30:17 UTC 2017 by mtesseract

Modules

[Index]

Downloads

Maintainers' corner

For package maintainers and hackage trustees


Readme for packer-messagepack-0.1.0.0

[back to package description]

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:

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

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]