diff options
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/Torrent/JSON.hs | 17 |
1 files changed, 16 insertions, 1 deletions
diff --git a/src/Data/Torrent/JSON.hs b/src/Data/Torrent/JSON.hs index 3fa8973d..6a0aa510 100644 --- a/src/Data/Torrent/JSON.hs +++ b/src/Data/Torrent/JSON.hs | |||
@@ -3,9 +3,14 @@ module Data.Torrent.JSON | |||
3 | , omitRecordPrefix | 3 | , omitRecordPrefix |
4 | ) where | 4 | ) where |
5 | 5 | ||
6 | import Control.Applicative | ||
6 | import Data.Aeson.TH | 7 | import Data.Aeson.TH |
8 | import Data.Aeson.Types | ||
9 | import Data.ByteString as BS | ||
10 | import Data.ByteString.Base16 as Base16 | ||
7 | import Data.Char | 11 | import Data.Char |
8 | import Data.List as L | 12 | import Data.List as L |
13 | import Data.Text.Encoding as T | ||
9 | 14 | ||
10 | 15 | ||
11 | -- | Ignore '_' prefix. | 16 | -- | Ignore '_' prefix. |
@@ -28,4 +33,14 @@ mapWhile p f = go | |||
28 | omitRecordPrefix :: Options | 33 | omitRecordPrefix :: Options |
29 | omitRecordPrefix = omitLensPrefix | 34 | omitRecordPrefix = omitLensPrefix |
30 | { fieldLabelModifier = mapWhile isUpper toLower . L.dropWhile isLower | 35 | { fieldLabelModifier = mapWhile isUpper toLower . L.dropWhile isLower |
31 | } \ No newline at end of file | 36 | } |
37 | |||
38 | instance ToJSON ByteString where | ||
39 | toJSON = String . T.decodeUtf8 . Base16.encode | ||
40 | |||
41 | instance FromJSON ByteString where | ||
42 | parseJSON v = do | ||
43 | (ok, bad) <- (Base16.decode . T.encodeUtf8) <$> parseJSON v | ||
44 | if BS.null bad | ||
45 | then return ok | ||
46 | else fail "parseJSON: unable to decode ByteString" | ||