py2json.tools

Higher-level tools to create python<->json codecs using py2json primitives.

This module provides a small Codec object and make_json_codec factory to wire py2json’s Ctor and fakit into a simple encode/decode interface. It also offers configurable “path protocols” for how dotted references are expressed.

Design goals: - Keep defaults simple: dotted paths resolved with fakit.dotpath_to_obj. - Support alternative path formats (e.g. colon-separated module:attr). - Use Ctor.deconstruct/Ctor.construct for python<->json conversions and

fakit.refakit for evaluating $fak expressions.

class py2json.tools.JsonCodec(*, func_loader: Callable[[Any], Callable] | None = None, path_parser: Callable[[str], str] | str | None = '.')[source]

A tiny codec that converts Python -> JSON-friendly dicts and back.

encode(obj) -> jsonable decode(j) -> python object

Options allow customizing the function loader used by refakit via func_loader and a path_parser to normalize string references.

decode(j: Any) Any[source]

Decode a JSON-friendly structure back into Python.

Behaviour: - If it’s a dict with the fak key (FAK), use refakit to evaluate it

(respecting the provided func_loader).

  • Otherwise, attempt to construct via Ctor.construct, which will handle nested ctor-jdicts produced by Ctor.deconstruct.

encode(obj: Any) Any[source]

Encode a Python object into a JSON-friendly structure.

For non-basic objects we delegate to Ctor.deconstruct(…, output_type=Ctor.JSON_DICT). Basic json types pass through unchanged.

py2json.tools.make_json_codec(*, func_loader: Callable[[Any], Callable] | None = None, path_parser: Callable[[str], str] | str | None = '.') JsonCodec[source]

Factory for a Codec configured with a func_loader and path protocol.

Parameters:
  • func_loader – Optional callable used by refakit to resolve function names.

  • path_protocol – ‘dot’ (default) or ‘colon’ — controls default path parsing.

  • path_parser – optional custom parser for string references.