gufe.serialization.json¶
Functions
|
Create JSONEncoder/JSONDecoder for special types. |
|
|
|
|
|
|
|
|
This supports the python 3.13 convention TODO: remove when 3.13 support is dropped |
Classes
|
Custom JSON encoding and decoding for non-default types. |
|
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
Noneifis_my_objandis_my_dictare 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 theclsargument injson.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 asjson.dumps(obj, cls=encoder)
- decoder¶
subclass of
JSONDecoder; use asjson.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