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

Convert objects to json #2050

Merged
merged 1 commit into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions superset/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
import functools
import json
import logging
import numpy
import pickle
import re
import six
import textwrap
from copy import deepcopy, copy
from datetime import timedelta, datetime, date
Expand Down Expand Up @@ -789,6 +791,18 @@ def get_df(self, sql, schema):
cur = eng.execute(sql, schema=schema)
cols = [col[0] for col in cur.cursor.description]
df = pd.DataFrame(cur.fetchall(), columns=cols)

def needs_conversion(df_series):
if df_series.empty:
return False
for df_type in [list, dict]:
if isinstance(df_series[0], df_type):
return True
return False

for k, v in df.dtypes.iteritems():
if v.type == numpy.object_ and needs_conversion(df[k]):
df[k] = df[k].apply(utils.json_dumps_w_dates)
return df

def compile_sqla_query(self, qry, schema=None):
Expand Down
4 changes: 4 additions & 0 deletions superset/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,10 @@ def json_int_dttm_ser(obj):
return obj


def json_dumps_w_dates(payload):
return json.dumps(payload, default=json_int_dttm_ser)


def error_msg_from_exception(e):
"""Translate exception into error message

Expand Down