@@ -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+
253283writeFileTextEnvelope
254284 :: HasTextEnvelope a
255285 => File content Out
@@ -274,9 +304,9 @@ readFileTextEnvelope
274304readFileTextEnvelope 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
281311readFileTextEnvelopeAnyOf
282312 :: [FromSomeType HasTextEnvelope b ]
@@ -285,9 +315,9 @@ readFileTextEnvelopeAnyOf
285315readFileTextEnvelopeAnyOf 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
292322readTextEnvelopeFromFile
293323 :: FilePath
0 commit comments