d3py icon indicating copy to clipboard operation
d3py copied to clipboard

self.vega.vega has <type 'numpy.int64'> included, so json.dumps failed!

Open honglei opened this issue 9 years ago • 1 comments

TypeError: 0 is not JSON serializable File "F:\0926\d3py-master\examples\d3py_vega_area.py", line 14, in fig += d3py.vega.Area() File "C:\Users\honglei\AppData\Local\Enthought\Canopy\User\Lib\site-packages\d3py-0.2.3-py2.7.egg\d3py\figure.py", line 197, in iadd self._add_vega(figure) File "C:\Users\honglei\AppData\Local\Enthought\Canopy\User\Lib\site-packages\d3py-0.2.3-py2.7.egg\d3py\figure.py", line 208, in _add_vega self.save_vega() File "C:\Users\honglei\AppData\Local\Enthought\Canopy\User\Lib\site-packages\d3py-0.2.3-py2.7.egg\d3py\figure.py", line 241, in save_vega vega = json.dumps(self.vega.vega, sort_keys=True, indent=4) File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json__init.py", line 250, in dumps sort_keys=sort_keys, **kw).encode(obj) File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 209, in encode chunks = list(chunks) File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 434, in _iterencode for chunk in _iterencode_dict(o, _current_indent_level): File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 408, in _iterencode_dict for chunk in chunks: File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 332, in _iterencode_list for chunk in chunks: File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 408, in _iterencode_dict for chunk in chunks: File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 332, in _iterencode_list for chunk in chunks: File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 408, in _iterencode_dict for chunk in chunks: File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 442, in _iterencode o = _default(o) File "C:\Users\honglei\AppData\Local\Enthought\Canopy\App\appdata\canopy-1.4.1.1975.win-x86_64\Lib\json\encoder.py", line 184, in default raise TypeError(repr(o) + " is not JSON serializable")

honglei avatar Sep 26 '14 15:09 honglei

import numpy
class NumpyAwareJSONEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.ndarray) and obj.ndim == 1:
            return obj.tolist()
        if isinstance(obj, numpy.integer):
            return int(obj)
        elif isinstance(obj, numpy.floating):
            return float(obj)
        return json.JSONEncoder.default(self, obj)

honglei avatar Sep 26 '14 16:09 honglei