on master help sign in
Support ByteString-keyed Maps.
commite8199b88af60291c1c6ef2cfcf4ccc0352d3dff7 authorBryan O'Sullivan <bos@mailrank.com> dateWed 9 Mar 2011, 12:18 (16 years ago) parentda9589a
1 file changed+25−3
Data/Aeson/Types.hs+25−3
Data/Aeson/Types.hs+25−3
@@ -457,7 +457,7 @@ instance FromJSON LT.Text where
457457 {-# INLINE parseJSON #-}
458458
459459instance ToJSON B.ByteString where
460− toJSON = String . decodeUtf8
460+ toJSON = String . decode
461461 {-# INLINE toJSON #-}
462462
463463instance FromJSON B.ByteString where
@@ -466,11 +466,11 @@ instance FromJSON B.ByteString where
466466 {-# INLINE parseJSON #-}
467467
468468instance ToJSON LB.ByteString where
469− toJSON = toJSON . B.concat . LB.toChunks
469+ toJSON = toJSON . strict
470470 {-# INLINE toJSON #-}
471471
472472instance FromJSON LB.ByteString where
473− parseJSON (String t) = pure . LB.fromChunks . (:[]) . encodeUtf8 $ t
473+ parseJSON (String t) = pure . lazy $ t
474474 parseJSON _ = empty
475475 {-# INLINE parseJSON #-}
476476
@@ -531,6 +531,18 @@ instance (ToJSON v) => ToJSON (M.Map String v) where
531531instance (FromJSON v) => FromJSON (M.Map String v) where
532532 parseJSON = fmap (M.mapKeysMonotonic unpack) . parseJSON
533533
534+instance (ToJSON v) => ToJSON (M.Map B.ByteString v) where
535+ toJSON = Object . transformMap decode toJSON
536+
537+instance (FromJSON v) => FromJSON (M.Map B.ByteString v) where
538+ parseJSON = fmap (M.mapKeysMonotonic encodeUtf8) . parseJSON
539+
540+instance (ToJSON v) => ToJSON (M.Map LB.ByteString v) where
541+ toJSON = Object . transformMap strict toJSON
542+
543+instance (FromJSON v) => FromJSON (M.Map LB.ByteString v) where
544+ parseJSON = fmap (M.mapKeysMonotonic lazy) . parseJSON
545+
534546instance ToJSON Value where
535547 toJSON a = a
536548 {-# INLINE toJSON #-}
@@ -609,3 +621,13 @@ mapA f = go
609621 where
610622 go (a:as) = (:) <$> f a <*> go as
611623 go _ = pure []
624+
625+strict :: LB.ByteString -> Text
626+strict = decode . B.concat . LB.toChunks
627+
628+lazy :: Text -> LB.ByteString
629+lazy = LB.fromChunks . (:[]) . encodeUtf8
630+
631+decode :: B.ByteString -> Text
632+decode = decodeUtf8
633+{-# INLINE decode #-}