Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use Map instead of a list of tuple for JsonObject #19

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Main where

import Control.Applicative
import Data.Char
import Data.Map.Strict hiding (empty)
import Numeric
import System.Exit

Expand All @@ -25,7 +26,7 @@ data JsonValue
| JsonNumber Double
| JsonString String
| JsonArray [JsonValue]
| JsonObject [(String, JsonValue)]
| JsonObject (Map String JsonValue)
deriving (Show, Eq)

data ParserError = ParserError Int String deriving (Show)
Expand Down Expand Up @@ -205,7 +206,7 @@ jsonArray = JsonArray <$> (charP '[' *> ws *> elements <* ws <* charP ']')
-- | Parser for json objects
jsonObject :: Parser JsonValue
jsonObject =
JsonObject <$>
JsonObject . fromList <$>
(charP '{' *> ws *> sepBy (ws *> charP ',' <* ws) pair <* ws <* charP '}')
where
pair = liftA2 (,) (stringLiteral <* ws <* charP ':' <* ws) jsonValue
Expand Down Expand Up @@ -246,7 +247,7 @@ main = do
Right (input, actualJsonAst) -> do
putStrLn ("[INFO] Parsed as: " ++ show actualJsonAst)
putStrLn
("[INFO] Remaining input (codes): " ++ show (map ord $ inputStr input))
("[INFO] Remaining input (codes): " ++ show (ord <$> inputStr input))
if actualJsonAst == expectedJsonAst
then putStrLn "[SUCCESS] Parser produced expected result."
else do
Expand All @@ -267,7 +268,7 @@ main = do
, "}"
]
expectedJsonAst =
JsonObject
JsonObject $ fromList
[ ( "hello"
, JsonArray
[ JsonBool False
Expand Down