polymerist.genutils.fileutils.jsonio

Utilities for extending the Python-JSON interface

Submodules

Attributes

JSONSerializable

Classes

JSONifiable

For type-hinting classes which are jsonifiable

TypeSerializer

Interface for defining how types which are not JSON serializable by default should be encoded and decoded

MultiTypeSerializer

For dynamically merging multiple TypeSerializer encoders and decoders

PathSerializer

For JSON-serializing OpenMM Quantities

QuantitySerializer

For JSON-serializing OpenMM Quantities

JSONDict

Dict subclass which also updates an underlying JSON file - effectively and on-disc dict

Functions

make_jsonifiable(→ C)

Modify a dataclass to make its attributes writeable-to and readable-from JSON files

append_to_json(→ None)

Add an entry to an existing JSON file

Package Contents

class polymerist.genutils.fileutils.jsonio.JSONifiable[source]

For type-hinting classes which are jsonifiable

polymerist.genutils.fileutils.jsonio.make_jsonifiable(cls: C | None = None, type_serializer: polymerist.genutils.fileutils.jsonio.serialize.TypeSerializer | polymerist.genutils.fileutils.jsonio.serialize.MultiTypeSerializer | None = None) C[source]

Modify a dataclass to make its attributes writeable-to and readable-from JSON files Can optionally specify additional TypeSerializers to support objects with attributes whose types are, by default, not JSON-serializable

polymerist.genutils.fileutils.jsonio.JSONSerializable
class polymerist.genutils.fileutils.jsonio.TypeSerializer[source]

Bases: abc.ABC

Interface for defining how types which are not JSON serializable by default should be encoded and decoded

python_type: ClassVar[Type[T]]
static encode(python_obj: T) JSONSerializable[source]
Abstractmethod:

static decode(json_obj: JSONSerializable) T[source]
Abstractmethod:

classmethod encoder_default(python_obj: Any) JSONSerializable[source]

Augmented Encoder for encoding registered objects along with type info for decoding

classmethod decoder_hook(json_dict: dict[JSONSerializable, JSONSerializable]) dict | T[source]
class polymerist.genutils.fileutils.jsonio.MultiTypeSerializer(*type_sers: tuple[Type[TypeSerializer]])[source]

For dynamically merging multiple TypeSerializer encoders and decoders

property type_sers: list[Type[TypeSerializer]]

Read-only wrapper for the internal registry of TypeSerializers

add_type_serializer(obj: TypeSerializer | MultiTypeSerializer) None[source]

For type, instance, and uniqueness checking of Type

encoder_default(python_obj: Any) JSONSerializable[source]
decoder_hook(json_dict: dict[JSONSerializable, JSONSerializable]) Any[source]
class polymerist.genutils.fileutils.jsonio.PathSerializer[source]

Bases: TypeSerializer

For JSON-serializing OpenMM Quantities

static encode(python_obj: pathlib.Path) str[source]

Stringify path object

static decode(json_obj: str) pathlib.Path[source]

Resolve string into system Path

class polymerist.genutils.fileutils.jsonio.QuantitySerializer[source]

Bases: TypeSerializer

For JSON-serializing OpenMM Quantities

static encode(python_obj: openmm.unit.Quantity) dict[str, str | float][source]

Separate openmm.unit.Quantity’s value and units to serialize as a single dict

static decode(json_obj: dict[str, str | float]) openmm.unit.Quantity[source]

Unpack a value-unit string dict back into a usable openmm.unit.Quantity

polymerist.genutils.fileutils.jsonio.append_to_json(json_path: pathlib.Path, **kwargs) None[source]

Add an entry to an existing JSON file

class polymerist.genutils.fileutils.jsonio.JSONDict(json_path: pathlib.Path, *args, **kwargs)[source]

Bases: dict

Dict subclass which also updates an underlying JSON file - effectively and on-disc dict !NOTE! - JSON doesn’t support non-string keys, so all keys given will be stringified - plan accordingly!

json_path: pathlib.Path