主要涉及的代碼python
def _iterencode(o, _current_indent_level): if isinstance(o, basestring): yield _encoder(o) elif o is None: yield 'null' elif o is True: yield 'true' elif o is False: yield 'false' elif isinstance(o, (int, long)): yield str(o) elif isinstance(o, float): yield _floatstr(o) elif isinstance(o, decimal): #support decimal type yield str(float(o)) elif isinstance(o, (list, tuple)): for chunk in _iterencode_list(o, _current_indent_level): yield chunk elif isinstance(o, dict): for chunk in _iterencode_dict(o, _current_indent_level): yield chunk else: if markers is not None: markerid = id(o) if markerid in markers: raise ValueError("Circular reference detected") markers[markerid] = o o = _default(o) for chunk in _iterencode(o, _current_indent_level): yield chunk if markers is not None: del markers[markerid] return _iterencode
可是必需要強制走 py的函數,默認是走_json.c封裝的模塊json
或者使用python-cjson替換python帶的默認json模塊函數
單獨修改 c 源代碼. code
增長對decimal類型的直接 dumps ci
否則,須要對全部decimal類型進行float轉換.string
import decimalit
import json import
obj['abc']=decimal.Decimal(100.00012)yield
josn.dumps(obj) float
代碼會提示錯誤.
須要針對 類型轉換爲 float 才能json轉碼.
...