summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bittorrent.cabal2
-rw-r--r--src/Data/Torrent/JSON.hs17
-rw-r--r--src/Network/BitTorrent/Core/PeerId.hs1
3 files changed, 18 insertions, 2 deletions
diff --git a/bittorrent.cabal b/bittorrent.cabal
index c9e04909..68410707 100644
--- a/bittorrent.cabal
+++ b/bittorrent.cabal
@@ -141,7 +141,7 @@ library
141 , hashable >= 1.2 141 , hashable >= 1.2
142 142
143 -- Codecs & Serialization 143 -- Codecs & Serialization
144 , aeson >= 0.6.2.0 && < 0.7 144 , aeson >= 0.7
145 , attoparsec >= 0.10 145 , attoparsec >= 0.10
146 , base16-bytestring >= 0.1 146 , base16-bytestring >= 0.1
147 , base32-bytestring >= 0.2 147 , base32-bytestring >= 0.2
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
6import Control.Applicative
6import Data.Aeson.TH 7import Data.Aeson.TH
8import Data.Aeson.Types
9import Data.ByteString as BS
10import Data.ByteString.Base16 as Base16
7import Data.Char 11import Data.Char
8import Data.List as L 12import Data.List as L
13import Data.Text.Encoding as T
9 14
10 15
11-- | Ignore '_' prefix. 16-- | Ignore '_' prefix.
@@ -28,4 +33,14 @@ mapWhile p f = go
28omitRecordPrefix :: Options 33omitRecordPrefix :: Options
29omitRecordPrefix = omitLensPrefix 34omitRecordPrefix = 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
38instance ToJSON ByteString where
39 toJSON = String . T.decodeUtf8 . Base16.encode
40
41instance 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"
diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs
index c860d9d4..f8d69e8b 100644
--- a/src/Network/BitTorrent/Core/PeerId.hs
+++ b/src/Network/BitTorrent/Core/PeerId.hs
@@ -62,6 +62,7 @@ import Text.PrettyPrint hiding ((<>))
62import Text.PrettyPrint.Class 62import Text.PrettyPrint.Class
63import Text.Read (readMaybe) 63import Text.Read (readMaybe)
64 64
65import Data.Torrent.JSON ()
65import Network.BitTorrent.Core.Fingerprint 66import Network.BitTorrent.Core.Fingerprint
66 67
67-- TODO use unpacked Word160 form (length is known statically) 68-- TODO use unpacked Word160 form (length is known statically)