Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
honglei opened this issue Sep 26, 2014 · 1 comment
Open

Comments

@honglei
Copy link

honglei commented Sep 26, 2014

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
Copy link
Author

honglei commented Sep 26, 2014

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant