Skip to content

Add JSON-string interop helpers: encode_json() and loads()#2

Merged
jeepinbird merged 2 commits into
mainfrom
claude/json-toon-codec-a2Kde
Jun 3, 2026
Merged

Add JSON-string interop helpers: encode_json() and loads()#2
jeepinbird merged 2 commits into
mainfrom
claude/json-toon-codec-a2Kde

Conversation

@jeepinbird

Copy link
Copy Markdown
Owner

Summary

Implements the ergonomic helpers from upstream issue #49 / PR #57, adapted to this slimmed codebase (not a verbatim port).

Data frequently arrives as raw JSON text — LLM tool outputs, REST API responses, logs — where the JSON null keyword has no direct TOON equivalent. Worth noting: this is an ergonomic win rather than a correctness fix. Our encode() already renders Python None as the TOON null literal, and json.loads already maps nullNone. These helpers just remove the boilerplate of wiring the two together for the common JSON-string → TOON path.

What's added

  • loads(json_string, **kwargs) — a thin wrapper over json.loads returning TOON-ready Python objects (nullNone). Extra kwargs are forwarded.
  • encode_json(json_string, options=None) — one-step JSON string → TOON, equivalent to encode(loads(json_string), options).
from toon_format import encode_json

encode_json('{"name": "Alice", "mood": null, "tags": [1, null, 3]}')
# name: Alice
# mood: null
# tags[3]: 1,null,3

Implementation notes

  • Both helpers live in a new json_io.py module that imports encode() directly. This sidesteps the circular-import problem the upstream PR ran into (it placed the helpers in the same module that defines encode, requiring careful ordering). Since we removed utils.py during the slim-down, a dedicated module is the natural home.
  • Exported from the public API (toon_format.__init__).
  • New tests in tests/test_json_integration.py cover nulls in objects/arrays, top-level null, kwargs forwarding, option forwarding, equivalence to the manual encode(json.loads(...)) pipeline, and JSONDecodeError on invalid input.
  • README and docs/api.md / docs/README.md updated.

Verification

  • Full suite: 805 passed, 13 skipped (818 collected; skips are by-design normalization fixtures).
  • ruff check src/ tests/ clean.

Generated by Claude Code

claude added 2 commits June 3, 2026 14:11
Implements the ergonomic helpers from upstream issue toon-format#49 / PR toon-format#57, adapted to
this slimmed codebase.

Data frequently arrives as raw JSON text (LLM tool outputs, REST API
responses, logs) where the JSON `null` keyword has no direct TOON equivalent.
encode() already renders Python None as the TOON null literal and json.loads
already maps null -> None, so these helpers just remove the boilerplate of
wiring the two together:

- loads(json_string, **kwargs): thin wrapper over json.loads returning
  TOON-ready Python objects
- encode_json(json_string, options=None): one-step JSON string -> TOON,
  equivalent to encode(loads(json_string), options)

Both live in a new json_io module that imports encode() directly (avoiding the
circular-import issue the upstream PR hit by placing them alongside encode in
its package init). Exported from the public API and covered by
tests/test_json_integration.py. README and docs/api updated.
Use utils.py as a shared home for small public helpers that complement the
core encode/decode API, rather than a JSON-specific module. The previous
circular-import concern doesn't apply here: it imports `from .encoder import
encode` directly rather than `from . import encode`.

Generalizes the module docstring accordingly; the public API
(toon_format.encode_json / loads) is unchanged.
@jeepinbird jeepinbird marked this pull request as ready for review June 3, 2026 15:39
@jeepinbird jeepinbird merged commit 12c8f9c into main Jun 3, 2026
@jeepinbird jeepinbird deleted the claude/json-toon-codec-a2Kde branch June 3, 2026 15:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants