def default(self, obj): # pylint: disable=method-hidden if hasattr(obj, "__json__") and six.callable(obj.__json__): return obj.__json__() elif isinstance(obj, bson.ObjectId): return str(obj) else: return JSONEncoder.default(self, obj)
def default(self, obj): ''' Converts an object and returns a ``JSON``-friendly structure. :param obj: object or structure to be converted into a ``JSON``-ifiable structure Considers the following special cases in order: * object has a callable __json__() attribute defined returns the result of the call to __json__() * date and datetime objects returns the object cast to str * Decimal objects returns the object cast to float * SQLAlchemy objects returns a copy of the object.__dict__ with internal SQLAlchemy parameters removed * SQLAlchemy ResultProxy objects Casts the iterable ResultProxy into a list of tuples containing the entire resultset data, returns the list in a dictionary along with the resultset "row" count. .. note:: {'count': 5, 'rows': [('Ed Jones',), ('Pete Jones',), ('Wendy Williams',), ('Mary Contrary',), ('Fred Smith',)]} * SQLAlchemy RowProxy objects Casts the RowProxy cursor object into a dictionary, probably losing its ordered dictionary behavior in the process but making it JSON-friendly. * webob_dicts objects returns webob_dicts.mixed() dictionary, which is guaranteed to be JSON-friendly. ''' if hasattr(obj, '__json__') and six.callable(obj.__json__): return obj.__json__() elif isinstance(obj, (date, datetime)): return str(obj) elif isinstance(obj, Decimal): # XXX What to do about JSONEncoder crappy handling of Decimals? # SimpleJSON has better Decimal encoding than the std lib # but only in recent versions return float(obj) elif is_saobject(obj): props = {} for key in obj.__dict__: if not key.startswith('_sa_'): props[key] = getattr(obj, key) return props elif isinstance(obj, ResultProxy): props = dict(rows=list(obj), count=obj.rowcount) if props['count'] < 0: props['count'] = len(props['rows']) return props elif isinstance(obj, RowProxy): return dict(obj) elif isinstance(obj, webob_dicts): return obj.mixed() else: return JSONEncoder.default(self, obj)
def default(self, obj): if hasattr(obj, '__json__') and callable(obj.__json__): return obj.__json__() elif isinstance(obj, (date, datetime)): return str(obj) elif isinstance(obj, Decimal): # XXX What to do about JSONEncoder crappy handling of Decimals? # SimpleJSON has better Decimal encoding than the std lib # but only in recent versions return float(obj) elif is_saobject(obj): props = {} for key in obj.__dict__: if not key.startswith('_sa_'): props[key] = getattr(obj, key) return props elif isinstance(obj, ResultProxy): props = dict(rows=list(obj), count=obj.rowcount) if props['count'] < 0: props['count'] = len(props['rows']) return props elif isinstance(obj, RowProxy): return dict(obj) elif isinstance(obj, (MultiDict, UnicodeMultiDict)): return obj.mixed() else: return JSONEncoder.default(self, obj)
def handler(self, obj): if isinstance(obj, Decimal): return ('%.' + str(self.round_decimal_places) + 'f') % obj elif isinstance(obj, date): return obj.strftime('%Y-%m-%d') elif isinstance(obj, datetime): return obj.strftime('%Y-%m-%d %H:%I:%S') return JSONEncoder.default(self, obj)
def default(self, obj): try: if isinstance(obj, datetime) or isinstance(obj, date): obj = obj.isoformat() iterable = iter(obj) except TypeError: pass else: return "".join(list(iterable)) return JSONEncoder.default(self, obj)
def default(self, obj): try: if isinstance(obj, Delivery) or isinstance(obj, Driver): return obj.to_dict() iterable = iter(obj) except TypeError: pass else: return list(iterable) return JSONEncoder.default(self, obj)
def default(self, o): # pylint: disable=method-hidden if isinstance(o, Decimal): return float(o) if isinstance(o, (datetime.date, Amount, Position)): return str(o) if isinstance(o, (set, frozenset)): return list(o) try: return JSONEncoder.default(self, o) except TypeError: return str(o)
def default(self, obj): if isinstance(obj, QuerySet): # TODO - follow relations. # See: https://code.djangoproject.com/ticket/4656 return loads(serialize('json', obj)) elif isinstance(obj, Decimal): return ('%.' + str(self.round_decimal_places) + 'f') % obj elif isinstance(obj, datetime): return obj.strftime('%Y-%m-%d %H:%I:%S') elif isinstance(obj, date): return obj.strftime('%Y-%m-%d') return JSONEncoder.default(self, obj)
def default(self, obj): if hasattr(obj, '__json__') and callable(obj.__json__): return obj.__json__() elif isinstance(obj, (datetime.date, datetime.datetime)): return str(obj) elif isinstance(obj, decimal.Decimal): return float(obj) elif isinstance(obj, MultiDict): return obj.mixed() elif isinstance(obj, Mapping): return dict(obj) elif isinstance(obj, uuid.UUID): return str(obj) else: return JSONEncoder.default(self, obj)
def default(self, o: Any) -> Any: # pylint: disable=too-many-return-statements if isinstance(o, Decimal): return float(o) if isinstance(o, (date, Amount, Position)): return str(o) if isinstance(o, (set, frozenset)): return list(o) if isinstance(o, Pattern): return o.pattern if is_dataclass(o): return {field.name: getattr(o, field.name) for field in fields(o)} try: return JSONEncoder.default(self, o) except TypeError: return str(o)
def default(self, obj): if hasattr(obj, '__json__'): return obj.__json__() elif isinstance(obj, (datetime.date, datetime.datetime)): return str(obj) elif isinstance(obj, decimal.Decimal): return float(obj) elif is_saobject(obj): props = {} for prop in object_mapper(obj).iterate_properties: if not hasattr(prop, 'uselist'): props[prop.key] = getattr(obj, prop.key) return props elif isinstance(obj, ResultProxy): return list(obj) elif isinstance(obj, RowProxy): return dict(obj) else: return JSONEncoder.default(self, obj)
def default(self, obj): if hasattr(obj, '__json__') and callable(obj.__json__): return obj.__json__() elif isinstance(obj, (datetime.date, datetime.datetime)): return str(obj) elif isinstance(obj, decimal.Decimal): return float(obj) elif is_saobject(obj): props = {} for key in obj.__dict__: if not key.startswith('_sa_'): props[key] = getattr(obj, key) return props elif isinstance(obj, ResultProxy): return dict(rows=list(obj), count=obj.rowcount) elif isinstance(obj, RowProxy): return dict(rows=dict(obj), count=1) elif isinstance(obj, MultiDict): return obj.mixed() else: return JSONEncoder.default(self, obj)
def default(self, o): _special_types = (datetime.date, datetime.time, datetime.datetime, decimal.Decimal) # We MUST check for a datetime.datetime instance before datetime.date. # datetime.datetime is a subclass of datetime.date, and therefore # instances of it are also instances of datetime.date. if isinstance(o, datetime.datetime) or o is datetime.datetime: if o is datetime.datetime: o = Null() return {'__datetime__': True, 'value': o.strftime(self.DATETIME_FMT)} if isinstance(o, datetime.date) or o is datetime.date: if o is datetime.date: o = Null() return {'__date__': True, 'value': o.strftime(self.DATE_FMT)} if isinstance(o, datetime.time) or o is datetime.time: if o is datetime.time: o = Null() return {'__time__': True, 'value': o.strftime(self.TIME_FMT)} if isinstance(o, decimal.Decimal) or o is decimal.Decimal: if o is decimal.Decimal: value = None else: value = unicode(o) return {'__decimal__': True, 'value': value} if isinstance(o, Null): return None else: return JSONEncoder.default(self, o)
def default(self, o): _special_types = (datetime.date, datetime.time, datetime.datetime, decimal.Decimal) # We MUST check for a datetime.datetime instance before datetime.date. # datetime.datetime is a subclass of datetime.date, and therefore # instances of it are also instances of datetime.date. if isinstance(o, datetime.datetime) or o is datetime.datetime: if o is datetime.datetime: o = Null() return {'__datetime__': True, 'value': o.strftime(self.DATETIME_FMT)} if isinstance(o, datetime.date) or o is datetime.date: if o is datetime.date: o = Null() return {'__date__': True, 'value': o.strftime(self.DATE_FMT)} if isinstance(o, datetime.time) or o is datetime.time: if o is datetime.time: o = Null() return {'__time__': True, 'value': o.strftime(self.TIME_FMT)} if isinstance(o, decimal.Decimal) or o is decimal.Decimal: if o is decimal.Decimal: value = None else: value = str(o) return {'__decimal__': True, 'value': value} if isinstance(o, Null): return None else: return JSONEncoder.default(self, o)
def default(self, o): if type(o) is Hit: d = {"__class__": Hit.__name__} d.update(o.__dict__) return d return JSONEncoder.default(self, o)
def default(self, o): if hasattr(o, "json"): return getattr(o, "json")() if isinstance(o, datetime.datetime): return o.ctime() return JSONEncoder.default(self, o)
def default(self, obj): if isinstance(obj, Transmittable): return obj.to_obj() return JSONEncoder.default(self, obj)
def default(self, o): if type(o) not in [str, unicode, dict, list]: return str(o) return JSONEncoder.default(self, o)
def default(self, obj): if isinstance(obj, datetime): return obj.isoformat(" ") return JSONEncoder.default(self, obj)
def default(self, obj): if isinstance(obj, JSONRPCError): return obj.__class__.__name__ else: return JSONEncoder.default(self, obj)
def default(self, obj): # pylint: disable=method-hidden if hasattr(obj, '__json__') and six.callable(obj.__json__): return obj.__json__() else: return JSONEncoder.default(self, obj)
def default(self, o): if hasattr(o, '__iter__'): return StreamArray(o) else: # pragma: no cover return JSONEncoder.default(self, o)
def default(self, o): if isinstance(o, ObjectId): return str(o) return JSONEncoder.default(self, o)
def default(self, obj): if isinstance(obj, datetime): return obj.isoformat(' ') return JSONEncoder.default(self, obj)