turbo_broccoli.custom.generic

Serialization of so-called generic object. See turbo_broccoli.generic.to_json.

 1"""
 2Serialization of so-called generic object. See
 3`turbo_broccoli.generic.to_json`.
 4"""
 5
 6from typing import Any, Iterable
 7
 8from ..context import Context
 9from ..exceptions import TypeNotSupported
10
11
12def to_json(obj: Any, ctx: Context) -> dict:
13    """
14    Serializes a generic object into JSON. The return document contains all
15    attributes listed in the object's `__turbo_broccoli__` attribute.
16    """
17    if not (
18        hasattr(obj, "__turbo_broccoli__")
19        and isinstance(obj.__turbo_broccoli__, Iterable)
20    ):
21        raise TypeNotSupported()
22    return {k: getattr(obj, k) for k in obj.__turbo_broccoli__}
def to_json(obj: Any, ctx: turbo_broccoli.context.Context) -> dict:
13def to_json(obj: Any, ctx: Context) -> dict:
14    """
15    Serializes a generic object into JSON. The return document contains all
16    attributes listed in the object's `__turbo_broccoli__` attribute.
17    """
18    if not (
19        hasattr(obj, "__turbo_broccoli__")
20        and isinstance(obj.__turbo_broccoli__, Iterable)
21    ):
22        raise TypeNotSupported()
23    return {k: getattr(obj, k) for k in obj.__turbo_broccoli__}

Serializes a generic object into JSON. The return document contains all attributes listed in the object's __turbo_broccoli__ attribute.