Skip to content

Commit 846306e

Browse files
authored
Merge pull request #1158 from IntersectMBO/expose-bytestring-key-reading
Expose ByteString-based text envelope deserialization
2 parents e19074f + 344355c commit 846306e

2 files changed

Lines changed: 39 additions & 6 deletions

File tree

cardano-api/src/Cardano/Api/Serialise/TextEnvelope.hs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ module Cardano.Api.Serialise.TextEnvelope
2020
-- ** Reading one of several key types
2121
, FromSomeType (..)
2222
, deserialiseFromTextEnvelopeAnyOf
23+
, decodeTextEnvelopeJSON
24+
, deserialiseFromTextEnvelopeJSON
25+
, deserialiseFromTextEnvelopeJSONAnyOf
2326
, readFileTextEnvelopeAnyOf
2427

2528
-- ** Data family instances

cardano-api/src/Cardano/Api/Serialise/TextEnvelope/Internal.hs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ module Cardano.Api.Serialise.TextEnvelope.Internal
3232
-- * Reading one of several key types
3333
, FromSomeType (..)
3434
, deserialiseFromTextEnvelopeAnyOf
35+
, decodeTextEnvelopeJSON
36+
, deserialiseFromTextEnvelopeJSON
37+
, deserialiseFromTextEnvelopeJSONAnyOf
3538
, readFileTextEnvelopeAnyOf
3639

3740
-- * Data family instances
@@ -250,6 +253,33 @@ deserialiseFromTextEnvelopeAnyOf types te =
250253

251254
matching (FromSomeType ttoken _f) = textEnvelopeType ttoken `legacyComparison` actualType
252255

256+
-- | Decode a JSON-encoded 'TextEnvelope' from a strict 'ByteString' (UTF-8).
257+
-- Returns 'TextEnvelopeAesonDecodeError' if the JSON parsing fails.
258+
decodeTextEnvelopeJSON :: ByteString -> Either TextEnvelopeError TextEnvelope
259+
decodeTextEnvelopeJSON bs =
260+
first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' bs
261+
262+
-- | Deserialise a value from a JSON-encoded text envelope 'ByteString' (UTF-8).
263+
-- This performs no file I\/O. Returns 'TextEnvelopeAesonDecodeError' for JSON
264+
-- parse failures, or downstream errors from 'deserialiseFromTextEnvelope' for
265+
-- type mismatches and CBOR decoding failures.
266+
deserialiseFromTextEnvelopeJSON
267+
:: HasTextEnvelope a
268+
=> ByteString -> Either TextEnvelopeError a
269+
deserialiseFromTextEnvelopeJSON bs =
270+
decodeTextEnvelopeJSON bs >>= deserialiseFromTextEnvelope
271+
272+
-- | Like 'deserialiseFromTextEnvelopeJSON' but accepts multiple target types.
273+
-- This performs no file I\/O. Returns 'TextEnvelopeAesonDecodeError' for JSON
274+
-- parse failures, or downstream errors from 'deserialiseFromTextEnvelopeAnyOf'
275+
-- for type mismatches and CBOR decoding failures.
276+
deserialiseFromTextEnvelopeJSONAnyOf
277+
:: [FromSomeType HasTextEnvelope b]
278+
-> ByteString
279+
-> Either TextEnvelopeError b
280+
deserialiseFromTextEnvelopeJSONAnyOf types bs =
281+
decodeTextEnvelopeJSON bs >>= deserialiseFromTextEnvelopeAnyOf types
282+
253283
writeFileTextEnvelope
254284
:: HasTextEnvelope a
255285
=> File content Out
@@ -274,9 +304,9 @@ readFileTextEnvelope
274304
readFileTextEnvelope path =
275305
runExceptT $ do
276306
content <- fileIOExceptT (unFile path) readFileBlocking
277-
firstExceptT (FileError (unFile path)) $ hoistEither $ do
278-
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' content
279-
deserialiseFromTextEnvelope te
307+
firstExceptT (FileError (unFile path)) $
308+
hoistEither $
309+
deserialiseFromTextEnvelopeJSON content
280310

281311
readFileTextEnvelopeAnyOf
282312
:: [FromSomeType HasTextEnvelope b]
@@ -285,9 +315,9 @@ readFileTextEnvelopeAnyOf
285315
readFileTextEnvelopeAnyOf types path =
286316
runExceptT $ do
287317
content <- fileIOExceptT (unFile path) readFileBlocking
288-
firstExceptT (FileError (unFile path)) $ hoistEither $ do
289-
te <- first TextEnvelopeAesonDecodeError $ Aeson.eitherDecodeStrict' content
290-
deserialiseFromTextEnvelopeAnyOf types te
318+
firstExceptT (FileError (unFile path)) $
319+
hoistEither $
320+
deserialiseFromTextEnvelopeJSONAnyOf types content
291321

292322
readTextEnvelopeFromFile
293323
:: FilePath

0 commit comments

Comments
 (0)