on 0.3.2.4 help sign in
typeable/aeson/Data/Aeson/Functions.hs
49 lines 1.4 kB history raw
1module Data.Aeson.Functions
2 (
3 hashMap
4 , mapHash
5 , transformMap
6 -- * String conversions
7 , decode
8 , strict
9 , lazy
10 ) where
12import Control.Arrow ((***), first)
13import Data.Hashable (Hashable)
14import Data.Text (Text)
15import Data.Text.Encoding (decodeUtf8, encodeUtf8)
16import qualified Data.ByteString as B
17import qualified Data.ByteString.Lazy as L
18import qualified Data.HashMap.Strict as H
19import qualified Data.Map as M
21-- | Transform one map into another. The ordering of keys must be
22-- preserved by the key transformation function.
23transformMap :: (Ord k2) => (k1 -> k2) -> (v1 -> v2)
24 -> M.Map k1 v1 -> M.Map k2 v2
25transformMap fk fv = M.fromAscList . map (fk *** fv) . M.toAscList
26{-# INLINE transformMap #-}
28-- | Transform a 'H.HashMap' into a 'M.Map'.
29hashMap :: (Ord k2) => (k1 -> k2) -> (v1 -> v2)
30 -> H.HashMap k1 v1 -> M.Map k2 v2
31hashMap fk kv = M.fromList . map (fk *** kv) . H.toList
32{-# INLINE hashMap #-}
34-- | Transform a 'M.Map' into a 'H.HashMap'.
35mapHash :: (Eq k2, Hashable k2) => (k1 -> k2) -> M.Map k1 v -> H.HashMap k2 v
36mapHash fk = H.fromList . map (first fk) . M.toList
37{-# INLINE mapHash #-}
39strict :: L.ByteString -> Text
40strict = decode . B.concat . L.toChunks
41{-# INLINE strict #-}
43lazy :: Text -> L.ByteString
44lazy = L.fromChunks . (:[]) . encodeUtf8
45{-# INLINE lazy #-}
47decode :: B.ByteString -> Text
48decode = decodeUtf8
49{-# INLINE decode #-}