diff --git a/src/Parser/JSON.md b/src/Parser/JSON.md index 7e83fd7..1f1ccde 100644 --- a/src/Parser/JSON.md +++ b/src/Parser/JSON.md @@ -121,8 +121,7 @@ Top level json value parser export value : Parser (t : JSONType ** JSONValue t) value = do - _ <- many whitespace - val <- oneOfE + surround whitespace $ oneOfE (throwParseError "Expected JSON Value") (the (List _) [ @@ -133,8 +132,6 @@ value = do , dpairize bool , dpairize null ]) - _ <- many whitespace - pure val ``` Now go through our json value types @@ -147,29 +144,27 @@ object = do where emptyObject : Parser (JSONValue TObject) emptyObject = do - _ <- parseExactChar '{' - _ <- many whitespace - _ <- parseExactChar '}' + delimited '{' '}' (nom whitespace) pure $ VObject Nil - firstKeyValue : Parser (t : JSONType ** (String, JSONValue t)) - firstKeyValue = do - _ <- many whitespace - VString key <- string - _ <- many whitespace + keyValue : Parser (t : JSONType ** (String, JSONValue t)) + keyValue = do + VString key <- surround whitespace string _ <- parseExactChar ':' (typ ** val) <- value pure (typ ** (key, val)) restKeyValue : Parser (t : JSONType ** (String, JSONValue t)) restKeyValue = do _ <- parseExactChar ',' - firstKeyValue + keyValue + pairs : Parser (List1 (t : JSONType ** (String, JSONValue t))) + pairs = do + first <- keyValue + rest <- many restKeyValue + pure $ first ::: rest occupiedObject : Parser (JSONValue TObject) occupiedObject = do - _ <- parseExactChar '{' - first <- firstKeyValue - rest <- many restKeyValue - _ <- parseExactChar '}' - let (types ** xs) = DList.fromList (first :: rest) + val <- delimited '{' '}' pairs + let (types ** xs) = DList.fromList (forget val) pure $ VObject xs ``` @@ -237,3 +232,23 @@ null = do _ <- exactString "null" pure VNull ``` + +## Unit tests + +Quick smoke test + +```idris +-- @@test JSON Quick Smoke +quickSmoke : IO Bool +quickSmoke = do + let input = "{\"string\":\"string\",\"number\":5,\"true\":true,\"false\":false,\"null\":null,\"array\":[1,2,3]}" + putStrLn input + Right parsed <- runFirstIO object input + | Left err => do + printLn err + pure False + putStrLn "Input: \{input}\nOutput: \{show parsed}" + case parsed of + VObject xs => ?quickSmoke_rhs_0 + _ => pure False +```