How cognizaunce/toon-python works
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: __init__.py API root · toon_encoder.py Encoder · normalize.py Normalize · writer.py Writer
Components 18
Public API
Stable entry points and caller-facing encode flows.
- API root · 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 · Converts arbitrary Python values into JSON-compatible data.
src/toon_format/normalize.py - Encoder · Classifies values and renders TOON structures, including tabular arrays and list fallbacks.
src/toon_format/toon_encoder.py - Writer · Builds the final TOON text, delimiters, indentation, and line structure.
src/toon_format/writer.py - Types · Shared option and internal type definitions.
src/toon_format/types.py - Constants · Default settings and shared encoder constants.
src/toon_format/constants.py
Quality
Tests and docs that define expected behavior.
- API docs · Human-facing reference for the public package interface.
docs/api.md - Format docs · Specification and rendering rules for TOON output.
docs/format.md - LLM docs · Usage guidance for LLM-oriented workflows and examples.
docs/llm-integration.md - API tests · Verifies the public entry points and exported behavior.
tests/test_api.py - Encoder tests · Exercises core rendering behavior and array classification.
tests/test_encoder.py - Normalize tests · Covers Python-to-JSON normalization rules.
tests/test_normalize_functions.py - JSON helper tests · Checks the JSON-string helper path and reuse of the normalized encoder.
tests/test_json_helper.py - Fixture tests · Spec and fixture conformance for encode output.
tests/test_spec_fixtures.py
Tooling
Packaging, typing, and CI/release support.
- Project config · Build metadata, dependencies, typing, and developer tooling configuration.
pyproject.toml - CI workflows · Lint, test, and publish automation.
Other components
- Consumers · Python code importing the package and calling encode APIs.
- json.loads · Standard-library JSON parser used by the JSON helper path.
Connections 21
- 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.