Add JSON-string interop helpers: encode_json() and loads()#2
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
nullkeyword has no direct TOON equivalent. Worth noting: this is an ergonomic win rather than a correctness fix. Ourencode()already renders PythonNoneas the TOONnullliteral, andjson.loadsalready mapsnull→None. 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 overjson.loadsreturning TOON-ready Python objects (null→None). Extra kwargs are forwarded.encode_json(json_string, options=None)— one-step JSON string → TOON, equivalent toencode(loads(json_string), options).Implementation notes
json_io.pymodule that importsencode()directly. This sidesteps the circular-import problem the upstream PR ran into (it placed the helpers in the same module that definesencode, requiring careful ordering). Since we removedutils.pyduring the slim-down, a dedicated module is the natural home.toon_format.__init__).tests/test_json_integration.pycover nulls in objects/arrays, top-level null, kwargs forwarding, option forwarding, equivalence to the manualencode(json.loads(...))pipeline, andJSONDecodeErroron invalid input.docs/api.md/docs/README.mdupdated.Verification
ruff check src/ tests/clean.Generated by Claude Code