]> granicus.if.org Git - vim/commitdiff
patch 8.0.0171: JS style JSON does not support single quotes v8.0.0171
authorBram Moolenaar <Bram@vim.org>
Wed, 11 Jan 2017 20:50:08 +0000 (21:50 +0100)
committerBram Moolenaar <Bram@vim.org>
Wed, 11 Jan 2017 20:50:08 +0000 (21:50 +0100)
Problem:    JS style JSON does not support single quotes.
Solution:   Allow for single quotes. (Yasuhiro Matsumoto, closes #1371)

runtime/doc/eval.txt
src/json.c
src/json_test.c
src/testdir/test_json.vim
src/version.c

index b3c73235fef3248e1f848f742b3c29fc8ace2e84..a53b5d23db79faf9e217cab378d2015fe7d00a89 100644 (file)
@@ -5229,6 +5229,7 @@ join({list} [, {sep}])                                    *join()*
 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.
 
index 2cc3a2a3946fbd551b54c6d5419ed2087157c330..faaeab35cc124be344b99f34da6867315808b0a0 100644 (file)
@@ -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 */
index 1a1fdcbada5972b971da31f30715808135c2f591..74463f3d2bbaae8ec9e243429b5896c2e29e09e4 100644 (file)
@@ -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
 
index ecca0388ee52344142e781627f6f01ceb579223c..bd348fc42ebebaa3de13111b88e6a2ce2889a0b4 100644 (file)
@@ -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:")
index 4bc287fccaa7530d5419ee939935f4d6c5b63ae9..dfbbe1831c29bd8463d96ffcd7de4c8c9916d6b1 100644 (file)
@@ -764,6 +764,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    171,
 /**/
     170,
 /**/