on master help sign in
Add the oft-requested decode function.
commit9ca5e71ff9d0e1aee110786916f72b8874b8e4ee authorBryan O'Sullivan <bos@serpentine.com> dateMon 19 Sep 2011, 18:20 (15 years ago) parent54c263a
1 file changed+14−1
Data/Aeson.hs+14−1
Data/Aeson.hs+14−1
@@ -28,11 +28,24 @@ module Data.Aeson
2828 , (.:)
2929 , (.:?)
3030 , object
31− -- * Encoding and parsing
31+ -- * Encoding and decoding
32+ , decode
3233 , encode
34+ -- * Parsing
3335 , json
3436 ) where
3537
3638import Data.Aeson.Encode
3739import Data.Aeson.Parser
3840import Data.Aeson.Types
41+import qualified Data.ByteString.Lazy as L
42+import qualified Data.Attoparsec.Lazy as L
43+
44+-- | Efficiently deserialize a JSON value from a lazy 'L.ByteString'.
45+decode :: (FromJSON a) => L.ByteString -> Maybe a
46+decode s = case L.parse json s of
47+ L.Done _ v -> case fromJSON v of
48+ Success a -> Just a
49+ _ -> Nothing
50+ _ -> Nothing
51+{-# INLINE decode #-}