@@ -9,6 +9,9 @@ module Data.Aeson.Decoding (
9
9
decodeStrict ,
10
10
eitherDecodeStrict ,
11
11
throwDecodeStrict ,
12
+ decodeStrictText ,
13
+ eitherDecodeStrictText ,
14
+ throwDecodeStrictText ,
12
15
toEitherValue ,
13
16
unescapeText ,
14
17
) where
@@ -17,11 +20,13 @@ import Control.Monad.Catch (MonadThrow (..))
17
20
import Data.Aeson.Types.Internal (AesonException (.. ), formatError )
18
21
19
22
import qualified Data.Aeson.Types as A
20
- import qualified Data.ByteString as B
21
- import qualified Data.ByteString.Lazy as L
23
+ import qualified Data.ByteString as BS
24
+ import qualified Data.ByteString.Lazy as LBS
25
+ import qualified Data.Text as T
22
26
23
27
import Data.Aeson.Decoding.ByteString
24
28
import Data.Aeson.Decoding.ByteString.Lazy
29
+ import Data.Aeson.Decoding.Text
25
30
import Data.Aeson.Decoding.Conversion
26
31
import Data.Aeson.Internal.Unescape (unescapeText )
27
32
@@ -32,23 +37,23 @@ import Data.Aeson.Internal.Unescape (unescapeText)
32
37
-- | Efficiently deserialize a JSON value from a strict 'B.ByteString'.
33
38
-- If this fails due to incomplete or invalid input, 'Nothing' is
34
39
-- returned.
35
- decodeStrict :: (A. FromJSON a ) => B . ByteString -> Maybe a
40
+ decodeStrict :: (A. FromJSON a ) => BS . ByteString -> Maybe a
36
41
decodeStrict bs = unResult (toResultValue (bsToTokens bs)) (\ _ -> Nothing ) $ \ v bs' -> case A. ifromJSON v of
37
42
A. ISuccess x
38
43
| bsSpace bs' -> Just x
39
44
| otherwise -> Nothing
40
45
A. IError _ _ -> Nothing
41
46
42
47
-- | Like 'decodeStrict' but returns an error message when decoding fails.
43
- eitherDecodeStrict :: (A. FromJSON a ) => B . ByteString -> Either String a
48
+ eitherDecodeStrict :: (A. FromJSON a ) => BS . ByteString -> Either String a
44
49
eitherDecodeStrict bs = unResult (toResultValue (bsToTokens bs)) Left $ \ v bs' -> case A. ifromJSON v of
45
50
A. ISuccess x
46
51
| bsSpace bs' -> Right x
47
52
| otherwise -> Left " Trailing garbage"
48
53
A. IError path msg -> Left $ formatError path msg
49
54
50
55
-- | Like 'decodeStrict' but throws an 'AesonException' when decoding fails.
51
- throwDecodeStrict :: forall a m . (A. FromJSON a , MonadThrow m ) => B . ByteString -> m a
56
+ throwDecodeStrict :: forall a m . (A. FromJSON a , MonadThrow m ) => BS . ByteString -> m a
52
57
throwDecodeStrict bs = unResult (toResultValue (bsToTokens bs)) (throwM . AesonException ) $ \ v bs' -> case A. ifromJSON v of
53
58
A. ISuccess x
54
59
| bsSpace bs' -> pure x
@@ -62,15 +67,15 @@ throwDecodeStrict bs = unResult (toResultValue (bsToTokens bs)) (throwM . AesonE
62
67
-- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
63
68
-- If this fails due to incomplete or invalid input, 'Nothing' is
64
69
-- returned.
65
- decode :: (A. FromJSON a ) => L . ByteString -> Maybe a
70
+ decode :: (A. FromJSON a ) => LBS . ByteString -> Maybe a
66
71
decode bs = unResult (toResultValue (lbsToTokens bs)) (\ _ -> Nothing ) $ \ v bs' -> case A. ifromJSON v of
67
72
A. ISuccess x
68
73
| lbsSpace bs' -> Just x
69
74
| otherwise -> Nothing
70
75
A. IError _ _ -> Nothing
71
76
72
77
-- | Like 'decode' but returns an error message when decoding fails.
73
- eitherDecode :: (A. FromJSON a ) => L . ByteString -> Either String a
78
+ eitherDecode :: (A. FromJSON a ) => LBS . ByteString -> Either String a
74
79
eitherDecode bs = unResult (toResultValue (lbsToTokens bs)) Left $ \ v bs' -> case A. ifromJSON v of
75
80
A. ISuccess x
76
81
| lbsSpace bs' -> Right x
@@ -80,9 +85,45 @@ eitherDecode bs = unResult (toResultValue (lbsToTokens bs)) Left $ \v bs' -> cas
80
85
-- | Like 'decode' but throws an 'AesonException' when decoding fails.
81
86
--
82
87
-- 'throwDecode' is in @aeson@ since 2.1.2.0, but this variant is added later.
83
- throwDecode :: forall a m . (A. FromJSON a , MonadThrow m ) => L . ByteString -> m a
88
+ throwDecode :: forall a m . (A. FromJSON a , MonadThrow m ) => LBS . ByteString -> m a
84
89
throwDecode bs = unResult (toResultValue (lbsToTokens bs)) (throwM . AesonException ) $ \ v bs' -> case A. ifromJSON v of
85
90
A. ISuccess x
86
91
| lbsSpace bs' -> pure x
87
92
| otherwise -> throwM $ AesonException " Trailing garbage"
88
93
A. IError path msg -> throwM $ AesonException $ formatError path msg
94
+
95
+ -------------------------------------------------------------------------------
96
+ -- Decoding: strict text
97
+ -------------------------------------------------------------------------------
98
+
99
+ -- | Efficiently deserialize a JSON value from a strict 'B.ByteString'.
100
+ -- If this fails due to incomplete or invalid input, 'Nothing' is
101
+ -- returned.
102
+ --
103
+ -- @since 2.2.1.0
104
+ decodeStrictText :: (A. FromJSON a ) => T. Text -> Maybe a
105
+ decodeStrictText bs = unResult (toResultValue (textToTokens bs)) (\ _ -> Nothing ) $ \ v bs' -> case A. ifromJSON v of
106
+ A. ISuccess x
107
+ | textSpace bs' -> Just x
108
+ | otherwise -> Nothing
109
+ A. IError _ _ -> Nothing
110
+
111
+ -- | Like 'decodeStrictText' but returns an error message when decoding fails.
112
+ --
113
+ -- @since 2.2.1.0
114
+ eitherDecodeStrictText :: (A. FromJSON a ) => T. Text -> Either String a
115
+ eitherDecodeStrictText bs = unResult (toResultValue (textToTokens bs)) Left $ \ v bs' -> case A. ifromJSON v of
116
+ A. ISuccess x
117
+ | textSpace bs' -> Right x
118
+ | otherwise -> Left " Trailing garbage"
119
+ A. IError path msg -> Left $ formatError path msg
120
+
121
+ -- | Like 'decodeStrictText' but throws an 'AesonException' when decoding fails.
122
+ --
123
+ -- @since 2.2.1.0
124
+ throwDecodeStrictText :: forall a m . (A. FromJSON a , MonadThrow m ) => T. Text -> m a
125
+ throwDecodeStrictText bs = unResult (toResultValue (textToTokens bs)) (throwM . AesonException ) $ \ v bs' -> case A. ifromJSON v of
126
+ A. ISuccess x
127
+ | textSpace bs' -> pure x
128
+ | otherwise -> throwM $ AesonException " Trailing garbage"
129
+ A. IError path msg -> throwM $ AesonException $ formatError path msg
0 commit comments