# cognizaunce/toon-python architecture

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

- Interactive diagram: https://gitdiagram.com/cognizaunce/toon-python
- Repository: https://github.com/cognizaunce/toon-python

## Overview

1. **Repository purpose**
   - This is a **Python library** for **encoding Python/JSON data into TOON text**.
   - The project is explicitly **encode-only** now: decoding, CLI conversion, and parser/scanner modules have been removed.

2. **Public API boundary**
   - The package root, `src/toon_format/__init__.py`, is the stable entry point.
   - It exposes three main functions:
     - `encode(...)` for arbitrary Python objects
     - `encode_normalized(...)` for already JSON-compatible data
     - `encode_json_to_toon(...)` for JSON strings
   - External callers should not need to import internal modules directly.

3. **Primary data flow**
   - There are two main input paths:
     - **Python object path**: `encode()` → normalization → encoding
     - **JSON-compatible path**: `encode_normalized()` → encoding only
   - A third convenience path exists:
     - `encode_json_to_toon()` → `json.loads()` → `encode_normalized()`
   - This separation is important architecturally because the project optimizes the common workflow of parsing JSON, mutating it, and re-encoding it.

4. **Normalization subsystem**
   - `src/toon_format/normalize.py` handles conversion of Python-native values into JSON-compatible forms.
   - Based on the README, it canonicalizes types such as:
     - `datetime`, `date` → ISO strings
     - `Decimal` → numeric approximation
     - `tuple`/`set`/`frozenset` → lists
     - `Path` → strings
     - non-finite floats, callables, unsupported objects → `null`
     - `-0.0` → `0`
   - This is the boundary between “arbitrary Python” and “JSON-safe” data.

5. **Encoding subsystem**
   - `src/toon_format/toon_encoder.py` is the core encoder.
   - It likely contains the logic for:
     - object field rendering
     - primitive arrays
     - array classification
     - tabular array detection for uniform primitive objects
     - fallback list-style rendering for mixed or nested arrays
   - The README highlights performance work here, so this is the main hot path.

6. **Formatting/output subsystem**
   - `src/toon_format/writer.py` appears to be responsible for assembling the final TOON text.
   - This boundary likely handles:
     - delimiters such as `,`, `\t`, `|`
     - indentation
     - length markers like `[3]:` or `[ #3 ]`-style variants
     - line construction and joining
   - In architecture terms, this is the string emission layer separated from value classification.

7. **Shared configuration and typing**
   - `src/toon_format/types.py` defines option and internal data types.
   - `src/toon_format/constants.py` stores defaults and shared constants, likely including supported delimiters and validation patterns.
   - `src/toon_format/py.typed` indicates the package ships typing information for consumers and static analysis.

8. **Format behavior that shapes the architecture**
   - TOON uses two major rendering modes:
     - **object-style key/value output**
     - **tabular arrays** when every element is an object with the same primitive keys
   - Arrays with nested structures or mixed types fall back to list notation.
   - This means the encoder must classify input before rendering, and that classification is central to the design.

9. **Performance-oriented boundaries**
   - The README describes optimization choices that likely correspond to internal encoder structure:
     - `encode_normalized()` skips the normalization pass
     - `encode_json_to_toon()` parses once and reuses the normalized encoder
     - array classification avoids repeated scans
     - key encoding/cache reuse and precompiled validation are used on hot paths
   - These are implementation details, but architecturally they indicate a pipeline designed around minimizing passes over data.

10. **Test architecture**
    - `tests/` is heavily focused on **encode behavior**, not parsing or decoding.
    - Coverage includes:
      - API surface
      - encoder behavior
      - normalization rules
      - JSON helper behavior
      - utility functions
      - spec/fixture conformance
    - The `tests/fixtures/encode/` set and `tests/fixtures.schema.json` suggest fixture-driven verification against format expectations.

11. **Documentation and specification references**
    - `docs/` contains format, API, and LLM-integration docs.
    - This implies the repo is not just an implementation; it also serves as a reference for the TOON output shape and intended usage in LLM workflows.
    - `docs/format.md` and `docs/api.md` are the key human-facing architecture references.

12. **Packaging, tooling, and CI**
    - The project is a modern Python package managed by `pyproject.toml`.
    - Development uses `uv`, `ruff`, `mypy`, and `pytest`.
    - GitHub Actions handles linting, tests, and publishing.
    - `.devcontainer/` indicates a containerized dev environment, but it is a development convenience rather than a runtime component.

13. **Operational/runtime boundaries**
    - This is a **library**, not a service: there is no server, worker, queue, or database layer in the repository.
    - The runtime boundary is simply “Python process imports package → calls encode APIs → receives TOON string.”
    - The only external standard-library dependency visible in the architecture is `json` for the JSON-string helper.

14. **How to draw the architecture**
    - A good diagram would show:
      - Consumers calling the public API at the package root
      - A normalization stage for general Python input
      - A JSON-compatible encoding stage
      - A writer/formatter stage producing TOON text
      - Tests/docs/CI as surrounding quality and release systems
    - The key conceptual split is **input normalization vs. TOON rendering**, with `encode_normalized()` bypassing the first stage for performance.

Main entry points: [`src/toon_format/__init__.py`](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/__init__.py) (API root), [`src/toon_format/toon_encoder.py`](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/toon_encoder.py) (Encoder), [`src/toon_format/normalize.py`](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/normalize.py) (Normalize), [`src/toon_format/writer.py`](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/writer.py) (Writer)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_api["Public API"]
  node_src_toon_format_init["API root<br/>public module<br/>[__init__.py]"]
end

subgraph group_group_core["Core encode"]
  node_src_toon_format_normalize["Normalize<br/>[normalize.py]"]
  node_src_toon_format_encoder["Encoder<br/>[toon_encoder.py]"]
  node_src_toon_format_writer["Writer<br/>emitter<br/>[writer.py]"]
  node_src_toon_format_types["Types<br/>[types.py]"]
  node_src_toon_format_constants["Constants<br/>[constants.py]"]
end

subgraph group_group_quality["Quality"]
  node_docs_api["API docs<br/>[api.md]"]
  node_docs_format["Format docs<br/>[format.md]"]
  node_docs_llm["LLM docs<br/>[llm-integration.md]"]
  node_tests_api["API tests<br/>[test_api.py]"]
  node_tests_encoder["Encoder tests<br/>[test_encoder.py]"]
  node_tests_normalize["Normalize tests"]
  node_tests_json["JSON helper tests"]
  node_tests_spec["Fixture tests"]
end

subgraph group_group_tooling["Tooling"]
  node_pyproject["Project config<br/>packaging<br/>[pyproject.toml]"]
  node_github_actions["CI workflows"]
end

node_external_consumers(("Consumers<br/>caller"))
node_json_stdlib(("json.loads<br/>stdlib"))

node_external_consumers -->|"imports"| node_src_toon_format_init
node_src_toon_format_init -->|"encode"| node_src_toon_format_normalize
node_src_toon_format_init -->|"encode_normalized"| node_src_toon_format_encoder
node_src_toon_format_init -->|"json helper"| node_json_stdlib
node_json_stdlib -->|"normalized path"| node_src_toon_format_init
node_src_toon_format_normalize -->|"normalized data"| node_src_toon_format_encoder
node_src_toon_format_encoder -->|"emit text"| node_src_toon_format_writer
node_src_toon_format_encoder -->|"uses"| node_src_toon_format_types
node_src_toon_format_encoder -->|"uses"| node_src_toon_format_constants
node_src_toon_format_writer -->|"uses"| node_src_toon_format_constants
node_docs_api -->|"documents"| node_src_toon_format_init
node_docs_format -->|"specifies"| node_src_toon_format_encoder
node_docs_llm -->|"guides"| node_src_toon_format_init
node_tests_api -->|"verifies"| node_src_toon_format_init
node_tests_encoder -->|"verifies"| node_src_toon_format_encoder
node_tests_normalize -->|"verifies"| node_src_toon_format_normalize
node_tests_json -->|"verifies"| node_src_toon_format_init
node_tests_spec -->|"compares"| node_docs_format
node_pyproject -->|"packages"| node_src_toon_format_types
node_github_actions -->|"runs"| node_tests_api
node_github_actions -->|"runs"| node_tests_encoder

click node_src_toon_format_init "https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/__init__.py"
click node_src_toon_format_normalize "https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/normalize.py"
click node_src_toon_format_encoder "https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/toon_encoder.py"
click node_src_toon_format_writer "https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/writer.py"
click node_src_toon_format_types "https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/types.py"
click node_src_toon_format_constants "https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/constants.py"
click node_docs_api "https://github.com/cognizaunce/toon-python/blob/main/docs/api.md"
click node_docs_format "https://github.com/cognizaunce/toon-python/blob/main/docs/format.md"
click node_docs_llm "https://github.com/cognizaunce/toon-python/blob/main/docs/llm-integration.md"
click node_tests_api "https://github.com/cognizaunce/toon-python/blob/main/tests/test_api.py"
click node_tests_encoder "https://github.com/cognizaunce/toon-python/blob/main/tests/test_encoder.py"
click node_tests_normalize "https://github.com/cognizaunce/toon-python/blob/main/tests/test_normalize_functions.py"
click node_tests_json "https://github.com/cognizaunce/toon-python/blob/main/tests/test_json_helper.py"
click node_tests_spec "https://github.com/cognizaunce/toon-python/blob/main/tests/test_spec_fixtures.py"
click node_pyproject "https://github.com/cognizaunce/toon-python/blob/main/pyproject.toml"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_src_toon_format_init toneBlue
class node_src_toon_format_normalize,node_src_toon_format_encoder,node_src_toon_format_writer,node_src_toon_format_types,node_src_toon_format_constants toneAmber
class node_docs_api,node_docs_format,node_docs_llm,node_tests_api,node_tests_encoder,node_tests_normalize,node_tests_json,node_tests_spec toneMint
class node_pyproject,node_github_actions toneRose
class node_external_consumers,node_json_stdlib toneNeutral
```

## Components

### Public API

Stable entry points and caller-facing encode flows.

- [API root](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/__init__.py): public module · Stable package entry point exposing encode, encode_normalized, and encode_json_to_toon. · `src/toon_format/__init__.py`

### Core encode

Normalization, classification, and TOON text emission.

- [Normalize](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/normalize.py): normalizer · Converts arbitrary Python values into JSON-compatible data. · `src/toon_format/normalize.py`
- [Encoder](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/toon_encoder.py): core encoder · Classifies values and renders TOON structures, including tabular arrays and list fallbacks. · `src/toon_format/toon_encoder.py`
- [Writer](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/writer.py): emitter · Builds the final TOON text, delimiters, indentation, and line structure. · `src/toon_format/writer.py`
- [Types](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/types.py): types · Shared option and internal type definitions. · `src/toon_format/types.py`
- [Constants](https://github.com/cognizaunce/toon-python/blob/main/src/toon_format/constants.py): constants · Default settings and shared encoder constants. · `src/toon_format/constants.py`

### Quality

Tests and docs that define expected behavior.

- [API docs](https://github.com/cognizaunce/toon-python/blob/main/docs/api.md): docs · Human-facing reference for the public package interface. · `docs/api.md`
- [Format docs](https://github.com/cognizaunce/toon-python/blob/main/docs/format.md): docs · Specification and rendering rules for TOON output. · `docs/format.md`
- [LLM docs](https://github.com/cognizaunce/toon-python/blob/main/docs/llm-integration.md): docs · Usage guidance for LLM-oriented workflows and examples. · `docs/llm-integration.md`
- [API tests](https://github.com/cognizaunce/toon-python/blob/main/tests/test_api.py): tests · Verifies the public entry points and exported behavior. · `tests/test_api.py`
- [Encoder tests](https://github.com/cognizaunce/toon-python/blob/main/tests/test_encoder.py): tests · Exercises core rendering behavior and array classification. · `tests/test_encoder.py`
- [Normalize tests](https://github.com/cognizaunce/toon-python/blob/main/tests/test_normalize_functions.py): tests · Covers Python-to-JSON normalization rules. · `tests/test_normalize_functions.py`
- [JSON helper tests](https://github.com/cognizaunce/toon-python/blob/main/tests/test_json_helper.py): tests · Checks the JSON-string helper path and reuse of the normalized encoder. · `tests/test_json_helper.py`
- [Fixture tests](https://github.com/cognizaunce/toon-python/blob/main/tests/test_spec_fixtures.py): tests · Spec and fixture conformance for encode output. · `tests/test_spec_fixtures.py`

### Tooling

Packaging, typing, and CI/release support.

- [Project config](https://github.com/cognizaunce/toon-python/blob/main/pyproject.toml): packaging · Build metadata, dependencies, typing, and developer tooling configuration. · `pyproject.toml`
- **CI workflows**: ci · Lint, test, and publish automation.

### Other components

- **Consumers**: caller · Python code importing the package and calling encode APIs.
- **json.loads**: stdlib · Standard-library JSON parser used by the JSON helper path.

## Connections

- Consumers → API root: imports · Callers enter through the stable package root.
- API root → Normalize: encode · encode() normalizes arbitrary Python objects before rendering.
- API root → Encoder: encode_normalized · encode_normalized() bypasses normalization and renders directly.
- API root → json.loads: json helper · encode_json_to_toon() parses JSON text first.
- json.loads → API root: normalized path · Parsed JSON is passed into the normalized encoding flow.
- Normalize → Encoder: normalized data · JSON-safe values flow into the encoder hot path.
- Encoder → Writer: emit text · The encoder delegates final string assembly to the writer layer.
- Encoder → Types: uses · Encoding options and internal shapes are defined in shared types.
- Encoder → Constants: uses · Shared defaults and format constants support classification and rendering.
- Writer → Constants: uses · Writer output relies on shared delimiters and formatting defaults.
- API docs → API root: documents · Describes the public entry points and caller-facing contract.
- Format docs → Encoder: specifies · Defines TOON rendering behavior and tabular vs list output rules.
- LLM docs → API root: guides · Shows intended usage in LLM workflows.
- API tests → API root: verifies · Checks the stable API surface.
- Encoder tests → Encoder: verifies · Covers object rendering, array classification, and fallback behavior.
- Normalize tests → Normalize: verifies · Validates conversion of Python-native values into JSON-safe forms.
- JSON helper tests → API root: verifies · Exercises the JSON-string helper path.
- Fixture tests → Format docs: compares · Fixture-driven checks align output with the documented format.
- Project config → Types: packages · Build and typing metadata support the shipped library.
- CI workflows → API tests: runs · CI executes the test suite and linting.
- CI workflows → Encoder tests: runs · CI validates core encoding behavior.
