A JSON parser
Stage 2 of 7v3 · 25ab388e

The three literal values

Recognize null, true and false without accepting prefixes or trailing junk.

The project contract

Your program must read one JSON document from standard input. If it is valid, write that same value to standard output serialized as compact JSON and exit with code 0. If it is not valid, write nothing to stdout, explain the error on stderr and exit with a code other than 0.

The tester runs, from the project root, the commands you declare in shipcode.yml:

challenge: json-parser
build: your-build-command # optional
run: your-run-command

Do not use your standard library's JSON parser, nor an equivalent dependency. You may use its data types and a serializer at the end: what you are building is the lexical and syntactic analysis.

Your goal

Build the cursor that will walk the whole input and recognize the literals null, true and false. This stage looks small on purpose: here you decide how to represent position, end of input and errors, before numbers or recursive structures are in the mix.

Mental model

A parser does not ask whether the input contains a word. It consumes characters from a position and only succeeds if it recognizes the complete token. After the value there may be whitespace, but no other character.

Keep two responsibilities apart: skipping JSON whitespace (only space, tab, carriage return and line feed) and consuming an exact token. That boundary will save you from bugs once arrays and objects arrive.

New words in this stage

  • Literal: a value written exactly as the language defines it. JSON literals are null, true, and false.
  • Cursor: the number identifying the character you are reading.
  • Consume: verify characters and move the cursor past them.
  • Token: a recognizable unit of text, such as true or [.

Build one piece at a time

  1. Store the input and a cursor starting at 0.
  2. Add an operation that looks at the current character without advancing and handles end-of-input safely.
  3. Add an operation that skips only JSON's four whitespace characters.
  4. Recognize one literal first, then repeat the pattern for the other two.
  5. Skip trailing whitespace and require the cursor to be at the end.
  6. Send errors to stderr and return a non-zero exit code.

Trace the cursor on paper for true and true!. The second input must stop before ! and report trailing content.

Common mistakes

Do not search for a word anywhere in the input, accept uppercase spellings, move the cursor before a full match, treat every invisible character as whitespace, or print diagnostics to stdout.

Before the tests

Try null, true, FALSE, nul, true!, and empty input. Every invalid case needs a non-zero exit code, empty stdout, and an error message on stderr.

Acceptance criteria

  • The three literals are accepted and written in their canonical form.
  • The whitespace allowed before and after the document is ignored.
  • Uppercase, incomplete literals and leftover content fail.
  • A failure exits with a code other than 0 and does not pollute stdout.

Before you run it

Try inputs like true, nul and false! by hand. Once the behavior is stable, run shipcode test --stage 01-valores.