• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #!/usr/bin/env python
2 
3 import json
4 import struct
5 import sys
6 import zlib
7 
8 try:
9     xrange          # Python 2
10     PY2 = True
11 except NameError:
12     PY2 = False
13     xrange = range  # Python 3
14 
15 
16 if __name__ == '__main__':
17   with open(sys.argv[1]) as fp:
18     obj = json.load(fp)
19   text = json.dumps(obj, separators=(',', ':')).encode('utf-8')
20   data = zlib.compress(text, zlib.Z_BEST_COMPRESSION)
21 
22   # To make decompression a little easier, we prepend the compressed data
23   # with the size of the uncompressed data as a 24 bits BE unsigned integer.
24   assert len(text) < 1 << 24, 'Uncompressed JSON must be < 16 MB.'
25   data = struct.pack('>I', len(text))[1:4] + data
26 
27   step = 20
28   slices = (data[i:i+step] for i in xrange(0, len(data), step))
29   slices = [','.join(str(ord(c) if PY2 else c) for c in s) for s in slices]
30   text = ',\n'.join(slices)
31 
32   with open(sys.argv[2], 'w') as fp:
33     fp.write(text)
34