Skip to main content

Native TOON parser and renderer for Django Rest Framework.

Project description

drf-toon

Native TOON (Token Oriented Object Notation) parser and renderer for Django REST Framework.

TOON is a token-efficient serialization format designed for LLM contexts. It can result in 30–60% fewer tokens than JSON.

drf-toon is built on toons, a high-performance Rust implementation meant to mimic the native python json package.

Compatibility

drf-toon supports Django 5.2 LTS on Python 3.10–3.13, matching Django's own support matrix. Every supported Python version is exercised in CI.

Django Python
5.2 LTS 3.10, 3.11, 3.12, 3.13

Support for additional Django releases can be added later by extending the CI matrix.

Installation

pip install drf-toon

Usage

Add the parser and renderer to your DRF settings:

# settings.py
REST_FRAMEWORK = {
    "DEFAULT_PARSER_CLASSES": [
        "drf_toon.parsers.TOONParser",
        "rest_framework.parsers.JSONParser",
    ],
    "DEFAULT_RENDERER_CLASSES": [
        "drf_toon.renderers.TOONRenderer",
        "rest_framework.renderers.JSONRenderer",
    ],
}

Or enable them per-view:

from rest_framework.views import APIView
from rest_framework.response import Response

from drf_toon.parsers import TOONParser
from drf_toon.renderers import TOONRenderer


class UserView(APIView):
    parser_classes = [TOONParser]
    renderer_classes = [TOONRenderer]

    def get(self, request):
        return Response({"name": "Alice", "age": 30})

Both classes use the application/toon media type. Send requests with Content-Type: application/toon to parse, and Accept: application/toon (or ?format=toon) to render.

Development

Install the dev dependencies and run the checks with uv:

uv sync --dev

uv run ruff check .          # lint
uv run ruff format .         # format
uv run ruff format --check . # format check (CI)
uv run mypy                  # type check
uv run pytest                # tests + coverage

Optionally install the git hooks:

uv run pre-commit install --install-hooks

This wires up both a pre-commit hook (autofix + format) and a pre-push hook that blocks the push if the code is not properly linted/formatted.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_toon-0.1.0.tar.gz (2.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_toon-0.1.0-py3-none-any.whl (3.5 kB view details)

Uploaded Python 3

File details

Details for the file drf_toon-0.1.0.tar.gz.

File metadata

  • Download URL: drf_toon-0.1.0.tar.gz
  • Upload date:
  • Size: 2.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for drf_toon-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d579bcd7bc91ecfc74e6da43dcd68527a540390362b2ce8c43a8ca370164b77f
MD5 ee607816d3964d0c5d3c0795f44ab956
BLAKE2b-256 056a8c1fcd247238a43cafc4eb65fa8d1bc6fa218d16baafa4651a39843c867d

See more details on using hashes here.

File details

Details for the file drf_toon-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: drf_toon-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.12.0 {"installer":{"name":"uv","version":"0.12.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for drf_toon-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 556ae6c5133bb848ca35e7a3e3df9374ca477323ba17a77ef12faf707929c194
MD5 45eb8ae3ccab2626eea0289dd8d9c45a
BLAKE2b-256 cdb4902d5e891378b4c01b857ff92f2ec4fab4e246f50872606bb1ca29a14a10

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page