Haskell implementation of Flat, a principled, portable and compact binary data format (specs).
{-# LANGUAGE DeriveGeneric, DeriveAnyClass #-}import Flatdata Direction = North | South | Center | East | West deriving (Show,Generic,Flat)Use flat to encode:
flat [North,South] -> "\149"and unflat to decode:
unflat (flat [North,South]) :: Decoded [Direction] -> Right [ North , South ]And thanks to Flat's bit-encoding, this list fits in 1 byte (rather than the 5 bytes that would be required by a traditional byte encoding):
flatBits [North,South] -> "10010101"For some hard data, see this comparison of the major haskell serialisation libraries.
Briefly:
- Size:
flatproduces significantly smaller binaries than all other libraries (3/4 times usually) - Serialization time:
store,persistandflatare faster - Deserialization time:
store,flat,persistandcerealare faster - Transfer time (serialisation time + transport time on the network + deserialisation at the receiving end):
flatis usually faster for all but the highest network speeds
Get the latest stable version from hackage.
Tested with:
-
GHC 7.10.3 to 9.4.2 (x64)
-
- Note: versions of
flatprior to 0.33 encodeDoublevalues incorrectly when they are not aligned with a byte boundary.
- Note: versions of
-
Data types with more than 512 constructors are currently unsupported
-
Longish compilation times
-
flatrelies more than other serialisation libraries on extensive inlining for its good performance, this unfortunately leads to longer compilation times.If you have many data types or very large ones this might become an issue.
A couple of good practices that will eliminate or mitigate this problem are:
-
During development, turn optimisations off (
stack --fastor-O0in the cabal file). -
Keep your serialisation code in a separate module or modules.
-
-
-
See also the full list of open issues.
Rust and TypeScript-JavaScript ports are under development.
Get in touch if you would like to help porting flat to other languages.
flat reuses ideas and readapts code from various packages, mainly: store, binary-bits and binary and includes bug fixes from a number of contributors.
To decode flat encoded data you need to know the type of the serialised data.
This is ok for applications that do not require long-term storage and that do not operate in open distributed systems.
For those who do, you might want to supplement flat with ZM - Language independent, reproducible, absolute types.