on master help sign in
Add jsonEOF, jsonEOF', and make the decode* functions use jsonEOF rather than json parser
commit1946dab944e8b017673463ecadf576f39da59fd8 authorEyal Lotem <eyal.lotem@gmail.com> dateTue 9 Jul 2013, 03:11 (13 years ago) parent363c250
2 files changed+17−7
Data/Aeson.hs+5−5 Data/Aeson/Parser/Internal.hs+12−2
Data/Aeson.hs+5−5
@@ -76,7 +76,7 @@ module Data.Aeson
7676 ) where
7777
7878import Data.Aeson.Encode (encode)
79−import Data.Aeson.Parser.Internal (decodeWith, eitherDecodeWith, json, json')
79+import Data.Aeson.Parser.Internal (decodeWith, eitherDecodeWith, jsonEOF, json, jsonEOF', json')
8080import Data.Aeson.Types
8181import qualified Data.ByteString.Lazy as L
8282
@@ -87,7 +87,7 @@ import qualified Data.ByteString.Lazy as L
8787-- This function parses immediately, but defers conversion. See
8888-- 'json' for details.
8989decode :: (FromJSON a) => L.ByteString -> Maybe a
90−decode = decodeWith json fromJSON
90+decode = decodeWith jsonEOF fromJSON
9191{-# INLINE decode #-}
9292
9393-- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
@@ -97,17 +97,17 @@ decode = decodeWith json fromJSON
9797-- This function parses and performs conversion immediately. See
9898-- 'json'' for details.
9999decode' :: (FromJSON a) => L.ByteString -> Maybe a
100−decode' = decodeWith json' fromJSON
100+decode' = decodeWith jsonEOF' fromJSON
101101{-# INLINE decode' #-}
102102
103103-- | Like 'decode' but returns an error message when decoding fails.
104104eitherDecode :: (FromJSON a) => L.ByteString -> Either String a
105−eitherDecode = eitherDecodeWith json fromJSON
105+eitherDecode = eitherDecodeWith jsonEOF fromJSON
106106{-# INLINE eitherDecode #-}
107107
108108-- | Like 'decode'' but returns an error message when decoding fails.
109109eitherDecode' :: (FromJSON a) => L.ByteString -> Either String a
110−eitherDecode' = eitherDecodeWith json' fromJSON
110+eitherDecode' = eitherDecodeWith jsonEOF' fromJSON
111111{-# INLINE eitherDecode' #-}
112112
113113-- $use
Data/Aeson/Parser/Internal.hs+12−2
@@ -15,11 +15,11 @@
1515module Data.Aeson.Parser.Internal
1616 (
1717 -- * Lazy parsers
18− json
18+ json, jsonEOF
1919 , value
2020 , jstring
2121 -- * Strict parsers
22− , json'
22+ , json', jsonEOF'
2323 , value'
2424 -- * Helpers
2525 , decodeWith
@@ -271,3 +271,13 @@ eitherDecodeWith p to s =
271271-- The 'json'' and 'value'' parsers combine identification with
272272-- conversion. They consume more CPU cycles up front, but have a
273273-- smaller memory footprint.
274+
275+-- | Parse a top-level JSON value followed by optional whitespace and
276+-- end-of-input. See also: 'json'.
277+jsonEOF :: Parser Value
278+jsonEOF = json <* skipSpace <* endOfInput
279+
280+-- | Parse a top-level JSON value followed by optional whitespace and
281+-- end-of-input. See also: 'json''.
282+jsonEOF' :: Parser Value
283+jsonEOF' = json' <* skipSpace <* endOfInput