flask-profiler
flask-profiler copied to clipboard
TypeError: Object of type 'Resource' class is not JSON serializable
trafficstars
I am creating a Flask Rest API using Flask RestPlus framework. In that I am trying to integrate the Flask Profiler and while running the api, it throws the following error. If I remove the Profile decorator it works fine but does not profile the resource.
TypeError: Object of type 'Resource' class is not JSON serializable
Below is the complete code. Can someone help me on this.
from flask import Flask, jsonify
from flask_restplus import Resource, Api
import flask_profiler as profiler
import json
app = Flask(__name__)
api = Api(app)
app.config["flask_profiler"] = {
"enabled": True,
"storage": {
"engine": "sqlite",
"file": "profiler.sqlite"
},
"basicAuth":{
"enabled": True,
"username": "admin",
"password": "admin"
},
"ignore": [
"^/static/.*"
]
}
profiler.init_app(app)
convert_obj_to_json_dict = lambda obj: obj.__dict__
class Person:
def __init__(self, firstname, lastname):
self.first_name = firstname
self.last_name = lastname
def toJSON(self):
return convert_obj_to_json_dict(self)
@api.route('/persons')
class PersonResource(Resource):
@profiler.profile()
def get(self):
persons = []
persons.append(Person("John", "Doe"))
persons.append(Person("Harry", "Walter"))
return [convert_obj_to_json_dict(person) for person in persons]
if __name__ == '__main__':
app.run(port=6000)