A high-performance JSON parser implementation in Go, built from scratch. This project follows a step-by-step approach to building a fully-featured JSON parser with comprehensive test coverage and validation capabilities.
- 🚀 High-performance lexical and syntactic analysis
- 🔍 Detailed error reporting with line and column information
- ✅ Full JSON specification support
- 📊 Optional performance benchmarking
- 🔒 Strict mode validation
- 💻 Command-line interface
- 📝 Comprehensive test suite
- Go 1.19 or higher
- Make (optional, for using the Makefile)
- Clone the repository:
git clone https://github.com/letsmakecakes/json-parser.git
cd json-parser
- Build the project:
make build
Or using Go directly:
go build -o build/jsonparser ./cmd/parser
Parse a JSON file:
./build/jsonparser input.json
Parse from standard input:
echo '{"key": "value"}' | ./build/jsonparser -
Usage: jsonparser [options] [file]
Options:
-file string
JSON file to parse (use '-' for stdin)
-verbose
Enable verbose output
-benchmark
Show parsing time
-strict
Enable strict mode validation
- Parse with verbose output:
./build/jsonparser -verbose input.json
- Parse with benchmarking:
./build/jsonparser -benchmark input.json
- Parse in strict mode:
./build/jsonparser -strict input.json
.
├── cmd
│ └── parser
│ └── main.go # Main entry point
├── internal
│ ├── lexer # Lexical analysis
│ │ ├── lexer.go
│ │ ├── token.go
│ │ └── lexer_test.go
│ ├── parser # Syntactic analysis
│ │ ├── parser.go
│ │ ├── ast.go
│ │ └── parser_test.go
│ └── validator # JSON validation
│ ├── validator.go
│ └── validator_test.go
├── pkg
│ └── errors # Error definitions
│ └── errors.go
├── test # Test files
│ ├── step1
│ ├── step2
│ ├── step3
│ ├── step4
│ └── step5
├── go.mod
├── go.sum
├── Makefile
└── README.md
Run all tests:
make test
Run specific test steps:
make test-step1
make test-step2
make test-step3
make test-step4
Format code:
make fmt
Run linter:
make lint
Clean build artifacts:
make clean
The parser provides detailed error messages with line and column information:
Error:
{"key": "value",}
^
unexpected comma at line 1, column 16
- Step 1: Basic JSON object parsing (
{}
) - Step 2: String key-value pairs
- Step 3: Multiple data types (strings, numbers, booleans, null)
- Step 4: Nested objects and arrays
- Step 5: Comprehensive validation and error handling
The parser includes optional benchmarking capabilities. When run with the -benchmark
flag, it provides:
- Total parsing time
- Processing speed (MB/s)
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License—see the LICENSE file for details.
- Inspired by the JSON specification
- Built the following best practices from the "Dragon Book" (Compilers: Principles, Techniques, and Tools)
Adwaith Rajeev (@letsmakecakes)
If you have any questions or run into issues, please open an issue in the GitHub repository.