-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_strings.py
More file actions
45 lines (33 loc) · 1.03 KB
/
json_strings.py
File metadata and controls
45 lines (33 loc) · 1.03 KB
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
import json
import pathlib
from typing import TypeAlias
JSON: TypeAlias = dict[str, "JSON"] | list["JSON"] | str | int | float | bool | None
def strings_json(j: JSON) -> set[str]:
strings = set()
if isinstance(j, list):
for e in j:
strings.update(strings_json(e))
elif isinstance(j, dict):
for e in j.keys():
strings.update(strings_json(e))
for e in j.values():
strings.update(strings_json(e))
elif isinstance(j, str):
for s in j.split('/'):
strings.update(s.split('\\'))
elif isinstance(j, (int, float, bool)):
pass
elif j is None:
pass
else:
raise Exception('unknown type ' + str(type(j)))
return strings
def strings_json_file(path: pathlib.Path) -> set[str]:
with open(path) as f:
j = json.load(f)
return strings_json(j)
def strings_folder(path: pathlib.Path) -> set[str]:
strings = set()
for p in path.rglob('*.json'):
strings.update(strings_json_file(p))
return strings