python - 使用 Flask 正确重载 json 编码和解码

标签 python json session flask decoding

我正在尝试向 Flask JSON 编码器/解码器添加一些重载以添加日期时间编码/解码,但仅通过“破解”才成功。

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

我做的是否“正确”,还是我错过了什么?

最佳答案

您可以通过显式调用基类的 default() 方法来使用基类的功能。我已经在我的自定义 JSONEncoder 中成功地做到了这一点:

class CustomJSONEncoder(JSONEncoder):
    def default(self, obj):
        # Calling custom encode function:
        jsonString = HelperFunctions.jsonEncodeHandler(obj)
        if (jsonString != obj):  # Encode function has done something
            return jsonString  # Return that
        return JSONEncoder.default(self, obj)  # else let the base class do the work

然而,在解码器中,您应该记住传递给 __init__() 函数的对象钩子(Hook),并从您自己的钩子(Hook)中调用它:

class CustomJSONDecoder(JSONDecoder):
    def __init__(self, *args, **kwargs):
        self.orig_obj_hook = kwargs.pop("object_hook", None)
        super(CustomJSONDecoder, self).__init__(*args,
            object_hook=self.custom_obj_hook, **kwargs)

    def custom_obj_hook(self, dct):
        # Calling custom decode function:
        dct = HelperFunctions.jsonDecodeHandler(dct)
        if (self.orig_obj_hook):  # Do we have another hook to call?
            return self.orig_obj_hook(dct)  # Yes: then do it
        return dct  # No: just return the decoded dict

顺便说一句:你的解码器中有一个错字:你在基类中注册的对象钩子(Hook)名为 self.decoder,但成员定义为 def decode(... )(末尾没有 r)。在您的示例中,您注册了一个空钩子(Hook),并且永远不会调用 decode()

关于python - 使用 Flask 正确重载 json 编码和解码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28544630/

相关文章:

java - 在已部署的系统中使用 session.setAttribute

asp.net-mvc - Cookie、 session 、数据库

java - Spring 安全。 session 不稳定

python - 在 cherrypy 中成功快速启动后启动后台进程

jquery - java列表返回json字符串,JSON.parse : expected property name or '}' ?

json - 如何使用 Go 将 JSON 文件解析为结构

java - 使用 JACKSON 或其他 java 库更改 JSON 键的最佳方法

python - 从一个字符串和一个字符串列表创建一个元组

python - Django Allowed_Hosts 错误。

python - joblib.load 和 pickle.load 错误 "No attribute ' XGBoostLabelEncoder'"