def from_json(cls, json_string: str = None, filename: Union[str, Path] = None, encoding: str = 'utf-8', errors: str = 'strict', **kwargs): """ Transform a json object string into a Box object. If the incoming json is a list, you must use BoxList.from_json. :param json_string: string to pass to `json.loads` :param filename: filename to open and pass to `json.load` :param encoding: File encoding :param errors: How to handle encoding errors :param kwargs: parameters to pass to `Box()` or `json.loads` :return: Box object from json data """ box_args = {} for arg in kwargs.copy(): if arg in BOX_PARAMETERS: box_args[arg] = kwargs.pop(arg) data = _from_json(json_string, filename=filename, encoding=encoding, errors=errors, **kwargs) if not isinstance(data, dict): raise BoxError( f'json data not returned as a dictionary, but rather a {type(data).__name__}' ) return cls(data, **box_args)
def from_json( cls, json_string: str = None, filename: Union[str, PathLike] = None, encoding: str = "utf-8", errors: str = "strict", multiline: bool = False, **kwargs, ): """ Transform a json object string into a BoxList object. If the incoming json is a dict, you must use Box.from_json. :param json_string: string to pass to `json.loads` :param filename: filename to open and pass to `json.load` :param encoding: File encoding :param errors: How to handle encoding errors :param multiline: One object per line :param kwargs: parameters to pass to `Box()` or `json.loads` :return: BoxList object from json data """ box_args = {} for arg in list(kwargs.keys()): if arg in BOX_PARAMETERS: box_args[arg] = kwargs.pop(arg) data = _from_json( json_string, filename=filename, encoding=encoding, errors=errors, multiline=multiline, **kwargs ) if not isinstance(data, list): raise BoxError(f"json data not returned as a list, but rather a {type(data).__name__}") return cls(data, **box_args)