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?
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/6KZ54WSNMPB3PQELLDZAQ27SLSJPEVNS/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to