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