changeset 13031:c35d266308c2 v8.0.1391

patch 8.0.1391: encoding empty string to JSON sometimes gives "null" commit https://github.com/vim/vim/commit/b29d328eb403a243431f4d0d5b360b4ac46bbc3e Author: Bram Moolenaar <Bram@vim.org> Date: Fri Dec 15 21:25:01 2017 +0100 patch 8.0.1391: encoding empty string to JSON sometimes gives "null" Problem: Encoding empty string to JSON sometimes gives "null". Solution: Handle NULL string as empty string. (closes https://github.com/vim/vim/issues/2446)
author Christian Brabandt <cb@256bit.org>
date Fri, 15 Dec 2017 21:30:05 +0100
parents 92a0a33f8296
children 91c5b705b55c
files src/json.c src/testdir/test_json.vim src/version.c
diffstat 3 files changed, 9 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/json.c
+++ b/src/json.c
@@ -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
 	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
 	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)
--- a/src/testdir/test_json.vim
+++ b/src/testdir/test_json.vim
@@ -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
--- a/src/version.c
+++ b/src/version.c
@@ -772,6 +772,8 @@ static char *(features[]) =
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    1391,
+/**/
     1390,
 /**/
     1389,