module Util.ByteString
( HexJSONByteString(..)
) where
import Data.Aeson (FromJSON(..), ToJSON(..), withText)
import Text.Hex (decodeHex, encodeHex)
newtype HexJSONByteString = HexJSONByteString { unHexJSONByteString :: ByteString }
instance ToJSON HexJSONByteString where
toJSON = toJSON . encodeHex . unHexJSONByteString
instance FromJSON HexJSONByteString where
parseJSON =
withText "Hex-encoded bytestring" $ \t ->
case decodeHex t of
Nothing -> fail "Invalid hex encoding"
Just res -> pure (HexJSONByteString res)