Python flask.json.JSONEncoder() Examples
The following are 9
code examples of flask.json.JSONEncoder().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
flask.json
, or try the search function
.
Example #1
Source File: app.py From api with BSD 3-Clause "New" or "Revised" License | 6 votes |
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)
Example #2
Source File: socket_comms.py From cstar_perf with Apache License 2.0 | 6 votes |
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()
Example #3
Source File: socket_comms.py From cstar_perf with Apache License 2.0 | 6 votes |
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))
Example #4
Source File: server.py From tensor2tensor with Apache License 2.0 | 5 votes |
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)
Example #5
Source File: api.py From babbage with MIT License | 5 votes |
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)
Example #6
Source File: api.py From babbage with MIT License | 5 votes |
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')
Example #7
Source File: server.py From BERT with Apache License 2.0 | 5 votes |
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)
Example #8
Source File: server.py From training_results_v0.5 with Apache License 2.0 | 5 votes |
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)
Example #9
Source File: app.py From clean-architecture with MIT License | 5 votes |
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