Skip to content

Instantly share code, notes, and snippets.

@colincoghill
Created August 30, 2013 02:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colincoghill/6385657 to your computer and use it in GitHub Desktop.
Save colincoghill/6385657 to your computer and use it in GitHub Desktop.
# JSON serialize an object, returns a generator that will yield in approximate
# sized chunks.
import json
def chunk_up(gen, csize):
""" Gather the string output from a generator and yield it in
chunks of approximately "csize" bytes.
"""
out = ""
for chunk in gen:
out += chunk
if len(out) > csize:
yield out
out = ""
yield out
# Fetch the data structure
# big = json.load(open("input.json"))
gen = json.JSONEncoder().iterencode(big)
for chunk in chunk_up(gen, 10240): # 10k chunks
print len(chunk)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment