on master help sign in
Merge
commit2b7345133f6c6115c1acf96a33e75d1d393b4a82 authorBryan O'Sullivan <bos@mailrank.com> dateSat 12 Mar 2011, 07:46 (16 years ago) parent97021b809faa3f
6 files changed+81−34
.hgignore+1−1 .hgtags+1 Data/Aeson/Functions.hs+20 Data/Aeson/Generic.hs+17−7 Data/Aeson/Types.hs+40−24 aeson.cabal+2−2
.hgignore+1−1
@@ -1,6 +1,6 @@
11^(?:dist|\.DS_Store)$
22^benchmarks/(?:AesonParse|EncodeFile|JsonParse|.*_p)$
3−.*\.(?:h[ip]|o|orig|out|pdf|prof|ps|rej)$
3+.*\.(?:aux|h[ip]|o|orig|out|pdf|prof|ps|rej)$
44
55syntax: glob
66*~
.hgtags+1
@@ -1,2 +1,3 @@
1105d9437b2280648ae25ac170697ec1c48eda7af0 0.1.0.0
22f4cf6abd5a81affb08c6563d51e38ac4f8451217 0.2.0.0
3+fe4084e5615941d9822834239e4c32a036291327 0.3.0.0
Data/Aeson/Functions.hs+20
@@ -3,10 +3,18 @@ module Data.Aeson.Functions
33 hashMap
44 , mapHash
55 , transformMap
6+ -- * String conversions
7+ , decode
8+ , strict
9+ , lazy
610 ) where
711
812import Control.Arrow ((***), first)
913import Data.Hashable (Hashable)
14+import Data.Text (Text)
15+import Data.Text.Encoding (decodeUtf8, encodeUtf8)
16+import qualified Data.ByteString as B
17+import qualified Data.ByteString.Lazy as L
1018import qualified Data.HashMap.Strict as H
1119import qualified Data.Map as M
1220
@@ -27,3 +35,15 @@ hashMap fk kv = M.fromList . map (fk *** kv) . H.toList
2735mapHash :: (Eq k2, Hashable k2) => (k1 -> k2) -> M.Map k1 v -> H.HashMap k2 v
2836mapHash fk = H.fromList . map (first fk) . M.toList
2937{-# INLINE mapHash #-}
38+
39+strict :: L.ByteString -> Text
40+strict = decode . B.concat . L.toChunks
41+{-# INLINE strict #-}
42+
43+lazy :: Text -> L.ByteString
44+lazy = L.fromChunks . (:[]) . encodeUtf8
45+{-# INLINE lazy #-}
46+
47+decode :: B.ByteString -> Text
48+decode = decodeUtf8
49+{-# INLINE decode #-}
Data/Aeson/Generic.hs+17−7
@@ -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
@@ -31,6 +31,7 @@ import Data.Int (Int8, Int16, Int32, Int64)
3131import Data.IntSet (IntSet)
3232import Data.Maybe (fromJust)
3333import Data.Text (Text, pack, unpack)
34+import Data.Text.Encoding (encodeUtf8)
3435import Data.Time.Clock (UTCTime)
3536import Data.Word (Word, Word8, Word16, Word32, Word64)
3637import qualified Data.Aeson.Types as T
@@ -88,18 +89,22 @@ toJSON = toJSON_generic
8889 set s = Array . V.fromList . map toJSON . Set.toList $ s
8990
9091 mapAny m
91− | tyrep == typeOf "" = remap pack
9292 | tyrep == typeOf DT.empty = remap id
9393 | tyrep == typeOf LT.empty = remap LT.toStrict
94+ | tyrep == typeOf "" = remap pack
95+ | tyrep == typeOf B.empty = remap decode
96+ | tyrep == typeOf L.empty = remap strict
9497 | otherwise = modError "toJSON" $
9598 "cannot convert map keyed by type " ++ show tyrep
9699 where tyrep = typeOf . head . Map.keys $ m
97100 remap f = Object . transformMap (f . fromJust . cast) toJSON $ m
98101
99102 hashMapAny m
100− | tyrep == typeOf "" = remap pack
101103 | tyrep == typeOf DT.empty = remap id
102104 | tyrep == typeOf LT.empty = remap LT.toStrict
105+ | tyrep == typeOf "" = remap pack
106+ | tyrep == typeOf B.empty = remap decode
107+ | tyrep == typeOf L.empty = remap strict
103108 | otherwise = modError "toJSON" $
104109 "cannot convert map keyed by type " ++ show tyrep
105110 where tyrep = typeOf . head . H.keys $ m
@@ -195,18 +200,21 @@ parseJSON j = parseJSON_generic j
195200 _ -> myFail
196201 mapAny :: forall e f. (Data e, Data f) => Parser (Map.Map f e)
197202 mapAny
198− | tyrep `elem` [typeOf LT.empty, typeOf DT.empty, typeOf ""] = res
203+ | tyrep `elem` stringyTypes = res
199204 | otherwise = myFail
200205 where res = case j of
201206 Object js -> Map.mapKeysMonotonic trans <$> T.mapM parseJSON js
202207 _ -> myFail
203208 trans
204− | tyrep == typeOf DT.empty = fromJust . cast . id
205− | tyrep == typeOf LT.empty = fromJust . cast . LT.fromStrict
206− | tyrep == typeOf "" = fromJust . cast . DT.unpack
209+ | tyrep == typeOf DT.empty = remap id
210+ | tyrep == typeOf LT.empty = remap LT.fromStrict
211+ | tyrep == typeOf "" = remap DT.unpack
212+ | tyrep == typeOf B.empty = remap encodeUtf8
213+ | tyrep == typeOf L.empty = remap lazy
207214 | otherwise = modError "parseJSON"
208215 "mapAny -- should never happen"
209216 tyrep = typeOf (undefined :: f)
217+ remap f = fromJust . cast . f
210218 hashMapAny :: forall e f. (Data e, Data f) => Parser (H.HashMap f e)
211219 hashMapAny
212220 | tyrep == typeOf "" = process DT.unpack
@@ -222,6 +230,8 @@ parseJSON j = parseJSON_generic j
222230 _ -> myFail
223231 tyrep = typeOf (undefined :: f)
224232 myFail = modFail "parseJSON" $ "bad data: " ++ show j
233+ stringyTypes = [typeOf LT.empty, typeOf DT.empty, typeOf B.empty,
234+ typeOf L.empty, typeOf ""]
225235
226236parseJSON_generic :: (Data a) => Value -> Parser a
227237parseJSON_generic j = generic
Data/Aeson/Types.hs+40−24
@@ -38,7 +38,7 @@ module Data.Aeson.Types
3838import Control.Applicative
3939import Control.DeepSeq (NFData(..))
4040import Control.Monad (MonadPlus(..))
41−import Data.Aeson.Functions (hashMap, mapHash, transformMap)
41+import Data.Aeson.Functions
4242import Data.Attoparsec.Char8 (Number(..))
4343import Data.Data (Data)
4444import Data.Int (Int8, Int16, Int32, Int64)
@@ -48,7 +48,7 @@ import Data.Monoid (Monoid(..))
4848import Data.Ratio (Ratio)
4949import Data.String (IsString(..))
5050import Data.Text (Text, pack, unpack)
51−import Data.Text.Encoding (decodeUtf8, encodeUtf8)
51+import Data.Text.Encoding (encodeUtf8)
5252import Data.Time.Clock (UTCTime)
5353import Data.Time.Format (FormatTime, formatTime, parseTime)
5454import Data.Typeable (Typeable)
@@ -458,7 +458,7 @@ instance FromJSON LT.Text where
458458 {-# INLINE parseJSON #-}
459459
460460instance ToJSON B.ByteString where
461− toJSON = String . decodeUtf8
461+ toJSON = String . decode
462462 {-# INLINE toJSON #-}
463463
464464instance FromJSON B.ByteString where
@@ -467,11 +467,11 @@ instance FromJSON B.ByteString where
467467 {-# INLINE parseJSON #-}
468468
469469instance ToJSON LB.ByteString where
470− toJSON = toJSON . B.concat . LB.toChunks
470+ toJSON = toJSON . strict
471471 {-# INLINE toJSON #-}
472472
473473instance FromJSON LB.ByteString where
474− parseJSON (String t) = pure . LB.fromChunks . (:[]) . encodeUtf8 $ t
474+ parseJSON (String t) = pure . lazy $ t
475475 parseJSON _ = empty
476476 {-# INLINE parseJSON #-}
477477
@@ -480,7 +480,7 @@ instance (ToJSON a) => ToJSON [a] where
480480 {-# INLINE toJSON #-}
481481
482482instance (FromJSON a) => FromJSON [a] where
483− parseJSON (Array a) = mapA parseJSON (V.toList a)
483+ parseJSON (Array a) = mapM parseJSON (V.toList a)
484484 parseJSON _ = empty
485485 {-# INLINE parseJSON #-}
486486
@@ -489,7 +489,7 @@ instance (ToJSON a) => ToJSON (Vector a) where
489489 {-# INLINE toJSON #-}
490490
491491instance (FromJSON a) => FromJSON (Vector a) where
492− parseJSON (Array a) = V.fromList <$> mapA parseJSON (V.toList a)
492+ parseJSON (Array a) = V.mapM parseJSON a
493493 parseJSON _ = empty
494494 {-# INLINE parseJSON #-}
495495
@@ -514,10 +514,8 @@ instance (ToJSON v) => ToJSON (M.Map Text v) where
514514 {-# INLINE toJSON #-}
515515
516516instance (FromJSON v) => FromJSON (M.Map Text v) where
517− parseJSON (Object o) = M.fromAscList <$> go (M.toAscList o)
518− where
519− go ((k,v):kvs) = ((:) . (,) k) <$> parseJSON v <*> go kvs
520− go _ = pure []
517+ parseJSON (Object o) = M.fromAscList <$> mapM go (M.toAscList o)
518+ where go (k,v) = ((,) k) <$> parseJSON v
521519 parseJSON _ = empty
522520
523521instance (ToJSON v) => ToJSON (M.Map LT.Text v) where
@@ -532,15 +530,25 @@ instance (ToJSON v) => ToJSON (M.Map String v) where
532530instance (FromJSON v) => FromJSON (M.Map String v) where
533531 parseJSON = fmap (M.mapKeysMonotonic unpack) . parseJSON
534532
533+instance (ToJSON v) => ToJSON (M.Map B.ByteString v) where
534+ toJSON = Object . transformMap decode toJSON
535+
536+instance (FromJSON v) => FromJSON (M.Map B.ByteString v) where
537+ parseJSON = fmap (M.mapKeysMonotonic encodeUtf8) . parseJSON
538+
539+instance (ToJSON v) => ToJSON (M.Map LB.ByteString v) where
540+ toJSON = Object . transformMap strict toJSON
541+
542+instance (FromJSON v) => FromJSON (M.Map LB.ByteString v) where
543+ parseJSON = fmap (M.mapKeysMonotonic lazy) . parseJSON
544+
535545instance (ToJSON v) => ToJSON (H.HashMap Text v) where
536546 toJSON = Object . hashMap id toJSON
537547 {-# INLINE toJSON #-}
538548
539549instance (FromJSON v) => FromJSON (H.HashMap Text v) where
540− parseJSON (Object o) = H.fromList <$> go (M.toList o)
541− where
542− go ((k,v):kvs) = ((:) . (,) k) <$> parseJSON v <*> go kvs
543− go _ = pure []
550+ parseJSON (Object o) = H.fromList <$> mapM go (M.toList o)
551+ where go (k,v) = ((,) k) <$> parseJSON v
544552 parseJSON _ = empty
545553
546554instance (ToJSON v) => ToJSON (H.HashMap LT.Text v) where
@@ -555,6 +563,18 @@ instance (ToJSON v) => ToJSON (H.HashMap String v) where
555563instance (FromJSON v) => FromJSON (H.HashMap String v) where
556564 parseJSON = fmap (mapHash unpack) . parseJSON
557565
566+instance (ToJSON v) => ToJSON (H.HashMap B.ByteString v) where
567+ toJSON = Object . hashMap decode toJSON
568+
569+instance (FromJSON v) => FromJSON (H.HashMap B.ByteString v) where
570+ parseJSON = fmap (mapHash encodeUtf8) . parseJSON
571+
572+instance (ToJSON v) => ToJSON (H.HashMap LB.ByteString v) where
573+ toJSON = Object . hashMap strict toJSON
574+
575+instance (FromJSON v) => FromJSON (H.HashMap LB.ByteString v) where
576+ parseJSON = fmap (mapHash lazy) . parseJSON
577+
558578instance ToJSON Value where
559579 toJSON a = a
560580 {-# INLINE toJSON #-}
@@ -565,8 +585,9 @@ instance FromJSON Value where
565585
566586-- | A newtype wrapper for 'UTCTime' that uses the same non-standard
567587-- serialization format as Microsoft .NET.
568−newtype DotNetTime = DotNetTime UTCTime
569− deriving (Eq, Ord, Read, Show, Typeable, FormatTime)
588+newtype DotNetTime = DotNetTime {
589+ fromDotNetTime :: UTCTime
590+ } deriving (Eq, Ord, Read, Show, Typeable, FormatTime)
570591
571592instance ToJSON DotNetTime where
572593 toJSON (DotNetTime t) =
@@ -582,7 +603,8 @@ instance FromJSON DotNetTime where
582603 {-# INLINE parseJSON #-}
583604
584605instance ToJSON UTCTime where
585− toJSON t = String (pack (formatTime defaultTimeLocale "%FT%X%QZ" t))
606+ toJSON t = String (pack (take 23 str ++ "Z"))
607+ where str = formatTime defaultTimeLocale "%FT%T%Q" t
586608 {-# INLINE toJSON #-}
587609
588610instance FromJSON UTCTime where
@@ -627,9 +649,3 @@ instance ToJSON a => ToJSON (Last a) where
627649instance FromJSON a => FromJSON (Last a) where
628650 parseJSON = fmap Last . parseJSON
629651 {-# INLINE parseJSON #-}
630−
631−mapA :: (Alternative m) => (t -> m a) -> [t] -> m [a]
632−mapA f = go
633− where
634− go (a:as) = (:) <$> f a <*> go as
635− go _ = pure []
aeson.cabal+2−2
@@ -1,5 +1,5 @@
11name: aeson
2−version: 0.2.0.0
2+version: 0.3.0.0
33license: BSD3
44license-file: LICENSE
55category: Text, Web, JSON
@@ -99,7 +99,7 @@ library
9999 syb,
100100 text >= 0.11.0.2,
101101 time,
102− unordered-containers,
102+ unordered-containers >= 0.1.2.0,
103103 vector >= 0.7
104104
105105 if flag(developer)