From 843fb61b0b852d12826d171e48771b8ecb6d1514 Mon Sep 17 00:00:00 2001 From: hhyo Date: Tue, 31 Jul 2018 20:22:19 +0800 Subject: [PATCH] =?UTF-8?q?JSONEncoder=E5=A2=9E=E5=8A=A0timedelta=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sql/extend_json_encoder.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/sql/extend_json_encoder.py b/sql/extend_json_encoder.py index 6b38149e..7c45cba6 100644 --- a/sql/extend_json_encoder.py +++ b/sql/extend_json_encoder.py @@ -1,7 +1,7 @@ # -*- coding: UTF-8 -*- import simplejson as json -from datetime import datetime, date +from datetime import datetime, date, timedelta from decimal import Decimal from functools import singledispatch @@ -36,6 +36,10 @@ def _(o): return o.strftime('%Y-%m-%d') +@convert.register(timedelta) +def _(o): + return o.total_seconds() + # @convert.register(Decimal) # def _(o): # return float(o) @@ -59,7 +63,9 @@ def default(self, obj): 'dm': dm, 'dt': dt, 'dat': dat, + 'tl': timedelta(minutes=30), 'bigint': 988983860501598208 } -#print(json.dumps(data, cls=ExtendJSONEncoder, bigint_as_string=True)) +# print(json.dumps(data, cls=ExtendJSONEncoder, bigint_as_string=True)) +# print(json.dumps(data, cls=ExtendJSONEncoder, bigint_as_string=True, default=str))