Skip to content

Instantly share code, notes, and snippets.

@adamczykm
Created March 13, 2018 19:05
Show Gist options
  • Select an option

  • Save adamczykm/12c9d632c381d5e974cec1d3966749c8 to your computer and use it in GitHub Desktop.

Select an option

Save adamczykm/12c9d632c381d5e974cec1d3966749c8 to your computer and use it in GitHub Desktop.
{-# 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