- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经看到曾经可以在 Flask 应用程序中使用 simplejson 作为 JSON 编码器/解码器,如下所示:
from simplejson import JSONEncoder, JSONDecoder
app.json_encoder = JSONEncoder
app.json_decoder = JSONDecoder
但是 ujson 没有这样的对象:
>>> from ujson import JSONEncoder
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'JSONEncoder' from 'ujson' (/.../site-packages/ujson.cpython-38-x86_64-linux-gnu.so
我试过的
from uuid import UUID, uuid4
import ujson as json
from flask import Flask, jsonify
from flask.json import JSONEncoder
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, UUID):
return str(obj)
return JSONEncoder.default(self, obj)
def encode(self, o):
return json.dumps(o)
app = Flask(__name__)
app.json_encoder = CustomJSONEncoder
@app.route("/")
def index():
return jsonify({"foo": uuid4()})
app.run()
但我不确定,因为对解码器的帮助显示了这一点:
| decode(self, s, _w=<built-in method match of re.Pattern object at 0x7f6a608404b0>, _PY3=True)
| Return the Python representation of ``s`` (a ``str`` or ``unicode``
| instance containing a JSON document)
|
| raw_decode(self, s, idx=0, _w=<built-in method match of re.Pattern object at 0x7f6a608404b0>, _PY3=True)
| Decode a JSON document from ``s`` (a ``str`` or ``unicode``
| beginning with a JSON document) and return a 2-tuple of the Python
| representation and the index in ``s`` where the document ended.
| Optionally, ``idx`` can be used to specify an offset in ``s`` where
| the JSON document begins.
|
| This can be used to decode a JSON document from a string that may
| have extraneous data at the end.
我的实现好吗?我将如何支持那些其他参数?什么时候
decode
当
raw_decode
用过的?
[2020-10-09 10:54:52,063] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
response = self.full_dispatch_request()
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
raise value
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "main.py", line 28, in index
return jsonify({"foo": uuid4()})
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/flask/json/__init__.py", line 370, in jsonify
dumps(data, indent=indent, separators=separators) + "\n",
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/flask/json/__init__.py", line 211, in dumps
rv = _json.dumps(obj, **kwargs)
File "/home/moose/.pyenv/versions/3.8.3/lib/python3.8/site-packages/simplejson/__init__.py", line 398, in dumps
return cls(
File "main.py", line 14, in encode
return json.dumps(o)
TypeError: UUID('1f45a2bc-c964-48f0-b2f5-9ef7a2557966') is not JSON serializable
最佳答案
您可以使用 try
像这样阻止:
import ujson as json
from flask.json import JSONEncoder
class CustomJSONEncoder(JSONEncoder):
def default(self, obj):
try:
return json.dumps(obj)
except TypeError:
return JSONEncoder.default(self, obj)
from flask import Flask
app = Flask(__name__)
app.json_encoder = CustomJSONEncoder
关于json - 如何将 ujson 用作 Flask 编码器/解码器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64203233/
我有以下查询: SELECT I.InsuranceID FROM Insurance I INNER JOIN JobDetail JD ON I.AccountID = JD.AccountID
我想在 SwiftUI 布局中将此函数用作具有不可变值的模板,但得到错误 Result of call to 'padding' 未使用: func keys (padding: CGFloat, t
直到最近我才使用 View 的标签元素,此后发现了一些很酷的用途。我遇到了一个不寻常的问题,希望有人能回答。这可能比 Android 更通用,但我不确定。它与 Java 如何处理 Integer 类有
这个问题在这里已经有了答案: What is the purpose of the var keyword and when should I use it (or omit it)? (19 个回
我有以下脚本(见下文)。我有两个问题: 1.在 Knockoutjs 的上下文中,下面这行是什么意思? ko.observable(null); 2.如何调用这里尚未定义的函数: that.activ
Java 社区中是否存在一种使用 with-repect-to 在方法中使用多个返回的思想流派,如下所示: public SomeClass someMethod(int someValue) {
我一直在尝试为我的网站创建一个小型社交媒体栏。出于某种原因,我无法计算出我想用来创建 Sprite 的图像无法加载。我还应该提一下,我在背景图像不显示方面遇到过类似的问题。 HTML调用是这样的:
我正在尝试使用 std::pair 枚举值作为 unordered_map 容器的键,但我在定义自定义哈希函数时遇到困难。 我尝试了以下方法: // Enum and pair declaration
我正在学习 JS/JQuery 以及匿名函数和闭包。我见过这样的例子: $('.button').click(function(){ /* Animations */ /* Other
我正在尝试使用菜单列表来浏览我的应用程序。尽管应用程序和路由运行良好,但我使用这段代码在控制台中收到了一些警告: {props.itemList.map((item, index) =>(
我只是想创建一个简单的测试,我在其中使用 DelegateHandlers 来实例化一个 HttpClient 而无需引入 Asp.net Core 包。我有 2 个删除处理程序 Throttling
我是answering another question在这里,用户有一个 ListView与 ItemsSource包含 UserControls .我说我不会推荐它,并被问为什么。 这真的让我很惊
我安装了3.5.2和 3.5.3使用 pyenv 的版本。 # pyenv versions * system (set by /usr/local/pyenv/version) 3.5.2
我正在使用 android studio 制作统一插件,但这里有问题。一些 SDK 提供仅使用 AppcompatActivity 来制作 fragment 但我的MainActivity , 正是
我在 Laravel 中使用 whereHas 来构建查询: })->whereHas('results', function ($query) use ($issued, $mode, $reque
我有一个 5Gb .dat 文件(> 1000 万行)。每行的格式如 aaaa bb cccc0123 xxx kkkkkkkkkkkkkk或 aaaaabbbcccc01234xxxkkkkkkkk
我有一个消费者类,它采用 NSInputStream 作为参数,它将被异步处理,并且我想推送来自生产者类的数据,该生产者类要求它提供 NSOutputStream 作为其输出源。现在我如何设置一个缓冲
我正在尝试使用 ENVs在 Symfony2 中设置我的参数。标量值很简单,但我有一些参数是数组,我需要使用 ENV 以某种方式设置它们。 有问题的参数: parameters: redis.se
在我的类作业中,我已经成功地做到了这一点,但只是在非常简单的程序中。今天,我有一个更复杂的程序,在我将 DEBUG 定义为一个符号后,Eclipse 做了可怕的笨拙的事情,并且在我删除定义后这些可怕的
我目前有 2 个复选框类别、一个下拉列表和一个表单中的提交按钮。该按钮应保持“禁用”状态,直到选中 A 类的一个复选框和选中 B 类选项之一并选择选择列表中的一个选项。它适用于复选框(当我在没有列表的
我是一名优秀的程序员,十分优秀!