def deserialize_file_content()

in azext_edge/edge/util/file_operations.py [0:0]


def deserialize_file_content(file_path: str) -> Any:
    extension = file_path.split(".")[-1]
    valid_extension = extension in ["json", "yaml", "yml", "csv"]
    content = read_file_content(file_path)
    result = None
    if not valid_extension or extension == "json":
        # will always be a list or dict
        result = _try_loading_as(
            loader=json.loads, content=content, error_type=json.JSONDecodeError, raise_error=valid_extension
        )
    if (not result and not valid_extension) or extension in ["yaml", "yml"]:
        # can be list, dict, str, int, bool, none
        result = _try_loading_as(
            loader=yaml.safe_load, content=content, error_type=yaml.YAMLError, raise_error=valid_extension
        )
    if (not result and not valid_extension) or extension == "csv":
        # iterrable object so lets cast to list
        result = _try_loading_as(
            loader=csv.DictReader, content=content.splitlines(), error_type=csv.Error, raise_error=valid_extension
        )
    if result is not None or valid_extension:
        return result
    raise FileOperationError(f"File contents for {file_path} cannot be read.")