tet.renderers.json module

JSON rendering with custom type adapters for Tet applications.

This module provides a JSON renderer with sensible defaults and the ability to register custom type adapters. It is included automatically when using the renderers.json feature.

Features

  • Automatic serialization of datetime.datetime and datetime.date to ISO 8601 format

  • SQLAlchemy keyed tuple support (when SQLAlchemy is installed)

  • Extensible via custom type adapters

Example

Adding a custom adapter for a model class:

from tet.config import application_factory

@application_factory(included_features=["renderers.json"])
def main(config):
    config.add_json_adapter(
        for_=MyModel,
        adapter=lambda obj, request: {"id": obj.id, "name": obj.name},
    )
    config.scan()

Using a custom JSON renderer factory:

from pyramid.renderers import JSON
from tet.renderers.json import construct_default_renderer

# Create a renderer with custom settings
renderer = construct_default_renderer(
    renderer_factory=JSON,
    serializer=custom_serializer,
)
tet.renderers.json.add_json_adapter(config: Configurator, *, for_: type, adapter: Callable[[Any], Any], renderer: str = 'json')[source]

Add a type adapter to a JSON renderer.

Parameters:
  • config – Pyramid Configurator

  • for – The type to adapt

  • adapter – Callable taking (obj, request) and returning JSON-serializable data

  • renderer – Name of the renderer to add the adapter to (default: ‘json’)

tet.renderers.json.construct_default_renderer(renderer_factory: ~typing.Callable[[...], ~typing.Any] = <class 'pyramid.renderers.JSON'>, **renderer_args)[source]

Construct a JSON renderer with default type adapters.

Adds adapters for: - datetime.datetime and datetime.date (ISO 8601 format) - SQLAlchemy AbstractKeyedTuple (converted to dict)

Parameters:
  • renderer_factory – Factory callable for creating the renderer

  • renderer_args – Additional arguments passed to the factory

Returns:

Configured JSON renderer instance

tet.renderers.json.hook_json_renderer(config: Configurator, *, renderer: Any, name: str = 'json')[source]

Register a JSON renderer with the given name.

Parameters:
  • config – Pyramid Configurator

  • renderer – The JSON renderer instance

  • name – Name for the renderer (default: ‘json’)

tet.renderers.json.includeme(config: Configurator)[source]

Pyramid includeme function for JSON rendering.

Registers a default JSON renderer and adds configuration directives: - config.add_json_renderer() - config.add_json_adapter()