-
Notifications
You must be signed in to change notification settings - Fork 1
/
my_json.py
47 lines (39 loc) · 1.36 KB
/
my_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import json
import os
import my_os
from my_os import current_milli_time
json_dir = 'json_dump'
my_os.mkdir_p(json_dir)
def json_path(title: str) -> str:
return json_dir + "/" + title + ".json"
def load_json(title: str):
file = json_path(title)
try:
print("Loading json: ", file)
print("Size of file: ", os.stat(file).st_size)
with open(file, 'r', encoding='utf-8') as jf:
return json.load(jf)
except FileNotFoundError:
print("No file: " + file)
except OSError:
print("OSError while reading file: " + file)
return None
def save_json(title: str, json_obj, pretty_print: bool = False, use_dumps: bool = True):
start = current_milli_time()
path = json_path(title)
print("Saving '{0}'...".format(path))
with open(path, 'w', encoding='utf-8') as f:
if pretty_print:
json.dump(json_obj, f, indent=4)
else:
if use_dumps:
# use dumps seems at least 5-6.5 times faster
s = json.dumps(json_obj, separators=(',', ':'))
f.write(s)
else:
json.dump(json_obj, f, separators=(',', ':'))
finish = current_milli_time()
print("\tSaving took {} ms. {}".format(
int(finish - start),
"using dumps" if use_dumps else "using dump"))
print("Finished!")