I'm trying to instantiate a basic Model instance for the user session in my Flask application. I'm caught off guard by the requirement that my class be JSON serializable. I thought the session dictionary was just an arbitrary construct to store session information but it sounds like there's more constraints surrounding its usage, one of which is apparently the values be JSON serializable. What other constraints are there and what is the purpose of this JSON constraint exactly? Is it a hard expectation for web apps to persist their user sessions via JSON? Where does this requirement come from/is inspired by?
@app.route( '/' , methods=['GET', 'POST'] )
def index():
"""index takes user to the home page of the application.
"""
# Create model instance for this user session
if 'model' not in session :
session['model'] = Model( )
File "C:\Anaconda3\lib\site-packages\flask\json.py", line 83, in default
return _json.JSONEncoder.default(self, o)
File "C:\Anaconda3\lib\json\encoder.py", line 173, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <Model.Model object at 0x000000000770B6D8> is not JSON serializable
Just for additional background it looks like the session object is a LocalProxy instance which may clue into the design of things.
>>> type( session )
<class 'werkzeug.local.LocalProxy'>