Commits

Bryan O'Sullivan  committed cf03d1c Merge

Merge pull request #130 from Peaker/master

Fix lazy I/O leak and trailing garbage ignore bug in decode functions

  • Participants
  • Parent commits 0246b00, 3af20e2

Comments (0)

Files changed (2)

File Data/Aeson.hs

     ) where
 
 import Data.Aeson.Encode (encode)
-import Data.Aeson.Parser.Internal (decodeWith, eitherDecodeWith, json, json')
+import Data.Aeson.Parser.Internal (decodeWith, eitherDecodeWith, jsonEOF, json, jsonEOF', json')
 import Data.Aeson.Types
 import qualified Data.ByteString.Lazy as L
 
 -- This function parses immediately, but defers conversion.  See
 -- 'json' for details.
 decode :: (FromJSON a) => L.ByteString -> Maybe a
-decode = decodeWith json fromJSON
+decode = decodeWith jsonEOF fromJSON
 {-# INLINE decode #-}
 
 -- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
 -- This function parses and performs conversion immediately.  See
 -- 'json'' for details.
 decode' :: (FromJSON a) => L.ByteString -> Maybe a
-decode' = decodeWith json' fromJSON
+decode' = decodeWith jsonEOF' fromJSON
 {-# INLINE decode' #-}
 
 -- | Like 'decode' but returns an error message when decoding fails.
 eitherDecode :: (FromJSON a) => L.ByteString -> Either String a
-eitherDecode = eitherDecodeWith json fromJSON
+eitherDecode = eitherDecodeWith jsonEOF fromJSON
 {-# INLINE eitherDecode #-}
 
 -- | Like 'decode'' but returns an error message when decoding fails.
 eitherDecode' :: (FromJSON a) => L.ByteString -> Either String a
-eitherDecode' = eitherDecodeWith json' fromJSON
+eitherDecode' = eitherDecodeWith jsonEOF' fromJSON
 {-# INLINE eitherDecode' #-}
 
 -- $use

File Data/Aeson/Parser/Internal.hs

 module Data.Aeson.Parser.Internal
     (
     -- * Lazy parsers
-      json
+      json, jsonEOF
     , value
     , jstring
     -- * Strict parsers
-    , json'
+    , json', jsonEOF'
     , value'
     -- * Helpers
     , decodeWith
 -- The 'json'' and 'value'' parsers combine identification with
 -- conversion.  They consume more CPU cycles up front, but have a
 -- smaller memory footprint.
+
+-- | Parse a top-level JSON value followed by optional whitespace and
+-- end-of-input.  See also: 'json'.
+jsonEOF :: Parser Value
+jsonEOF = json <* skipSpace <* endOfInput
+
+-- | Parse a top-level JSON value followed by optional whitespace and
+-- end-of-input.  See also: 'json''.
+jsonEOF' :: Parser Value
+jsonEOF' = json' <* skipSpace <* endOfInput