# just-vedant/json-to-code-generator architecture

> Architecture diagram of the GitHub repository just-vedant/json-to-code-generator, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-10-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/just-vedant/json-to-code-generator
- Repository: https://github.com/just-vedant/json-to-code-generator

## Overview

##### Project Type and Purpose
- This is a **compiler-like CLI tool** implemented in C++ that reads JSON schema definitions and emits idiomatic code in C++, Python, or Java.
- Key stages: lexical/parsing, AST construction, semantic validation, multi-target code generation.

##### File Structure
- **src/** holds core modules:
- main.cpp + cli.cpp/h: CLI front‐end and argument parsing
- json_parser.cpp/h: JSON lexical analysis & AST builder
- ast.cpp/h: AST node definitions
- semantic.cpp/h: semantic checks on the AST
- code_generator.cpp/h: orchestrates per-language backends
- cpp_generator, python_generator, java_generator: emit language-specific code
- config.cpp/h: load runtime settings (flags, output path)
- compiler.cpp/h: top-level compilation pipeline
- **external/nlohmann/json.hpp**: third-party JSON library for low-level tokenization/parsing.
- **examples/**: sample JSON schema inputs (User, Product, Order, Database).
- **CMakeLists.txt**: build scripts.

##### Extract Architecture from README
- Confirms four compiler phases: Lexical Analysis → Parsing → Semantic Analysis → Code Generation (multi-target).
- Highlights CLI usage, schema format, and output directory.

##### Main Components
a. **CLI Module**
- Parses command-line args (input file/dir, target langs, verbose flag).
b. **Configuration Loader**
- Reads flags, sets up logging/verbosity, output paths.
c. **JSON Parser (Lexer + Parser)**
- Uses nlohmann to tokenize & parse raw JSON into an internal AST.
d. **AST**
- In-memory representation of classes, fields, methods.
e. **Semantic Analyzer**
- Walks AST to enforce schema rules (types, required fields).
f. **Code Generator Orchestrator**
- Takes validated AST and dispatches to each language backend.
g. **Language Backends**
- CppGenerator, JavaGenerator, PythonGenerator: each emits .cpp/.h, .java, .py files respectively.
h. **File System / Output Writer**
- Persists generated code under the specified output directory.
i. **External Library**
- nlohmann::json used by JSON Parser.

##### Relationships & Data Flow
- Inside **Compiler Pipeline**:
1) **JSON Parser** → builds **AST**
2) **AST** → **Semantic Analyzer** → produces Validated AST
3) Validated AST → **Code Generator Orchestrator**
4) Orchestrator → parallel calls to **Language Backends**
5) Backends → **File System**

##### Architectural Patterns & Principles
- **Pipeline Pattern**: each compiler phase is a stage.
- **Strategy Pattern**: language backends implement a common generator interface.
- **Separation of Concerns**: parsing, validation, generation are decoupled.
- **Extensibility**: adding a new language backend only requires implementing Generator interface and registering it.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "Inputs"
        Examples["Example Schemas (*.json)"]:::external
    end

    Examples -->|"invoke"| CLI["CLI Module"]:::parserAst
    CLI -->|parse args| Config["Configuration Loader"]:::parserAst

    subgraph "Compiler Pipeline"
        Parser["JSON Parser"]:::parserAst
        AST["AST"]:::parserAst
        Semantic["Semantic Analyzer"]:::semantic
        Orchestrator["Code Generator Orchestrator"]:::generator
    end

    Config -->|configure| Parser
    Parser -->|parse| AST
    AST -->|build AST| Semantic
    Semantic -->|validate| Orchestrator

    subgraph "Language Backends"
        CppGen["C++ Generator"]:::generator
        JavaGen["Java Generator"]:::generator
        PyGen["Python Generator"]:::generator
    end

    Orchestrator -->|generate cpp| CppGen
    Orchestrator -->|generate java| JavaGen
    Orchestrator -->|generate python| PyGen

    subgraph "Output"
        FS["File System / Output Writer"]:::generator
    end

    CppGen -->|write files| FS
    JavaGen -->|write files| FS
    PyGen -->|write files| FS

    Parser -.->|uses| External["nlohmann::json"]:::external
    AST -.->|defines| External
    FS -->|build script| Build["CMakeLists.txt"]:::external

    click CLI "https://github.com/just-vedant/json-to-code-generator/blob/main/src/main.cpp"
    click Config "https://github.com/just-vedant/json-to-code-generator/blob/main/src/config.cpp"
    click Parser "https://github.com/just-vedant/json-to-code-generator/blob/main/src/json_parser.cpp"
    click External "https://github.com/just-vedant/json-to-code-generator/blob/main/external/nlohmann/json.hpp"
    click AST "https://github.com/just-vedant/json-to-code-generator/blob/main/src/ast.cpp"
    click Semantic "https://github.com/just-vedant/json-to-code-generator/blob/main/src/semantic.cpp"
    click Orchestrator "https://github.com/just-vedant/json-to-code-generator/blob/main/src/code_generator.cpp"
    click CppGen "https://github.com/just-vedant/json-to-code-generator/blob/main/src/cpp_generator.cpp"
    click JavaGen "https://github.com/just-vedant/json-to-code-generator/blob/main/src/java_generator.cpp"
    click PyGen "https://github.com/just-vedant/json-to-code-generator/blob/main/src/python_generator.cpp"
    click FS "https://github.com/just-vedant/json-to-code-generator/blob/main/src/code_generator.cpp"
    click Examples "https://github.com/just-vedant/json-to-code-generator/tree/main/examples"
    click Build "https://github.com/just-vedant/json-to-code-generator/blob/main/CMakeLists.txt"

    classDef parserAst fill:#cce5ff,stroke:#0056b3,color:#000;
    classDef semantic fill:#d4edda,stroke:#155724,color:#000;
    classDef generator fill:#fff3cd,stroke:#856404,color:#000;
    classDef external fill:#e2e3e5,stroke:#6c757d,color:#000;
```
