Skip to content

Commit

Permalink
Add an NFData instance for Value
Browse files Browse the repository at this point in the history
  • Loading branch information
bos committed Jan 26, 2011
1 parent 809740f commit 4b9b9e0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
9 changes: 9 additions & 0 deletions Data/Aeson/Types.hs
Expand Up @@ -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)
Expand All @@ -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 (.=) #-}
Expand Down
1 change: 1 addition & 0 deletions aeson.cabal
Expand Up @@ -35,6 +35,7 @@ library
blaze-builder >= 0.2.1.4,
bytestring,
containers == 0.3.*,
deepseq,
old-locale,
text >= 0.11.0.2,
time,
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/ParseFile.hs
@@ -1,5 +1,6 @@
{-# LANGUAGE BangPatterns, OverloadedStrings #-}

import Control.DeepSeq
import Control.Exception
import Control.Monad
import Data.Aeson
Expand All @@ -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
Expand Down

0 comments on commit 4b9b9e0

Please sign in to comment.