Skip to content
Browse files

Add an NFData instance for Value

  • Loading branch information...
1 parent 809740f commit 4b9b9e0aaaf81987977f682f2e365e1e8fd954bb @bos committed
Showing with 12 additions and 1 deletion.
  1. +9 −0 Data/Aeson/Types.hs
  2. +1 −0 aeson.cabal
  3. +2 −1 benchmarks/ParseFile.hs
View
9 Data/Aeson/Types.hs
@@ -13,6 +13,7 @@ module Data.Aeson.Types
) where
import Control.Applicative
+import Control.DeepSeq
import Data.Map (Map)
import Data.Text (Text, pack, unpack)
import Data.Time.Clock (UTCTime)
@@ -34,6 +35,14 @@ data Value = Object Object
| Null
deriving (Eq, Show, Typeable)
+instance NFData Value where
+ rnf (Object o) = rnf o
+ rnf (Array a) = V.foldl' (\x y -> rnf y `seq` x) () a
+ rnf (String s) = rnf s
+ rnf (Number n) = rnf n
+ rnf (Bool b) = rnf b
+ rnf Null = ()
+
(.=) :: ToJSON a => Text -> a -> Object
name .= value = M.singleton name (toJSON value)
{-# INLINE (.=) #-}
View
1 aeson.cabal
@@ -35,6 +35,7 @@ library
blaze-builder >= 0.2.1.4,
bytestring,
containers == 0.3.*,
+ deepseq,
old-locale,
text >= 0.11.0.2,
time,
View
3 benchmarks/ParseFile.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE BangPatterns, OverloadedStrings #-}
+import Control.DeepSeq
import Control.Exception
import Control.Monad
import Data.Aeson
@@ -23,7 +24,7 @@ main = do
let refill = B.hGet h 1024
result <- parseWith refill json =<< refill
case result of
- Done _ _ -> loop (good+1) bad
+ Done _ r -> rnf r `seq` loop (good+1) bad
_ -> loop good (bad+1)
(good, _) <- loop 0 0
end <- getCurrentTime

0 comments on commit 4b9b9e0

Please sign in to comment.
Something went wrong with that request. Please try again.