From 47e56d698e2c8837656600561874915cf40c0d4e Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Fri, 4 Apr 2014 06:01:59 +0400 Subject: Remove aeson dependency --- bittorrent.cabal | 3 -- src/Data/Torrent.hs | 20 -------- src/Data/Torrent/InfoHash.hs | 10 ---- src/Data/Torrent/JSON.hs | 56 ---------------------- src/Data/Torrent/Layout.hs | 10 ---- src/Data/Torrent/Piece.hs | 16 ------- src/Data/Torrent/Progress.hs | 4 -- src/Network/BitTorrent/Core/NodeInfo.hs | 9 +--- src/Network/BitTorrent/Core/PeerAddr.hs | 8 ---- src/Network/BitTorrent/Core/PeerId.hs | 4 +- src/Network/BitTorrent/Exchange/Block.hs | 4 -- .../BitTorrent/Exchange/Connection/Status.hs | 4 -- src/Network/BitTorrent/Internal/Cache.hs | 25 ---------- src/Network/BitTorrent/Tracker/Message.hs | 8 ---- src/Network/BitTorrent/Tracker/Session.hs | 12 ----- 15 files changed, 2 insertions(+), 191 deletions(-) delete mode 100644 src/Data/Torrent/JSON.hs diff --git a/bittorrent.cabal b/bittorrent.cabal index 5c53d3fc..91d227b2 100644 --- a/bittorrent.cabal +++ b/bittorrent.cabal @@ -91,8 +91,6 @@ library System.Torrent.FileMap System.Torrent.Storage other-modules: Paths_bittorrent - Data.Torrent.JSON - build-depends: base == 4.* , lifted-base , bits-extras >= 0.1.2 @@ -141,7 +139,6 @@ library , hashable >= 1.2 -- Codecs & Serialization - , aeson >= 0.7 , attoparsec >= 0.10 , base16-bytestring >= 0.1 , base32-bytestring >= 0.2 diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index ccd5e666..b233937b 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs @@ -71,8 +71,6 @@ import qualified Crypto.Hash.SHA1 as C import Control.DeepSeq import Control.Exception import Control.Lens -import Data.Aeson.Types (ToJSON(..), FromJSON(..), Value(..), withText) -import Data.Aeson.TH import Data.BEncode as BE import Data.BEncode.Types as BE import Data.ByteString as BS @@ -93,7 +91,6 @@ import Text.PrettyPrint.Class import System.FilePath import Data.Torrent.InfoHash as IH -import Data.Torrent.JSON import Data.Torrent.Layout import Data.Torrent.Piece import Network.BitTorrent.Core.NodeInfo @@ -125,8 +122,6 @@ data InfoDict = InfoDict -- BEP 27: } deriving (Show, Read, Eq, Typeable) -$(deriveJSON omitRecordPrefix ''InfoDict) - makeLensesFor [ ("idInfoHash" , "infohash" ) , ("idLayoutInfo", "layoutInfo") @@ -242,21 +237,6 @@ data Torrent = Torrent -- encrypted SHA-1 hash of the info dictionary). } deriving (Show, Eq, Typeable) -instance FromJSON URI where - parseJSON = withText "URI" $ - maybe (fail "could not parse URI") pure . parseURI . T.unpack - -instance ToJSON URI where - toJSON = String . T.pack . show - -instance ToJSON NominalDiffTime where - toJSON = toJSON . posixSecondsToUTCTime - -instance FromJSON NominalDiffTime where - parseJSON v = utcTimeToPOSIXSeconds <$> parseJSON v - -$(deriveJSON omitRecordPrefix ''Torrent) - makeLensesFor [ ("tAnnounce" , "announce" ) , ("tAnnounceList", "announceList") diff --git a/src/Data/Torrent/InfoHash.hs b/src/Data/Torrent/InfoHash.hs index 9eec631c..f322ac6f 100644 --- a/src/Data/Torrent/InfoHash.hs +++ b/src/Data/Torrent/InfoHash.hs @@ -24,7 +24,6 @@ module Data.Torrent.InfoHash import Control.Applicative import Control.Monad -import Data.Aeson import Data.BEncode import Data.ByteString as BS import Data.ByteString.Char8 as BC @@ -146,15 +145,6 @@ instance Convertible Text InfoHash where instance IsString InfoHash where fromString = either (error . prettyConvertError) id . safeConvert . T.pack --- | Convert to base16 encoded JSON string. -instance ToJSON InfoHash where - toJSON (InfoHash ih) = String $ T.decodeUtf8 $ Base16.encode ih - --- | Convert from base16\/base32\/base64 encoded JSON string. -instance FromJSON InfoHash where - parseJSON = withText "InfoHash" $ - either (fail . prettyConvertError) pure . safeConvert - ignoreErrorMsg :: Either a b -> Maybe b ignoreErrorMsg = either (const Nothing) Just diff --git a/src/Data/Torrent/JSON.hs b/src/Data/Torrent/JSON.hs deleted file mode 100644 index 845b288a..00000000 --- a/src/Data/Torrent/JSON.hs +++ /dev/null @@ -1,56 +0,0 @@ -{-# OPTIONS_GHC -fno-warn-orphans #-} -module Data.Torrent.JSON - ( omitLensPrefix - , omitRecordPrefix - ) where - -import Control.Applicative -import Data.Aeson.TH -import Data.Aeson.Types -import Data.ByteString as BS -import Data.ByteString.Base16 as Base16 -import Data.Char -import Data.IP -import Data.List as L -import Data.Text.Encoding as T - - --- | Ignore '_' prefix. -omitLensPrefix :: Options -omitLensPrefix = defaultOptions - { fieldLabelModifier = L.dropWhile (== '_') - , constructorTagModifier = id - , allNullaryToStringTag = True - , omitNothingFields = True - } - -mapWhile :: (a -> Bool) -> (a -> a) -> [a] -> [a] -mapWhile p f = go - where - go [] = [] - go (x : xs) - | p x = f x : go xs - | otherwise = x : xs - -omitRecordPrefix :: Options -omitRecordPrefix = omitLensPrefix - { fieldLabelModifier = mapWhile isUpper toLower . L.dropWhile isLower - } - -instance ToJSON ByteString where - toJSON = String . T.decodeUtf8 . Base16.encode - -instance FromJSON ByteString where - parseJSON v = do - (ok, bad) <- (Base16.decode . T.encodeUtf8) <$> parseJSON v - if BS.null bad - then return ok - else fail "parseJSON: unable to decode ByteString" - -instance ToJSON IP where - toJSON = toJSON . show - -instance FromJSON IP where - parseJSON v = do - str <- parseJSON v - return $ read str diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs index ef8d45eb..cc529840 100644 --- a/src/Data/Torrent/Layout.hs +++ b/src/Data/Torrent/Layout.hs @@ -64,8 +64,6 @@ module Data.Torrent.Layout import Control.Applicative import Control.DeepSeq import Control.Lens -import Data.Aeson.TH -import Data.Aeson.Types (FromJSON, ToJSON) import Data.BEncode import Data.BEncode.Types import Data.ByteString as BS @@ -82,8 +80,6 @@ import Text.PrettyPrint.Class import System.FilePath import System.Posix.Types -import Data.Torrent.JSON - {----------------------------------------------------------------------- -- File attribytes -----------------------------------------------------------------------} @@ -91,8 +87,6 @@ import Data.Torrent.JSON -- | Size of a file in bytes. type FileSize = FileOffset -deriving instance FromJSON FileOffset -deriving instance ToJSON FileOffset deriving instance BEncode FileOffset {----------------------------------------------------------------------- @@ -125,8 +119,6 @@ data FileInfo a = FileInfo { , Functor, Foldable ) -$(deriveJSON omitRecordPrefix ''FileInfo) - makeLensesFor [ ("fiLength", "fileLength") , ("fiMD5Sum", "fileMD5Sum") @@ -210,8 +202,6 @@ data LayoutInfo , liDirName :: !ByteString } deriving (Show, Read, Eq, Typeable) -$(deriveJSON omitRecordPrefix ''LayoutInfo) - makeLensesFor [ ("liFile" , "singleFile" ) , ("liFiles" , "multiFile" ) diff --git a/src/Data/Torrent/Piece.hs b/src/Data/Torrent/Piece.hs index 03335819..d4b2c399 100644 --- a/src/Data/Torrent/Piece.hs +++ b/src/Data/Torrent/Piece.hs @@ -47,8 +47,6 @@ module Data.Torrent.Piece import Control.DeepSeq import Control.Lens import qualified Crypto.Hash.SHA1 as SHA1 -import Data.Aeson (ToJSON(..), FromJSON(..), Value(..), withText) -import Data.Aeson.TH import Data.BEncode import Data.BEncode.Types import Data.Bits @@ -63,8 +61,6 @@ import Data.Typeable import Text.PrettyPrint import Text.PrettyPrint.Class -import Data.Torrent.JSON - -- TODO add torrent file validation class Lint a where @@ -139,8 +135,6 @@ data Piece a = Piece , pieceData :: !a } deriving (Show, Read, Eq, Functor, Typeable) -$(deriveJSON omitRecordPrefix ''Piece) - instance NFData (Piece a) -- | Payload bytes are omitted. @@ -163,14 +157,6 @@ hashPiece Piece {..} = SHA1.hashlazy pieceData newtype HashList = HashList { unHashList :: ByteString } deriving (Show, Read, Eq, BEncode, Typeable) --- | Represented as base64 encoded JSON string. -instance ToJSON HashList where - toJSON (HashList bs) = String $ T.decodeUtf8 $ Base64.encode bs - -instance FromJSON HashList where - parseJSON = withText "HashArray" $ - either fail (return . HashList) . Base64.decode . T.encodeUtf8 - -- | Empty hash list. instance Default HashList where def = HashList "" @@ -184,8 +170,6 @@ data PieceInfo = PieceInfo -- ^ Concatenation of all 20-byte SHA1 hash values. } deriving (Show, Read, Eq, Typeable) -$(deriveJSON omitRecordPrefix ''PieceInfo) - -- | Number of bytes in each piece. makeLensesFor [("piPieceLength", "pieceLength")] ''PieceInfo diff --git a/src/Data/Torrent/Progress.hs b/src/Data/Torrent/Progress.hs index ffcbf2aa..4719020a 100644 --- a/src/Data/Torrent/Progress.hs +++ b/src/Data/Torrent/Progress.hs @@ -36,7 +36,6 @@ module Data.Torrent.Progress import Control.Applicative import Control.Lens hiding ((%=)) -import Data.Aeson.TH import Data.ByteString.Lazy.Builder as BS import Data.ByteString.Lazy.Builder.ASCII as BS import Data.Default @@ -49,8 +48,6 @@ import Network.HTTP.Types.QueryLike import Text.PrettyPrint as PP import Text.PrettyPrint.Class -import Data.Torrent.JSON - -- | Progress data is considered as dynamic within one client -- session. This data also should be shared across client application @@ -64,7 +61,6 @@ data Progress = Progress } deriving (Show, Read, Eq) $(makeLenses ''Progress) -$(deriveJSON omitLensPrefix ''Progress) -- | UDP tracker compatible encoding. instance Serialize Progress where diff --git a/src/Network/BitTorrent/Core/NodeInfo.hs b/src/Network/BitTorrent/Core/NodeInfo.hs index fa20caf5..fe17c097 100644 --- a/src/Network/BitTorrent/Core/NodeInfo.hs +++ b/src/Network/BitTorrent/Core/NodeInfo.hs @@ -39,8 +39,6 @@ module Network.BitTorrent.Core.NodeInfo ) where import Control.Applicative -import Data.Aeson (ToJSON, FromJSON) -import Data.Aeson.TH import Data.Bits import Data.ByteString as BS import Data.ByteString.Char8 as BC @@ -62,7 +60,6 @@ import System.Entropy import Text.PrettyPrint as PP hiding ((<>)) import Text.PrettyPrint.Class -import Data.Torrent.JSON import Network.BitTorrent.Core.PeerAddr (PeerAddr (..)) {----------------------------------------------------------------------- @@ -76,7 +73,7 @@ import Network.BitTorrent.Core.PeerAddr (PeerAddr (..)) -- Normally, /this/ node id should be saved between invocations -- of the client software. newtype NodeId = NodeId ByteString - deriving (Show, Eq, Ord, BEncode, FromJSON, ToJSON, Typeable) + deriving (Show, Eq, Ord, BEncode, Typeable) nodeIdSize :: Int nodeIdSize = 20 @@ -146,8 +143,6 @@ data NodeAddr a = NodeAddr , nodePort :: {-# UNPACK #-} !PortNumber } deriving (Eq, Typeable, Functor) -$(deriveJSON omitRecordPrefix ''NodeAddr) - instance Show a => Show (NodeAddr a) where showsPrec i NodeAddr {..} = showsPrec i nodeHost <> showString ":" <> showsPrec i nodePort @@ -202,8 +197,6 @@ data NodeInfo a = NodeInfo , nodeAddr :: !(NodeAddr a) } deriving (Show, Eq, Functor) -$(deriveJSON omitRecordPrefix ''NodeInfo) - instance Eq a => Ord (NodeInfo a) where compare = comparing nodeId diff --git a/src/Network/BitTorrent/Core/PeerAddr.hs b/src/Network/BitTorrent/Core/PeerAddr.hs index adf64c7f..92fb83a7 100644 --- a/src/Network/BitTorrent/Core/PeerAddr.hs +++ b/src/Network/BitTorrent/Core/PeerAddr.hs @@ -33,8 +33,6 @@ module Network.BitTorrent.Core.PeerAddr import Control.Applicative import Control.Monad -import Data.Aeson (ToJSON, FromJSON) -import Data.Aeson.TH import Data.BEncode as BS import Data.BEncode.BDict (BKey) import Data.ByteString.Char8 as BS8 @@ -58,7 +56,6 @@ import Text.Read (readMaybe) import qualified Text.ParserCombinators.ReadP as RP import Data.Torrent.InfoHash -import Data.Torrent.JSON import Network.BitTorrent.Core.PeerId @@ -66,9 +63,6 @@ import Network.BitTorrent.Core.PeerId -- Port number -----------------------------------------------------------------------} -deriving instance ToJSON PortNumber -deriving instance FromJSON PortNumber - instance BEncode PortNumber where toBEncode = toBEncode . fromEnum fromBEncode = fromBEncode >=> portNumber @@ -208,8 +202,6 @@ data PeerAddr a = PeerAddr , peerPort :: {-# UNPACK #-} !PortNumber } deriving (Show, Eq, Ord, Typeable, Functor) -$(deriveJSON omitRecordPrefix ''PeerAddr) - peer_ip_key, peer_id_key, peer_port_key :: BKey peer_ip_key = "ip" peer_id_key = "peer id" diff --git a/src/Network/BitTorrent/Core/PeerId.hs b/src/Network/BitTorrent/Core/PeerId.hs index f8d69e8b..a180ff30 100644 --- a/src/Network/BitTorrent/Core/PeerId.hs +++ b/src/Network/BitTorrent/Core/PeerId.hs @@ -34,7 +34,6 @@ module Network.BitTorrent.Core.PeerId ) where import Control.Applicative -import Data.Aeson import Data.BEncode as BE import Data.ByteString as BS import Data.ByteString.Internal as BS @@ -62,14 +61,13 @@ import Text.PrettyPrint hiding ((<>)) import Text.PrettyPrint.Class import Text.Read (readMaybe) -import Data.Torrent.JSON () import Network.BitTorrent.Core.Fingerprint -- TODO use unpacked Word160 form (length is known statically) -- | Peer identifier is exactly 20 bytes long bytestring. newtype PeerId = PeerId { getPeerId :: ByteString } - deriving (Show, Eq, Ord, BEncode, ToJSON, FromJSON, Typeable) + deriving (Show, Eq, Ord, BEncode, Typeable) peerIdLen :: Int peerIdLen = 20 diff --git a/src/Network/BitTorrent/Exchange/Block.hs b/src/Network/BitTorrent/Exchange/Block.hs index 6e5b960a..16c124e9 100644 --- a/src/Network/BitTorrent/Exchange/Block.hs +++ b/src/Network/BitTorrent/Exchange/Block.hs @@ -57,7 +57,6 @@ module Network.BitTorrent.Exchange.Block import Prelude hiding (span) import Control.Applicative -import Data.Aeson.TH import Data.ByteString as BS hiding (span) import Data.ByteString.Lazy as BL hiding (span) import Data.ByteString.Lazy.Builder as BS @@ -70,7 +69,6 @@ import Numeric import Text.PrettyPrint as PP hiding ((<>)) import Text.PrettyPrint.Class -import Data.Torrent.JSON import Data.Torrent.Piece {----------------------------------------------------------------------- @@ -114,8 +112,6 @@ data BlockIx = BlockIx { , ixLength :: {-# UNPACK #-} !BlockSize } deriving (Show, Eq, Typeable) -$(deriveJSON omitRecordPrefix ''BlockIx) - -- | First block in torrent. Useful for debugging. instance Default BlockIx where def = BlockIx 0 0 defaultTransferSize diff --git a/src/Network/BitTorrent/Exchange/Connection/Status.hs b/src/Network/BitTorrent/Exchange/Connection/Status.hs index b4b10371..f6abc580 100644 --- a/src/Network/BitTorrent/Exchange/Connection/Status.hs +++ b/src/Network/BitTorrent/Exchange/Connection/Status.hs @@ -34,14 +34,12 @@ module Network.BitTorrent.Exchange.Connection.Status ) where import Control.Lens -import Data.Aeson.TH import Data.Default import Data.Maybe import Data.Monoid import Text.PrettyPrint as PP hiding ((<>)) import Text.PrettyPrint.Class -import Data.Torrent.JSON import Network.BitTorrent.Exchange.Message @@ -61,7 +59,6 @@ data PeerStatus = PeerStatus } deriving (Show, Eq, Ord) $(makeLenses ''PeerStatus) -$(deriveJSON omitLensPrefix ''PeerStatus) instance Pretty PeerStatus where pretty PeerStatus {..} = @@ -104,7 +101,6 @@ data ConnectionStatus = ConnectionStatus } deriving (Show, Eq) $(makeLenses ''ConnectionStatus) -$(deriveJSON omitRecordPrefix ''ConnectionStatus) instance Pretty ConnectionStatus where pretty ConnectionStatus {..} = diff --git a/src/Network/BitTorrent/Internal/Cache.hs b/src/Network/BitTorrent/Internal/Cache.hs index 5c3f379b..8c74467a 100644 --- a/src/Network/BitTorrent/Internal/Cache.hs +++ b/src/Network/BitTorrent/Internal/Cache.hs @@ -32,7 +32,6 @@ module Network.BitTorrent.Internal.Cache ) where import Control.Applicative -import Data.Aeson import Data.Monoid import Data.Default import Data.Time @@ -56,30 +55,6 @@ data Cached a = Cached -- INVARIANT: minUpdateInterval <= updateInterval --- | Examples: --- --- { data: "some string", --- observed: "2014-03-20T19:49:39.505Z", --- expires: "2014-03-20T19:50:10.881Z" } --- --- or: --- --- { expired: "2014-03-20T19:50:10.881Z" } --- -instance ToJSON a => ToJSON (Cached a) where - toJSON Cached {..} - | currentTime < expireTime = object - [ "observed" .= posixSecondsToUTCTime lastUpdated - , "expires" .= posixSecondsToUTCTime expireTime - , "data" .= cachedData - ] - | otherwise = object - [ "expired" .= posixSecondsToUTCTime expireTime - ] - where - expireTime = currentTime + updateInterval - currentTime = unsafePerformIO getPOSIXTime - instance Default (Cached a) where def = mempty diff --git a/src/Network/BitTorrent/Tracker/Message.hs b/src/Network/BitTorrent/Tracker/Message.hs index ffe36c82..cdc07af8 100644 --- a/src/Network/BitTorrent/Tracker/Message.hs +++ b/src/Network/BitTorrent/Tracker/Message.hs @@ -97,7 +97,6 @@ module Network.BitTorrent.Tracker.Message import Control.Applicative import Control.Monad -import Data.Aeson.TH import Data.BEncode as BE hiding (Result) import Data.BEncode.BDict as BE import Data.ByteString as BS @@ -126,7 +125,6 @@ import System.Entropy import Text.Read (readMaybe) import Data.Torrent.InfoHash -import Data.Torrent.JSON import Data.Torrent.Progress import Network.BitTorrent.Core @@ -150,8 +148,6 @@ data AnnounceEvent | Completed deriving (Show, Read, Eq, Ord, Enum, Bounded, Typeable) -$(deriveJSON omitRecordPrefix ''AnnounceEvent) - -- | HTTP tracker protocol compatible encoding. instance QueryValueLike AnnounceEvent where toQueryValue e = toQueryValue (Char.toLower x : xs) @@ -224,8 +220,6 @@ data AnnounceQuery = AnnounceQuery , reqEvent :: Maybe AnnounceEvent } deriving (Show, Eq, Typeable) -$(deriveJSON omitRecordPrefix ''AnnounceQuery) - -- | UDP tracker protocol compatible encoding. instance Serialize AnnounceQuery where put AnnounceQuery {..} = do @@ -636,8 +630,6 @@ data ScrapeEntry = ScrapeEntry { , siName :: !(Maybe Text) } deriving (Show, Eq, Typeable) -$(deriveJSON omitRecordPrefix ''ScrapeEntry) - -- | HTTP tracker protocol compatible encoding. instance BEncode ScrapeEntry where toBEncode ScrapeEntry {..} = toDict $ diff --git a/src/Network/BitTorrent/Tracker/Session.hs b/src/Network/BitTorrent/Tracker/Session.hs index 5aa9c0eb..560acf84 100644 --- a/src/Network/BitTorrent/Tracker/Session.hs +++ b/src/Network/BitTorrent/Tracker/Session.hs @@ -47,8 +47,6 @@ import Control.Exception import Control.Concurrent import Control.Concurrent.Chan.Split as CS import Control.Monad -import Data.Aeson -import Data.Aeson.TH import Data.Default import Data.Fixed import Data.Foldable as F @@ -60,7 +58,6 @@ import Data.Traversable import Network.URI import Data.Torrent.InfoHash -import Data.Torrent.JSON import Network.BitTorrent.Core import Network.BitTorrent.Internal.Cache import Network.BitTorrent.Internal.Types @@ -95,8 +92,6 @@ data LastScrape = LastScrape , scrapeSeeders :: Maybe Int } deriving (Show, Eq) -$(deriveJSON omitRecordPrefix ''LastScrape) - -- | Single tracker session. data TrackerSession = TrackerSession { -- | Used to notify 'Stopped' and 'Completed' events. @@ -109,13 +104,6 @@ data TrackerSession = TrackerSession , trackerScrape :: Cached LastScrape } -instance ToJSON (TierEntry TrackerSession) where - toJSON (uri, TrackerSession {..}) = object - [ "uri" .= uri - , "peers" .= trackerPeers - , "scrape" .= trackerScrape - ] - -- | Not contacted. instance Default TrackerSession where def = TrackerSession Nothing def def -- cgit v1.2.3