flask-profiler icon indicating copy to clipboard operation
flask-profiler copied to clipboard

TypeError: Object of type 'Resource' class is not JSON serializable

Open NiteshSaxena opened this issue 5 years ago • 0 comments
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)

NiteshSaxena avatar Dec 19 '19 08:12 NiteshSaxena