From 62b2a7e234443ed1cba0bf6232c5a8f64cab9f1c Mon Sep 17 00:00:00 2001 From: mikeantonacci Date: Sat, 9 May 2020 19:32:03 -0500 Subject: [PATCH 1/2] Use Map instead of a list of tuple for JsonObject --- Main.hs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/Main.hs b/Main.hs index 06e6176..a510f07 100644 --- a/Main.hs +++ b/Main.hs @@ -7,6 +7,7 @@ import Control.Applicative import Data.Char import Numeric import System.Exit +import Data.Map.Strict hiding (empty) data Input = Input { inputLoc :: Int @@ -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) @@ -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 @@ -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 @@ -267,7 +268,7 @@ main = do , "}" ] expectedJsonAst = - JsonObject + JsonObject $ fromList [ ( "hello" , JsonArray [ JsonBool False From 7cc10acec0e07d3587f32e7a284f3555d20cf744 Mon Sep 17 00:00:00 2001 From: mikeantonacci Date: Sat, 9 May 2020 19:52:18 -0500 Subject: [PATCH 2/2] stylish-haskell formatting --- Main.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Main.hs b/Main.hs index a510f07..7cccdff 100644 --- a/Main.hs +++ b/Main.hs @@ -5,9 +5,9 @@ module Main where import Control.Applicative import Data.Char +import Data.Map.Strict hiding (empty) import Numeric import System.Exit -import Data.Map.Strict hiding (empty) data Input = Input { inputLoc :: Int