Skip to content

Instantly share code, notes, and snippets.

@eruffaldi
Last active Feb 7, 2018
Embed
What would you like to do?
Incremental JSON
import json
import sys
def main():
if len(sys.argv) < 2:
print "requires JSON file"
else:
try:
x = json.load(open(sys.argv[1],"rb"))
print "file is correct"
return
except:
pass
file = open(sys.argv[1],"a")
file.write("]")
file.close()
try:
x = json.load(open(sys.argv[1],"rb"))
print "file fixed correctly"
except:
print "file broken"
if __name__ == "__main__":
main()
import json
import time
class IncrementalJson:
def __init__(self,target):
"""Create an Incremental JSON Serializer as Array"""
self.target = target
self.target.write("[")
self.first = True
def close(self):
""" Close everything"""
self.target.write("]")
self.target.flush()
try:
self.target.close()
except:
pass
def append(self,x):
""" Append any object that can be serialized as JSON"""
if self.first:
self.target.write("\n")
self.first = False
else:
self.target.write(",\n")
json.dump(x,self.target)
self.target.flush()
def __enter__(self):
""" Use it for with IncremntalJson(outfile) as x:"""
return self
def __exit__(self,type, value, traceback):
self.close()
if __name__ == "__main__":
with IncrementalJson(open("my.json","wb")) as inc:
inc.append("ciao")
inc.append(dict(what="fine",number=20))
time.sleep(1000)
for x in range(1,20):
inc.append("ciao:%d" % x)
decoded = json.load(open("my.json","r"))
print decoded
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment