Skip to content

Mixins

Set of mixins for Models and Fields.

JsonMixin

Contains the methods for converting Fields to JSON and back.

Source code in src\ramifice\utils\mixins.py
class JsonMixin:
    """Contains the methods for converting Fields to JSON and back."""

    def to_dict(self) -> dict[str, Any]:
        """Convert object instance to a dictionary."""
        json_dict: dict[str, Any] = {}
        for name, data in self.__dict__.items():
            if not callable(data):
                json_dict[name] = data
        return json_dict

    def to_json(self) -> str:
        """Convert object instance to a JSON string."""
        return orjson.dumps(self.to_dict()).decode("utf-8")

    @classmethod
    def from_dict(cls, json_dict: dict[str, Any]) -> Any:
        """Convert JSON string to a object instance."""
        obj = cls()
        for name, data in json_dict.items():
            obj.__dict__[name] = data
        return obj

    @classmethod
    def from_json(cls, json_str: str) -> Any:
        """Convert JSON string to a object instance."""
        json_dict = orjson.loads(json_str)
        return cls.from_dict(json_dict)

from_dict(json_dict) classmethod

Convert JSON string to a object instance.

Source code in src\ramifice\utils\mixins.py
@classmethod
def from_dict(cls, json_dict: dict[str, Any]) -> Any:
    """Convert JSON string to a object instance."""
    obj = cls()
    for name, data in json_dict.items():
        obj.__dict__[name] = data
    return obj

from_json(json_str) classmethod

Convert JSON string to a object instance.

Source code in src\ramifice\utils\mixins.py
@classmethod
def from_json(cls, json_str: str) -> Any:
    """Convert JSON string to a object instance."""
    json_dict = orjson.loads(json_str)
    return cls.from_dict(json_dict)

to_dict()

Convert object instance to a dictionary.

Source code in src\ramifice\utils\mixins.py
def to_dict(self) -> dict[str, Any]:
    """Convert object instance to a dictionary."""
    json_dict: dict[str, Any] = {}
    for name, data in self.__dict__.items():
        if not callable(data):
            json_dict[name] = data
    return json_dict

to_json()

Convert object instance to a JSON string.

Source code in src\ramifice\utils\mixins.py
def to_json(self) -> str:
    """Convert object instance to a JSON string."""
    return orjson.dumps(self.to_dict()).decode("utf-8")