on master help sign in
Make Value an instance of Hashable.
commit4df5ade293f7412393e364fe4bb861b6529cbcb9 authorBryan O'Sullivan <bos@mailrank.com> dateTue 10 May 2011, 15:48 (15 years ago) parenta1bfeb8
1 file changed+10
Data/Aeson/Types.hs+10
Data/Aeson/Types.hs+10
@@ -45,7 +45,9 @@ import Control.Monad (MonadPlus(..), ap)
4545import Data.Aeson.Functions
4646import Data.Attoparsec.Char8 (Number(..))
4747import Data.Data (Data)
48+import Data.Hashable (Hashable(..))
4849import Data.Int (Int8, Int16, Int32, Int64)
50+import Data.List (foldl')
4951import Data.Map (Map)
5052import Data.Monoid (Dual(..), First(..), Last(..))
5153import Data.Monoid (Monoid(..))
@@ -201,6 +203,14 @@ instance IsString Value where
201203 fromString = String . pack
202204 {-# INLINE fromString #-}
203205
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+
204214-- | The empty array.
205215emptyArray :: Value
206216emptyArray = Array V.empty