Problem: JS style JSON does not support single quotes.
Solution: Allow for single quotes. (Yasuhiro Matsumoto, closes #1371)
js_decode({string}) *js_decode()*
This is similar to |json_decode()| with these differences:
- Object key names do not have to be in quotes.
+ - Strings can be in single quotes.
- Empty items in an array (between two commas) are allowed and
result in v:none items.
}
static int
-json_decode_string(js_read_T *reader, typval_T *res)
+json_decode_string(js_read_T *reader, typval_T *res, int quote)
{
garray_T ga;
int len;
if (res != NULL)
ga_init2(&ga, 1, 200);
- p = reader->js_buf + reader->js_used + 1; /* skip over " */
- while (*p != '"')
+ p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */
+ while (*p != quote)
{
/* The JSON is always expected to be utf-8, thus use utf functions
* here. The string is converted below if needed. */
}
reader->js_used = (int)(p - reader->js_buf);
- if (*p == '"')
+ if (*p == quote)
{
++reader->js_used;
if (res != NULL)
if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
&& (options & JSON_JS)
- && reader->js_buf[reader->js_used] != '"')
+ && reader->js_buf[reader->js_used] != '"'
+ && reader->js_buf[reader->js_used] != '\'')
{
char_u *key;
continue;
case '"': /* string */
- retval = json_decode_string(reader, cur_item);
+ retval = json_decode_string(reader, cur_item, *p);
+ break;
+
+ case '\'':
+ if (options & JSON_JS)
+ retval = json_decode_string(reader, cur_item, *p);
+ else
+ {
+ EMSG(_(e_invarg));
+ retval = FAIL;
+ }
break;
case ',': /* comma: empty item */
reader.js_buf = (char_u *)" \"foo";
reader.js_end = reader.js_buf + STRLEN(reader.js_buf);
reader.js_cookie = " \"foobar\" ";
- assert(json_decode_string(&reader, NULL) == OK);
+ assert(json_decode_string(&reader, NULL, '"') == OK);
}
#endif
call assert_equal("", json_decode('""'))
call assert_equal({'n': 1}, json_decode('{"n":1,}'))
+ call assert_fails("call json_decode(\"{'n':'1',}\")", 'E474:')
+ call assert_fails("call json_decode(\"'n'\")", 'E474:')
call assert_fails('call json_decode("\"")', "E474:")
call assert_fails('call json_decode("blah")', "E474:")
call assert_equal(v:none, js_decode(''))
call assert_equal(type(v:none), type(js_decode('')))
call assert_equal("", js_decode('""'))
+ call assert_equal("", js_decode("''"))
+ call assert_equal('n', js_decode("'n'"))
call assert_equal({'n': 1}, js_decode('{"n":1,}'))
+ call assert_equal({'n': '1'}, js_decode("{'n':'1',}"))
call assert_fails('call js_decode("\"")', "E474:")
call assert_fails('call js_decode("blah")', "E474:")
static int included_patches[] =
{ /* Add new patch number below this line */
+/**/
+ 171,
/**/
170,
/**/