Created
March 13, 2018 19:05
-
-
Save adamczykm/12c9d632c381d5e974cec1d3966749c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE RankNTypes #-} | |
| {-# LANGUAGE ExistentialQuantification #-} | |
| {-# LANGUAGE FlexibleInstances #-} | |
| {-# LANGUAGE NoImplicitPrelude #-} | |
| {-# LANGUAGE GeneralizedNewtypeDeriving #-} | |
| module Serialization where | |
| import Protolude | |
| import Data.Binary | |
| data SerializationError = BinaryDecodingError Text | |
| class Serializable a where | |
| serialize :: a -> LByteString | |
| deserialize :: LByteString -> Either SerializationError a | |
| newtype BinarySerializable a = MkBinarySerializable a | |
| instance Binary a => Serializable (BinarySerializable a) where | |
| serialize (MkBinarySerializable a)= encode a | |
| deserialize = bimap (\(_,_, errString) -> BinaryDecodingError (show errString)) | |
| (\(_,_, a) -> MkBinarySerializable a) | |
| . decodeOrFail |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment