codec: First-class record construction and bidirectional serialization
Tired of writing complementary parseJSON
/toJSON
, peek
/poke
or
Binary get
/put
functions?
codec
provides easy bidirectional serialization of plain Haskell
records in any Applicative context. All you need to do is provide a
de/serializer for every record field in any order you like, and you get
a de/serializer for the whole structure. The type system ensures that
you provide every record exactly once. It also includes a library for
general record construction in an Applicative context, of which creating
codecs is just one application.
JSON!
userCodec :: JSONCodec User userCodec = obj "user object" $ User $>> f_username >-< "user" >>> f_userEmail >-< "email" >>> f_userLanguages >-< "languages" >>> f_userReferrer >-< opt "referrer" instance FromJSON User where parseJSON = parseVal userCodec instance ToJSON User where toJSON = produceVal userCodec
Bit fields!
ipv4Codec :: BinaryCodec IPv4 ipv4Codec = toBytes $ IPv4 $>> f_version >-< word8 4 >>> f_ihl >-< word8 4 >>> f_dscp >-< word8 6 >>> f_ecn >-< word8 2 >>> f_totalLength >-< word16be 16 >>> f_identification >-< word16be 16 >>> f_flags >-< word8 3 >>> f_fragmentOffset >-< word16be 13 >>> f_timeToLive >-< word8 8 >>> f_protocol >-< word8 8 >>> f_headerChecksum >-< word16be 16 >>> f_sourceIP >-< word32be 32 >>> f_destIP >-< word32be 32 instance Binary IPv4 where get = parse ipv4Codec put = produce ipv4Codec
Storable!
timeSpecCodec :: ForeignCodec TimeSpec timeSpecCodec = TimeSpec $>> f_seconds >-< field (#offset struct timespec, tv_sec) cInt >>> f_nanoseconds >-< field (#offset struct timespec, tv_nsec) cInt instance Storable TimeSpec where peek = peekWith timeSpecCodec poke = pokeWith timeSpecCodec ...
All of these examples use the same types and logic for constructing Codecs, and it's very easy to create Codecs for any parsing/serialization library.
See Data.Codec for an introduction.
Modules
- Control
- Lens
- Control.Lens.Codec
- Lens
- Data
- Aeson
- Data.Aeson.Codec
- Binary
- Bits
- Data.Binary.Bits.Codec
- Data.Binary.Codec
- Bits
- Data.Codec
- Data.Codec.Testing
- Aeson
- Foreign
- Foreign.Codec
Downloads
- codec-0.1.tar.gz [browse] (Cabal source package)
- Package description (as included in the package)
Maintainer's Corner
For package maintainers and hackage trustees
Candidates
Versions [RSS] | 0.1, 0.1.1, 0.2, 0.2.1 |
---|---|
Dependencies | aeson (>=0.8.0.2), base (>=4.6 && <4.9), binary (>=0.7), binary-bits (>=0.5), bytestring (>=0.10), data-default-class (>=0.0.1), mtl (>=2.2.1), template-haskell (>=2.8), text (>=1.2.0.4), transformers (>=0.4.2.0), unordered-containers (>=0.2.5.1) [details] |
License | BSD-3-Clause |
Author | Patrick Chilton |
Maintainer | chpatrick@gmail.com |
Category | Data |
Home page | https://github.com/chpatrick/codec |
Source repo | head: git clone https://github.com/chpatrick/codec.git |
Uploaded | by PatrickChilton at 2015-06-02T13:50:11Z |
Distributions | |
Reverse Dependencies | 1 direct, 0 indirect [details] |
Downloads | 3789 total (12 in the last 30 days) |
Rating | 2.0 (votes: 1) [estimated by Bayesian average] |
Your Rating | |
Status | Docs not available [build log] All reported builds failed as of 2016-12-09 [all 6 reports] |