def json2generator(data, arrayKey=None):
"""
??????? ???????????? ?????????? json ? ?????????. ??? ????????? ???????? ?????? ?????? ?? ???????? ??????? ??????. ????? ?????? ????????? ?????? ??? ??????? (??????? ????? ??????????? ? ?????????). arrayKey ?????? ????????? ?? ??????, ????? ???? ???????? (key1.key2)
"""
from ijson import common
# from io import BytesIO
from cStringIO import StringIO
#! yajl2 ??????? ???????? ??????????? ???????, ?? ?? ?????? ?????? ??? ? ?? ??????? ??? ??????????, ????? "Yajl shared object cannot be found"
try: import ijson.backends.yajl2_cffi as ijson
except:
try: from ijson.backends import yajl2 as ijson
except:
try: from ijson.backends import yajl as ijson
except: from ijson.backends import python as ijson
try: f=StringIO(data)
except: f=StringIO(data.encode('utf-8'))
def _fixJSON(event):
# ??????? ?????????? "????" ?????????, ??????? ???????? ??? ???????? ???? ???????? ? decimal()
if event[1]=='number':
return (event[0], event[1], float(event[2]) if math.modf(event[2])[0] else int(event[2]))
else: return event
events=imap(_fixJSON, ijson.parse(f))
g=common.items(events, (arrayKey+'.item' if arrayKey else 'item'))
# g=ijson.items(f, (arrayKey+'.item' if arrayKey else 'item'))
return g
评论列表
文章目录