Well, `json` and the other modules could add another standard: `serialize` and `deserialize`
As an example, this is how I deserialize from a custom class:
def __init__(self, source): path = None
try: # most common case: JSON string self._data_raw = json.loads(source) except TypeError: try: # check if it's a dict-like object source.items self._data_raw = copy.deepcopy(source) except AttributeError: try: # maybe a file object? self._data_raw = json.load(f) except AttributeError: # maybe a PathLike? path = source except JSONDecodeError: # maybe a path string? path = source
if path: with open(path) as f: self._data_raw = json.load(f)
Apart the `dict` check, this logic could be applied to a `json.deserialize()` function. Python let you function overloading more simple, so why not use it?