Skip to content

feat: add to_json convenience API - #15

Merged
alesanfra merged 3 commits into
alesanfra:mainfrom
micheal000010000-hub:feature/add-to-json-api
May 20, 2026
Merged

feat: add to_json convenience API#15
alesanfra merged 3 commits into
alesanfra:mainfrom
micheal000010000-hub:feature/add-to-json-api

Conversation

@micheal000010000-hub

Copy link
Copy Markdown
Contributor

Description

Adds a convenience API toons.to_json() for converting TOON strings directly to JSON strings.

This reduces boilerplate for a common interoperability workflow where users currently need to manually combine TOONS parsing with Python’s json module:

import json
import toons

json.dumps(toons.loads(raw))

The new helper preserves existing parsing behavior and options (strict, expand_paths) while providing a simpler one-step API.

Changes

  • Added toons.to_json() public API in src/lib.rs
  • Added matching type stub in toons.pyi
  • Added integration tests covering:
    • default JSON formatting
    • compact JSON output (indent=None)
    • strict parsing behavior
    • decode error propagation
    • path expansion behavior
  • Added README usage example

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Related Issues

N/A

Testing

Baseline (main):

pytest
# 747 passed, 1 xfailed

Feature branch:

cargo check
maturin develop
pytest tests/integration/test_to_json.py -v
pytest tests/integration

Results:

cargo check              # passed
maturin develop          # passed
pytest test_to_json      # 5 passed
pytest tests/integration # 752 passed, 1 xfailed

Checklist

  • New tests added for new functionality
  • Code follows project style guidelines
  • Documentation updated
  • No new warnings generated
  • Conventional commit format used
  • Pre-commit ran on all modified files

@alesanfra alesanfra left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @micheal000010000-hub, nice contribution! The PR is already in great shape. There's just one minor adjustment needed regarding the default indentation value.

In Python, when a keyword argument is nullable, we generally expect the same behavior whether the argument is omitted entirely or explicitly passed as None. Setting 2 as the default indentation feels a bit opinionated (e.g., why 2 instead of 4 or 1?).

As noted in my inline suggestion, I'd recommend matching the behavior of the standard json module to keep things as conventional and neutral as possible. Let me know if that makes sense to you!

Comment thread src/lib.rs Outdated
/// "age": 30
/// }
#[pyfunction]
#[pyo3(signature = (s, *, strict=true, expand_paths=None, indent=2))]

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a fan of the default indent set to 2, I would match the behavior of the json module, it feels more natural IMHO.

Suggested change
#[pyo3(signature = (s, *, strict=true, expand_paths=None, indent=2))]
#[pyo3(signature = (s, *, strict=true, expand_paths=None, indent=None))]

@micheal000010000-hub

micheal000010000-hub commented May 19, 2026

Copy link
Copy Markdown
Contributor Author

@alesanfra

Thanks for the review — that makes sense.

I agree that matching Python’s standard json module behavior is the more intuitive choice here, especially since indent is nullable and omitted vs explicitly passing None should behave consistently.

I’ve updated the implementation to default indent to None, adjusted the type stub, tests, and README example accordingly.

Review the diff and let me know

@alesanfra
alesanfra merged commit 6fb760f into alesanfra:main May 20, 2026
17 checks passed
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