serialise-0.1.0.0: A binary serialisation library for Haskell values.

Copyright(c) Duncan Coutts 2015-2017
LicenseBSD3-style (see LICENSE.txt)
Maintainerduncan@community.haskell.org
Stabilityexperimental
Portabilitynon-portable (GHC extensions)
Safe HaskellNone
LanguageHaskell2010

Codec.Serialise

Contents

Description

This module provides functions to serialise and deserialise Haskell values for storage or transmission, to and from lazy ByteStrings. It also provides a type class and utilities to help you make your types serialisable.

For a full tutorial on using this module, see Codec.CBOR.Tutorial.

Synopsis

High level, one-shot API

The following API exposes a high level interface allowing you to quickly convert between arbitrary Haskell values (which are an instance of Serialise) and lazy ByteStrings.

serialise :: Serialise a => a -> ByteString Source #

Serialise a Haskell value to an external binary representation.

The output is represented as a lazy ByteString and is constructed incrementally.

Since: 0.2.0.0

deserialise :: Serialise a => ByteString -> a Source #

Deserialise a Haskell value from the external binary representation (which must have been made using serialise or related function).

Throws: DeserialiseFailure if the given external representation is invalid or does not correspond to a value of the expected type.

Since: 0.2.0.0

deserialiseOrFail :: Serialise a => ByteString -> Either DeserialiseFailure a Source #

Deserialise a Haskell value from the external binary representation, or get back a DeserialiseFailure.

Since: 0.2.0.0

Deserialisation exceptions

data DeserialiseFailure :: * #

An exception type that may be returned (by pure functions) or thrown (by IO actions) that fail to deserialise a given input.

Since: 0.2.0.0

Incremental encoding interface

The following API allows you to encode or decode CBOR values incrementally, which is useful for large structures that require you to stream values in over time.

serialiseIncremental :: Serialise a => a -> Builder Source #

Serialise a Haskell value to an external binary representation.

The output is represented as a Builder and is constructed incrementally. The representation as a Builder allows efficient concatenation with other data.

Since: 0.2.0.0

deserialiseIncremental :: Serialise a => ST s (IDecode s a) Source #

Deserialise a Haskell value from the external binary representation.

This allows input data to be provided incrementally, rather than all in one go. It also gives an explicit representation of deserialisation errors.

Note that the incremental behaviour is only for the input data, not the output value: the final deserialised value is constructed and returned as a whole, not incrementally.

Since: 0.2.0.0

data IDecode s a :: * -> * -> * #

An Incremental decoder, used to represent the result of attempting to run a decoder over a given input, and return a value of type a.

Constructors

Partial (Maybe ByteString -> ST s (IDecode s a))

The decoder has consumed the available input and needs more to continue. Provide Just if more input is available and Nothing otherwise, and you will get a new IDecode.

Done !ByteString !ByteOffset a

The decoder has successfully finished. Except for the output value you also get any unused input as well as the number of bytes consumed.

Fail !ByteString !ByteOffset DeserialiseFailure

The decoder ran into an error. The decoder either used fail or was not provided enough input. Contains any unconsumed input, the number of bytes consumed, and a DeserialiseFailure exception describing the reason why the failure occurred.

The Serialise class

class Serialise a where Source #

Types that are instances of the Serialise class allow values to be quickly encoded or decoded directly to a CBOR representation, for object transmission or storage.

Since: 0.2.0.0

Methods

encode :: a -> Encoding Source #

Definition for encoding a given type into a binary representation, using the Encoding Monoid.

Since: 0.2.0.0

encode :: (Generic a, GSerialiseEncode (Rep a)) => a -> Encoding Source #

Definition for encoding a given type into a binary representation, using the Encoding Monoid.

Since: 0.2.0.0

decode :: Decoder s a Source #

Definition of a given Decoder for a type.

Since: 0.2.0.0

decode :: (Generic a, GSerialiseDecode (Rep a)) => Decoder s a Source #

Definition of a given Decoder for a type.

Since: 0.2.0.0

encodeList :: [a] -> Encoding Source #

Utility to support specialised encoding for some list type - used for Char/String instances in this package.

Since: 0.2.0.0

decodeList :: Decoder s [a] Source #

Utility to support specialised decoding for some list type - used for Char/String instances in this package.

Since: 0.2.0.0

Instances

Serialise Bool Source #

Since: 0.2.0.0

Serialise Char Source #

Since: 0.2.0.0

Serialise Double Source #

Since: 0.2.0.0

Serialise Float Source #

Since: 0.2.0.0

Serialise Int Source #

Since: 0.2.0.0

Serialise Int8 Source #

Since: 0.2.0.0

Serialise Int16 Source #

Since: 0.2.0.0

Serialise Int32 Source #

Since: 0.2.0.0

Serialise Int64 Source #

Since: 0.2.0.0

Serialise Integer Source #

Since: 0.2.0.0

Serialise Natural Source #

Since: 0.2.0.0

Serialise Ordering Source #

Since: 0.2.0.0

Serialise Word Source #

Since: 0.2.0.0

Serialise Word8 Source #

Since: 0.2.0.0

Serialise Word16 Source #

Since: 0.2.0.0

Serialise Word32 Source #

Since: 0.2.0.0

Serialise Word64 Source #

Since: 0.2.0.0

Serialise RuntimeRep Source #

Since: 0.2.0.0

Serialise VecCount Source #

Since: 0.2.0.0

Serialise VecElem Source #

Since: 0.2.0.0

Serialise SomeTypeRep Source #

Since: 0.2.0.0

Serialise () Source #

Since: 0.2.0.0

Serialise TyCon Source #

Since: 0.2.0.0

Serialise KindRep Source #

Since: 0.2.0.0

Serialise TypeLitSort Source #

Since: 0.2.0.0

Serialise Version Source #

Since: 0.2.0.0

Serialise ExitCode Source #

Since: 0.2.0.0

Serialise All Source #

Since: 0.2.0.0

Serialise Any Source #

Since: 0.2.0.0

Serialise CChar Source #

Since: 0.2.0.0

Serialise CSChar Source #

Since: 0.2.0.0

Serialise CUChar Source #

Since: 0.2.0.0

Serialise CShort Source #

Since: 0.2.0.0

Serialise CUShort Source #

Since: 0.2.0.0

Serialise CInt Source #

Since: 0.2.0.0

Serialise CUInt Source #

Since: 0.2.0.0

Serialise CLong Source #

Since: 0.2.0.0

Serialise CULong Source #

Since: 0.2.0.0

Serialise CLLong Source #

Since: 0.2.0.0

Serialise CULLong Source #

Since: 0.2.0.0

Serialise CFloat Source #

Since: 0.2.0.0

Serialise CDouble Source #

Since: 0.2.0.0

Serialise CPtrdiff Source #

Since: 0.2.0.0

Serialise CSize Source #

Since: 0.2.0.0

Serialise CWchar Source #

Since: 0.2.0.0

Serialise CSigAtomic Source #

Since: 0.2.0.0

Serialise CClock Source #

Since: 0.2.0.0

Serialise CTime Source #

Since: 0.2.0.0

Serialise CUSeconds Source #

Since: 0.2.0.0

Serialise CSUSeconds Source #

Since: 0.2.0.0

Serialise CIntPtr Source #

Since: 0.2.0.0

Serialise CUIntPtr Source #

Since: 0.2.0.0

Serialise CIntMax Source #

Since: 0.2.0.0

Serialise CUIntMax Source #

Since: 0.2.0.0

Serialise Fingerprint Source #

Since: 0.2.0.0

Serialise ByteString Source #

Since: 0.2.0.0

Serialise ByteString Source #

Since: 0.2.0.0

Serialise Text Source #

Since: 0.2.0.0

Serialise Term Source #

Since: 0.2.0.0

Serialise IntSet Source #

Since: 0.2.0.0

Serialise Half Source #

Since: 0.2.0.0

Serialise Text Source #

Since: 0.2.0.0

Serialise UTCTime Source #

UTCTime is encoded using the extended time format which is currently in Internet Draft state, https://tools.ietf.org/html/draft-bormann-cbor-time-tag-00.

Since: 0.2.0.0

Serialise a => Serialise [a] Source #

Since: 0.2.0.0

Methods

encode :: [a] -> Encoding Source #

decode :: Decoder s [a] Source #

encodeList :: [[a]] -> Encoding Source #

decodeList :: Decoder s [[a]] Source #

Serialise a => Serialise (Maybe a) Source #

Since: 0.2.0.0

(Serialise a, Integral a) => Serialise (Ratio a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Complex a) Source #

Since: 0.2.0.0

Serialise (Fixed e) Source #

Values are serialised in units of least precision represented as Integer.

Since: 0.2.0.0

Serialise a => Serialise (Min a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Max a) Source #

Since: 0.2.0.0

Serialise a => Serialise (First a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Last a) Source #

Since: 0.2.0.0

Serialise a => Serialise (WrappedMonoid a) Source # 
Serialise a => Serialise (Option a) Source #

Since: 0.2.0.0

Serialise a => Serialise (NonEmpty a) Source #

Since: 0.2.0.0

Serialise a => Serialise (ZipList a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Identity a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Dual a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Sum a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Product a) Source #

Since: 0.2.0.0

Serialise a => Serialise (First a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Last a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Down a) Source #

Since: 0.2.0.0

Serialise a => Serialise (IntMap a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Tree a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Seq a) Source #

Since: 0.2.0.0

(Ord a, Serialise a) => Serialise (Set a) Source #

Since: 0.2.0.0

(Serialise a, Hashable a, Eq a) => Serialise (HashSet a) Source #

Since: 0.2.0.0

(Serialise a, Unbox a) => Serialise (Vector a) Source #

Since: 0.2.0.0

(Serialise a, Storable a) => Serialise (Vector a) Source #

Since: 0.2.0.0

(Serialise a, Prim a) => Serialise (Vector a) Source #

Since: 0.2.0.0

Serialise a => Serialise (Vector a) Source #

Since: 0.2.0.0

(Serialise a, Serialise b) => Serialise (Either a b) Source #

Since: 0.2.0.0

Typeable k a => Serialise (TypeRep k a) Source #

Since: 0.2.0.0

(Serialise a, Serialise b) => Serialise (a, b) Source #

Since: 0.2.0.0

Methods

encode :: (a, b) -> Encoding Source #

decode :: Decoder s (a, b) Source #

encodeList :: [(a, b)] -> Encoding Source #

decodeList :: Decoder s [(a, b)] Source #

Serialise (Proxy k a) Source #

Since: 0.2.0.0

(Ord k, Serialise k, Serialise v) => Serialise (Map k v) Source #

Since: 0.2.0.0

(Serialise k, Hashable k, Eq k, Serialise v) => Serialise (HashMap k v) Source #

Since: 0.2.0.0

(Serialise a, Serialise b, Serialise c) => Serialise (a, b, c) Source #

Since: 0.2.0.0

Methods

encode :: (a, b, c) -> Encoding Source #

decode :: Decoder s (a, b, c) Source #

encodeList :: [(a, b, c)] -> Encoding Source #

decodeList :: Decoder s [(a, b, c)] Source #

Serialise a => Serialise (Const k a b) Source #

Since: 0.2.0.0

Methods

encode :: Const k a b -> Encoding Source #

decode :: Decoder s (Const k a b) Source #

encodeList :: [Const k a b] -> Encoding Source #

decodeList :: Decoder s [Const k a b] Source #

Serialise (f a) => Serialise (Alt k f a) Source #

Since: 0.2.0.0

Methods

encode :: Alt k f a -> Encoding Source #

decode :: Decoder s (Alt k f a) Source #

encodeList :: [Alt k f a] -> Encoding Source #

decodeList :: Decoder s [Alt k f a] Source #

(Serialise a, Serialise b, Serialise c, Serialise d) => Serialise (a, b, c, d) Source #

Since: 0.2.0.0

Methods

encode :: (a, b, c, d) -> Encoding Source #

decode :: Decoder s (a, b, c, d) Source #

encodeList :: [(a, b, c, d)] -> Encoding Source #

decodeList :: Decoder s [(a, b, c, d)] Source #

(Serialise a, Serialise b, Serialise c, Serialise d, Serialise e) => Serialise (a, b, c, d, e) Source #

Since: 0.2.0.0

Methods

encode :: (a, b, c, d, e) -> Encoding Source #

decode :: Decoder s (a, b, c, d, e) Source #

encodeList :: [(a, b, c, d, e)] -> Encoding Source #

decodeList :: Decoder s [(a, b, c, d, e)] Source #

(Serialise a, Serialise b, Serialise c, Serialise d, Serialise e, Serialise f) => Serialise (a, b, c, d, e, f) Source #

Since: 0.2.0.0

Methods

encode :: (a, b, c, d, e, f) -> Encoding Source #

decode :: Decoder s (a, b, c, d, e, f) Source #

encodeList :: [(a, b, c, d, e, f)] -> Encoding Source #

decodeList :: Decoder s [(a, b, c, d, e, f)] Source #

(Serialise a, Serialise b, Serialise c, Serialise d, Serialise e, Serialise f, Serialise g) => Serialise (a, b, c, d, e, f, g) Source #

Since: 0.2.0.0

Methods

encode :: (a, b, c, d, e, f, g) -> Encoding Source #

decode :: Decoder s (a, b, c, d, e, f, g) Source #

encodeList :: [(a, b, c, d, e, f, g)] -> Encoding Source #

decodeList :: Decoder s [(a, b, c, d, e, f, g)] Source #

(Serialise a, Serialise b, Serialise c, Serialise d, Serialise e, Serialise f, Serialise g, Serialise h) => Serialise (a, b, c, d, e, f, g, h) Source #

Since: 0.2.0.0

Methods

encode :: (a, b, c, d, e, f, g, h) -> Encoding Source #

decode :: Decoder s (a, b, c, d, e, f, g, h) Source #

encodeList :: [(a, b, c, d, e, f, g, h)] -> Encoding Source #

decodeList :: Decoder s [(a, b, c, d, e, f, g, h)] Source #

(Serialise a, Serialise b, Serialise c, Serialise d, Serialise e, Serialise f, Serialise g, Serialise h, Serialise i) => Serialise (a, b, c, d, e, f, g, h, i) Source #

Since: 0.2.0.0

Methods

encode :: (a, b, c, d, e, f, g, h, i) -> Encoding Source #

decode :: Decoder s (a, b, c, d, e, f, g, h, i) Source #

encodeList :: [(a, b, c, d, e, f, g, h, i)] -> Encoding Source #

decodeList :: Decoder s [(a, b, c, d, e, f, g, h, i)] Source #

IO operations

Convenient utilities for basic IO operations.

FilePath API

writeFileSerialise Source #

Arguments

:: Serialise a 
=> FilePath

The file to write to.

-> a

The value to be serialised and written.

-> IO () 

Serialise a ByteString and write it directly to the specified file.

Since: 0.2.0.0

readFileDeserialise Source #

Arguments

:: Serialise a 
=> FilePath

The file to read from.

-> IO a

The deserialised value.

Read the specified file (internally, by reading a ByteString) and attempt to decode it into a Haskell value using deserialise (the type of which is determined by the choice of the result type).

Throws: DeserialiseFailure if the file fails to deserialise properly.

Since: 0.2.0.0

Handle API

hPutSerialise Source #

Arguments

:: Serialise a 
=> Handle

The Handle to write to.

-> a

The value to be serialised and written.

-> IO () 

Serialise a ByteString (via serialise) and write it directly to the specified Handle.

Since: 0.2.0.0