Skip to content

encoders ¤

This module contains data encoders/serializers and decoders/deserializers.

The available formats are:

Classes:

Functions:

JSONEncoder ¤

JSONEncoder(
    *args: Any,
    full: bool = False,
    docstring_parser: Parser | None = None,
    docstring_options: dict[str, Any] | None = None,
    **kwargs: Any
)

          flowchart TD
          griffe.encoders.JSONEncoder[JSONEncoder]

          

          click griffe.encoders.JSONEncoder href "" "griffe.encoders.JSONEncoder"
          

JSON encoder.

JSON encoders can be used directly, or through the json.dump or json.dumps methods.

Examples:

>>> from griffe.encoders import JSONEncoder
>>> JSONEncoder(full=True).encode(..., **kwargs)
>>> import json
>>> from griffe.encoders import JSONEncoder
>>> json.dumps(..., cls=JSONEncoder, full=True, **kwargs)

Parameters:

  • *args (Any, default: () ) –
  • full (bool, default: False ) –

    Whether to dump full data or base data. If you plan to reload the data in Python memory using the json_decoder, you don't need the full data as it can be infered again using the base data. If you want to feed a non-Python tool instead, dump the full data.

  • docstring_parser (Parser | None, default: None ) –

    Deprecated. The docstring parser to use. By default, no parsing is done.

  • docstring_options (dict[str, Any] | None, default: None ) –

    Deprecated. Additional docstring parsing options.

  • **kwargs (Any, default: {} ) –

Methods:

  • default

    Return a serializable representation of the given object.

default ¤

default(obj: Any) -> Any

Return a serializable representation of the given object.

Parameters:

  • obj (Any) –

    The object to serialize.

Returns:

  • Any

    A serializable representation.

json_decoder ¤

json_decoder(
    obj_dict: dict[str, Any]
) -> (
    dict[str, Any] | Object | Alias | Parameter | str | Expr
)

Decode dictionaries as data classes.

The json.loads method walks the tree from bottom to top.

Examples:

>>> import json
>>> from griffe.encoders import json_decoder
>>> json.loads(..., object_hook=json_decoder)

Parameters:

  • obj_dict (dict[str, Any]) –

    The dictionary to decode.

Returns: