Add datetime.date support to JSONEncoder
This commit is contained in:
parent
1ba6c2248d
commit
61263e08f9
3 changed files with 24 additions and 3 deletions
1
CHANGES
1
CHANGES
|
|
@ -61,6 +61,7 @@ Version 1.0
|
||||||
- The automatically provided ``OPTIONS`` method is now correctly disabled if
|
- The automatically provided ``OPTIONS`` method is now correctly disabled if
|
||||||
the user registered an overriding rule with the lowercase-version
|
the user registered an overriding rule with the lowercase-version
|
||||||
``options`` (issue ``#1288``).
|
``options`` (issue ``#1288``).
|
||||||
|
- flask.json.jsonify now supports the datetime.date type
|
||||||
|
|
||||||
Version 0.10.2
|
Version 0.10.2
|
||||||
--------------
|
--------------
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
"""
|
"""
|
||||||
import io
|
import io
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import datetime
|
from datetime import date
|
||||||
from .globals import current_app, request
|
from .globals import current_app, request
|
||||||
from ._compat import text_type, PY2
|
from ._compat import text_type, PY2
|
||||||
|
|
||||||
|
|
@ -74,8 +74,8 @@ class JSONEncoder(_json.JSONEncoder):
|
||||||
return list(iterable)
|
return list(iterable)
|
||||||
return JSONEncoder.default(self, o)
|
return JSONEncoder.default(self, o)
|
||||||
"""
|
"""
|
||||||
if isinstance(o, datetime):
|
if isinstance(o, date):
|
||||||
return http_date(o)
|
return http_date(o.timetuple())
|
||||||
if isinstance(o, uuid.UUID):
|
if isinstance(o, uuid.UUID):
|
||||||
return str(o)
|
return str(o)
|
||||||
if hasattr(o, '__html__'):
|
if hasattr(o, '__html__'):
|
||||||
|
|
|
||||||
|
|
@ -12,9 +12,11 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import datetime
|
||||||
import flask
|
import flask
|
||||||
from logging import StreamHandler
|
from logging import StreamHandler
|
||||||
from werkzeug.http import parse_cache_control_header, parse_options_header
|
from werkzeug.http import parse_cache_control_header, parse_options_header
|
||||||
|
from werkzeug.http import http_date
|
||||||
from flask._compat import StringIO, text_type
|
from flask._compat import StringIO, text_type
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -29,6 +31,24 @@ def has_encoding(name):
|
||||||
|
|
||||||
class TestJSON(object):
|
class TestJSON(object):
|
||||||
|
|
||||||
|
def test_jsonify_date_types(self):
|
||||||
|
"""Test jsonify with datetime.date and datetime.datetime types."""
|
||||||
|
|
||||||
|
test_dates = (
|
||||||
|
datetime.datetime(1973, 3, 11, 6, 30, 45),
|
||||||
|
datetime.date(1975, 1, 5)
|
||||||
|
)
|
||||||
|
|
||||||
|
app = flask.Flask(__name__)
|
||||||
|
c = app.test_client()
|
||||||
|
|
||||||
|
for i, d in enumerate(test_dates):
|
||||||
|
url = '/datetest{0}'.format(i)
|
||||||
|
app.add_url_rule(url, str(i), lambda val=d: flask.jsonify(x=val))
|
||||||
|
rv = c.get(url)
|
||||||
|
assert rv.mimetype == 'application/json'
|
||||||
|
assert flask.json.loads(rv.data)['x'] == http_date(d.timetuple())
|
||||||
|
|
||||||
def test_json_bad_requests(self):
|
def test_json_bad_requests(self):
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
@app.route('/json', methods=['POST'])
|
@app.route('/json', methods=['POST'])
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue