【发布时间】:2015-04-17 03:40:21
【问题描述】:
我正在尝试向 Flask JSON 编码器/解码器添加一些重载以添加日期时间编码/解码,但仅通过“hack”成功。
from flask import Flask, flash, url_for, redirect, render_template_string
from flask.json import JSONEncoder, JSONDecoder
template = """
<!DOCTYPE html>
<html><head><title>Test JSON encoder/decoder</title></head><body>
{% with messages = get_flashed_messages(with_categories=true) %}{% if messages %}{% for message in messages %}
<p>Flash: {{ message }}</p>
{% endfor %}{% endif %}{% endwith %}
<p>Flash should be: ['Flash message', 'success']</p>
<p><a href="{{ url_for('index') }}">Try again</a></p>
</body></html>
"""
class CustomJSONEncoder(JSONEncoder):
""" Do nothing custom json encoder """
def default(self, obj):
# My custom logic here
# ...
# or
return super(CustomJSONEncoder, self).defaults(obj)
class CustomJSONDecoder(JSONDecoder):
""" Do nothing custom json decoder """
def __init__(self, *args, **kargs):
_ = kargs.pop('object_hook', None)
super(CustomJSONDecoder, self).__init__(object_hook=self.decoder, *args, **kargs)
def decoder(self, d):
# My custom logic here
# ...
# or
return d
app = Flask(__name__, static_url_path='')
app.config['SECRET_KEY'] = 'secret-key'
app.json_encoder = CustomJSONEncoder
app.json_decoder = CustomJSONDecoder
@app.route('/')
def index():
flash('Flash message', 'success')
return redirect(url_for('display'))
@app.route('/b')
def display():
return render_template_string(template)
if __name__ == '__main__':
app.run(debug=True, port=5200)
我应该像这样从 Flask.sessions.TaggedJSONSerializer 复制一些代码:
import uuid
from base64 import b64decode
from werkzeug.http import parse_date
from markupsafe import Markup
from flask._compat import iteritems
class CustomJSONDecoder(JSONDecoder):
""" Do nothing custom json decoder """
def __init__(self, *args, **kargs):
_ = kargs.pop('object_hook', None)
super(CustomJSONDecoder, self).__init__(object_hook=self.decoder, *args, **kargs)
def decode(self, d):
# My custom logic here
# ...
# Copy of the code from Flask.sessions.TaggedJSONSerializer(object).loads(self, value).object_hook(obj)
if len(d) == 1:
the_key, the_value = next(iteritems(d))
if the_key == ' t':
return tuple(the_value)
elif the_key == ' u':
return uuid.UUID(the_value)
elif the_key == ' b':
return b64decode(the_value)
elif the_key == ' m':
return Markup(the_value)
elif the_key == ' d':
return parse_date(the_value)
return d
我做得“正确”还是有什么我想念的?
【问题讨论】:
标签: python json session flask decoding