编辑:使用
添加新输出logging.info(id(type(Decimal(10))))
logging.info(id(type(obj)))
我正在尝试转储到JSON。我知道我需要将Decimal转换为float,所以这是我的代码:
def get(self):
results = CheckPulseRequest.get(get_args(self.request))
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(results, cls=DateTimeEncoder))
然后我按如下方式定义了DateTimeEncoder:
class DateTimeEncoder(json.JSONEncoder):
logging.info("In DateTimeEncoder Class")
def default(self, obj):
logging.info(id(type(Decimal(10))))
logging.info(id(type(obj)))
logging.info(type(Decimal(10)))
logging.info(type(obj))
logging.info(isinstance(Decimal(10), (int, long, float, complex, Decimal)))
logging.info(isinstance(obj, (int, long, float, complex, Decimal)))
if isinstance(obj, Decimal):
logging.info("Is Instance of Decimal")
return float(obj)
elif hasattr(obj, 'isoformat'):
logging.info("Is ISO Format")
return obj.isoformat()
else:
logging.info("Else")
return json.JSONEncoder.default(self, obj)
我得到以下日志输出。我的十进制对象不会转换为浮点数。请注意,该对象与Decimal具有相同的类型,但isinstance测试仅在Decimal(10)测试中为True。我错过了什么?
INFO 2015-02-23 18:49:45,737 check_pulse_request.py:25] Getting Check Pulse Request ID: 4
INFO 2015-02-23 18:49:46,374 main.py:179] 140647859901984
INFO 2015-02-23 18:49:46,375 main.py:180] 140647856664608
INFO 2015-02-23 18:49:46,375 main.py:181] <class 'decimal.Decimal'>
INFO 2015-02-23 18:49:46,375 main.py:182] <class 'decimal.Decimal'>
INFO 2015-02-23 18:49:46,375 main.py:183] True
INFO 2015-02-23 18:49:46,375 main.py:184] False
INFO 2015-02-23 18:49:46,376 main.py:193] Else
ERROR 2015-02-23 18:49:46,376 webapp2.py:1552] Decimal('-122.39906660000000') is not JSON serializable
Traceback (most recent call last):
File "/Users/sheridangray/Projects/adt-bundle-mac-x86_64-20140702/sdk/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/Users/sheridangray/Projects/adt-bundle-mac-x86_64-20140702/sdk/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Users/sheridangray/Projects/adt-bundle-mac-x86_64-20140702/sdk/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Users/sheridangray/Projects/adt-bundle-mac-x86_64-20140702/sdk/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Users/sheridangray/Projects/adt-bundle-mac-x86_64-20140702/sdk/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Users/sheridangray/Projects/adt-bundle-mac-x86_64-20140702/sdk/google-cloud-sdk/platform/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/sheridangray/Projects/city-pulse-appengine/city-pulse/main.py", line 38, in get
self.response.out.write(json.dumps(results, cls=DateTimeEncoder))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/Users/sheridangray/Projects/city-pulse-appengine/city-pulse/main.py", line 194, in default
return json.JSONEncoder.default(self, obj)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: Decimal('-122.39906660000000') is not JSON serializable
答案 0 :(得分:0)
我真的没有#34;修复&#34;问题,但通过将我的MySQL数据库列更改为FLOAT来解决它。