Spaces:
Runtime error
Runtime error
File size: 786 Bytes
7288748 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import json
from abc import ABC, abstractmethod
from typing import Any
from pathlib import Path
class Serializer(ABC):
@abstractmethod
def dump(self, obj: Any, save_path: Path) -> None:
pass
@abstractmethod
def load(self, load_path: Path) -> Any:
pass
class JsonSerializer(Serializer):
def __init__(self,
sort_keys: bool = True,
indent: int = 4):
self.sort_keys = sort_keys
self.indent = indent
def dump(self, obj: Any, save_path: Path) -> None:
with open(save_path, "w") as file:
json.dump(obj, file, sort_keys=self.sort_keys, indent=self.indent)
def load(self, load_path: Path) -> Any:
with open(load_path, "r") as file:
return json.load(file) |