Created
October 30, 2019 17:45
-
-
Save shapr/4318370dbbf059ec4129f9afa5736094 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| {-# LANGUAGE OverloadedStrings #-} | |
| {-# LANGUAGE ScopedTypeVariables #-} | |
| module Main where | |
| import Data.Aeson | |
| import qualified Data.ByteString.Lazy as BSL | |
| import qualified Data.Text as T | |
| main :: IO () | |
| main = do | |
| contents <- BSL.readFile "works.nofilter.json" | |
| let (t :: Maybe Test) = decode contents | |
| -- let something = parse json contents | |
| print t | |
| putStrLn "Hello, Haskell! Let's see if this works!" | |
| data Test = Test { status :: T.Text | |
| , message :: Message | |
| -- , nope :: Maybe T.Text | |
| } deriving (Show, Eq, Ord) | |
| instance FromJSON Test where | |
| -- parseJSON (Object v) = parseField v "status" | |
| parseJSON (Object v) = Test <$> v .: "status" <*> v .: "message" | |
| parseJSON _ = error "we didn't do this yet!" | |
| data Message = Message { | |
| total_result :: Integer | |
| , items :: [SResult] | |
| } deriving (Show, Ord, Eq) | |
| instance FromJSON Message where | |
| parseJSON (Object v) = Message <$> v .: "total-results" <*> v .: "items" | |
| parseJSON _ = error "no" | |
| data SResult = SResult { | |
| doi :: T.Text | |
| , title :: [T.Text] | |
| , authors :: [Author] | |
| -- , pubDate :: [Int] | |
| } deriving (Show, Eq, Ord) | |
| instance FromJSON SResult where | |
| parseJSON (Object v) = SResult | |
| <$> v .: "DOI" | |
| <*> v .: "title" | |
| <*> v .: "author" | |
| -- <*> v .: "published-print" -- .: "date-parts" | |
| parseJSON _ = error "bad SResult" | |
| data Author = Author { | |
| given :: T.Text | |
| -- , family :: T.Text | |
| } deriving (Show, Eq, Ord) | |
| instance FromJSON Author where | |
| parseJSON (Object v) = Author | |
| <$> v .: "given" | |
| -- <*> v .: "family" | |
| parseJSON _ = error "bad author" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment