Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
NiteshSaxena opened this issue Dec 19, 2019 · 0 comments
Open

Comments

@NiteshSaxena
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant