bytes-0.15.3: Sharing code for serialization between binary and cereal

Copyright(c) Edward Kmett 2013-2015
LicenseBSD3
MaintainerEdward Kmett <ekmett@gmail.com>
Stabilityexperimental
Portabilitynon-portable
Safe HaskellTrustworthy
LanguageHaskell98

Data.Bytes.Serial

Contents

Description

This module contains two main classes, each providing methods to serialize and deserialize types. Serial is the primary class, to be used for the canonical way to serialize a specific type. SerialEndian is used to provide endian-specific methods for serializing a type.

Synopsis

Serialization

class Serial a where Source #

Methods to serialize and deserialize type a to a binary representation

Instances provided here for fixed-with Integers and Words are big endian. Instances for strict and lazy bytestrings store also the length of bytestring big endian. Instances for Word and Int are host endian as they are machine-specific types.

Methods

serialize :: MonadPut m => a -> m () Source #

serialize :: (MonadPut m, GSerial (Rep a), Generic a) => a -> m () Source #

deserialize :: MonadGet m => m a Source #

deserialize :: (MonadGet m, GSerial (Rep a), Generic a) => m a Source #

Instances

Serial Bool Source # 

Methods

serialize :: MonadPut m => Bool -> m () Source #

deserialize :: MonadGet m => m Bool Source #

Serial Char Source # 

Methods

serialize :: MonadPut m => Char -> m () Source #

deserialize :: MonadGet m => m Char Source #

Serial Double Source # 
Serial Float Source # 

Methods

serialize :: MonadPut m => Float -> m () Source #

deserialize :: MonadGet m => m Float Source #

Serial Int Source # 

Methods

serialize :: MonadPut m => Int -> m () Source #

deserialize :: MonadGet m => m Int Source #

Serial Int8 Source # 

Methods

serialize :: MonadPut m => Int8 -> m () Source #

deserialize :: MonadGet m => m Int8 Source #

Serial Int16 Source # 

Methods

serialize :: MonadPut m => Int16 -> m () Source #

deserialize :: MonadGet m => m Int16 Source #

Serial Int32 Source # 

Methods

serialize :: MonadPut m => Int32 -> m () Source #

deserialize :: MonadGet m => m Int32 Source #

Serial Int64 Source # 

Methods

serialize :: MonadPut m => Int64 -> m () Source #

deserialize :: MonadGet m => m Int64 Source #

Serial Integer Source #
>>> (runGetL deserialize $ runPutL $ serialize (1822304234^100::Integer))::Integer
115368812579128172803867366576339947332796540054052185472042218522037227934707037623902492207671987696439966697503243972076991940820348847422930433939639982092916577692754723458548819441583937289395076910527534916776189405228720063994377687015476947534961767053653973945346259230972683338173842343243493433367681264359887291905132383269175086733345253389374961758293922003996035662362278340494093804835649459223465051596978792130073960666112508481814461273829244289795707398202762289955919352549768394583446336873179280924584333491364188425976869717125645749497258775598562132278030402205794994603544837805140410310712693778605743100915046769381631247123664460203591228745772887977959388457679427407639421147498028487544882346912935398848298806021505673449774474457435816552278997100556732447852816961683577731381792363312695347606768120122976105200574809419685234274705929886121600174028733812771637390342332436695318974693376
Serial Ordering Source #
>>> runGetL deserialize $ runPutL $ serialize LT::Ordering
LT
>>> runGetL deserialize $ runPutL $ serialize EQ::Ordering
EQ
>>> runGetL deserialize $ runPutL $ serialize GT::Ordering
GT
Serial Word Source # 

Methods

serialize :: MonadPut m => Word -> m () Source #

deserialize :: MonadGet m => m Word Source #

Serial Word8 Source # 

Methods

serialize :: MonadPut m => Word8 -> m () Source #

deserialize :: MonadGet m => m Word8 Source #

Serial Word16 Source # 
Serial Word32 Source # 
Serial Word64 Source # 
Serial () Source # 

Methods

serialize :: MonadPut m => () -> m () Source #

deserialize :: MonadGet m => m () Source #

Serial Natural Source #
>>> runGetL deserialize (runPutL (serialize (10^10::Natural))) :: Natural
10000000000
Serial Void Source # 

Methods

serialize :: MonadPut m => Void -> m () Source #

deserialize :: MonadGet m => m Void Source #

Serial Version Source # 
Serial All Source # 

Methods

serialize :: MonadPut m => All -> m () Source #

deserialize :: MonadGet m => m All Source #

Serial Any Source # 

Methods

serialize :: MonadPut m => Any -> m () Source #

deserialize :: MonadGet m => m Any Source #

Serial ByteString Source # 
Serial ByteString Source # 
Serial IntSet Source # 
Serial Scientific Source # 
Serial Text Source # 

Methods

serialize :: MonadPut m => Text -> m () Source #

deserialize :: MonadGet m => m Text Source #

Serial Text Source # 

Methods

serialize :: MonadPut m => Text -> m () Source #

deserialize :: MonadGet m => m Text Source #

Serial AbsoluteTime Source #
>>> (runGetL deserialize $ runPutL $ serialize (addAbsoluteTime 18.2 taiEpoch))::AbsoluteTime
1858-11-17 00:00:18.2 TAI
Serial LocalTime Source # 
Serial ZonedTime Source # 
Serial TimeOfDay Source # 
Serial TimeZone Source # 
Serial UTCTime Source #
>>> (runGetL deserialize $ runPutL $ serialize (read "2014-01-01 10:54:42.478031 UTC"::UTCTime))::UTCTime
2014-01-01 10:54:42.478031 UTC
Serial NominalDiffTime Source #
>>> (runGetL deserialize $ runPutL $ serialize (1.82::DiffTime))::DiffTime
1.82s
Serial Day Source #
>>> (runGetL deserialize $ runPutL $ serialize (ModifiedJulianDay 1))::Day
1858-11-18

Methods

serialize :: MonadPut m => Day -> m () Source #

deserialize :: MonadGet m => m Day Source #

Serial UniversalTime Source #
>>> getModJulianDate $ (runGetL deserialize $ runPutL $ serialize (ModJulianDate $ 5 % 11)::UniversalTime)
5 % 11
Serial DiffTime Source #
>>> (runGetL deserialize $ runPutL $ serialize (1.82::DiffTime))::DiffTime
1.82s
Serial a => Serial [a] Source # 

Methods

serialize :: MonadPut m => [a] -> m () Source #

deserialize :: MonadGet m => m [a] Source #

Serial a => Serial (Maybe a) Source # 

Methods

serialize :: MonadPut m => Maybe a -> m () Source #

deserialize :: MonadGet m => m (Maybe a) Source #

(Serial a, Integral a) => Serial (Ratio a) Source #
>>> (runGetL deserialize $ runPutL $ serialize (5 % 11::Ratio Int))::Ratio Int
5 % 11

Methods

serialize :: MonadPut m => Ratio a -> m () Source #

deserialize :: MonadGet m => m (Ratio a) Source #

Serial a => Serial (Identity a) Source # 

Methods

serialize :: MonadPut m => Identity a -> m () Source #

deserialize :: MonadGet m => m (Identity a) Source #

HasResolution a => Serial (Fixed a) Source #
>>> (runGetL deserialize $ runPutL $ serialize (1.82::Fixed E2))::Fixed E2
1.82

Methods

serialize :: MonadPut m => Fixed a -> m () Source #

deserialize :: MonadGet m => m (Fixed a) Source #

Serial a => Serial (ZipList a) Source # 

Methods

serialize :: MonadPut m => ZipList a -> m () Source #

deserialize :: MonadGet m => m (ZipList a) Source #

Serial a => Serial (Dual a) Source # 

Methods

serialize :: MonadPut m => Dual a -> m () Source #

deserialize :: MonadGet m => m (Dual a) Source #

Serial a => Serial (Sum a) Source # 

Methods

serialize :: MonadPut m => Sum a -> m () Source #

deserialize :: MonadGet m => m (Sum a) Source #

Serial a => Serial (Product a) Source # 

Methods

serialize :: MonadPut m => Product a -> m () Source #

deserialize :: MonadGet m => m (Product a) Source #

Serial a => Serial (First a) Source # 

Methods

serialize :: MonadPut m => First a -> m () Source #

deserialize :: MonadGet m => m (First a) Source #

Serial a => Serial (Last a) Source # 

Methods

serialize :: MonadPut m => Last a -> m () Source #

deserialize :: MonadGet m => m (Last a) Source #

Serial a => Serial (Down a) Source # 

Methods

serialize :: MonadPut m => Down a -> m () Source #

deserialize :: MonadGet m => m (Down a) Source #

Serial v => Serial (IntMap v) Source # 

Methods

serialize :: MonadPut m => IntMap v -> m () Source #

deserialize :: MonadGet m => m (IntMap v) Source #

Serial a => Serial (Seq a) Source # 

Methods

serialize :: MonadPut m => Seq a -> m () Source #

deserialize :: MonadGet m => m (Seq a) Source #

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

Methods

serialize :: MonadPut m => Set a -> m () Source #

deserialize :: MonadGet m => m (Set a) Source #

(Serial v, Hashable v, Eq v) => Serial (HashSet v) Source # 

Methods

serialize :: MonadPut m => HashSet v -> m () Source #

deserialize :: MonadGet m => m (HashSet v) Source #

(Bits n, Integral n, Bits (Unsigned n), Integral (Unsigned n)) => Serial (VarInt n) Source #

Integer/Word types serialized to base-128 variable-width ints.

>>> import Data.Monoid (mconcat)
>>> import qualified Data.ByteString.Lazy as BSL
>>> mconcat $ BSL.toChunks $ runPutL $ serialize (97 :: Word64)
"\NUL\NUL\NUL\NUL\NUL\NUL\NULa"
>>> mconcat $ BSL.toChunks $ runPutL $ serialize (97 :: VarInt Word64)
"a"

Methods

serialize :: MonadPut m => VarInt n -> m () Source #

deserialize :: MonadGet m => m (VarInt n) Source #

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

Methods

serialize :: MonadPut m => Either a b -> m () Source #

deserialize :: MonadGet m => m (Either a b) Source #

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

Methods

serialize :: MonadPut m => (a, b) -> m () Source #

deserialize :: MonadGet m => m (a, b) Source #

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

Methods

serialize :: MonadPut m => Map k v -> m () Source #

deserialize :: MonadGet m => m (Map k v) Source #

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

Methods

serialize :: MonadPut m => HashMap k v -> m () Source #

deserialize :: MonadGet m => m (HashMap k v) Source #

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

Methods

serialize :: MonadPut m => (a, b, c) -> m () Source #

deserialize :: MonadGet m => m (a, b, c) Source #

Serial (f a) => Serial (Reverse * f a) Source # 

Methods

serialize :: MonadPut m => Reverse * f a -> m () Source #

deserialize :: MonadGet m => m (Reverse * f a) Source #

Serial a => Serial (Constant * a b) Source # 

Methods

serialize :: MonadPut m => Constant * a b -> m () Source #

deserialize :: MonadGet m => m (Constant * a b) Source #

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

Methods

serialize :: MonadPut m => (a, b, c, d) -> m () Source #

deserialize :: MonadGet m => m (a, b, c, d) Source #

(Serial (f a), Serial (g a)) => Serial (Product * f g a) Source # 

Methods

serialize :: MonadPut m => Product * f g a -> m () Source #

deserialize :: MonadGet m => m (Product * f g a) Source #

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

Methods

serialize :: MonadPut m => (a, b, c, d, e) -> m () Source #

deserialize :: MonadGet m => m (a, b, c, d, e) Source #

Specifying endianness

class SerialEndian a where Source #

Methods to serialize and deserialize type a to a big and little endian binary representations. Methods suffixed with "host" are automatically defined to use equal the methods corresponding to the current machine's native endianness, but they can be overridden.

Instances

SerialEndian Char Source # 
SerialEndian Double Source # 
SerialEndian Float Source # 
SerialEndian Int16 Source # 
SerialEndian Int32 Source # 
SerialEndian Int64 Source # 
SerialEndian Word16 Source # 
SerialEndian Word32 Source # 
SerialEndian Word64 Source # 

Higher-order

These classes provide us with the ability to serialize containers that need polymorphic recursion.

class Serial1 f where Source #

Methods

serializeWith :: MonadPut m => (a -> m ()) -> f a -> m () Source #

serializeWith :: (MonadPut m, GSerial1 (Rep1 f), Generic1 f) => (a -> m ()) -> f a -> m () Source #

deserializeWith :: MonadGet m => m a -> m (f a) Source #

deserializeWith :: (MonadGet m, GSerial1 (Rep1 f), Generic1 f) => m a -> m (f a) Source #

Instances

Serial1 [] Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> [a] -> m () Source #

deserializeWith :: MonadGet m => m a -> m [a] Source #

Serial1 Maybe Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> Maybe a -> m () Source #

deserializeWith :: MonadGet m => m a -> m (Maybe a) Source #

Serial1 IntMap Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> IntMap a -> m () Source #

deserializeWith :: MonadGet m => m a -> m (IntMap a) Source #

Serial1 Seq Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> Seq a -> m () Source #

deserializeWith :: MonadGet m => m a -> m (Seq a) Source #

Serial a => Serial1 (Either a) Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> Either a a -> m () Source #

deserializeWith :: MonadGet m => m a -> m (Either a a) Source #

Serial a => Serial1 ((,) a) Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> (a, a) -> m () Source #

deserializeWith :: MonadGet m => m a -> m (a, a) Source #

(Ord k, Serial k) => Serial1 (Map k) Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> Map k a -> m () Source #

deserializeWith :: MonadGet m => m a -> m (Map k a) Source #

(Hashable k, Eq k, Serial k) => Serial1 (HashMap k) Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> HashMap k a -> m () Source #

deserializeWith :: MonadGet m => m a -> m (HashMap k a) Source #

(Serial a, Serial b) => Serial1 ((,,) a b) Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> (a, b, a) -> m () Source #

deserializeWith :: MonadGet m => m a -> m (a, b, a) Source #

(Serial a, Serial b, Serial c) => Serial1 ((,,,) a b c) Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> (a, b, c, a) -> m () Source #

deserializeWith :: MonadGet m => m a -> m (a, b, c, a) Source #

(Serial a, Serial b, Serial c, Serial d) => Serial1 ((,,,,) a b c d) Source # 

Methods

serializeWith :: MonadPut m => (a -> m ()) -> (a, b, c, d, a) -> m () Source #

deserializeWith :: MonadGet m => m a -> m (a, b, c, d, a) Source #

serialize1 :: (MonadPut m, Serial1 f, Serial a) => f a -> m () Source #

deserialize1 :: (MonadGet m, Serial1 f, Serial a) => m (f a) Source #

class Serial2 f where Source #

Minimal complete definition

serializeWith2, deserializeWith2

Methods

serializeWith2 :: MonadPut m => (a -> m ()) -> (b -> m ()) -> f a b -> m () Source #

deserializeWith2 :: MonadGet m => m a -> m b -> m (f a b) Source #

Instances

Serial2 Either Source # 

Methods

serializeWith2 :: MonadPut m => (a -> m ()) -> (b -> m ()) -> Either a b -> m () Source #

deserializeWith2 :: MonadGet m => m a -> m b -> m (Either a b) Source #

Serial2 (,) Source # 

Methods

serializeWith2 :: MonadPut m => (a -> m ()) -> (b -> m ()) -> (a, b) -> m () Source #

deserializeWith2 :: MonadGet m => m a -> m b -> m (a, b) Source #

Serial a => Serial2 ((,,) a) Source # 

Methods

serializeWith2 :: MonadPut m => (a -> m ()) -> (b -> m ()) -> (a, a, b) -> m () Source #

deserializeWith2 :: MonadGet m => m a -> m b -> m (a, a, b) Source #

(Serial a, Serial b) => Serial2 ((,,,) a b) Source # 

Methods

serializeWith2 :: MonadPut m => (a -> m ()) -> (b -> m ()) -> (a, b, a, b) -> m () Source #

deserializeWith2 :: MonadGet m => m a -> m b -> m (a, b, a, b) Source #

(Serial a, Serial b, Serial c) => Serial2 ((,,,,) a b c) Source # 

Methods

serializeWith2 :: MonadPut m => (a -> m ()) -> (b -> m ()) -> (a, b, c, a, b) -> m () Source #

deserializeWith2 :: MonadGet m => m a -> m b -> m (a, b, c, a, b) Source #

serialize2 :: (MonadPut m, Serial2 f, Serial a, Serial b) => f a b -> m () Source #

deserialize2 :: (MonadGet m, Serial2 f, Serial a, Serial b) => m (f a b) Source #

Storable

store :: (MonadPut m, Storable a) => a -> m () Source #

serialize any Storable in a host-specific format.

restore :: forall m a. (MonadGet m, Storable a) => m a Source #

deserialize any Storable in a host-specific format.

Generics

You probably will never need to care that these exist except they provide us with default definitions for Serial and SerialEndian

class GSerial f where Source #

Used internally to provide generic serialization

Minimal complete definition

gserialize, gdeserialize

Methods

gserialize :: MonadPut m => f a -> m () Source #

gdeserialize :: MonadGet m => m (f a) Source #

Instances

GSerial V1 Source # 

Methods

gserialize :: MonadPut m => V1 a -> m () Source #

gdeserialize :: MonadGet m => m (V1 a) Source #

GSerial U1 Source # 

Methods

gserialize :: MonadPut m => U1 a -> m () Source #

gdeserialize :: MonadGet m => m (U1 a) Source #

Serial a => GSerial (K1 i a) Source # 

Methods

gserialize :: MonadPut m => K1 i a a -> m () Source #

gdeserialize :: MonadGet m => m (K1 i a a) Source #

(GSerial f, GSerial g) => GSerial ((:+:) f g) Source # 

Methods

gserialize :: MonadPut m => (f :+: g) a -> m () Source #

gdeserialize :: MonadGet m => m ((f :+: g) a) Source #

(GSerial f, GSerial g) => GSerial ((:*:) f g) Source # 

Methods

gserialize :: MonadPut m => (f :*: g) a -> m () Source #

gdeserialize :: MonadGet m => m ((f :*: g) a) Source #

GSerial f => GSerial (M1 i c f) Source # 

Methods

gserialize :: MonadPut m => M1 i c f a -> m () Source #

gdeserialize :: MonadGet m => m (M1 i c f a) Source #

class GSerialEndian f where Source #

Used internally to provide generic big-endian serialization

Methods

gserializeBE :: MonadPut m => f a -> m () Source #

gserializeBE :: (MonadPut m, GSerial f) => f a -> m () Source #

gdeserializeBE :: MonadGet m => m (f a) Source #

gdeserializeBE :: (MonadGet m, GSerial f) => m (f a) Source #

gserializeLE :: MonadPut m => f a -> m () Source #

gserializeLE :: (MonadPut m, GSerial f) => f a -> m () Source #

gdeserializeLE :: MonadGet m => m (f a) Source #

gdeserializeLE :: (MonadGet m, GSerial f) => m (f a) Source #

Instances

SerialEndian a => GSerialEndian (K1 i a) Source # 

Methods

gserializeBE :: MonadPut m => K1 i a a -> m () Source #

gdeserializeBE :: MonadGet m => m (K1 i a a) Source #

gserializeLE :: MonadPut m => K1 i a a -> m () Source #

gdeserializeLE :: MonadGet m => m (K1 i a a) Source #

class GSerial1 f where Source #

Used internally to provide generic serialization

Minimal complete definition

gserializeWith, gdeserializeWith

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> f a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m (f a) Source #

Instances

GSerial1 V1 Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> V1 a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m (V1 a) Source #

GSerial1 U1 Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> U1 a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m (U1 a) Source #

GSerial1 Par1 Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> Par1 a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m (Par1 a) Source #

Serial1 f => GSerial1 (Rec1 f) Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> Rec1 f a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m (Rec1 f a) Source #

Serial a => GSerial1 (K1 i a) Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> K1 i a a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m (K1 i a a) Source #

(GSerial1 f, GSerial1 g) => GSerial1 ((:+:) f g) Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> (f :+: g) a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m ((f :+: g) a) Source #

(GSerial1 f, GSerial1 g) => GSerial1 ((:*:) f g) Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> (f :*: g) a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m ((f :*: g) a) Source #

(Serial1 f, GSerial1 g) => GSerial1 ((:.:) f g) Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> (f :.: g) a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m ((f :.: g) a) Source #

GSerial1 f => GSerial1 (M1 i c f) Source # 

Methods

gserializeWith :: MonadPut m => (a -> m ()) -> M1 i c f a -> m () Source #

gdeserializeWith :: MonadGet m => m a -> m (M1 i c f a) Source #