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
|
, text
|
||||||
, bytestring
|
, bytestring
|
||||||
, base16-bytestring
|
, base16-bytestring
|
||||||
|
, aeson
|
||||||
|
|
||||||
test-suite test-suite
|
test-suite test-suite
|
||||||
type: exitcode-stdio-1.0
|
type: exitcode-stdio-1.0
|
||||||
|
|
|
@ -6,10 +6,13 @@ module Data.HexString ( HexString
|
||||||
, toBytes
|
, toBytes
|
||||||
, toText ) where
|
, 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 as BS
|
||||||
|
import qualified Data.ByteString.Base16 as BS16 (decode, encode)
|
||||||
import qualified Data.ByteString.Lazy as BSL
|
import qualified Data.ByteString.Lazy as BSL
|
||||||
|
|
||||||
import qualified Data.Text as T
|
import qualified Data.Text as T
|
||||||
|
@ -23,6 +26,12 @@ data HexString =
|
||||||
HexString BS.ByteString
|
HexString BS.ByteString
|
||||||
deriving ( Show, Eq, Ord )
|
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
|
-- | Smart constructor which validates that all the text are actually
|
||||||
-- hexadecimal characters.
|
-- hexadecimal characters.
|
||||||
hexString :: BS.ByteString -> HexString
|
hexString :: BS.ByteString -> HexString
|
||||||
|
|
Loading…
Reference in a new issue