Skip to content

Commit b29d328

Browse files
committed
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 #2446)
1 parent 4697399 commit b29d328

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

src/json.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ write_string(garray_T *gap, char_u *str)
9292
char_u numbuf[NUMBUFLEN];
9393

9494
if (res == NULL)
95-
ga_concat(gap, (char_u *)"null");
95+
ga_concat(gap, (char_u *)"\"\"");
9696
else
9797
{
9898
#if defined(FEAT_MBYTE) && defined(USE_ICONV)
@@ -237,7 +237,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
237237
case VAR_LIST:
238238
l = val->vval.v_list;
239239
if (l == NULL)
240-
ga_concat(gap, (char_u *)"null");
240+
ga_concat(gap, (char_u *)"[]");
241241
else
242242
{
243243
if (l->lv_copyID == copyID)
@@ -272,7 +272,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
272272
case VAR_DICT:
273273
d = val->vval.v_dict;
274274
if (d == NULL)
275-
ga_concat(gap, (char_u *)"null");
275+
ga_concat(gap, (char_u *)"{}");
276276
else
277277
{
278278
if (d->dv_copyID == copyID)

src/testdir/test_json.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ func Test_json_encode()
103103
call assert_fails('echo json_encode(function("tr"))', 'E474:')
104104
call assert_fails('echo json_encode([function("tr")])', 'E474:')
105105

106+
call assert_equal('{"a":""}', json_encode({'a': test_null_string()}))
107+
call assert_equal('{"a":[]}', json_encode({"a": test_null_list()}))
108+
call assert_equal('{"a":{}}', json_encode({"a": test_null_dict()}))
109+
106110
silent! let res = json_encode(function("tr"))
107111
call assert_equal("", res)
108112
endfunc

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,8 @@ static char *(features[]) =
771771

772772
static int included_patches[] =
773773
{ /* Add new patch number below this line */
774+
/**/
775+
1391,
774776
/**/
775777
1390,
776778
/**/

0 commit comments

Comments
 (0)