Adds JSON instances for HexString
This commit is contained in:
parent
adc3eb566d
commit
3087439daf
2 changed files with 12 additions and 2 deletions
|
@ -31,6 +31,7 @@ library
|
|||
, text
|
||||
, bytestring
|
||||
, base16-bytestring
|
||||
, aeson
|
||||
|
||||
test-suite test-suite
|
||||
type: exitcode-stdio-1.0
|
||||
|
|
|
@ -6,10 +6,13 @@ module Data.HexString ( HexString
|
|||
, toBytes
|
||||
, toText ) where
|
||||
|
||||
import Data.Word (Word8)
|
||||
import Control.Applicative (pure)
|
||||
|
||||
import Data.Aeson
|
||||
import Data.Word (Word8)
|
||||
|
||||
import qualified Data.ByteString.Base16 as BS16 (decode, encode)
|
||||
import qualified Data.ByteString as BS
|
||||
import qualified Data.ByteString.Base16 as BS16 (decode, encode)
|
||||
import qualified Data.ByteString.Lazy as BSL
|
||||
|
||||
import qualified Data.Text as T
|
||||
|
@ -23,6 +26,12 @@ data HexString =
|
|||
HexString BS.ByteString
|
||||
deriving ( Show, Eq, Ord )
|
||||
|
||||
instance FromJSON HexString where
|
||||
parseJSON = withText "HexString" $ pure . hexString . TE.encodeUtf8
|
||||
|
||||
instance ToJSON HexString where
|
||||
toJSON = String . toText
|
||||
|
||||
-- | Smart constructor which validates that all the text are actually
|
||||
-- hexadecimal characters.
|
||||
hexString :: BS.ByteString -> HexString
|
||||
|
|
Loading…
Reference in a new issue