Last active
February 7, 2018 14:21
-
-
Save eruffaldi/ba87165b14769f967d5ca903a1160869 to your computer and use it in GitHub Desktop.
Incremental JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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