https://github.com/letsmakecakes/json-parser
A lightweight JSON parser built in Go, designed to tokenize and parse JSON.
https://github.com/letsmakecakes/json-parser
cli go golang json lexer lexer-parser parser tokenization
Last synced: about 13 hours ago
JSON representation
A lightweight JSON parser built in Go, designed to tokenize and parse JSON.
- Host: GitHub
- URL: https://github.com/letsmakecakes/json-parser
- Owner: letsmakecakes
- License: mit
- Created: 2024-10-08T02:17:57.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-12-30T08:00:30.000Z (over 1 year ago)
- Last Synced: 2025-03-03T06:48:38.667Z (about 1 year ago)
- Topics: cli, go, golang, json, lexer, lexer-parser, parser, tokenization
- Language: Go
- Homepage:
- Size: 68.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Go JSON Parser
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.
## Features
- π 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
## Requirements
- Go 1.19 or higher
- Make (optional, for using the Makefile)
## Installation
1. Clone the repository:
```bash
git clone https://github.com/letsmakecakes/json-parser.git
cd json-parser
```
2. Build the project:
```bash
make build
```
Or using Go directly:
```bash
go build -o build/jsonparser ./cmd/parser
```
## Usage
### Basic Usage
Parse a JSON file:
```bash
./build/jsonparser input.json
```
Parse from standard input:
```bash
echo '{"key": "value"}' | ./build/jsonparser -
```
### Command Line Options
```bash
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
```
### Examples
1. Parse with verbose output:
```bash
./build/jsonparser -verbose input.json
```
2. Parse with benchmarking:
```bash
./build/jsonparser -benchmark input.json
```
3. Parse in strict mode:
```bash
./build/jsonparser -strict input.json
```
## Project Structure
```
.
βββ 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
```
## Development
### Running Tests
Run all tests:
```bash
make test
```
Run specific test steps:
```bash
make test-step1
make test-step2
make test-step3
make test-step4
```
### Development Commands
Format code:
```bash
make fmt
```
Run linter:
```bash
make lint
```
Clean build artifacts:
```bash
make clean
```
## Error Handling
The parser provides detailed error messages with line and column information:
```
Error:
{"key": "value",}
^
unexpected comma at line 1, column 16
```
## Implementation Steps
1. **Step 1**: Basic JSON object parsing (`{}`)
2. **Step 2**: String key-value pairs
3. **Step 3**: Multiple data types (strings, numbers, booleans, null)
4. **Step 4**: Nested objects and arrays
5. **Step 5**: Comprehensive validation and error handling
## Performance
The parser includes optional benchmarking capabilities. When run with the `-benchmark` flag, it provides:
- Total parsing time
- Processing speed (MB/s)
## Contributing
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Commit your changes (`git commit -m 'Add amazing feature'`)
4. Push to the branch (`git push origin feature/amazing-feature`)
5. Open a Pull Request
## License
This project is licensed under the MIT Licenseβsee the [LICENSE](LICENSE) file for details.
## Acknowledgments
- Inspired by the [JSON specification](https://tools.ietf.org/html/std90)
- Built the following best practices from the "Dragon Book" (Compilers: Principles, Techniques, and Tools)
## Author
Adwaith Rajeev ([@letsmakecakes](https://github.com/letsmakecakes))
## Support
If you have any questions or run into issues, please open an issue in the GitHub repository.