Skip to content

Instantly share code, notes, and snippets.

@glinesbdev
Last active July 2, 2018 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glinesbdev/8bb4c2756340c8315b2db4db8d398b05 to your computer and use it in GitHub Desktop.
Save glinesbdev/8bb4c2756340c8315b2db4db8d398b05 to your computer and use it in GitHub Desktop.
Haskell - Read JSON and parse data
{-# LANGUAGE
OverloadedStrings
, DeriveGeneric
#-}
module Lib where
import Prelude as P
import Data.Aeson
import Data.Text
import Data.Text.Read
import Data.Maybe
import Text.Read
import GHC.Generics
import qualified Data.ByteString.Lazy as L
type BranchId = Int
data Chapter = Chapter
{ branchId :: BranchId
, text :: String
, choices :: [Choice]
} deriving (Show, Generic)
data Choice = Choice
{ choice :: String
, branch :: BranchId
} deriving (Show, Generic)
instance FromJSON Chapter
instance FromJSON Choice
jsonFile :: FilePath
jsonFile = "data/story.json"
getJSON :: IO L.ByteString
getJSON = L.readFile jsonFile
chapterFromId :: BranchId -> [Chapter] -> Chapter
chapterFromId bId cs = P.head $ P.filter (\c -> branchId c == bId) cs
textFromChapter :: Chapter -> String
textFromChapter c = text c
choicesFromChapter :: Chapter -> [Choice]
choicesFromChapter c = choices c
main :: BranchId -> IO ()
main bId = do
d <- (eitherDecode <$> getJSON) :: IO (Either String [Chapter])
case d of
Left err -> putStrLn $ "error: " ++ err
Right cs -> do
print $ textFromChapter $ chapterFromId bId $ cs
putStrLn $ "Choose a branch: "
print $ choicesFromChapter $ chapterFromId bId $ cs
c <- getLine
case (readMaybe c :: Maybe Int) of
Just int -> print $ chapterFromId int $ cs
Nothing -> putStrLn $ "Please enter a number"
[
{
"branchId": 1,
"text": "You feel a warm, tingling spot on your head. As you touch it, you realize your hand is covered in blood... your blood. The sound around you is muffled; almost as if you had cotton stuffed deep into both of your ears. Your vision is blurry. You feel like you need to put on some glasses but you can't ever remember wearing glasses.",
"choices": [
{
"choice": "You look around for some glasses to clear your vision.",
"branch": 2
},
{
"choice": "You look for a cloth to bandage your head wound.",
"branch": 3
},
{
"choice": "You try an pop your ear drums to relieve the pressure inside your head.",
"branch": 4
}
]
},
{
"branchId": 2,
"text": "As you search around in what seems like darkness for some glasses, you come across some glasses nearby. You can't believe there is a pair lying right next to you! As you put on the glasses, you notice that your vision isn't getting any better. Meanwhile, you can feel your head continuously pounding as well as the blood coming from your head.",
"choices": [
{
"choice": "Look around for a cloth to bandage your head wound.",
"branch": 3
},
{
"choice": "You try an pop your ear drums to relieve the pressure inside your head.",
"branch": 4
}
]
},
{
"branchId": 3,
"text": "You can't see two inches in front of your face but you need to find a way to stop the bleeding. As you wildly throw your hands in every conceivable direction, you finally land on what seems could be a bandage. You immediatly wrap it tight around the wound in your head and the pressure starts to feel more and more relieved. Now that you have taken care of your head wound, you realize that you still cannot see...",
"choices": [
{
"choice": "Look around for some glasses to help your vision.",
"branch": 2
},
{
"choice": "Try to clear your head by popping your ear drums.",
"branch": 4
}
]
},
{
"branchId": 4,
"text": "As you put clasp your fingers around your nostrils and blow to relieve the pressure, the pain is immense; you can feel more and more blood coming from your head. However, you notice something. Almost as if it's screaming. You cannot tell who it is or where they are. Maybe you should follow the sound of the screaming?",
"choices": [
{
"choice": "Follow the sounds of the screaming in the distance.",
"branch": 5
},
{
"choice": "Look around for a bandage of some kind for that head wound.",
"branch": 3
},
{
"choice": "Try to find some glasses for that annoying, blurry vision.",
"branch": 2
}
]
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment