ThinkAloud
Projects
ThinkAloud
  1. 01Projects21 ready to build and explain
Browse projects
Theme

Projects from Coding Challenges by John Crickett

Projects/JSON Parser

Build your own JSON 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

  • Challenge #2
  • Intermediate
  • 6 concepts
  • 6 steps
  • Not yet checked
Start JSON Parser

No sign-up. You tell the mentor your language: Java, Python or Node.js.

What you'll learn · 6 concepts

The concepts behind JSON Parser

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.

  1. 1Grammar and parsingStep 2, 3, 4, 5, 6

    "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 ↗

  2. 2Strings: escapes and UnicodeStep 3

    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) ↗

  3. 3Numbers: grammar and precisionStep 4

    Step 3 adds numeric values, and the test suite includes leading zeros and other malformed numbers.

    Covers: the number grammar · floating-point precision

    RFC 8259, section 6: Numbers ↗ · The Floating-Point Guide ↗

  4. 4Error reporting: where and why input is invalidStep 6

    Step 5 asks for useful error messages on invalid input.

    Covers: tracking line and column · expected versus found

    Crafting Interpreters: Parsing Expressions, Syntax Errors ↗

  5. 5Exit codes as a program's answerStep 2

    The challenge defines valid and invalid by exit code, starting in step 1.

    Bash manual: Exit Status ↗ · exit(3), Linux manual page ↗

  6. 6Conformance test suitesStep 1, 6

    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

Build JSON Parser step by step

Steps of 30 to 60 minutes, in your own editor and language. Each one has a check, so you know when it works.

  1. 01

    Set up

    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

  2. 02

    An empty object

    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

  3. 03

    String keys and values

    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

  4. 04

    Numbers, booleans and null

    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

  5. 05

    Nested objects and arrays

    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

  6. 06

    Your own tests and the full suite

    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

Ready to start JSON Parser?

Start JSON Parser

From Coding Challenges #2 ↗ by John Crickett. Notes written in our own words.

Projects from Coding Challenges by John Crickett