-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsonc.py
More file actions
26 lines (21 loc) · 838 Bytes
/
Copy pathjsonc.py
File metadata and controls
26 lines (21 loc) · 838 Bytes
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
'''
Module to load comment-containing JSON. For this module, comments must be on a
line by themselves and must start with two slashes (//).
'''
import json
import re
from typing import Any
def loads(string: str) -> Any:
multiline_comments = re.compile(r'/\*'
r'.*?' # Non-greedy matching
r'\*/', re.DOTALL)
line_endings = re.compile(r'[\r\n]+')
string = multiline_comments.sub('', string)
return json.loads('\n'.join(
i for i in line_endings.split(string) if not i.strip().startswith('//')))
def load(filename: str) -> Any:
with open(filename) as f:
return loads(''.join(f.readlines()))
JSONDecodeError = json.JSONDecodeError
if __name__ == '__main__':
exit('This is a library module not meant to be executed directly.')