pylayerstates.utils.json

This module provides a base interface for JSON serialization and deserialization operations. It defines an abstract base class IJsonOp that implements common JSON/YAML file I/O operations and requires concrete classes to implement the actual serialization logic.

The module supports both JSON and YAML formats for data persistence, with methods for reading from and writing to files in either format.

IJsonOp

class pylayerstates.utils.json.IJsonOp[source]

An interface class that provides JSON serialization/deserialization capabilities.

This class defines a common interface for objects that need to be serialized to and deserialized from JSON/YAML formats. Concrete classes must implement the _to_json() and _from_json() methods.

Example:
>>> class MyData(IJsonOp):
...     def _to_json(self):
...         return {"data": self.data}
...
...     @classmethod
...     def _from_json(cls, data):
...         return cls(data["data"])
classmethod from_json(data)[source]

Create an instance from JSON data.

Parameters:

data (dict) – JSON-formatted data

Returns:

An instance of the class

Return type:

IJsonOp

Raises:

TypeError – If the created object is not an instance of the class

property json

Get the JSON representation of the object.

Returns:

JSON-serializable representation of the object

Return type:

dict

classmethod read_json(json_file)[source]

Create an instance by reading from a JSON file.

Parameters:

json_file (str) – Path to the input JSON file

Returns:

An instance of the class

Return type:

IJsonOp

classmethod read_yaml(yaml_file)[source]

Create an instance by reading from a YAML file.

Parameters:

yaml_file (str) – Path to the input YAML file

Returns:

An instance of the class

Return type:

IJsonOp

to_json(json_file)[source]

Save the object to a JSON file.

Parameters:

json_file (str) – Path to the output JSON file

to_yaml(yaml_file)[source]

Save the object to a YAML file.

Parameters:

yaml_file (str) – Path to the output YAML file