Guest User

Untitled

a guest
Nov 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. {-# LANGUAGE OverloadedStrings #-}
  2. {-# LANGUAGE TemplateHaskell #-}
  3. {-# LANGUAGE NoMonomorphismRestriction #-}
  4.  
  5. import Data.Aeson
  6. import Data.Aeson.TH
  7. import qualified Data.ByteString.Lazy.Char8 as B
  8. import Data.Attoparsec.ByteString.Lazy
  9. import Network.HTTP
  10. import Control.Applicative
  11. import Control.Monad
  12.  
  13. data Post = Post {
  14. capcode :: Maybe String,
  15. closed :: Maybe Bool,
  16. com :: String,
  17. country :: Maybe String,
  18. country_name :: Maybe String,
  19. email :: String,
  20. ext :: Maybe String,
  21. filedeleted :: Maybe Bool,
  22. filename :: Maybe String,
  23. fsize :: Maybe Int,
  24. h :: Maybe Int,
  25. id :: Maybe String,
  26. md5 :: Maybe String,
  27. name :: String,
  28. no :: Integer,
  29. now :: String,
  30. resto :: Integer,
  31. spoiler :: Maybe Bool,
  32. sticky :: Maybe Bool,
  33. sub :: String,
  34. tim :: Integer,
  35. time :: Integer,
  36. tn_h :: Maybe Int,
  37. tn_w :: Maybe Int,
  38. trip :: String,
  39. w :: Maybe Int
  40. } deriving (Show, Eq)
  41.  
  42. data Posts = Posts {
  43. posts :: [Post]
  44. } deriving (Show, Eq)
  45.  
  46. $(deriveToJSON Prelude.id ''Post)
  47. $(deriveJSON Prelude.id ''Posts)
  48.  
  49. --toBool' :: Parser (Maybe a) -> Parser (Maybe Bool)
  50. --toBool' = liftM toBool
  51. toBool' m = do
  52. v <- m
  53. return $ toBool v
  54.  
  55. --toBool :: Eq a => Num a => Maybe a -> Maybe Bool
  56. toBool = (((0) /=) <$>)
  57.  
  58. instance FromJSON Post where
  59. parseJSON (Object v) = Post <$>
  60. v .:? "capcode" <*>
  61. (toBool' $ v .:? "closed") <*>
  62. v .: "com" <*>
  63. v .:? "country" <*>
  64. v .:? "country_name" <*>
  65. v .: "email" <*>
  66. v .:? "ext" <*>
  67. (toBool' $ v .:? "filedeleted") <*>
  68. v .:? "filename" <*>
  69. v .:? "fsize" <*>
  70. v .:? "h" <*>
  71. v .:? "id" <*>
  72. v .:? "md5" <*>
  73. v .: "name" <*>
  74. v .: "no" <*>
  75. v .: "now" <*>
  76. v .: "resto" <*>
  77. (toBool' $ v .:? "spoiler") <*>
  78. (toBool' $ v .:? "sticky") <*>
  79. v .: "sub" <*>
  80. v .: "tim" <*>
  81. v .: "time" <*>
  82. v .:? "tn_h" <*>
  83. v .:? "tn_w" <*>
  84. v .: "trip" <*>
  85. v .:? "w"
  86. parseJSON _ = fail "not an object"
Add Comment
Please, Sign In to add comment