json.dump(data, fp)
[docs]    def load(self) -> Any:
        with open(self.persist_path, "r") as fp:
            return json.load(fp)
[docs]class BsonSerializer(BaseSerializer):
    """Serializes data in binary json using the `bson` python package."""
[docs]    def __init__(self, persist_path: str) -> None:
        super().__init__(persist_path)
        self.bson = guard_import("bson")
[docs]    @classmethod
    def extension(cls) -> str:
        return "bson"
[docs]    def save(self, data: Any) -> None:
        with open(self.persist_path, "wb") as fp:
            fp.write(self.bson.dumps(data))
[docs]    def load(self) -> Any:
        with open(self.persist_path, "rb") as fp:
            return self.bson.loads(fp.read())
[docs]class ParquetSerializer(BaseSerializer):
    """Serializes data in `Apache Parquet` format using the `pyarrow` package."""
[docs]    def __init__(self, persist_path: str) -> None:
        super().__init__(persist_path)
        self.pd = guard_import("pandas")
        self.pa = guard_import("pyarrow")
        self.pq = guard_import("pyarrow.parquet")
[docs]    @classmethod
    def extension(cls) -> str:
        return "parquet"
[docs]    def save(self, data: Any) -> None:
        df = self.pd.DataFrame(data)
        table = self.pa.Table.from_pandas(df)
        if os.path.exists(self.persist_path):
            backup_path = str(self.persist_path) + "-backup"
            os.rename(self.persist_path, backup_path)