]> granicus.if.org Git - vim/commitdiff
patch 8.0.1391: encoding empty string to JSON sometimes gives "null" v8.0.1391
authorBram Moolenaar <Bram@vim.org>
Fri, 15 Dec 2017 20:25:01 +0000 (21:25 +0100)
committerBram Moolenaar <Bram@vim.org>
Fri, 15 Dec 2017 20:25:01 +0000 (21:25 +0100)
Problem:    Encoding empty string to JSON sometimes gives "null".
Solution:   Handle NULL string as empty string. (closes #2446)

src/json.c
src/testdir/test_json.vim
src/version.c

index e5e85eb6dbb8126cf893a43019337a4e2711b584..6f914ea03dd5fef0676e75c2e6df27056f54c643 100644 (file)
@@ -92,7 +92,7 @@ write_string(garray_T *gap, char_u *str)
     char_u     numbuf[NUMBUFLEN];
 
     if (res == NULL)
-       ga_concat(gap, (char_u *)"null");
+       ga_concat(gap, (char_u *)"\"\"");
     else
     {
 #if defined(FEAT_MBYTE) && defined(USE_ICONV)
@@ -237,7 +237,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
        case VAR_LIST:
            l = val->vval.v_list;
            if (l == NULL)
-               ga_concat(gap, (char_u *)"null");
+               ga_concat(gap, (char_u *)"[]");
            else
            {
                if (l->lv_copyID == copyID)
@@ -272,7 +272,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
        case VAR_DICT:
            d = val->vval.v_dict;
            if (d == NULL)
-               ga_concat(gap, (char_u *)"null");
+               ga_concat(gap, (char_u *)"{}");
            else
            {
                if (d->dv_copyID == copyID)
index 6c3d16ce5b22b12c2ed42c7df1df8dd52f4e597d..acd2ea85b5d44b690d104cd8d4e4af11efbe7be4 100644 (file)
@@ -103,6 +103,10 @@ func Test_json_encode()
   call assert_fails('echo json_encode(function("tr"))', 'E474:')
   call assert_fails('echo json_encode([function("tr")])', 'E474:')
 
+  call assert_equal('{"a":""}', json_encode({'a': test_null_string()}))
+  call assert_equal('{"a":[]}', json_encode({"a": test_null_list()}))
+  call assert_equal('{"a":{}}', json_encode({"a": test_null_dict()}))
+
   silent! let res = json_encode(function("tr"))
   call assert_equal("", res)
 endfunc
index 16ba82a3bdafe715ff675744be44893e84db7a52..af507e4b7c1c9257a559d66cac98fc76a05ff675 100644 (file)
@@ -771,6 +771,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1391,
 /**/
     1390,
 /**/