This repository has been archived by the owner on Jun 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
jsonc_decoder.py
252 lines (231 loc) · 8.3 KB
/
jsonc_decoder.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
'''
Adds implementation of JSONDecoder which adds support for C-style single line
comments to JSON - the JSONCDecoder class.
source:
https://gist.github.com/Nusiq/4d6cc83a6acc8b373b5e56801d273ba3
'''
import json
from json import scanner, JSONDecodeError
from json.decoder import WHITESPACE, WHITESPACE_STR, scanstring
import re
FLAGS = re.VERBOSE | re.MULTILINE | re.DOTALL
INLINE_COMMENT = re.compile(r'//[^\n]*\n?', FLAGS)
INLINE_COMMENT_STRING_START='//'
MULTILINE_COMMENT = re.compile(r"/[*]([^*]|([*][^/]))*[*]+/", FLAGS)
MULTILINE_COMMENT_STRING_START='/*'
def parse_object(
s_and_end, strict, scan_once, object_hook, object_pairs_hook,
memo=None, _w=WHITESPACE.match, _ws=WHITESPACE_STR,
_ilcs=INLINE_COMMENT_STRING_START, _ilc=INLINE_COMMENT.match,
_mlcs=MULTILINE_COMMENT_STRING_START, _mlc=MULTILINE_COMMENT.match
):
'''
Modified json.decoder.JSONObject function from standard json module
(python 3.7.7).
'''
s, end = s_and_end
pairs = []
pairs_append = pairs.append
# Backwards compatibility
if memo is None:
memo = {}
memo_get = memo.setdefault
# Use a slice to prevent IndexError from being raised, the following
# check will raise a more specific ValueError if the string is empty
nextchar = s[end:end + 1]
# Normally we expect nextchar == '"'
if nextchar != '"':
while True: # Handle comments and whitespaces
if nextchar in _ws:
end = _w(s, end).end()
elif s[end:].startswith(_ilcs):
end = _ilc(s, end).end()
elif s[end:].startswith(_mlcs):
end = _mlc(s, end).end()
else:
break
nextchar = s[end:end + 1]
# Trivial empty object
if nextchar == '}':
if object_pairs_hook is not None:
result = object_pairs_hook(pairs)
return result, end + 1
pairs = {}
if object_hook is not None:
pairs = object_hook(pairs)
return pairs, end + 1
elif nextchar != '"':
raise JSONDecodeError(
"Expecting property name enclosed in double quotes", s, end)
end += 1
while True:
key, end = scanstring(s, end, strict)
key = memo_get(key, key)
# To skip some function call overhead we optimize the fast paths where
# the JSON key separator is ": " or just ":".
if s[end:end + 1] != ':':
while True: # Handle comments and whitespaces
if s[end:end + 1] in _ws:
end = _w(s, end).end()
elif s[end:].startswith(_ilcs):
end = _ilc(s, end).end()
elif s[end:].startswith(_mlcs):
end = _mlc(s, end).end()
else:
break
if s[end:end + 1] != ':':
raise JSONDecodeError("Expecting ':' delimiter", s, end)
end += 1
try:
while True: # Handle comments and whitespaces
if s[end] in _ws:
end = _w(s, end).end()
elif s[end:].startswith(_ilcs):
end = _ilc(s, end).end()
elif s[end:].startswith(_mlcs):
end = _mlc(s, end).end()
else:
break
except IndexError:
pass
try:
value, end = scan_once(s, end)
except StopIteration as err:
raise JSONDecodeError("Expecting value", s, err.value) from None
pairs_append((key, value))
try:
nextchar = s[end]
while True: # Handle comments and whitespaces
if nextchar in _ws:
end = _w(s, end).end()
elif s[end:].startswith(_ilcs):
end = _ilc(s, end).end()
elif s[end:].startswith(_mlcs):
end = _mlc(s, end).end()
else:
break
nextchar = s[end]
except IndexError:
nextchar = ''
end += 1
if nextchar == '}':
break
elif nextchar != ',':
raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
while True: # Handle comments and whitespaces
if s[end] in _ws:
end = _w(s, end).end()
elif s[end:].startswith(_ilcs):
end = _ilc(s, end).end()
elif s[end:].startswith(_mlcs):
end = _mlc(s, end).end()
else:
break
nextchar = s[end:end + 1]
end += 1
if nextchar != '"':
raise JSONDecodeError(
"Expecting property name enclosed in double quotes", s, end - 1)
if object_pairs_hook is not None:
result = object_pairs_hook(pairs)
return result, end
pairs = dict(pairs)
if object_hook is not None:
pairs = object_hook(pairs)
return pairs, end
def parse_array(
s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR,
_ilcs=INLINE_COMMENT_STRING_START, _ilc=INLINE_COMMENT.match,
_mlcs=MULTILINE_COMMENT_STRING_START, _mlc=MULTILINE_COMMENT.match
):
'''
Modified json.decoder.JSONArray function from standard module json
(python 3.7.7).
'''
s, end = s_and_end
values = []
nextchar = s[end:end + 1]
while True: # Handle comments and whitespaces
if nextchar in _ws:
end = _w(s, end).end()
elif s[end:].startswith(_ilcs):
end = _ilc(s, end).end()
elif s[end:].startswith(_mlcs):
end = _mlc(s, end).end()
else:
break
nextchar = s[end:end + 1]
# Look-ahead for trivial empty array
if nextchar == ']':
return values, end + 1
_append = values.append
while True:
try:
value, end = scan_once(s, end)
except StopIteration as err:
raise JSONDecodeError("Expecting value", s, err.value) from None
_append(value)
nextchar = s[end:end + 1]
while True: # Handle comments and whitespaces
if nextchar in _ws:
end = _w(s, end).end()
elif s[end:].startswith(_ilcs):
end = _ilc(s, end).end()
elif s[end:].startswith(_mlcs):
end = _mlc(s, end).end()
else:
break
nextchar = s[end:end + 1]
end += 1
if nextchar == ']':
break
elif nextchar != ',':
raise JSONDecodeError("Expecting ',' delimiter", s, end - 1)
try:
while True: # Handle comments and whitespaces
if s[end] in _ws:
end = _w(s, end).end()
elif s[end:].startswith(_ilcs):
end = _ilc(s, end).end()
elif s[end:].startswith(_mlcs):
end = _mlc(s, end).end()
else:
break
except IndexError:
pass
return values, end
class JSONCDecoder(json.JSONDecoder):
'''
JSONDecoder with support for C-style comments. Similar to JSONC files from
Visual Studio code but without support for trailing commas.
'''
def __init__(self, *args, **kwargs):
json.JSONDecoder.__init__(self, *args, **kwargs)
self.parse_object = parse_object
self.parse_array = parse_array
# we need to recreate the internal scan function ..
self.scan_once = scanner.py_make_scanner(self)
def decode(
self, s, _w=WHITESPACE.match,
_ws=WHITESPACE_STR,
_ilcs=INLINE_COMMENT_STRING_START, _ilc=INLINE_COMMENT.match,
_mlcs=MULTILINE_COMMENT_STRING_START, _mlc=MULTILINE_COMMENT.match
):
idx = 0
try:
while True: # Handle comments and whitespaces
if s[idx] in _ws:
idx = _w(s, idx).end()
elif s[idx:].startswith(_ilcs):
idx = _ilc(s, idx).end()
elif s[idx:].startswith(_mlcs):
idx = _mlc(s, idx).end()
else:
break
except IndexError:
pass
obj, end = self.raw_decode(s, idx)
end = _w(s, end).end()
if end != len(s):
raise JSONDecodeError("Extra data", s, end)
return obj