This project implements a basic JSON parser in Go using a recursive descent strategy.
It tokenizes a JSON string and then parses the tokens into a nested Go map[string]any
or []any
structure.
- Parses basic JSON structures: objects (
{}
), arrays ([]
) - Parses JSON value types: strings (
"..."
), numbers, booleans (true
,false
),null
- Uses recursive descent for parsing logic.
- Includes basic logging for tracing the tokenization and parsing process.
- Provides a
Makefile
for easy building and testing. - Includes unit tests using the
testify
library.
Make sure you have Go installed (version 1.18+ recommended).
-
Get dependencies:
go mod tidy
-
Run the parser with the example JSON:
make run # or go run main.go
-
Run tests:
make test
-
Run tests with coverage:
make test-coverage
This will generate an HTML coverage report (
coverage.out
) and open it in your browser.
- Tokenization: The
ParseJSON
method first iterates through the input string, identifying and creating tokens (like{
,}
,"name"
,:
,30
,true
, etc.). - Parsing: After tokenization, the
parse
method is called. It looks at the current token and dispatches toparseObject
orparseArray
based on the token type ({
or[
). These functions recursively callparse
to handle nested structures and values. - Recursive Descent: The parser descends through the token stream, building up the Go data structure (
map[string]any
for objects,[]any
for arrays) as it goes.