Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {-# LANGUAGE OverloadedStrings #-}
- {-# LANGUAGE TemplateHaskell #-}
- {-# LANGUAGE NoMonomorphismRestriction #-}
- import Data.Aeson
- import Data.Aeson.TH
- import qualified Data.ByteString.Lazy.Char8 as B
- import Data.Attoparsec.ByteString.Lazy
- import Network.HTTP
- import Control.Applicative
- import Control.Monad
- data Post = Post {
- capcode :: Maybe String,
- closed :: Maybe Bool,
- com :: String,
- country :: Maybe String,
- country_name :: Maybe String,
- email :: String,
- ext :: Maybe String,
- filedeleted :: Maybe Bool,
- filename :: Maybe String,
- fsize :: Maybe Int,
- h :: Maybe Int,
- id :: Maybe String,
- md5 :: Maybe String,
- name :: String,
- no :: Integer,
- now :: String,
- resto :: Integer,
- spoiler :: Maybe Bool,
- sticky :: Maybe Bool,
- sub :: String,
- tim :: Integer,
- time :: Integer,
- tn_h :: Maybe Int,
- tn_w :: Maybe Int,
- trip :: String,
- w :: Maybe Int
- } deriving (Show, Eq)
- data Posts = Posts {
- posts :: [Post]
- } deriving (Show, Eq)
- $(deriveToJSON Prelude.id ''Post)
- $(deriveJSON Prelude.id ''Posts)
- --toBool' :: Parser (Maybe a) -> Parser (Maybe Bool)
- --toBool' = liftM toBool
- toBool' m = do
- v <- m
- return $ toBool v
- --toBool :: Eq a => Num a => Maybe a -> Maybe Bool
- toBool = (((0) /=) <$>)
- instance FromJSON Post where
- parseJSON (Object v) = Post <$>
- v .:? "capcode" <*>
- (toBool' $ v .:? "closed") <*>
- v .: "com" <*>
- v .:? "country" <*>
- v .:? "country_name" <*>
- v .: "email" <*>
- v .:? "ext" <*>
- (toBool' $ v .:? "filedeleted") <*>
- v .:? "filename" <*>
- v .:? "fsize" <*>
- v .:? "h" <*>
- v .:? "id" <*>
- v .:? "md5" <*>
- v .: "name" <*>
- v .: "no" <*>
- v .: "now" <*>
- v .: "resto" <*>
- (toBool' $ v .:? "spoiler") <*>
- (toBool' $ v .:? "sticky") <*>
- v .: "sub" <*>
- v .: "tim" <*>
- v .: "time" <*>
- v .:? "tn_h" <*>
- v .:? "tn_w" <*>
- v .: "trip" <*>
- v .:? "w"
- parseJSON _ = fail "not an object"
Add Comment
Please, Sign In to add comment