mirror of
https://github.com/vim/vim.git
synced 2025-09-23 03:43:49 -04:00
patch 7.4.1269
Problem: Encoding {'key':} to JSON doesn't give an error (Tyru) Solution: Give an error.
This commit is contained in:
18
src/json.c
18
src/json.c
@@ -16,7 +16,7 @@
|
|||||||
#include "vim.h"
|
#include "vim.h"
|
||||||
|
|
||||||
#if defined(FEAT_EVAL) || defined(PROTO)
|
#if defined(FEAT_EVAL) || defined(PROTO)
|
||||||
static int json_encode_item(garray_T *gap, typval_T *val, int copyID);
|
static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int allow_none);
|
||||||
static int json_decode_item(js_read_T *reader, typval_T *res);
|
static int json_decode_item(js_read_T *reader, typval_T *res);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -29,7 +29,7 @@ json_encode(typval_T *val)
|
|||||||
|
|
||||||
/* Store bytes in the growarray. */
|
/* Store bytes in the growarray. */
|
||||||
ga_init2(&ga, 1, 4000);
|
ga_init2(&ga, 1, 4000);
|
||||||
json_encode_item(&ga, val, get_copyID());
|
json_encode_item(&ga, val, get_copyID(), TRUE);
|
||||||
return ga.ga_data;
|
return ga.ga_data;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ write_string(garray_T *gap, char_u *str)
|
|||||||
* Return FAIL or OK.
|
* Return FAIL or OK.
|
||||||
*/
|
*/
|
||||||
static int
|
static int
|
||||||
json_encode_item(garray_T *gap, typval_T *val, int copyID)
|
json_encode_item(garray_T *gap, typval_T *val, int copyID, int allow_none)
|
||||||
{
|
{
|
||||||
char_u numbuf[NUMBUFLEN];
|
char_u numbuf[NUMBUFLEN];
|
||||||
char_u *res;
|
char_u *res;
|
||||||
@@ -135,7 +135,10 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
|
|||||||
{
|
{
|
||||||
case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
|
case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
|
||||||
case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
|
case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
|
||||||
case VVAL_NONE: break;
|
case VVAL_NONE: if (!allow_none)
|
||||||
|
/* TODO: better error */
|
||||||
|
EMSG(_(e_invarg));
|
||||||
|
break;
|
||||||
case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
|
case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
@@ -152,7 +155,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case VAR_FUNC:
|
case VAR_FUNC:
|
||||||
/* no JSON equivalent */
|
/* no JSON equivalent TODO: better error */
|
||||||
EMSG(_(e_invarg));
|
EMSG(_(e_invarg));
|
||||||
return FAIL;
|
return FAIL;
|
||||||
|
|
||||||
@@ -172,7 +175,8 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
|
|||||||
ga_append(gap, '[');
|
ga_append(gap, '[');
|
||||||
for (li = l->lv_first; li != NULL && !got_int; )
|
for (li = l->lv_first; li != NULL && !got_int; )
|
||||||
{
|
{
|
||||||
if (json_encode_item(gap, &li->li_tv, copyID) == FAIL)
|
if (json_encode_item(gap, &li->li_tv, copyID, TRUE)
|
||||||
|
== FAIL)
|
||||||
return FAIL;
|
return FAIL;
|
||||||
li = li->li_next;
|
li = li->li_next;
|
||||||
if (li != NULL)
|
if (li != NULL)
|
||||||
@@ -213,7 +217,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
|
|||||||
write_string(gap, hi->hi_key);
|
write_string(gap, hi->hi_key);
|
||||||
ga_append(gap, ':');
|
ga_append(gap, ':');
|
||||||
if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
|
if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
|
||||||
copyID) == FAIL)
|
copyID, FALSE) == FAIL)
|
||||||
return FAIL;
|
return FAIL;
|
||||||
}
|
}
|
||||||
ga_append(gap, '}');
|
ga_append(gap, '}');
|
||||||
|
@@ -74,6 +74,7 @@ func Test_encode()
|
|||||||
|
|
||||||
call assert_fails('echo jsonencode(function("tr"))', 'E474:')
|
call assert_fails('echo jsonencode(function("tr"))', 'E474:')
|
||||||
call assert_fails('echo jsonencode([function("tr")])', 'E474:')
|
call assert_fails('echo jsonencode([function("tr")])', 'E474:')
|
||||||
|
call assert_fails('echo jsonencode({"key":v:none})', 'E474:')
|
||||||
endfunc
|
endfunc
|
||||||
|
|
||||||
func Test_decode()
|
func Test_decode()
|
||||||
|
@@ -742,6 +742,8 @@ static char *(features[]) =
|
|||||||
|
|
||||||
static int included_patches[] =
|
static int included_patches[] =
|
||||||
{ /* Add new patch number below this line */
|
{ /* Add new patch number below this line */
|
||||||
|
/**/
|
||||||
|
1269,
|
||||||
/**/
|
/**/
|
||||||
1268,
|
1268,
|
||||||
/**/
|
/**/
|
||||||
|
Reference in New Issue
Block a user