ProjectsJSON Parser
A program that reads a file and says whether it is valid JSON, exiting with 0 if it is and 1 if it isn't, with a message that helps fix the problem. Building it step by step teaches the two stages every parser and compiler has, breaking text into tokens and then checking them against a grammar, and how a precise specification settles every edge case.
From Coding Challenges #2 ↗ by John Crickett
No sign-up. You tell the mentor your language: Java, Python or Node.js.
What you'll learn · 6 concepts
In the order the steps use them. Each one links to the best places to learn it, and you explain it in your own words once its steps are built.
"Valid" means "matches the JSON grammar". Each step adds more of its rules, and step 4 brings in nesting.
Covers: the grammar as the definition of valid · lexing: characters into tokens · recursive descent · nesting depth and the call stack
RFC 8259: the JSON specification ↗ · Crafting Interpreters: Scanning ↗ · Crafting Interpreters: Parsing Expressions ↗
Step 2 adds string keys and values, and the test suite checks escapes and illegal control characters.
Covers: required escapes and control characters · backslash-u escapes and surrogate pairs
RFC 8259, section 7: Strings ↗ · Introducing JSON (json.org) ↗
Step 3 adds numeric values, and the test suite includes leading zeros and other malformed numbers.
Covers: the number grammar · floating-point precision
Step 5 asks for useful error messages on invalid input.
Covers: tracking line and column · expected versus found
The challenge defines valid and invalid by exit code, starting in step 1.
Each step comes with its own folder of test files, and the last step runs a public suite.
Covers: pass and fail cases · suites older than the specification
JSON_checker (json.org) ↗ · Parsing JSON is a Minefield (Nicolas Seriot) ↗
What you'll build · 6 steps
Steps of 30 to 60 minutes, in your own editor and language. Each one has a check, so you know when it works.
Create the project and download the challenge's test data, a folder of valid and invalid files for each step.
Works whenYou can run your program on a file and see its exit code.
Concepts: conformance test suites
Accept the simplest valid input, an empty object, and reject an invalid file. Print a message and exit with 0 for valid and 1 for invalid. Start with a very simple lexer and parser, and automate running the tests.
Works whenEvery file in the step 1 test folder gets the right exit code.
Concepts: grammar and parsing · exit codes as a program's answer
Accept an object whose keys and values are strings.
Works whenEvery file in the step 2 test folder gets the right exit code.
Concepts: strings · grammar and parsing
Accept objects whose values are strings, numbers, true, false or null.
Works whenEvery file in the step 3 test folder gets the right exit code.
Concepts: numbers · grammar and parsing
Accept objects whose values are themselves objects or arrays, nested to any reasonable depth.
Works whenEvery file in the step 4 test folder gets the right exit code.
Concepts: grammar and parsing
Add your own valid and invalid cases, make error messages say where and why the input is invalid, then run the public JSON_checker suite.
Works whenYour parser accepts the suite's pass files and rejects its fail files with useful messages. Any case where it disagrees with the suite, you can explain using the current specification.
Concepts: error reporting · conformance test suites · grammar and parsing