Skip to content

Commit

Permalink
Convert objects to json (#2050)
Browse files Browse the repository at this point in the history
  • Loading branch information
bkyryliuk authored Jan 27, 2017
1 parent 63bef2f commit f6ac95e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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

0 comments on commit f6ac95e

Please sign in to comment.