mikedewar / d3py

a plottling library for python, based on D3
1.42k stars 202 forks source link

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

Open honglei opened 10 years ago

honglei commented 10 years ago

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\jsoninit.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 commented 10 years ago
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)