gufe.serialization.json

Functions

custom_json_factory(coding_methods)

Create JSONEncoder/JSONDecoder for special types.

default_from_dict(dct)

inherited_is_my_dict(dct, cls)

is_npy_dtype_dict(dct)

is_openff_quantity_dict(dct)

is_openff_unit_dict(dct)

is_python_313_path_dict(dct)

This supports the python 3.13 convention TODO: remove when 3.13 support is dropped

Classes

JSONCodec(cls, to_dict, from_dict[, ...])

Custom JSON encoding and decoding for non-default types.

JSONSerializerDeserializer(codecs)

Tools to serialize and deserialize objects as JSON.

class gufe.serialization.json.JSONCodec(cls: type | None, to_dict: Callable[[Any], dict], from_dict: Callable[[dict], Any], is_my_obj: Callable[[Any], bool] | None = None, is_my_dict=None)

Custom JSON encoding and decoding for non-default types.

Parameters:
  • cls (class) – Class for this codec. Assumes that all subclasses should be treated the same way. Can be None if is_my_obj and is_my_dict are given.

  • to_dict (Callable) – method that converts the object to a dictionary

  • from_dict (Callable) – method that restores the object based on the dictionary made by to_dict

  • is_my_obj (Optional[Callable]) – Method to determine whether the input object should be treated by this encoder. Default behavior is to use isinstance(cls), and to create a dict that also includes the class name and the module of the object.

  • is_my_dict (Optional[Callable]) – Method to determine whether the input dictionary should be treated by this decoder. Default behavior assumes usage of the default is_my_obj.

default(obj: Any) Any
object_hook(dct: dict) Any
gufe.serialization.json.custom_json_factory(coding_methods: Iterable[JSONCodec]) tuple[type[JSONEncoder], type[JSONDecoder]]

Create JSONEncoder/JSONDecoder for special types.

Factory method. Dynamically creates classes that enable all the provided coding_methods. Returns classes, not instances, as classes are used by the cls argument in json.loads / json.dumps.

Parameters:

coding_methods (Iterable[JSONCodec]) – codecs to use

Returns:

subclasses of JSONEncoder/JSONDecoder that use support the provided codecs

Return type:

Tuple[Type[JSONEncoder], Type[JSONDecoder]]

class gufe.serialization.json.JSONSerializerDeserializer(codecs: Iterable[JSONCodec])

Tools to serialize and deserialize objects as JSON.

This wrapper object is necessary so that we can register new codecs after the original initialization.

encoder

subclass of JSONEncoder; use as json.dumps(obj, cls=encoder)

decoder

subclass of JSONDecoder; use as json.loads(string, cls=decoder)

Parameters:

codecs (list of JSONCodecs) – codecs supported

add_codec(codec: JSONCodec)

Add a new codec to the supported codecs

Parameters:

codec (JSONCodec) – codec to add

serializer(obj: Any) str

Callable that dumps to JSON

deserializer(string: str) Any

Callable to loads JSON

gufe.serialization.json.default_from_dict(dct)
gufe.serialization.json.inherited_is_my_dict(dct, cls)
gufe.serialization.json.is_npy_dtype_dict(dct)
gufe.serialization.json.is_openff_unit_dict(dct)
gufe.serialization.json.is_openff_quantity_dict(dct)
gufe.serialization.json.is_python_313_path_dict(dct: dict) bool

This supports the python 3.13 convention TODO: remove when 3.13 support is dropped