Commits

Bryan O'Sullivan  committed 5a84672

Add the oft-requested decode function.

  • Participants
  • Parent commits d2b8c90

Comments (0)

Files changed (1)

File Data/Aeson.hs

     , (.:)
     , (.:?)
     , object
-    -- * Encoding and parsing
+    -- * Encoding and decoding
+    , decode
     , encode
+    -- * Parsing
     , json
     ) where
 
 import Data.Aeson.Encode
 import Data.Aeson.Parser
 import Data.Aeson.Types
+import qualified Data.ByteString.Lazy as L
+import qualified Data.Attoparsec.Lazy as L
+
+-- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
+decode :: (FromJSON a) => L.ByteString -> Maybe a
+decode s = case L.parse json s of
+             L.Done _ v -> case fromJSON v of
+                             Success a -> Just a
+                             _         -> Nothing
+             _          -> Nothing
+{-# INLINE decode #-}