diff options
author | Bram Moolenaar <Bram@vim.org> | 2017-01-11 21:50:08 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2017-01-11 21:50:08 +0100 |
commit | ee142add229cbcd58bc76d59f23e02517df14379 (patch) | |
tree | 5b6252210596b5a331e00ab4a89ea5017fc07a37 /src | |
parent | e32abbe42c921c5e521420417abe5bc301e540b3 (diff) |
patch 8.0.0171: JS style JSON does not support single quotesv8.0.0171
Problem: JS style JSON does not support single quotes.
Solution: Allow for single quotes. (Yasuhiro Matsumoto, closes #1371)
Diffstat (limited to 'src')
-rw-r--r-- | src/json.c | 23 | ||||
-rw-r--r-- | src/json_test.c | 2 | ||||
-rw-r--r-- | src/testdir/test_json.vim | 5 | ||||
-rw-r--r-- | src/version.c | 2 |
4 files changed, 25 insertions, 7 deletions
diff --git a/src/json.c b/src/json.c index 2cc3a2a394..faaeab35cc 100644 --- a/src/json.c +++ b/src/json.c @@ -378,7 +378,7 @@ json_skip_white(js_read_T *reader) } 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; @@ -389,8 +389,8 @@ json_decode_string(js_read_T *reader, typval_T *res) 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. */ @@ -504,7 +504,7 @@ json_decode_string(js_read_T *reader, typval_T *res) } reader->js_used = (int)(p - reader->js_buf); - if (*p == '"') + if (*p == quote) { ++reader->js_used; if (res != NULL) @@ -620,7 +620,8 @@ json_decode_item(js_read_T *reader, typval_T *res, int options) 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; @@ -690,7 +691,17 @@ json_decode_item(js_read_T *reader, typval_T *res, int options) 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 */ diff --git a/src/json_test.c b/src/json_test.c index 1a1fdcbada..74463f3d2b 100644 --- a/src/json_test.c +++ b/src/json_test.c @@ -181,7 +181,7 @@ test_fill_called_on_string(void) 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 diff --git a/src/testdir/test_json.vim b/src/testdir/test_json.vim index ecca0388ee..bd348fc42e 100644 --- a/src/testdir/test_json.vim +++ b/src/testdir/test_json.vim @@ -145,6 +145,8 @@ func Test_json_decode() 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:") @@ -255,8 +257,11 @@ func Test_js_decode() 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:") diff --git a/src/version.c b/src/version.c index 4bc287fcca..dfbbe1831c 100644 --- a/src/version.c +++ b/src/version.c @@ -765,6 +765,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 171, +/**/ 170, /**/ 169, |