# benjamine/jsondiffpatch architecture

> Architecture diagram of the GitHub repository benjamine/jsondiffpatch, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-09). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/benjamine/jsondiffpatch
- Repository: https://github.com/benjamine/jsondiffpatch

## Overview

##### Project Type and Purpose
- Monorepo for an open-source JavaScript/TypeScript library called **jsondiffpatch**.
- Provides a **core diff/patch engine**, multiple **formatters** (JSON, HTML, console, JSON-Patch, annotated), a **plugin-style pipeline** for filters, and a small companion package (`diff-mcp`).
- Ships as both a CLI tool and a browser/Node ESM library.

##### High-Level File/Package Structure
- **`.github/workflows/`**: CI, release, and GitHub Pages deployment pipelines.
- **`packages/jsondiffpatch/`**:
- `src/contexts/`, `src/filters/`, `src/formatters/`, `src/processor.ts`, `src/index.ts` → core engine, pipes & filters implementation, formatters.
- `test/`, `bin/jsondiffpatch.js` → CLI entry point and test suites.
- **`packages/diff-mcp/`**: secondary diff/patch package, its own src, tests, and server example.
- **`demos/`**: user-facing examples (console, HTML visual diff, plugin demo).
- **`docs/`**: API guides (arrays, deltas, formatters, plugins, React).
- Top-level TypeScript configs, `package.json` (workspace dependencies), lockfile, and license/readme.

##### Architecture Overview – Main Components
A. **Input Layer**
- JSON objects or strings (via CLI, browser script, or API call).
- Optional third-party library for text diffs: **diff-match-patch**.
B. **Core Diff Engine**
- Central `DiffPatcher` class (pipes & filters pattern).
- **Filters** (arrays, nested objects, dates, texts) determine delta entries.
- Produces a **delta** (compact JSON structure).
C. **Patch & Reverse Engine**
- Same pipeline concept to apply (`patch`) or revert (`reverse`/`unpatch`) a delta.
D. **Formatters**
- Plug-in modules (`console`, `html`, `annotated`, `jsonpatch`, etc.)
- Transform delta + source into human-readable or machine-readable output.
E. **CLI & Bundles**
- `bin/jsondiffpatch.js` → CLI wrapper calling core engine + formatters.
- Bundled ESM builds for browser demos (`html-demo`) and npm package.
F. **Demos & Docs**
- Demonstrate usage patterns, embed formatters, highlight plugin extension points.
G. **CI/CD & Release**
- GitHub Actions workflows for build, test, release to npm, and docs site.

##### Component Relationships and Data Flows
1. **User Input** (CLI `< left.json > right.json` or API call)
→ 2. **Core Diff Engine** (create or default instance)
→ passes through **filter chain** → outputs **delta**
→ 3a. **Patch/Reverse Module** (optionally apply/unapply delta)
→ 3b. **Formatter Modules** (choose one or multiple)
→ render to console, HTML, annotated JSON, or JSON-Patch.
→ 4. **Output** (console STDOUT, `
` in browser, or returned JS object).
- **Demos** wire Input → Core Engine → Formatter → UI.
- **diff-mcp** is a thin wrapper around core engine for MC Protocol (metadata-centric).

##### Architectural Patterns and Principles
- **Pipes & Filters**: every diff/patch/reverse is a sequence of filter functions.
- **Separation of Concerns**:
- Core diff logic vs. formatters vs. CLI/bundling.
- Demo code lives outside core package.
- **Monorepo Workspace**: shared toolchain, isolated packages.
- **ESM-only distribution** for modern browser and Node.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Input Layer
    subgraph "Input Layer"
        IJSON["JSON Input"]:::io
        IMPATCH["diff-match-patch (optional)"]:::external
    end

    %% Core Diff Engine
    subgraph "Core Diff Engine"
        DP["DiffPatcher"]:::core
        PROC["processor.ts"]:::core
        PIPE["pipe.ts"]:::core
    end

    %% Contexts
    subgraph "Contexts (diff/patch/reverse)"
        CTXT["context.ts"]:::core
        DIFFCTX["diff.ts"]:::core
        PATCHCTX["patch.ts"]:::core
        REVCTX["reverse.ts"]:::core
    end

    %% Filters
    subgraph "Filters"
        ARR["arrays.ts"]:::core
        NEST["nested.ts"]:::core
        DATES["dates.ts"]:::core
        TEXTS["texts.ts"]:::core
        TRIV["trivial.ts"]:::core
        LCS["lcs.ts"]:::core
    end

    %% Delta
    DELTA["Delta (compact JSON)"]:::io

    %% Patch & Reverse Engine
    subgraph "Patch & Reverse"
        PATCH["patch.ts"]:::core
        REVERSE["reverse.ts"]:::core
    end

    %% Formatters
    subgraph "Formatters"
        FCON["console.ts"]:::core
        FHTML["html.ts"]:::core
        FANN["annotated.ts"]:::core
        JSONP["jsonpatch.ts"]:::core
        JSONPA["jsonpatch-apply.ts"]:::core
        BASEF["base.ts"]:::core
        CSSANN["annotated.css"]:::core
        CSSHTML["html.css"]:::core
    end

    %% Consumers
    subgraph "Consumers & Demos"
        CLI["CLI (bin/jsondiffpatch.js)"]:::consumer
        BROWSER["Browser Demo"]:::consumer
        CONSOLED["Console Demo"]:::consumer
        HTMLD["HTML Demo"]:::consumer
        PLUGIND["Plugin Demo"]:::consumer
        DIFFMCP["diff-mcp"]:::consumer
    end

    %% CI/CD & Registry
    subgraph "CI/CD & Distribution"
        GHCI[".github Actions"]:::infra
        NPM["npm Registry"]:::infra
        DOCSSITE["Docs Site"]:::infra
    end

    %% Data Flow
    IJSON -->|input| DP
    IMPATCH -->|optional text diff| DP
    DP -->|"pipeline orchestrator"| PIPE
    PIPE -->|uses| CTXT
    CTXT -->|diff impl| DIFFCTX
    CTXT -->|patch impl| PATCHCTX
    CTXT -->|reverse impl| REVCTX
    PIPE -->|filter chain| ARR
    PIPE -->|filter chain| NEST
    PIPE -->|filter chain| DATES
    PIPE -->|filter chain| TEXTS
    PIPE -->|filter chain| TRIV
    PIPE -->|filter chain| LCS
    DP -->|produces| DELTA
    DELTA -->|apply/unapply| PATCH
    DELTA -->|apply/unapply| REVERSE
    DELTA -->|input to| FCON
    DELTA -->|input to| FHTML
    DELTA -->|input to| FANN
    DELTA -->|input to| JSONP
    FCON -->|output| CLI
    FCON -->|output| CONSOLED
    FHTML -->|output| HTMLD
    FANN -->|output| PLUGIND
    JSONP -->|output| BROWSER
    CLI -->|consumes engine| DP
    DIFFMCP -->|wrapper around| DP
    GHCI -->|build & test| DP
    GHCI -->|release demo/docs| DOCSSITE
    GHCI -->|publish| NPM

    %% Click Events
    click DP "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/diffpatcher.ts"
    click PROC "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/processor.ts"
    click PIPE "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/pipe.ts"
    click CTXT "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/contexts/context.ts"
    click DIFFCTX "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/contexts/diff.ts"
    click PATCHCTX "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/contexts/patch.ts"
    click REVCTX "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/contexts/reverse.ts"
    click ARR "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/filters/arrays.ts"
    click NEST "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/filters/nested.ts"
    click DATES "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/filters/dates.ts"
    click TEXTS "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/filters/texts.ts"
    click TRIV "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/filters/trivial.ts"
    click LCS "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/filters/lcs.ts"
    click PATCH "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/contexts/patch.ts"
    click REVERSE "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/contexts/reverse.ts"
    click FCON "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/formatters/console.ts"
    click FHTML "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/formatters/html.ts"
    click FANN "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/formatters/annotated.ts"
    click JSONP "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/formatters/jsonpatch.ts"
    click JSONPA "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/formatters/jsonpatch-apply.ts"
    click BASEF "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/formatters/base.ts"
    click CSSANN "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/formatters/styles/annotated.css"
    click CSSHTML "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/src/formatters/styles/html.css"
    click CLI "https://github.com/benjamine/jsondiffpatch/blob/master/packages/jsondiffpatch/bin/jsondiffpatch.js"
    click DIFFMCP "https://github.com/benjamine/jsondiffpatch/blob/master/packages/diff-mcp/src/index.ts"
    click DIFFMCP "https://github.com/benjamine/jsondiffpatch/blob/master/packages/diff-mcp/src/server.ts"
    click DIFFMCP "https://github.com/benjamine/jsondiffpatch/blob/master/packages/diff-mcp/src/mcp.spec.ts"
    click CONSOLED "https://github.com/benjamine/jsondiffpatch/blob/master/demos/console-demo/demo.ts"
    click HTMLD "https://github.com/benjamine/jsondiffpatch/blob/master/demos/html-demo/demo.ts"
    click HTMLD "https://github.com/benjamine/jsondiffpatch/blob/master/demos/html-demo/demo/index.html"
    click PLUGIND "https://github.com/benjamine/jsondiffpatch/blob/master/demos/numeric-plugin-demo/numeric-plugin.ts"
    click GHCI "https://github.com/benjamine/jsondiffpatch/blob/master/.github/workflows/CI.yml"
    click GHCI "https://github.com/benjamine/jsondiffpatch/blob/master/.github/workflows/release.yml"
    click GHCI "https://github.com/benjamine/jsondiffpatch/blob/master/.github/workflows/release-mcp.yml"
    click GHCI "https://github.com/benjamine/jsondiffpatch/blob/master/.github/workflows/gh-pages.yml"

    %% Styles
    classDef core fill:#D0E8FF,stroke:#69B,stroke-width:1px;
    classDef io fill:#DFFFE0,stroke:#7A7,stroke-width:1px;
    classDef consumer fill:#FFE4B5,stroke:#EAA800,stroke-width:1px;
    classDef infra fill:#F0F0F0,stroke:#999,stroke-width:1px;
    classDef external fill:#F8F8F8,stroke:#CCC,stroke-width:1px;
```
