JSON Parser
A robust, hand-written JSON parser implemented in Java, following an incremental development methodology.
๐ Overview
This project implements a complete JSON parser from scratch. It performs lexical analysis (tokenization) and syntax analysis (parsing) to construct an Abstract Syntax Tree (AST) representing the parsed JSON structure.
The implementation is inspired by the principles of compiler construction, specifically following the incremental approach often found in literature like Terence Parr's "Implementing Compilers". The development progressed through several "Steps," each increasing the complexity of the supported JSON grammar (from basic primitives to nested objects and arrays).
๐ Architecture
The parser is divided into three main components:
1. Lexical Analysis (JsonLexer)
The JsonLexer scans the input string and converts it into a stream of Token objects. It handles:
- Whitespace skipping.
- Identifying JSON primitives (strings, numbers, booleans, null).
- Recognizing structural characters (
{, }, [, ], :, ,).
- Handling escape sequences within strings.
2. Syntax Analysis (JsonParser)
The JsonParser consumes the token stream and applies the JSON grammar rules. It uses a recursive descent parsing strategy to build the tree structure.
3. Abstract Syntax Tree (AST)
The parser produces a heterogeneous AST using a JsonNode interface. This allows for a type-safe and expressive representation of the JSON data:
JsonArrayNode: Represents a JSON array (contains a List<JsonNode>).
JsonObjectNode: Represents a JSON object (contains a Map<String, JsonNode>).
JsonStringNode: Represents a JSON string.
JsonNumberNode: Represents a JSON number.
JsonBooleanNode: Represents a JSON boolean (true/false).
JsonNullNode: Represents a JSON null.
๐ Getting Started
Prerequisites
- Java 21 or higher.
- Apache Maven installed.
Building the Project
To compile the source code, run:
mvn clean compile
Running Tests
The project is extensively tested using JUnit 5. To run the full suite of tests:
mvn test
๐ Development Workflow & Modernization
While the project currently utilizes Maven, it was originally developed with a manual, step-based testing approach. The following roadmap outlines the transition to a modern, automated development workflow:
1. Automated Testing (Current Status: Transitioning)
- Current: Uses a mix of JUnit 5 and manual shell scripts (
valid.sh, invalid.sh, etc.) to verify specific parsing steps.
- Goal: Consolidate all validation into the JUnit 5 suite. Use parameterized tests to handle the large volume of JSON test files in
src/test/resources.
2. Continuous Integration (Proposed)
- Goal: Implement GitHub Actions to automatically run the Maven test suite on every
push and pull_request. This ensures that new changes do not break existing parsing logic.
3. Code Quality & Linting (Proposed)
- Goal: Integrate a Java formatter (such as
google-java-format) via the maven-checkstyle-plugin or spotless-maven-plugin. This ensures consistent code style across the project.
4. Project Refactoring (Proposed)
- Goal: Once the learning/challenge phase is complete, the "Step" classes (e.g.,
Step1.java, Step3.java) should be removed in favor of a single, clean, production-ready implementation.
๐ Project Structure
.
โโโ pom.xml # Maven configuration
โโโ src
โ โโโ main
โ โ โโโ java
โ โ โ โโโ it/diamondnet/challenge/
โ โ โ โโโ JsonLexer.java # Lexical analyzer
โ โ โ โโโ JsonParser.java # Syntax analyzer
โ โ โ โโโ JsonNode.java # AST Interface
โ โ โ โโโ ... (AST Implementations)
โ โ โโโ resources # Grammar definitions and images
โ โโโ test
โ โโโ java
โ โ โโโ it/diamondnet/challenge/ # JUnit 5 tests
โ โโโ resources # JSON test files (organized by steps)
โโโ target # Compiled classes and reports
๐ License
This project is for educational purposes, following the Coding Challenges guidelines.