jsonparser

module
v0.0.0-...-993d2ad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 30, 2024 License: MIT

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:
git clone https://github.com/letsmakecakes/json-parser.git
cd json-parser
  1. Build the project:
make build

Or using Go directly:

go build -o build/jsonparser ./cmd/parser

Usage

Basic Usage

Parse a JSON file:

./build/jsonparser input.json

Parse from standard input:

echo '{"key": "value"}' | ./build/jsonparser -
Command Line Options
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:
./build/jsonparser -verbose input.json
  1. Parse with benchmarking:
./build/jsonparser -benchmark input.json
  1. Parse in strict mode:
./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:

make test

Run specific test steps:

make test-step1
make test-step2
make test-step3
make test-step4
Development Commands

Format code:

make fmt

Run linter:

make lint

Clean build artifacts:

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 file for details.

Acknowledgments

  • Inspired by the JSON specification
  • Built the following best practices from the "Dragon Book" (Compilers: Principles, Techniques, and Tools)

Author

Adwaith Rajeev (@letsmakecakes)

Support

If you have any questions or run into issues, please open an issue in the GitHub repository.

Directories ΒΆ

Path Synopsis
cmd
parser command
internal
pkg

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL