on master help sign in
Make Value an instance of Hashable.
4df5adeBryan O'Sullivan
Data/Aeson/Types.hs+10
@@ -45,7 +45,9 @@ import Control.Monad (MonadPlus(..), ap)
45import Data.Aeson.Functions
45import Data.Aeson.Functions
46import Data.Attoparsec.Char8 (Number(..))
46import Data.Attoparsec.Char8 (Number(..))
47import Data.Data (Data)
47import Data.Data (Data)
48+import Data.Hashable (Hashable(..))
48import Data.Int (Int8, Int16, Int32, Int64)
49import Data.Int (Int8, Int16, Int32, Int64)
50+import Data.List (foldl')
49import Data.Map (Map)
51import Data.Map (Map)
50import Data.Monoid (Dual(..), First(..), Last(..))
52import Data.Monoid (Dual(..), First(..), Last(..))
51import Data.Monoid (Monoid(..))
53import Data.Monoid (Monoid(..))
@@ -201,6 +203,14 @@ instance IsString Value where
201 fromString = String . pack
203 fromString = String . pack
202 {-# INLINE fromString #-}
204 {-# INLINE fromString #-}
203
205
206+instance Hashable Value where
207+ hash (Object o) = foldl' hashWithSalt 0 . M.toList $ o
208+ hash (Array a) = V.foldl' hashWithSalt 1 a
209+ hash (String s) = 2 `hashWithSalt` s
210+ hash (Number n) = 3 `hashWithSalt` case n of I i -> hash i; D d -> hash d
211+ hash (Bool b) = 4 `hashWithSalt` b
212+ hash Null = 5
213+
204-- | The empty array.
214-- | The empty array.
205emptyArray :: Value
215emptyArray :: Value
206emptyArray = Array V.empty
216emptyArray = Array V.empty