<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def to_dict(jfile, key_path=None, in_memory=True, ignore_prefix=('.', '_'), parse_decimal=False):
 """ input json to dict Parameters jfile : str, file_like or path_like if str, must be existing file or folder, if file_like, must have 'read' method if path_like, must have 'iterdir' method (see pathlib.Path) key_path : list[str] a list of keys to index into the json before parsing it in_memory : bool if true reads full json into memory before filtering keys (this is faster but uses more memory) ignore_prefix : list[str] ignore folders beginning with these prefixes parse_decimal : bool whether to parse numbers as Decimal instances (retains exact precision) Examples -------- {'a': 1, 'b': [1.1, 2.1], 'c': {'d': 'e'}} {'a': 1, 'b': [Decimal('1.1'), Decimal('2.1')], 'c': {'d': 'e'}} 'e' dir1: dir2: dir3: """

    key_path = [] if key_path is None else key_path

    if isinstance(jfile, basestring):
        if not os.path.exists(jfile):
            raise IOError('jfile does not exist: {}'.format(jfile))
        if os.path.isdir(jfile):
            data = {}
            jpath = pathlib.Path(jfile)
            _folder_to_json(jpath, key_path[:], in_memory, ignore_prefix,
                            data, parse_decimal)
            if isinstance(list(data.keys())[0], _Terminus):
                data = data.values()[0]
        else:
            with open(jfile, 'r') as file_obj:
                if key_path and not in_memory:
                    data = _file_with_keys(file_obj, key_path, parse_decimal)
                elif key_path:
                    data = json.load(
                        file_obj, object_hook=decode,
                        parse_float=Decimal if parse_decimal else float)
                    data = indexes(data, key_path)
                else:
                    data = json.load(
                        file_obj, object_hook=decode,
                        parse_float=Decimal if parse_decimal else float)
    elif hasattr(jfile, 'read'):
        if key_path and not in_memory:
            data = _file_with_keys(jfile, key_path, parse_decimal)
        elif key_path:
            data = json.load(
                jfile, object_hook=decode,
                parse_float=Decimal if parse_decimal else float)
            data = indexes(data, key_path)
        else:
            data = json.load(
                jfile, object_hook=decode,
                parse_float=Decimal if parse_decimal else float)
    elif hasattr(jfile, 'iterdir'):
        if jfile.is_file():
            with jfile.open() as file_obj:
                if key_path and not in_memory:
                    data = _file_with_keys(file_obj, key_path, parse_decimal)
                elif key_path:
                    data = json.load(
                        file_obj, object_hook=decode,
                        parse_float=Decimal if parse_decimal else float)
                    data = indexes(data, key_path)
                else:
                    data = json.load(
                        file_obj, object_hook=decode,
                        parse_float=Decimal if parse_decimal else float)
        else:
            data = {}
            _folder_to_json(jfile, key_path[:], in_memory, ignore_prefix,
                            data, parse_decimal)
            if isinstance(list(data.keys())[0], _Terminus):
                data = data.values()[0]
    else:
        raise ValueError(
            'jfile should be a str, '
            'file_like or path_like object: {}'.format(jfile))

    return data