1
from flask import Flask, request, abort
from flask_restful import Api, Resource
import jsonpickle

app = Flask(__name__)
api = Api(app)

user_dict = {}
user_id = 0

class User(Resource):
    @staticmethod
    def get(path_user_id):
        if path_user_id not in user_dict:
            abort(400)

        test = jsonpickle.encode(user_dict.get(path_user_id), unpicklable=False)

        return jsonpickle.encode(user_dict.get(path_user_id), unpicklable=False)

    @staticmethod
    def put(path_user_id):
        if path_user_id not in user_dict:
            abort(400)

        update_and_add_user_helper(path_user_id, request.get_json())

    @staticmethod
    def delete(path_user_id):
        if path_user_id not in user_dict:
            abort(400)

        user_dict.pop(path_user_id, None)


class UserList(Resource):
    @staticmethod
    def get():
        return jsonpickle.encode(user_dict, unpicklable=False)

    @staticmethod
    def post():
        global user_id
        user_id = user_id + 1
        update_and_add_user_helper(user_id, request.get_json())


def update_and_add_user_helper(u_id, request_payload):
    name = request_payload["name"]
    age = request_payload["age"]
    address = request_payload["address"]
    city = request_payload["city"]
    state = request_payload["state"]
    zip_code = request_payload["zip"]
    user_dict[u_id] = Person(name, age, address, city, state, zip_code)


class Person:
    def __init__(self, name, age, address, city, state, zip_code):
        self.name = name
        self.age = age
        self.address = address
        self.city = city
        self.state = state
        self.zip_code = zip_code

api.add_resource(User, "/users/<int:path_user_id>")
api.add_resource(UserList, "/users")

if __name__ == "__main__":
    app.run(debug=True)

After debugging the get method (by user id), I'm getting this

test = '{"address": "123 Chestnut Ln", "age": 35, "city": "Denton", "name": "James Smith", "state": "Texas", "zip_code": "76210"}'

However the response that is being displayed is: "{\"1\": {\"address\": \"123 Chestnut Ln\", \"age\": 35, \"city\": \"Denton\", \"name\": \"James Smith\", \"state\": \"Texas\", \"zip_code\": \"76210\"}}"

Is Flask serializing the object upon returning?

Also, please assume a POST request was done and there are values in the dictionary.

ShadyBears
  • 3,955
  • 13
  • 44
  • 66

1 Answers1

1

I've tried to search flask docs, with no success, so I've tried to fiddle with your code, so here's what I've found:

When I've changed:

return jsonpickle.encode(user_dict.get(path_user_id), unpicklable=False)

to

return user_dict.get(path_user_id)

I got TypeError: Object of type 'Person' is not JSON serializable exception. It seems that flask serializes responses after all.

Alex Bodnya
  • 120
  • 10
  • Thanks... now I just need to find out how to turn that off. I definitely don't want it serializing twice. – ShadyBears May 08 '19 at 17:25
  • why don't you use it, instead of throwing it out? You can design method for the 'Person' class, which returns dict with required data about person and call it. After that flask takes care of serialization. – Alex Bodnya May 08 '19 at 17:31