Team Ai
Apppublic

cactus183/patchbench-dev

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
buggy_code.py50 linesDownload Raw Back to hard_03
1def encode(obj):2    """Encode a Python object into a JSON string."""3    if obj is None:4        return "null"5    if obj is True:6        return "true"7    if obj is False:8        return "false"9    if isinstance(obj, int):10        return str(obj)11    if isinstance(obj, str):12        return '"' + obj + '"'  # BUG: doesn't escape internal quotes13    if isinstance(obj, list):14        return "[" + ",".join(encode(x) for x in obj) + "]"15    if isinstance(obj, dict):16        return "{" + ",".join(encode(k) + ":" + encode(v) for k, v in obj.items()) + "}"17    raise ValueError(f"Cannot encode: {obj}")18 19 20def decode(s):21    """Decode a JSON string into a Python object."""22    s = s.strip()23    if s == "null":24        return None25    if s == "true":26        return True27    if s == "false":28        return False29    if s.startswith('"') and s.endswith('"'):30        return s[1:-1]  # BUG: doesn't unescape backslash-escaped quotes31    if s.startswith("["):32        inner = s[1:-1]33        if not inner:34            return []35        return [decode(x) for x in inner.split(",")]  # BUG: naive split fails on nested36    if s.startswith("{"):37        inner = s[1:-1]38        if not inner:39            return {}40        items = inner.split(",")41        result = {}42        for item in items:43            k, v = item.split(":", 1)44            result[decode(k)] = decode(v)45        return result46    try:47        return int(s)48    except ValueError:49        raise ValueError(f"Cannot decode: {s}")50