@@ -22,7 +22,7 @@ module Data.Aeson.Generic
22import Control.Applicative ((<$>))
22import Control.Applicative ((<$>))
23import Control.Arrow (first)
23import Control.Arrow (first)
24import Control.Monad.State.Strict
24import Control.Monad.State.Strict
25−import Data.Aeson.Functions (hashMap, transformMap)
25+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
@@ -30,6 +30,7 @@ import Data.Int (Int8, Int16, Int32, Int64)
30import Data.IntSet (IntSet)
30import Data.IntSet (IntSet)
31import Data.Maybe (fromJust)
31import Data.Maybe (fromJust)
32import Data.Text (Text, pack, unpack)
32import Data.Text (Text, pack, unpack)
33+import Data.Text.Encoding (encodeUtf8)
33import Data.Time.Clock (UTCTime)
34import Data.Time.Clock (UTCTime)
34import Data.Word (Word, Word8, Word16, Word32, Word64)
35import Data.Word (Word, Word8, Word16, Word32, Word64)
35import qualified Data.Aeson.Types as T
36import qualified Data.Aeson.Types as T
@@ -87,18 +88,22 @@ toJSON = toJSON_generic
87 set s = Array . V.fromList . map toJSON . Set.toList $ s
88 set s = Array . V.fromList . map toJSON . Set.toList $ s
90− | tyrep == typeOf "" = remap pack
91 | tyrep == typeOf DT.empty = remap id
91 | tyrep == typeOf DT.empty = remap id
92 | tyrep == typeOf LT.empty = remap LT.toStrict
92 | 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
93 | otherwise = modError "toJSON" $
96 | otherwise = modError "toJSON" $
94 "cannot convert map keyed by type " ++ show tyrep
97 "cannot convert map keyed by type " ++ show tyrep
95 where tyrep = typeOf . head . Map.keys $ m
98 where tyrep = typeOf . head . Map.keys $ m
96 remap f = Object . transformMap (f . fromJust . cast) toJSON $ m
99 remap f = Object . transformMap (f . fromJust . cast) toJSON $ m
98 hashMapAny m
101 hashMapAny m
99− | tyrep == typeOf "" = remap pack
100 | tyrep == typeOf DT.empty = remap id
102 | tyrep == typeOf DT.empty = remap id
101 | tyrep == typeOf LT.empty = remap LT.toStrict
103 | 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
102 | otherwise = modError "toJSON" $
107 | otherwise = modError "toJSON" $
103 "cannot convert map keyed by type " ++ show tyrep
108 "cannot convert map keyed by type " ++ show tyrep
104 where tyrep = typeOf . head . H.keys $ m
109 where tyrep = typeOf . head . H.keys $ m
@@ -194,20 +199,24 @@ parseJSON j = parseJSON_generic j
194 _ -> myFail
199 _ -> myFail
195 mapAny :: forall e f. (Data e, Data f) => Parser (Map.Map f e)
200 mapAny :: forall e f. (Data e, Data f) => Parser (Map.Map f e)
197− | tyrep `elem` [typeOf LT.empty, typeOf DT.empty, typeOf ""] = res
202+ | tyrep `elem` stringyTypes = res
198 | otherwise = myFail
203 | otherwise = myFail
199 where res = case j of
204 where res = case j of
200 Object js -> Map.mapKeysMonotonic trans <$> T.mapM parseJSON js
205 Object js -> Map.mapKeysMonotonic trans <$> T.mapM parseJSON js
201 _ -> myFail
206 _ -> myFail
203− | tyrep == typeOf DT.empty = fromJust . cast . id
208+ | tyrep == typeOf DT.empty = remap id
204− | tyrep == typeOf LT.empty = fromJust . cast . LT.fromStrict
209+ | tyrep == typeOf LT.empty = remap LT.fromStrict
205− | tyrep == typeOf "" = fromJust . cast . DT.unpack
210+ | tyrep == typeOf "" = remap DT.unpack
211+ | tyrep == typeOf B.empty = remap encodeUtf8
212+ | tyrep == typeOf L.empty = remap lazy
206 | otherwise = modError "parseJSON"
213 | otherwise = modError "parseJSON"
207 "mapAny -- should never happen"
214 "mapAny -- should never happen"
208 tyrep = typeOf (undefined :: f)
215 tyrep = typeOf (undefined :: f)
216+ remap f = fromJust . cast . f
209 myFail = modFail "parseJSON" $ "bad data: " ++ show j
217 myFail = modFail "parseJSON" $ "bad data: " ++ show j
210−
218+ stringyTypes = [typeOf LT.empty, typeOf DT.empty, typeOf B.empty,
219+ typeOf L.empty, typeOf ""]
212parseJSON_generic :: (Data a) => Value -> Parser a
221parseJSON_generic :: (Data a) => Value -> Parser a
213parseJSON_generic j = generic
222parseJSON_generic j = generic