@@ -26,6 +26,7 @@ import Data.Aeson.Functions
26import Data.Aeson.Types hiding (FromJSON(..), ToJSON(..), fromJSON)
26import Data.Aeson.Types hiding (FromJSON(..), ToJSON(..), fromJSON)
27import Data.Attoparsec.Number (Number)
27import Data.Attoparsec.Number (Number)
28import Data.Generics
28import Data.Generics
29+import Data.Hashable (Hashable)
29import Data.Int (Int8, Int16, Int32, Int64)
30import Data.Int (Int8, Int16, Int32, Int64)
30import Data.IntSet (IntSet)
31import Data.IntSet (IntSet)
31import Data.Maybe (fromJust)
32import Data.Maybe (fromJust)
@@ -159,7 +160,7 @@ parseJSON j = parseJSON_generic j
159 `ext1R` list
160 `ext1R` list
160 `ext1R` vector
161 `ext1R` vector
161 `ext2R'` mapAny
162 `ext2R'` mapAny
162− -- Don't know how to support parsing HashMaps :-(
163+ `ext2R'` hashMapAny
163 -- Use the standard encoding for all base types.
164 -- Use the standard encoding for all base types.
164 `extR` (value :: F Integer)
165 `extR` (value :: F Integer)
165 `extR` (value :: F Int)
166 `extR` (value :: F Int)
@@ -214,6 +215,20 @@ parseJSON j = parseJSON_generic j
214 "mapAny -- should never happen"
215 "mapAny -- should never happen"
215 tyrep = typeOf (undefined :: f)
216 tyrep = typeOf (undefined :: f)
216 remap f = fromJust . cast . f
217 remap f = fromJust . cast . f
218+ hashMapAny :: forall e f. (Data e, Data f) => Parser (H.HashMap f e)
220+ | tyrep == typeOf "" = process DT.unpack
221+ | tyrep == typeOf LT.empty = process LT.fromStrict
222+ | tyrep == typeOf DT.empty = process id
223+ | otherwise = myFail
225+ process f = maybe myFail return . cast =<< parseWith f
226+ parseWith :: (Eq c, Hashable c) => (Text -> c) -> Parser (H.HashMap c e)
227+ parseWith f = case j of
228+ Object js -> H.fromList . map (first f) . Map.toList <$>
231+ tyrep = typeOf (undefined :: f)
217 myFail = modFail "parseJSON" $ "bad data: " ++ show j
232 myFail = modFail "parseJSON" $ "bad data: " ++ show j
218 stringyTypes = [typeOf LT.empty, typeOf DT.empty, typeOf B.empty,
233 stringyTypes = [typeOf LT.empty, typeOf DT.empty, typeOf B.empty,
219 typeOf L.empty, typeOf ""]
234 typeOf L.empty, typeOf ""]