Bryan O'Sullivan avatar Bryan O'Sullivan committed 610bd81

Support ByteString-keyed Maps.

Comments (0)

Files changed (1)

Data/Aeson/Types.hs

     {-# INLINE parseJSON #-}
 
 instance ToJSON B.ByteString where
-    toJSON = String . decodeUtf8
+    toJSON = String . decode
     {-# INLINE toJSON #-}
 
 instance FromJSON B.ByteString where
     {-# INLINE parseJSON #-}
 
 instance ToJSON LB.ByteString where
-    toJSON = toJSON . B.concat . LB.toChunks
+    toJSON = toJSON . strict
     {-# INLINE toJSON #-}
 
 instance FromJSON LB.ByteString where
-    parseJSON (String t) = pure . LB.fromChunks . (:[]) . encodeUtf8 $ t
+    parseJSON (String t) = pure . lazy $ t
     parseJSON _          = empty
     {-# INLINE parseJSON #-}
 
 instance (FromJSON v) => FromJSON (M.Map String v) where
     parseJSON = fmap (M.mapKeysMonotonic unpack) . parseJSON
 
+instance (ToJSON v) => ToJSON (M.Map B.ByteString v) where
+    toJSON = Object . transformMap decode toJSON
+
+instance (FromJSON v) => FromJSON (M.Map B.ByteString v) where
+    parseJSON = fmap (M.mapKeysMonotonic encodeUtf8) . parseJSON
+
+instance (ToJSON v) => ToJSON (M.Map LB.ByteString v) where
+    toJSON = Object . transformMap strict toJSON
+
+instance (FromJSON v) => FromJSON (M.Map LB.ByteString v) where
+    parseJSON = fmap (M.mapKeysMonotonic lazy) . parseJSON
+
 instance ToJSON Value where
     toJSON a = a
     {-# INLINE toJSON #-}
   where
     go (a:as) = (:) <$> f a <*> go as
     go _      = pure []
+
+strict :: LB.ByteString -> Text
+strict = decode . B.concat . LB.toChunks
+
+lazy :: Text -> LB.ByteString
+lazy = LB.fromChunks . (:[]) . encodeUtf8
+
+decode :: B.ByteString -> Text
+decode = decodeUtf8
+{-# INLINE decode #-}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.