Skip to content

Commit

Permalink
Use Base64url to convert bytes to JSON string
Browse files Browse the repository at this point in the history
  • Loading branch information
Saizan committed Apr 27, 2023
1 parent 6983d83 commit 4a9304c
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
6 changes: 3 additions & 3 deletions cborg-json/cborg-json.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ library
base >=4.11 && < 4.19,
aeson >=0.7 && <2.2,
aeson-pretty >=0.8 && <0.9,
base16-bytestring >= 1.0 && < 1.1,
base64-bytestring >=1.0 && <1.3,
unordered-containers >=0.2 && <0.3,
scientific >=0.3 && <0.4,
text >=1.1 && <2.1,
Expand Down Expand Up @@ -89,11 +89,11 @@ test-suite tests
build-depends:
base >= 4.7 && < 4.19,
base-orphans,
base16-bytestring >= 0.1 && < 0.2,
base16-bytestring >= 1.0 && < 1.1,
bytestring >= 0.10.4 && < 0.12,
cborg,
cborg-json,
aeson >= 0.7 && < 1.6,
aeson >= 0.7 && < 2.2,
QuickCheck >= 2.9 && < 2.15,
tasty >= 0.11 && < 1.5,
tasty-hunit >= 0.9 && < 0.11,
Expand Down
7 changes: 3 additions & 4 deletions cborg-json/src/Codec/CBOR/JSON.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ import Codec.CBOR.Encoding
import Codec.CBOR.Decoding
import Data.Aeson ( Value(..) )
import qualified Data.Aeson as Aeson
import qualified Data.ByteString.Base16 as HEX
import qualified Data.HashMap.Lazy as HM
import qualified Data.ByteString.Base64.URL as Base64url
import Data.Scientific as Scientific
import qualified Data.Text as T
import qualified Data.Text.Encoding as TE
Expand Down Expand Up @@ -80,12 +79,12 @@ decodeValue lenient = do
TypeListLenIndef -> decodeListLenIndef >> decodeListIndef lenient []
TypeMapLen -> decodeMapLen >>= flip (decodeMapN lenient) mempty

TypeBytes -> packHex <$> decodeBytes
TypeBytes -> bytesToBase64Text <$> decodeBytes

_ -> fail $ "unexpected CBOR token type for a JSON value: "
++ show tkty
where
packHex = String . TE.decodeLatin1 . HEX.encode
bytesToBase64Text = String . TE.decodeLatin1 . Base64url.encode


decodeNumberIntegral :: Decoder s Value
Expand Down
6 changes: 3 additions & 3 deletions cborg-json/tests/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ tests :: TestTree
tests =
testGroup "CBOR-JSON"
[ testGroup "unit tests"
[ testCase "decode variable ByteString as HexString" $
Right ("", String "303132") @=? deserialiseFromBytes (decodeValue True)
(fromStrict . fst $ HEX.decode "5803303132")
[ testCase "decode variable ByteString as Base62Url String" $
Right ("", String "MDEy") @=? deserialiseFromBytes (decodeValue True)
(fromStrict . either error id $ HEX.decode "5803303132")
]
]

0 comments on commit 4a9304c

Please sign in to comment.