Skip to content

Commit ee142ad

Browse files
committed
patch 8.0.0171: JS style JSON does not support single quotes
Problem: JS style JSON does not support single quotes. Solution: Allow for single quotes. (Yasuhiro Matsumoto, closes #1371)
1 parent e32abbe commit ee142ad

File tree

5 files changed

+26
-7
lines changed

5 files changed

+26
-7
lines changed

runtime/doc/eval.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5229,6 +5229,7 @@ join({list} [, {sep}]) *join()*
52295229
js_decode({string}) *js_decode()*
52305230
This is similar to |json_decode()| with these differences:
52315231
- Object key names do not have to be in quotes.
5232+
- Strings can be in single quotes.
52325233
- Empty items in an array (between two commas) are allowed and
52335234
result in v:none items.
52345235

src/json.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ json_skip_white(js_read_T *reader)
378378
}
379379

380380
static int
381-
json_decode_string(js_read_T *reader, typval_T *res)
381+
json_decode_string(js_read_T *reader, typval_T *res, int quote)
382382
{
383383
garray_T ga;
384384
int len;
@@ -389,8 +389,8 @@ json_decode_string(js_read_T *reader, typval_T *res)
389389
if (res != NULL)
390390
ga_init2(&ga, 1, 200);
391391

392-
p = reader->js_buf + reader->js_used + 1; /* skip over " */
393-
while (*p != '"')
392+
p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */
393+
while (*p != quote)
394394
{
395395
/* The JSON is always expected to be utf-8, thus use utf functions
396396
* here. The string is converted below if needed. */
@@ -504,7 +504,7 @@ json_decode_string(js_read_T *reader, typval_T *res)
504504
}
505505

506506
reader->js_used = (int)(p - reader->js_buf);
507-
if (*p == '"')
507+
if (*p == quote)
508508
{
509509
++reader->js_used;
510510
if (res != NULL)
@@ -620,7 +620,8 @@ json_decode_item(js_read_T *reader, typval_T *res, int options)
620620

621621
if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
622622
&& (options & JSON_JS)
623-
&& reader->js_buf[reader->js_used] != '"')
623+
&& reader->js_buf[reader->js_used] != '"'
624+
&& reader->js_buf[reader->js_used] != '\'')
624625
{
625626
char_u *key;
626627

@@ -690,7 +691,17 @@ json_decode_item(js_read_T *reader, typval_T *res, int options)
690691
continue;
691692

692693
case '"': /* string */
693-
retval = json_decode_string(reader, cur_item);
694+
retval = json_decode_string(reader, cur_item, *p);
695+
break;
696+
697+
case '\'':
698+
if (options & JSON_JS)
699+
retval = json_decode_string(reader, cur_item, *p);
700+
else
701+
{
702+
EMSG(_(e_invarg));
703+
retval = FAIL;
704+
}
694705
break;
695706

696707
case ',': /* comma: empty item */

src/json_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ test_fill_called_on_string(void)
181181
reader.js_buf = (char_u *)" \"foo";
182182
reader.js_end = reader.js_buf + STRLEN(reader.js_buf);
183183
reader.js_cookie = " \"foobar\" ";
184-
assert(json_decode_string(&reader, NULL) == OK);
184+
assert(json_decode_string(&reader, NULL, '"') == OK);
185185
}
186186
#endif
187187

src/testdir/test_json.vim

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ func Test_json_decode()
145145
call assert_equal("", json_decode('""'))
146146

147147
call assert_equal({'n': 1}, json_decode('{"n":1,}'))
148+
call assert_fails("call json_decode(\"{'n':'1',}\")", 'E474:')
149+
call assert_fails("call json_decode(\"'n'\")", 'E474:')
148150

149151
call assert_fails('call json_decode("\"")', "E474:")
150152
call assert_fails('call json_decode("blah")', "E474:")
@@ -255,8 +257,11 @@ func Test_js_decode()
255257
call assert_equal(v:none, js_decode(''))
256258
call assert_equal(type(v:none), type(js_decode('')))
257259
call assert_equal("", js_decode('""'))
260+
call assert_equal("", js_decode("''"))
258261

262+
call assert_equal('n', js_decode("'n'"))
259263
call assert_equal({'n': 1}, js_decode('{"n":1,}'))
264+
call assert_equal({'n': '1'}, js_decode("{'n':'1',}"))
260265

261266
call assert_fails('call js_decode("\"")', "E474:")
262267
call assert_fails('call js_decode("blah")', "E474:")

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
171,
767769
/**/
768770
170,
769771
/**/

0 commit comments

Comments
 (0)