renderer_json (class)

class renderer_json(info)[source]

Bases: pyramid.renderers.JSON

The json renderer - can return content to browser or a file to download

Methods

add_adapter(type_or_iface, adapter)

When an object of the type (or interface) type_or_iface fails to automatically encode using the serializer, the renderer will use the adapter adapter to convert it into a JSON-serializable object.

datetime_adapter(obj, request)

decimal_adapter(obj, request)

add_adapter(type_or_iface, adapter)[source]

When an object of the type (or interface) type_or_iface fails to automatically encode using the serializer, the renderer will use the adapter adapter to convert it into a JSON-serializable object. The adapter must accept two arguments: the object and the currently active request.

class Foo:
    x = 5

def foo_adapter(obj, request):
    return obj.x

renderer = JSON(indent=4)
renderer.add_adapter(Foo, foo_adapter)

When you’ve done this, the JSON renderer will be able to serialize instances of the Foo class when they’re encountered in your view results.