# HG changeset patch # User Thierry Florac # Date 1537193662 -7200 # Node ID 52a03aa1cf9d662448c224c0af7cbea97872063c # Parent 830d5222e8065c7560d62c84fa4ee183e036cb11 Added default JSON encoder to serialize date and datetime objects diff -r 830d5222e806 -r 52a03aa1cf9d src/pyams_utils/json.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/pyams_utils/json.py Mon Sep 17 16:14:22 2018 +0200 @@ -0,0 +1,39 @@ +# +# Copyright (c) 2008-2018 Thierry Florac +# All Rights Reserved. +# +# This software is subject to the provisions of the Zope Public License, +# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution. +# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED +# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS +# FOR A PARTICULAR PURPOSE. +# + +__docformat__ = 'restructuredtext' + + +# import standard library +import json + +from datetime import date, datetime + +# import interfaces + +# import packages + + +def default_json_encoder(obj): + if isinstance(obj, (date, datetime)): + return obj.isoformat() + else: + return obj + + +json._default_encoder = json.JSONEncoder(skipkeys=False, + ensure_ascii=True, + check_circular=True, + allow_nan=True, + indent=None, + separators=None, + default=default_json_encoder)