Created
October 13, 2015 15:14
-
-
Save desbo/5d0401ca0d160d4e6ee6 to your computer and use it in GitHub Desktop.
puzzle parser
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 #-} | |
import Control.Monad | |
import Data.Aeson | |
import Data.Aeson.Types | |
import qualified Data.ByteString.Lazy as B (readFile) | |
import Data.Time | |
data CrosswordType = Quick | Cryptic | |
deriving Show | |
data Coordinate = Coordinate Int Int | |
deriving Show | |
data Dimensions = Dimensions Int Int | |
deriving Show | |
data CAPIResponse = CAPIResponse Crossword | |
deriving Show | |
data Entry = Entry { | |
number :: Int | |
, direction :: String | |
, position :: Coordinate | |
, length :: Int | |
, clue :: String | |
, solution :: String | |
} deriving Show | |
data Crossword = Crossword { | |
crosswordType :: CrosswordType | |
, id :: Int | |
, name :: String | |
, pdfUrl :: String | |
, date :: Day | |
, dimensions :: Dimensions | |
, entries :: [Entry] | |
} deriving Show | |
toType :: String -> CrosswordType | |
toType "quick" = Quick | |
toType "cryptic" = Cryptic | |
toDay :: String -> Day | |
toDay s = readTime defaultTimeLocale "%Y-%m-%d" s | |
instance FromJSON Entry where | |
parseJSON (Object o) = Entry <$> | |
o .: "number" <*> | |
o .: "direction" <*> | |
o .: "position" <*> | |
o .: "length" <*> | |
o .: "clue" <*> | |
o .: "solution" | |
instance FromJSON Dimensions where | |
parseJSON (Object o) = Dimensions <$> o .: "cols" <*> o .: "rows" | |
instance FromJSON Coordinate where | |
parseJSON (Object o) = Coordinate <$> o .: "x" <*> o .: "y" | |
instance FromJSON Crossword where | |
parseJSON (Object o) = Crossword <$> | |
liftM toType (o .: "type") <*> | |
o .: "number" <*> | |
o .: "name" <*> | |
o .: "pdf" <*> | |
liftM toDay (o .: "date") <*> | |
o .: "dimensions" <*> | |
o .: "entries" | |
main = do | |
json <- B.readFile "14174.json" | |
let crossword = decode json :: Maybe Crossword | |
print crossword |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment