Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2d24e94

Browse files
committed
Issue #5067: improve some json error messages.
Patch by Serhiy Storchaka.
1 parent 24319ac commit 2d24e94

5 files changed

Lines changed: 14 additions & 12 deletions

File tree

Doc/library/json.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ Using json.tool from the shell to validate and pretty-print::
9999
"json": "obj"
100100
}
101101
$ echo '{1.2:3.4}' | python -mjson.tool
102-
Expecting property name: line 1 column 1 (char 1)
102+
Expecting property name enclosed in double quotes: line 1 column 1 (char 1)
103103

104104
.. highlight:: python3
105105

Lib/json/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@
9797
"json": "obj"
9898
}
9999
$ echo '{ 1.2:3.4}' | python -m json.tool
100-
Expecting property name: line 1 column 2 (char 2)
100+
Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
101101
"""
102102
__version__ = '2.0.9'
103103
__all__ = [

Lib/json/decoder.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
173173
pairs = object_hook(pairs)
174174
return pairs, end + 1
175175
elif nextchar != '"':
176-
raise ValueError(errmsg("Expecting property name", s, end))
176+
raise ValueError(errmsg(
177+
"Expecting property name enclosed in double quotes", s, end))
177178
end += 1
178179
while True:
179180
key, end = scanstring(s, end, strict)
@@ -183,7 +184,7 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
183184
if s[end:end + 1] != ':':
184185
end = _w(s, end).end()
185186
if s[end:end + 1] != ':':
186-
raise ValueError(errmsg("Expecting : delimiter", s, end))
187+
raise ValueError(errmsg("Expecting ':' delimiter", s, end))
187188
end += 1
188189

189190
try:
@@ -211,12 +212,13 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
211212
if nextchar == '}':
212213
break
213214
elif nextchar != ',':
214-
raise ValueError(errmsg("Expecting , delimiter", s, end - 1))
215+
raise ValueError(errmsg("Expecting ',' delimiter", s, end - 1))
215216
end = _w(s, end).end()
216217
nextchar = s[end:end + 1]
217218
end += 1
218219
if nextchar != '"':
219-
raise ValueError(errmsg("Expecting property name", s, end - 1))
220+
raise ValueError(errmsg(
221+
"Expecting property name enclosed in double quotes", s, end - 1))
220222
if object_pairs_hook is not None:
221223
result = object_pairs_hook(pairs)
222224
return result, end
@@ -250,7 +252,7 @@ def JSONArray(s_and_end, scan_once, _w=WHITESPACE.match, _ws=WHITESPACE_STR):
250252
if nextchar == ']':
251253
break
252254
elif nextchar != ',':
253-
raise ValueError(errmsg("Expecting , delimiter", s, end))
255+
raise ValueError(errmsg("Expecting ',' delimiter", s, end))
254256
try:
255257
if s[end] in _ws:
256258
end += 1

Lib/json/tool.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"json": "obj"
88
}
99
$ echo '{ 1.2:3.4}' | python -m json.tool
10-
Expecting property name: line 1 column 2 (char 2)
10+
Expecting property name enclosed in double quotes: line 1 column 2 (char 2)
1111
1212
"""
1313
import sys

Modules/_json.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -634,7 +634,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
634634

635635
/* read key */
636636
if (str[idx] != '"') {
637-
raise_errmsg("Expecting property name", pystr, idx);
637+
raise_errmsg("Expecting property name enclosed in double quotes", pystr, idx);
638638
goto bail;
639639
}
640640
key = scanstring_unicode(pystr, idx + 1, strict, &next_idx);
@@ -655,7 +655,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
655655
/* skip whitespace between key and : delimiter, read :, skip whitespace */
656656
while (idx <= end_idx && IS_WHITESPACE(str[idx])) idx++;
657657
if (idx > end_idx || str[idx] != ':') {
658-
raise_errmsg("Expecting : delimiter", pystr, idx);
658+
raise_errmsg("Expecting ':' delimiter", pystr, idx);
659659
goto bail;
660660
}
661661
idx++;
@@ -695,7 +695,7 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
695695
break;
696696
}
697697
else if (str[idx] != ',') {
698-
raise_errmsg("Expecting , delimiter", pystr, idx);
698+
raise_errmsg("Expecting ',' delimiter", pystr, idx);
699699
goto bail;
700700
}
701701
idx++;
@@ -777,7 +777,7 @@ _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssi
777777
break;
778778
}
779779
else if (str[idx] != ',') {
780-
raise_errmsg("Expecting , delimiter", pystr, idx);
780+
raise_errmsg("Expecting ',' delimiter", pystr, idx);
781781
goto bail;
782782
}
783783
idx++;

0 commit comments

Comments
 (0)