本文整理汇总了Python中flask.json.JSONEncoder类的典型用法代码示例。如果您正苦于以下问题:Python JSONEncoder类的具体用法?Python JSONEncoder怎么用?Python JSONEncoder使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JSONEncoder类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: default
def default(self, obj):
if isinstance(obj, User):
dic={'login':obj.login,'p':obj.p,'name':obj.name,'logged':obj.logged}
return dic
if isinstance(obj, Repository):
dic={'org':obj.org,'name':obj.name,'directory':obj.directory,'json_exists':obj.json_exists,'exists':obj.exists,'content':obj.content,'file_name':obj.file_name}
return dic
else:
JSONEncoder.default(self, obj)
示例2: default
def default(self, obj):
if isinstance(obj, date):
return dateutil.simple_format_date(obj)
elif isinstance(obj, datetime):
return dateutil.simple_format_datetime(obj)
elif isinstance(obj, Iterable) and not isinstance(obj, (dict, list, set)):
# 这个实现可能有问题
return JSONEncoder.default(self, list(obj))
return JSONEncoder.default(self, obj)
示例3: default
def default(self, obj):
from datatypes import User
app.logger.debug("running custom jsonencoder")
if isinstance(obj, User):
# Implement code to convert Passport object to a dict
user_dict = {"name": obj.displayName(), "userID": obj.userID, "isAdmin": obj.isAdmin,
"isMCRMember": obj.isMCRMember,
"isAssociateMember": obj.isAssociateMember, "isCRA": obj.isCRA,
"isCollegeBill": obj.isCollegeBill}
return user_dict
else:
JSONEncoder.default(self, obj)
示例4: default
def default(self, obj):
try:
if isinstance(obj, date):
return JSONEncoder.default(
self,
datetime(obj.year, obj.month, obj.day))
iterable = iter(obj)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, obj)
示例5: default
def default(self, obj):
if isinstance(obj, ObjectId):
return json_util.dumps(obj)
elif isinstance(obj, datetime.datetime):
return calendar.timegm(obj.utctimetuple())*1000
else:
return JSONEncoder.default(self, obj)
示例6: default
def default(self, obj):
try:
if isinstance(obj, datetime):
if obj.utcoffset() is not None:
obj = obj - obj.utcoffset()
millis = int(
calendar.timegm(obj.timetuple()) * 1000 +
obj.microsecond / 1000
)
return millis
if isinstance(obj, sqlalchemy.orm.state.InstanceState):
return ""
if isinstance(obj, ScannerStatistic):
return ""
if isinstance(obj, ScannerLocation):
return ""
iterable = iter(obj)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, obj)
示例7: default
def default(self, o):
if isinstance(o, PersistentID):
return o.hex
elif isinstance(o, Enum):
return o.name
else:
return FlaskJSONEncoder.default(self, o)
示例8: default
def default(self, obj):
try:
if isinstance(obj, datetime):
return serialize_datatime(obj)
except TypeError:
pass
return JSONEncoder.default(self, obj)
示例9: default
def default(self, obj):
if isinstance(obj, Character):
return obj.__dict__
elif isinstance(obj, Weapon):
return obj.__dict__
else:
return JSONEncoder.default(self, obj)
示例10: default
def default(self, obj):
try:
if isinstance(obj, datetime):
if obj.utcoffset() is not None:
obj = obj - obj.utcoffset()
millis = int(
calendar.timegm(obj.timetuple()) * 1000 +
obj.microsecond / 1000
)
return millis
if isinstance(obj, date):
millis = int(
calendar.timegm(obj.timetuple()) * 1000
)
return millis
if isinstance(obj, np.int64):
val = int(obj)
return val
iterable = iter(obj)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, obj)
示例11: default
def default(self, o):
if isinstance(o, Segment):
return {
'id': o.id,
'text_id': o.text_id,
'slug': o.slug,
'content': xml_transform(o.content),
}
elif isinstance(o, Text):
return {
'id': o.id,
'name': o.name,
'slug': o.slug,
'xmlid_prefix': o.xmlid_prefix
}
elif isinstance(o, Card):
return {
'primary': self.default(o.primary),
'translations': [self.default(x) for x in o.translations],
'commentaries': [self.default(x) for x in o.commentaries],
}
elif isinstance(o, ChildCard):
return {
'text_id': o.text_id,
'segments': [self.default(x) for x in o.segments],
}
return JSONEncoder.default(self, o)
示例12: default
def default(self, obj):
if isinstance(obj, datetime):
if obj.utcoffset() is not None:
obj = obj - obj.utcoffset()
return str(obj)
else:
return JSONEncoder.default(self, obj)
示例13: default
def default(self, o): # pylint: disable=method-hidden
"""Convert unserializable types for JSON encoding."""
if isinstance(o, datetime):
# Stuffr uses ISO dates
return o.isoformat()
return JSONEncoder.default(self, o)
示例14: default
def default(self, obj):
if hasattr(obj, '__getitem__') and hasattr(obj, 'keys'):
return dict(obj)
if isinstance(obj, datetime.datetime):
return obj.strftime('%Y-%m-%d %H:%M:%S')
if isinstance(obj, ObjectId):
return str(obj)
return _JSONEncoder.default(self, obj)
示例15: default
def default(self, o):
if isinstance(o, datetime.date):
if isinstance(o, datetime.datetime):
return str(o.replace(microsecond=0))
return str(o)
if hasattr(o, 'as_dict'):
return o.as_dict()
return JSONEncoder.default(self, o)