forked from waynebhayes/CellUniverse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsonc.py
56 lines (51 loc) · 1.29 KB
/
jsonc.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
48
49
50
51
52
53
54
55
56
import json
_DEFAULT = 0
_FORWARD = 1
_IN_QUOTE = 2
_ESCAPE = 3
_IN_BLOCK = 4
_IN_LINE = 5
_OUT_BLOCK = 6
def _strip_comments(s):
state = _DEFAULT
t = []
for c in s:
if state == _DEFAULT:
if c == '"':
state = _IN_QUOTE
elif c == '/':
state = _FORWARD
continue
elif c == ' ' or c == '\r' or c == '\n':
continue
elif state == _IN_QUOTE:
if c == '\\':
state = _ESCAPE
elif c == '"':
state = _DEFAULT
elif state == _FORWARD:
if c == '/':
state = _IN_LINE
continue
elif c == '*':
state = _IN_BLOCK
continue
elif state == _ESCAPE:
state = _IN_QUOTE
elif state == _IN_LINE:
if c == '\r' or c == '\n':
state = _DEFAULT
continue
elif state == _IN_BLOCK:
if c == '*':
state = _OUT_BLOCK
continue
elif state == _OUT_BLOCK:
if c == '/':
state = _DEFAULT
continue
t.append(c)
return ''.join(t)
def load(fp):
s = _strip_comments(fp.read())
return json.loads(s)