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
Makefilefor easy building and testing. - Includes unit tests using the
testifylibrary.
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
ParseJSONmethod first iterates through the input string, identifying and creating tokens (like{,},"name",:,30,true, etc.). - Parsing: After tokenization, the
parsemethod is called. It looks at the current token and dispatches toparseObjectorparseArraybased on the token type ({or[). These functions recursively callparseto handle nested structures and values. - Recursive Descent: The parser descends through the token stream, building up the Go data structure (
map[string]anyfor objects,[]anyfor arrays) as it goes.