Skip to content
Myr

update json.dumps for http_date changes

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Grey Li · committed by David Lord · parent dcd3b5c · Browse files · 3 files+12 added−17 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+12 added−17 removed

  • tests:+11 added−13 removed
  • other:+1 added−4 removed

3 files+12 added−17 removed

modified:src/flask/json/__init__.py+1 added-4 removed
1 unmodified line
2import json as _json3import uuid4import warnings5from datetime import date6from datetime import datetime67from jinja2.utils import htmlsafe_json_dumps as _jinja_htmlsafe_dumps8from werkzeug.http import http_date9
29 unmodified lines
39 :meth:`json.JSONEncoder.default`. Python does not support40 overriding how basic types like ``str`` or ``list`` are41 serialized, they are handled before this method.42 """44 if isinstance(o, datetime):45 return http_date(o.utctimetuple())43 if isinstance(o, date):47 return http_date(o.timetuple())44 return http_date(o)45 if isinstance(o, uuid.UUID):46 return str(o)47 if dataclasses and dataclasses.is_dataclass(o):48 return dataclasses.asdict(o)
284 unmodified lines
modified:tests/test_json_tag.py+2 added-1 removed
1from datetime import datetime2from datetime import timezone3from uuid import uuid445import pytest6
13 unmodified lines
20 [(1, 2, 3)],21 b"\xff",22 Markup("<html>"),23 uuid4(),23 datetime.utcnow().replace(microsecond=0),24 datetime.now(tz=timezone.utc).replace(microsecond=0),25 ),26)27def test_dump_load_unchanged(data):28 s = TaggedJSONSerializer()
58 unmodified lines
modified:tests/test_json.py+9 added-12 removed
127 unmodified lines
128 assert rv.mimetype == "application/json"129 assert flask.json.loads(rv.data) == a_list130131132def test_jsonifytypes(app, client):133 """Test jsonify with datetime.date and datetime.datetime types."""134 test_dates = (135 datetime.datetime(1973, 3, 11, 6, 30, 45),136 datetime.date(1975, 1, 5),137 )132@pytest.mark.parametrize(133 "value", [datetime.datetime(1973, 3, 11, 6, 30, 45), datetime.date(1975, 1, 5)]134)135def test_jsonify_datetime(app, client, value):136 @app.route("/")137 def index():138 return flask.jsonify(value=value)139139 for i, d in enumerate(test_dates):140 url = f"/datetest{i}"141 app.add_url_rule(url, str(i), lambda val=d: flask.jsonify(x=val))142 rv = client.get(url)143 assert rv.mimetype == "application/json"144 assert flask.json.loads(rv.data)["x"] == http_date(d.timetuple())140 r = client.get()141 assert r.json["value"] == http_date(value)142143144class FixedOffset(datetime.tzinfo):145 """Fixed offset in hours east from UTC.
257 unmodified lines