forked from orbit-oss/flask
Merge pull request #3917 from greyli/fix-date-parse
update json.dumps for http_date changes
This commit is contained in:
commit
4846e25e92
3 changed files with 12 additions and 17 deletions
|
|
@ -3,7 +3,6 @@ import json as _json
|
|||
import uuid
|
||||
import warnings
|
||||
from datetime import date
|
||||
from datetime import datetime
|
||||
|
||||
from jinja2.utils import htmlsafe_json_dumps as _jinja_htmlsafe_dumps
|
||||
from werkzeug.http import http_date
|
||||
|
|
@ -41,10 +40,8 @@ class JSONEncoder(_json.JSONEncoder):
|
|||
overriding how basic types like ``str`` or ``list`` are
|
||||
serialized, they are handled before this method.
|
||||
"""
|
||||
if isinstance(o, datetime):
|
||||
return http_date(o.utctimetuple())
|
||||
if isinstance(o, date):
|
||||
return http_date(o.timetuple())
|
||||
return http_date(o)
|
||||
if isinstance(o, uuid.UUID):
|
||||
return str(o)
|
||||
if dataclasses and dataclasses.is_dataclass(o):
|
||||
|
|
|
|||
|
|
@ -129,19 +129,16 @@ def test_jsonify_arrays(app, client):
|
|||
assert flask.json.loads(rv.data) == a_list
|
||||
|
||||
|
||||
def test_jsonifytypes(app, client):
|
||||
"""Test jsonify with datetime.date and datetime.datetime types."""
|
||||
test_dates = (
|
||||
datetime.datetime(1973, 3, 11, 6, 30, 45),
|
||||
datetime.date(1975, 1, 5),
|
||||
)
|
||||
@pytest.mark.parametrize(
|
||||
"value", [datetime.datetime(1973, 3, 11, 6, 30, 45), datetime.date(1975, 1, 5)]
|
||||
)
|
||||
def test_jsonify_datetime(app, client, value):
|
||||
@app.route("/")
|
||||
def index():
|
||||
return flask.jsonify(value=value)
|
||||
|
||||
for i, d in enumerate(test_dates):
|
||||
url = f"/datetest{i}"
|
||||
app.add_url_rule(url, str(i), lambda val=d: flask.jsonify(x=val))
|
||||
rv = client.get(url)
|
||||
assert rv.mimetype == "application/json"
|
||||
assert flask.json.loads(rv.data)["x"] == http_date(d.timetuple())
|
||||
r = client.get()
|
||||
assert r.json["value"] == http_date(value)
|
||||
|
||||
|
||||
class FixedOffset(datetime.tzinfo):
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
from datetime import datetime
|
||||
from datetime import timezone
|
||||
from uuid import uuid4
|
||||
|
||||
import pytest
|
||||
|
|
@ -20,7 +21,7 @@ from flask.json.tag import TaggedJSONSerializer
|
|||
b"\xff",
|
||||
Markup("<html>"),
|
||||
uuid4(),
|
||||
datetime.utcnow().replace(microsecond=0),
|
||||
datetime.now(tz=timezone.utc).replace(microsecond=0),
|
||||
),
|
||||
)
|
||||
def test_dump_load_unchanged(data):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue