我想知道是否有任何库允许我在令牌的基础上将对象编码为json,这样我就不必在内存中保存一个非常大的对象树。
答案 0 :(得分:0)
是的,您可以将json.dump()(而不是dumps()
)与流一起使用:
来自https://docs.python.org/2/library/json.html
>>> json.dump(['streaming API'], io)
>>> io.getvalue()
'["streaming API"]'
答案 1 :(得分:0)
对于数组对象,可以这样做。字典不会有太大的不同。
import json
# repos.json is from github,
# curl -i https://api.github.com/orgs/<organisation>/repos
# with some editing
repos = json.load(open("repos.json", "r"))
# for comparing the input vs. output
json.dump(repos, open("repos2.json", "w"),
indent=2, check_circular=False, sort_keys=True)
with open("output.json", "w") as fp:
first = True
fp.write("[\n")
# simulate what could be done if repo objects were read
# one by one from somewhere
for repo in repos:
if not first:
fp.write(",\n")
else:
first=False
json.dump(repo, fp, indent=2, check_circular=False, sort_keys=True)
fp.write("\n]\n")
答案 2 :(得分:0)
为了流式传输对象,您需要一个实现__iter__()
和委托的包装器对象:
def StreamingList(object):
def __init__(self, source):
handle = source.open()
def __iter():
return handle.__iter__() # the returned iterator must return one JSON-serializable object at a time
然后你可以这样做:
json.dumps(StreamingList(filename))
这是草图