on master help sign in
Add an NFData instance for Value
commit33b98d1215ddf3a23f903ead95e413ab85e5cedf authorBryan O'Sullivan <bos@serpentine.com> dateTue 25 Jan 2011, 21:45 (16 years ago) parenta6b9419
3 files changed+12−1
Data/Aeson/Types.hs+9 aeson.cabal+1 benchmarks/ParseFile.hs+2−1
Data/Aeson/Types.hs+9
@@ -13,6 +13,7 @@ module Data.Aeson.Types
1313 ) where
1414
1515import Control.Applicative
16+import Control.DeepSeq
1617import Data.Map (Map)
1718import Data.Text (Text, pack, unpack)
1819import Data.Time.Clock (UTCTime)
@@ -34,6 +35,14 @@ data Value = Object Object
3435 | Null
3536 deriving (Eq, Show, Typeable)
3637
38+instance NFData Value where
39+ rnf (Object o) = rnf o
40+ rnf (Array a) = V.foldl' (\x y -> rnf y `seq` x) () a
41+ rnf (String s) = rnf s
42+ rnf (Number n) = rnf n
43+ rnf (Bool b) = rnf b
44+ rnf Null = ()
45+
3746(.=) :: ToJSON a => Text -> a -> Object
3847name .= value = M.singleton name (toJSON value)
3948{-# INLINE (.=) #-}
aeson.cabal+1
@@ -35,6 +35,7 @@ library
3535 blaze-builder >= 0.2.1.4,
3636 bytestring,
3737 containers == 0.3.*,
38+ deepseq,
3839 old-locale,
3940 text >= 0.11.0.2,
4041 time,
benchmarks/ParseFile.hs+2−1
@@ -1,5 +1,6 @@
11{-# LANGUAGE BangPatterns, OverloadedStrings #-}
22
3+import Control.DeepSeq
34import Control.Exception
45import Control.Monad
56import Data.Aeson
@@ -23,7 +24,7 @@ main = do
2324 let refill = B.hGet h 1024
2425 result <- parseWith refill json =<< refill
2526 case result of
26− Done _ _ -> loop (good+1) bad
27+ Done _ r -> rnf r `seq` loop (good+1) bad
2728 _ -> loop good (bad+1)
2829 (good, _) <- loop 0 0
2930 end <- getCurrentTime