diff options
Diffstat (limited to 'src/mailman/rest/helpers.py')
| -rw-r--r-- | src/mailman/rest/helpers.py | 12 |
1 files changed, 11 insertions, 1 deletions
diff --git a/src/mailman/rest/helpers.py b/src/mailman/rest/helpers.py index be5d2b565..b17cdbca7 100644 --- a/src/mailman/rest/helpers.py +++ b/src/mailman/rest/helpers.py @@ -32,6 +32,7 @@ __all__ = [ import json import hashlib +from datetime import datetime from lazr.config import as_boolean from restish.http import Response @@ -61,6 +62,15 @@ def path_to(resource): +class ExtendedEncoder(json.JSONEncoder): + """An extended JSON encoder which knows about other data types.""" + + def default(self, obj): + if isinstance(obj, datetime): + return obj.isoformat() + return json.JSONEncoder.default(self, obj) + + def etag(resource): """Calculate the etag and return a JSON representation. @@ -78,7 +88,7 @@ def etag(resource): assert 'http_etag' not in resource, 'Resource already etagged' etag = hashlib.sha1(repr(resource)).hexdigest() resource['http_etag'] = '"{0}"'.format(etag) - return json.dumps(resource) + return json.dumps(resource, cls=ExtendedEncoder) |
