Skip to content
Myr

Add datetime.date support to JSONEncoder

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

authored by Ben Jones · committed by Ben Jones · parent 1ba6c22 · Browse files · 3 files+24 added−3 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+24 added−3 removed

  • tests:+20 added−0 removed
  • other:+4 added−3 removed

3 files+24 added−3 removed

modified:flask/json.py+3 added-3 removed
8 unmodified lines
9 :license: BSD, see LICENSE for more details.10"""11import io12import uuid13from datetime import datetime13from datetime import date14from .globals import current_app, request15from ._compat import text_type, PY21617from werkzeug.http import http_date
55 unmodified lines
73 else:74 return list(iterable)75 return JSONEncoder.default(self, o)76 """77 if isinstance(o, datetime):78 return http_date(o)77 if isinstance(o, date):78 return http_date(o.timetuple())79 if isinstance(o, uuid.UUID):80 return str(o)81 if hasattr(o, '__html__'):82 return text_type(o.__html__())
173 unmodified lines
modified:tests/test_helpers.py+20 added
10 unmodified lines
1112import pytest1314import os15import datetime16import flask17from logging import StreamHandler18from werkzeug.http import parse_cache_control_header, parse_options_header19from werkzeug.http import http_date20from flask._compat import StringIO, text_type212223def has_encoding(name):
6 unmodified lines
303132class TestJSON(object):3334 def test_jsonify_date_types(self):35 """Test jsonify with datetime.date and datetime.datetime types."""3637 test_dates = (38 datetime.datetime(1973, 3, 11, 6, 30, 45),39 datetime.date(1975, 1, 5)40 )4142 app = flask.Flask(__name__)43 c = app.test_client()4445 for i, d in enumerate(test_dates):46 url = '/datetest{0}'.format(i)47 app.add_url_rule(url, str(i), lambda val=d: flask.jsonify(x=val))48 rv = c.get(url)49 assert rv.mimetype == 'application/json'50 assert flask.json.loads(rv.data)['x'] == http_date(d.timetuple())5152 def test_json_bad_requests(self):53 app = flask.Flask(__name__)54 @app.route('/json', methods=['POST'])55 def return_json():
601 unmodified lines
modified:CHANGES+1 added
59 unmodified lines
60 part of https://github.com/kennethreitz/httpbin/issues/16861- The automatically provided ``OPTIONS`` method is now correctly disabled if62 the user registered an overriding rule with the lowercase-version63 ``options`` (issue ``#1288``).64- flask.json.jsonify now supports the datetime.date type6566Version 0.10.267--------------68
478 unmodified lines