Python flask.json.JSONEncoder() Examples

The following are code examples for showing how to use flask.json.JSONEncoder(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: flasky   Author: RoseOu   File: widgets.py    MIT License 5 votes vote down vote up
def default(self, o):
            if isinstance(o, _LazyString):
                return str(o)
            return JSONEncoder.default(self, o) 
Example 2
Project: flask_restapi   Author: iwwxiong   File: renders.py    MIT License 5 votes vote down vote up
def render(self):
        data = {
            'status': {
                'code': self.code,
                'message': self.message
            },
            'data': self.data
        }
        return json.dumps(data, cls=JSONEncoder, ensure_ascii=False) 
Example 3
Project: oa_qian   Author: sunqb   File: widgets.py    Apache License 2.0 5 votes vote down vote up
def default(self, o):
            if isinstance(o, _LazyString):
                return str(o)
            return JSONEncoder.default(self, o) 
Example 4
Project: dbot-server   Author: ATNIO   File: metric.py    MIT License 5 votes vote down vote up
def default(self, obj):
        if isinstance(obj, bytes):
            return str(obj, encoding='utf-8');
        return json.JSONEncoder.default(self, obj) 
Example 5
Project: tensor2tensor   Author: tensorflow   File: server.py    Apache License 2.0 5 votes vote down vote up
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 6
Project: babbage   Author: openspending   File: api.py    MIT License 5 votes vote down vote up
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 7
Project: babbage   Author: openspending   File: api.py    MIT License 5 votes vote down vote up
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 8
Project: BERT   Author: yyht   File: server.py    Apache License 2.0 5 votes vote down vote up
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
Project: clean-architecture   Author: Enforcer   File: app.py    MIT License 5 votes vote down vote up
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 
Example 10
Project: warehouse-api   Author: arkhn   File: main.py    Apache License 2.0 5 votes vote down vote up
def default(self, o):
        if isinstance(o, ObjectId):
            return str(o)
        return json.JSONEncoder.default(self, o) 
Example 11
Project: warehouse-api   Author: arkhn   File: main.py    Apache License 2.0 5 votes vote down vote up
def create_app():
    app = Flask(__name__)
    app.register_blueprint(api, url_prefix='/api')
    app.json_encoder = JSONEncoder

    # load fhirstore at application startup
    db.get_store()
    # initialize models
    models.init()

    return app 
Example 12
Project: WRGameVideos-API   Author: thundernet8   File: widgets.py    GNU General Public License v2.0 5 votes vote down vote up
def default(self, o):
            if isinstance(o, _LazyString):
                return str(o)
            return JSONEncoder.default(self, o) 
Example 13
Project: pipa-pay-server   Author: davidvon   File: widgets.py    Apache License 2.0 5 votes vote down vote up
def default(self, o):
            if isinstance(o, _LazyString):
                return str(o)
            return JSONEncoder.default(self, o)