本文整理汇总了Python中flask.json.JSONEncoder方法的典型用法代码示例。如果您正苦于以下问题:Python json.JSONEncoder方法的具体用法?Python json.JSONEncoder怎么用?Python json.JSONEncoder使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类flask.json
的用法示例。
在下文中一共展示了json.JSONEncoder方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: default
# 需要导入模块: from flask import json [as 别名]
# 或者: from flask.json import JSONEncoder [as 别名]
def default(self, o):
if isinstance(o, datetime.datetime):
if o.tzinfo:
# eg: '2015-09-25T23:14:42.588601+00:00'
return o.isoformat("T")
else:
# No timezone present - assume UTC.
# eg: '2015-09-25T23:14:42.588601Z'
return o.isoformat("T") + "Z"
if isinstance(o, datetime.date):
return o.isoformat()
if isinstance(o, Decimal):
return float(o)
if isinstance(o, set):
return list(o)
return json.JSONEncoder.default(self, o)
示例2: respond
# 需要导入模块: from flask import json [as 别名]
# 或者: from flask.json import JSONEncoder [as 别名]
def respond(self, follow_up=True, **kwargs):
"""Send a response back to the peer
Normally, a response requires another response from the peer until done=True. If follow_up==False, then we will not look for a response yet. This is used in action='wait' responses where we are going to send multiple responses in a row to the peer.
"""
if self['type'] == 'response':
assert self['done'] == False, "Can't respond to a response that is already marked done."
data = {'command_id': self['command_id'], 'type':'response'}
data.update(kwargs)
if not data.has_key('done'):
data['done'] = False
data_str = json.dumps(data, cls=JSONEncoder)
log.debug("Sending response : {data}".format(data=data_str))
self.ws.send(data_str)
if data['done'] == False and follow_up:
# We are still expecting a response to our response:
return self.receive()
示例3: send
# 需要导入模块: from flask import json [as 别名]
# 或者: from flask.json import JSONEncoder [as 别名]
def send(self, await_response=True):
"""Send this command to our peer for them to execute"""
#In order to send a command, there needs to be a
#new_command==True flag on this command. Otherwise, this
#command will be assumed to have originated from our peer and
#should not be sent.
if not getattr(self, 'new_command', False):
raise AssertionError("Cannot send command that is not marked as new_command")
data = json.dumps(self, cls=JSONEncoder)
log.debug("Sending command : {data}".format(data=data))
self.ws.send(data)
if not await_response:
return
# Wait for the response:
response = json.loads(self.ws.receive())
log.debug("Received response : {response}".format(response=response))
if response.get('type') == 'response':
if response.get('command_id') == self['command_id']:
return Response(self.ws, response)
else:
raise AssertionError("Unexpected response id in : {stuff}".format(stuff=response))
else:
raise AssertionError("Was expecting a response, instead got {stuff}".format(stuff=response))
示例4: default
# 需要导入模块: from flask import json [as 别名]
# 或者: from flask.json import JSONEncoder [as 别名]
def default(self, obj):
obj_type = type(obj)
if obj_type in _NUMPY_INT_DTYPES:
return int(obj)
if obj_type in _NUMPY_FP_DTYPES:
return float(obj)
return json.JSONEncoder.default(self, obj)
示例5: default
# 需要导入模块: from flask import json [as 别名]
# 或者: from flask.json import JSONEncoder [as 别名]
def default(self, obj):
if isinstance(obj, date):
return obj.isoformat()
if isinstance(obj, Decimal):
return float(obj)
if isinstance(obj, set):
return [o for o in obj]
if map_is_class and isinstance(obj, map):
return [o for o in obj]
if hasattr(obj, 'to_dict'):
return obj.to_dict()
return json.JSONEncoder.default(self, obj)
示例6: jsonify
# 需要导入模块: from flask import json [as 别名]
# 或者: from flask.json import JSONEncoder [as 别名]
def jsonify(obj, status=200, headers=None):
""" Custom JSONificaton to support obj.to_dict protocol. """
data = JSONEncoder().encode(obj)
if 'callback' in request.args:
cb = request.args.get('callback')
data = '%s && %s(%s)' % (cb, cb, data)
return Response(data, headers=headers, status=status,
mimetype='application/json')
示例7: create_app
# 需要导入模块: from flask import json [as 别名]
# 或者: from flask.json import JSONEncoder [as 别名]
def create_app() -> Flask:
app = Flask(__name__)
app.json_encoder = JSONEncoder
app.register_blueprint(auctions_blueprint, url_prefix="/auctions")
app.register_blueprint(shipping_blueprint, url_prefix="/shipping")
# TODO: move this config
app.config["SECRET_KEY"] = "super-secret"
app.config["DEBUG"] = True
app.config["SECURITY_SEND_REGISTER_EMAIL"] = False
app.config["SECURITY_REGISTERABLE"] = True
app.config["SECURITY_PASSWORD_SALT"] = "99f885320c0f867cde17876a7849904c41a2b8120a9a9e76d1789e458e543af9"
app.config["WTF_CSRF_ENABLED"] = False
app_context = bootstrap_app()
FlaskInjector(app, modules=[AuctionsWeb()], injector=app_context.injector)
@app.before_request
def transaction_start() -> None:
request.tx = app_context.connection_provider.open().begin()
request.session = app_context.connection_provider.provide_session()
@app.after_request
def transaction_commit(response: Response) -> Response:
try:
if hasattr(request, "tx") and response.status_code < 400:
request.tx.commit()
finally:
app_context.connection_provider.close_if_present()
return response
# has to be after DB-hooks, because it relies on DB
security_setup(app)
return app