[Sqlalchemy-tickets] Issue #3422: Cant JSON serialize sqlalchemy.util.langhelpers.symbol (zzzeek/sq
Brought to you by:
zzzeek
|
From: Ken S. <iss...@bi...> - 2015-05-15 16:01:37
|
New issue 3422: Cant JSON serialize sqlalchemy.util.langhelpers.symbol https://bitbucket.org/zzzeek/sqlalchemy/issue/3422/cant-json-serialize Ken Sheppardson: Starting with version 0.9.0, simplejson sees util.symbol objects as integers, causing it to generate invalid JSON. We're trying to serialize stack traces and function arguments as JSON, and although we've defined a custom JSONEncoder default() method to handle non-standard data types, we're getting invalid JSON like: ``` ... "args": [ "<sqlalchemy.orm.attributes.ScalarAttributeImpl object at 0x4966090>", "<sqlalchemy.orm.state.InstanceState object at 0x52ca650>", "{'_data': {'follower_count': None, 'followers': None, 'following': None, 'locale': u'zh_hans', 'region': None, 'timezone': u'Africa/Cairo', 'utc_offset': Decimal('2.0')}, '_modified': {'locale': u'zh_hans'}, '_sa_instance_state': <sqlalchemy.orm.state.InstanceState object at 0x52ca650>, 'locale': u'zh_hans'}", symbol('PASSIVE_OFF') ], ... ``` ...because while our custom default() method handles data types simplejson doesn't recognize, it thinks "symbol('PASSIVE_OFF')" is an int, and dumps the raw value into the JSON. To reproduce (using python 2.7.6, sqlalchemy 1.0.4, simplejson 3.6.5): ``` Python 2.7.6 (default, Sep 9 2014, 15:04:36) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> import sqlalchemy as sa >>> import json >>> sym = sa.util.symbol("PASSIVE_OFF") >>> data = ['str1', 123, sym] >>> print json.JSONEncoder().encode(data) ["str1", 123, symbol('PASSIVE_OFF')] >>> print sym symbol('PASSIVE_OFF') >>> type(sym) <class 'sqlalchemy.util.langhelpers.symbol'> >>> isinstance(sym,int) True ``` Python 3 handles this slightly differently: rather than "symbol('PASSIVE_OFF')" in the JSON, we see integers like -4343837943729882877 in the output of encode(). |