@@ -22,7 +22,7 @@ module Data.Aeson.Generic
2222import Control.Applicative ((<$>))
2323import Control.Arrow (first)
2424import Control.Monad.State.Strict
25−import Data.Aeson.Functions (hashMap, transformMap)
25+import Data.Aeson.Functions
2626import Data.Aeson.Types hiding (FromJSON(..), ToJSON(..), fromJSON)
2727import Data.Attoparsec.Number (Number)
2828import Data.Generics
@@ -30,6 +30,7 @@ import Data.Int (Int8, Int16, Int32, Int64)
3030import Data.IntSet (IntSet)
3131import Data.Maybe (fromJust)
3232import Data.Text (Text, pack, unpack)
33+import Data.Text.Encoding (encodeUtf8)
3334import Data.Time.Clock (UTCTime)
3435import Data.Word (Word, Word8, Word16, Word32, Word64)
3536import qualified Data.Aeson.Types as T
@@ -87,18 +88,22 @@ toJSON = toJSON_generic
8788 set s = Array . V.fromList . map toJSON . Set.toList $ s
8889
8990 mapAny m
90− | tyrep == typeOf "" = remap pack
9191 | tyrep == typeOf DT.empty = remap id
9292 | tyrep == typeOf LT.empty = remap LT.toStrict
93+ | tyrep == typeOf "" = remap pack
94+ | tyrep == typeOf B.empty = remap decode
95+ | tyrep == typeOf L.empty = remap strict
9396 | otherwise = modError "toJSON" $
9497 "cannot convert map keyed by type " ++ show tyrep
9598 where tyrep = typeOf . head . Map.keys $ m
9699 remap f = Object . transformMap (f . fromJust . cast) toJSON $ m
97100
98101 hashMapAny m
99− | tyrep == typeOf "" = remap pack
100102 | tyrep == typeOf DT.empty = remap id
101103 | tyrep == typeOf LT.empty = remap LT.toStrict
104+ | tyrep == typeOf "" = remap pack
105+ | tyrep == typeOf B.empty = remap decode
106+ | tyrep == typeOf L.empty = remap strict
102107 | otherwise = modError "toJSON" $
103108 "cannot convert map keyed by type " ++ show tyrep
104109 where tyrep = typeOf . head . H.keys $ m
@@ -194,20 +199,24 @@ parseJSON j = parseJSON_generic j
194199 _ -> myFail
195200 mapAny :: forall e f. (Data e, Data f) => Parser (Map.Map f e)
196201 mapAny
197− | tyrep `elem` [typeOf LT.empty, typeOf DT.empty, typeOf ""] = res
202+ | tyrep `elem` stringyTypes = res
198203 | otherwise = myFail
199204 where res = case j of
200205 Object js -> Map.mapKeysMonotonic trans <$> T.mapM parseJSON js
201206 _ -> myFail
202207 trans
203− | tyrep == typeOf DT.empty = fromJust . cast . id
204− | tyrep == typeOf LT.empty = fromJust . cast . LT.fromStrict
205− | tyrep == typeOf "" = fromJust . cast . DT.unpack
208+ | tyrep == typeOf DT.empty = remap id
209+ | tyrep == typeOf LT.empty = remap LT.fromStrict
210+ | tyrep == typeOf "" = remap DT.unpack
211+ | tyrep == typeOf B.empty = remap encodeUtf8
212+ | tyrep == typeOf L.empty = remap lazy
206213 | otherwise = modError "parseJSON"
207214 "mapAny -- should never happen"
208215 tyrep = typeOf (undefined :: f)
216+ remap f = fromJust . cast . f
209217 myFail = modFail "parseJSON" $ "bad data: " ++ show j
210−
218+ stringyTypes = [typeOf LT.empty, typeOf DT.empty, typeOf B.empty,
219+ typeOf L.empty, typeOf ""]
211220
212221parseJSON_generic :: (Data a) => Value -> Parser a
213222parseJSON_generic j = generic