From e03b2165b7e88c70751780ec35b79bdb5e3a2adb Mon Sep 17 00:00:00 2001 From: Sam Truzjan Date: Thu, 31 Oct 2013 17:32:33 +0400 Subject: Add instance JSON Torrent --- src/Data/Torrent.hs | 32 +++++++++++++++++++++----------- 1 file changed, 21 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/Data/Torrent.hs b/src/Data/Torrent.hs index 15ada35f..90ec0cc4 100644 --- a/src/Data/Torrent.hs +++ b/src/Data/Torrent.hs @@ -52,13 +52,6 @@ module Data.Torrent , isTorrentPath , fromFile , toFile - -{- - , nullTorrent - , mktorrent - - --} ) where import Prelude hiding (sum) @@ -68,17 +61,19 @@ 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 import qualified Data.ByteString.Char8 as BC (pack, unpack) import qualified Data.ByteString.Lazy as BL -import Data.Char +import Data.Char as Char import Data.Hashable as Hashable import qualified Data.List as L -import Data.Text (Text) -import Data.Time.Clock.POSIX +import Data.Text as T +import Data.Time +import Data.Time.Clock.POSIX import Data.Typeable import Network.URI import System.FilePath @@ -110,7 +105,7 @@ data InfoDict = InfoDict -- BEP 27: } deriving (Show, Read, Eq, Typeable) -$(deriveJSON (L.map toLower . L.dropWhile isLower) ''InfoDict) +$(deriveJSON (L.map Char.toLower . L.dropWhile isLower) ''InfoDict) makeLensesFor [ ("idInfoHash" , "infohash" ) @@ -189,6 +184,21 @@ 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 + +$(deriveToJSON (L.map Char.toLower . L.dropWhile isLower) ''Torrent) + makeLensesFor [ ("tAnnounce" , "announce" ) , ("tAnnounceList", "announceList") -- cgit v1.2.3