Weird TypeError from json.dumps
Question: In python 3.4.0, using `json.dumps()` throws me a TypeError in one case but
works like a charm in other case (which I think is equivalent to the first
one).

I have a dict where keys are strings and values are numbers and other dicts
(i.e. something like `{'x': 1.234, 'y': -5.678, 'z': {'a': 4, 'b': 0, 'c':
-6}}`).

This fails (the stacktrace is not from this particular code snippet but from
my larger script which I won't paste here but it is essentialy the same):

    
    
    >>> x = dict(foo()) # obtain the data and make a new dict of it to really be sure
    >>> import json
    >>> json.dumps(x)
    Traceback (most recent call last):
      File "/mnt/data/gandalv/progs/pycharm-3.4/helpers/pydev/pydevd.py", line 1733, in <module>
        debugger.run(setup['file'], None, None)
      File "/mnt/data/gandalv/progs/pycharm-3.4/helpers/pydev/pydevd.py", line 1226, in run
        pydev_imports.execfile(file, globals, locals)  # execute the script
      File "/mnt/data/gandalv/progs/pycharm-3.4/helpers/pydev/_pydev_execfile.py", line 38, in execfile
        exec(compile(contents+"\n", file, 'exec'), glob, loc) #execute the script
      File "/mnt/data/gandalv/School/PhD/Other work/Krachy/code/recalculate.py", line 54, in <module>
        ls[1] = json.dumps(f)
      File "/usr/lib/python3.4/json/__init__.py", line 230, in dumps
        return _default_encoder.encode(obj)
      File "/usr/lib/python3.4/json/encoder.py", line 192, in encode
        chunks = self.iterencode(o, _one_shot=True)
      File "/usr/lib/python3.4/json/encoder.py", line 250, in iterencode
        return _iterencode(o, 0)
      File "/usr/lib/python3.4/json/encoder.py", line 173, in default
        raise TypeError(repr(o) + " is not JSON serializable")
    TypeError: 306 is not JSON serializable
    

The `306` is one of the values in one of ther inner dicts in `x`. It is not
always the same number, sometimes it is a different number contained in the
dict, apparently because of the unorderedness of a dict.

However, this works like a charm:

    
    
    >>> x = foo() # obtain the data and make a new dict of it to really be sure
    >>> import ast
    >>> import json
    >>> x2 = ast.literal_eval(repr(x))
    >>> x == x2
    True
    >>> json.dumps(x2)
    "{...}" # the json representation of dict as it should be
    

Could anyone, please, tell me why does this happen or what could be the cause?
The most confusing part is that those two dicts (the original one and the one
obtained through evaluation of the representation of the original one) are
equal but the `dumps()` function behaves differently for each of them.


Answer: The cause was that the numbers inside the `dict` were not ordinary python
`int`s but `numpy.in64`s which are apparently not supported by the json
encoder.

