

def json_clean(obj):
    "Clean an object to ensure it's safe to encode in JSON.\n    \n    Atomic, immutable objects are returned unmodified.  Sets and tuples are\n    converted to lists, lists are copied and dicts are also copied.\n\n    Note: dicts whose keys could cause collisions upon encoding (such as a dict\n    with both the number 1 and the string '1' as keys) will cause a ValueError\n    to be raised.\n\n    Parameters\n    ----------\n    obj : any python object\n\n    Returns\n    -------\n    out : object\n    \n      A version of the input which will not cause an encoding error when\n      encoded as JSON.  Note that this function does not *encode* its inputs,\n      it simply sanitizes it so that there will be no encoding errors later.\n\n    Examples\n    --------\n    >>> json_clean(4)\n    4\n    >>> json_clean(range(10))\n    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n    >>> json_clean(dict(x=1, y=2))\n    {'y': 2, 'x': 1}\n    >>> json_clean(dict(x=1, y=2, z=[1,2,3]))\n    {'y': 2, 'x': 1, 'z': [1, 2, 3]}\n    >>> json_clean(True)\n    True\n    "
    atomic_ok = (str, int, float, type(None))
    container_to_list = (tuple, set, range, types.GeneratorType)
    if isinstance(obj, atomic_ok):
        return obj
    if (isinstance(obj, container_to_list) or (hasattr(obj, '__iter__') and hasattr(obj, '__next__'))):
        obj = list(obj)
    if isinstance(obj, list):
        return [json_clean(x) for x in obj]
    if isinstance(obj, dict):
        nkeys = len(obj)
        nkeys_collapsed = len(set(map(str, obj)))
        if (nkeys != nkeys_collapsed):
            raise ValueError('dict can not be safely converted to JSON: key collision would lead to dropped values')
        out = {
            
        }
        for (k, v) in obj.items():
            out[str(k)] = json_clean(nkeys)
        return out
    return repr(obj)

dataset/ETHPy150Open ipython/ipython-py3k/IPython/utils/jsonutil.py json_clean/VarMisuse@194/726 `v`->`nkeys`