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

Issues with Date JSON Serialization #486

Closed
JohnOmernik opened this issue May 18, 2016 · 2 comments · Fixed by #492
Closed

Issues with Date JSON Serialization #486

JohnOmernik opened this issue May 18, 2016 · 2 comments · Fixed by #492

Comments

@JohnOmernik
Copy link

I am having an issue where Python reports a circular reference.

I believe the the issue comes up here:

def json_iso_dttm_ser(obj):
"""
json serializer that deals with dates
>>> dttm = datetime(1970, 1, 1)
>>> json.dumps({'dttm': dttm}, default=json_iso_dttm_ser)
'{"dttm": "1970-01-01T00:00:00"}'
"""
if isinstance(obj, datetime):
obj = obj.isoformat()
return obj

(in utils.py)

Based on http://stackoverflow.com/questions/14249115/serializing-output-to-json-valueerror-circular-reference-detected

My data I am querying my data source directly (this is the exact query that caravel is trying to run) I get these results:

trans_day is selected as a dttm column... I am just confused at why this cirucular reference is coming up...

+-------------+--------+
| trans_day | cnt |
+-------------+--------+
| 2015-06-02 | 13265 |
| 2015-01-06 | 13031 |
| 2015-05-19 | 12875 |
| 2015-06-16 | 12746 |
| 2015-05-28 | 12724 |
| 2015-05-26 | 12692 |
| 2015-04-21 | 12617 |
| 2015-06-03 | 12410 |
| 2015-05-05 | 12317 |
| 2015-04-07 | 12308 |
+-------------+--------+

The full stack trace is:

Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in call
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functionsrule.endpoint
File "/usr/local/lib/python2.7/dist-packages/flask_appbuilder/security/decorators.py", line 26, in wraps
return f(self, _args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/caravel/models.py", line 1294, in wrapper
return f(_args, *_kwargs)
File "/usr/local/lib/python2.7/dist-packages/caravel/views.py", line 557, in explore
payload = obj.get_json()
File "/usr/local/lib/python2.7/dist-packages/caravel/viz.py", line 269, in get_json
return self.json_dumps(payload)
File "/usr/local/lib/python2.7/dist-packages/caravel/viz.py", line 375, in json_dumps
return json.dumps(obj, default=utils.json_iso_dttm_ser)
File "/usr/lib/python2.7/json/init.py", line 251, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
ValueError: Circular reference detected

@JohnOmernik
Copy link
Author

So it doesn't seem to matter whether the grouped column (trans_day) is marked as dttm column. It has the same error no matter what. So It must be auto detected the YYYY-MM-DD format and trying to do something with it?

@xrmx
Copy link
Contributor

xrmx commented May 19, 2016

Same here with 0.9.0, i got this on a new project when opening the page to create a new slice (http://127.0.0.1:8088/caravel/explore/table/3/)

I've added this to help debugging:

diff --git a/caravel/utils.py b/caravel/utils.py
index 7e3d4c0..f117576 100644
--- a/caravel/utils.py
+++ b/caravel/utils.py
@@ -221,6 +221,10 @@ def json_iso_dttm_ser(obj):
     """
     if isinstance(obj, datetime):
         obj = obj.isoformat()
+    else:
+        raise TypeError(
+             "Unserializable object {} of type {}".format(obj, type(obj))
+        )
     return obj


It explodes with this:

TypeError: Unserializable object 0 of type <class 'numpy.int64'>

This how my data is structured:

    ('metrica', 'TEXT'),
    ('disciplina', 'TEXT'),
    ('opera', 'TEXT'),
    ('casa_editrice', 'TEXT'),
    ('tipologia', 'TEXT'),
    ('data', 'DATE'),
    ('scuola', 'TEXT'),
    ('tipologia_utente', 'TEXT'),
    ('provincia', 'TEXT'),
    ('nazione', 'TEXT')

xrmx added a commit to xrmx/superset that referenced this issue May 19, 2016
It looks like COUNT(*) returns a numpy.int64 value that the
default JSONEncoder does not handle.

While at if we get a type we are not handling make it easier to
debug the issue by throwing a TypeError exception with useful
data.

Fix apache#486
mistercrunch pushed a commit that referenced this issue May 20, 2016
It looks like COUNT(*) returns a numpy.int64 value that the
default JSONEncoder does not handle.

While at if we get a type we are not handling make it easier to
debug the issue by throwing a TypeError exception with useful
data.

Fix #486
zhaoyongjie pushed a commit to zhaoyongjie/incubator-superset that referenced this issue Nov 17, 2021
zhaoyongjie pushed a commit to zhaoyongjie/incubator-superset that referenced this issue Nov 24, 2021
zhaoyongjie pushed a commit to zhaoyongjie/incubator-superset that referenced this issue Nov 25, 2021
zhaoyongjie pushed a commit to zhaoyongjie/incubator-superset that referenced this issue Nov 26, 2021
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

Successfully merging a pull request may close this issue.

2 participants