serialise-0.2.6.1: 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 HaskellSafe-Inferred
LanguageHaskell2010

Codec.Serialise

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.Serialise.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: cborg-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

Minimal complete definition

Nothing

Methods

encode :: a -> Encoding Source #

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

Since: 0.2.0.0

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

decode :: Decoder s a Source #

Definition of a given Decoder for a type.

Since: 0.2.0.0

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

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

Instances details
Serialise All Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Any Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise SomeTypeRep Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Version Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Void Source #

Since: 0.2.4.0

Instance details

Defined in Codec.Serialise.Class

Serialise CChar Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CClock Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CDouble Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CFloat Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CInt Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CIntMax Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CIntPtr Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CLLong Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CLong Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CPtrdiff Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CSChar Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CSUSeconds Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CShort Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CSigAtomic Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CSize Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CTime Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CUChar Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CUInt Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CUIntMax Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CUIntPtr Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CULLong Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CULong Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CUSeconds Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CUShort Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise CWchar Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Fingerprint Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise ExitCode Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Int16 Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Int32 Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Int64 Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Int8 Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Word16 Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Word32 Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Word64 Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Word8 Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise ByteString Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise ByteString Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise ShortByteString Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Term Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise IntSet Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise KindRep Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Ordering Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise TyCon Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise TypeLitSort Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Half Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Text Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Text Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Instance details

Defined in Codec.Serialise.Class

Serialise Integer Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Natural Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise () Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Bool Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Char Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Double Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Float Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Int Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Levity Source #

Since: 0.2.6.0

Instance details

Defined in Codec.Serialise.Class

Serialise RuntimeRep Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise VecCount Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise VecElem Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise Word Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (ZipList a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Complex a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Identity a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (First a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Last a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Down a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (First a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Last a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Max a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Min a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (WrappedMonoid a) Source # 
Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Dual a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Product a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Sum a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (IntMap a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Seq a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Tree a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Maybe a) Source #

Since: 0.2.4.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Vector a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (NonEmpty a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise (Maybe a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise a => Serialise [a] Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Methods

encode :: [a] -> Encoding Source #

decode :: Decoder s [a] Source #

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

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

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise (Fixed e) Source #

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Serialise (Proxy a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Typeable a => Serialise (TypeRep a) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.4.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.4.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.4.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.4.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

Methods

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

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

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

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

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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 b, Serialise c, Serialise d) => Serialise (a, b, c, d) Source #

Since: 0.2.0.0

Instance details

Defined in Codec.Serialise.Class

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

Instance details

Defined in Codec.Serialise.Class

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

Instance details

Defined in Codec.Serialise.Class

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

Instance details

Defined in Codec.Serialise.Class

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

Instance details

Defined in Codec.Serialise.Class

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

Instance details

Defined in Codec.Serialise.Class

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