forked from aniani/vim
patch 7.4.1279
Problem: jsonencode() is not producing strict JSON. Solution: Add jsencode() and jsdecode(). Make jsonencode() and jsondecode() strict.
This commit is contained in:
47
src/eval.c
47
src/eval.c
@@ -628,6 +628,8 @@ static void f_job_stop(typval_T *argvars, typval_T *rettv);
|
||||
static void f_job_status(typval_T *argvars, typval_T *rettv);
|
||||
#endif
|
||||
static void f_join(typval_T *argvars, typval_T *rettv);
|
||||
static void f_jsdecode(typval_T *argvars, typval_T *rettv);
|
||||
static void f_jsencode(typval_T *argvars, typval_T *rettv);
|
||||
static void f_jsondecode(typval_T *argvars, typval_T *rettv);
|
||||
static void f_jsonencode(typval_T *argvars, typval_T *rettv);
|
||||
static void f_keys(typval_T *argvars, typval_T *rettv);
|
||||
@@ -8206,6 +8208,8 @@ static struct fst
|
||||
{"job_stop", 1, 1, f_job_stop},
|
||||
#endif
|
||||
{"join", 1, 2, f_join},
|
||||
{"jsdecode", 1, 1, f_jsdecode},
|
||||
{"jsencode", 1, 1, f_jsencode},
|
||||
{"jsondecode", 1, 1, f_jsondecode},
|
||||
{"jsonencode", 1, 1, f_jsonencode},
|
||||
{"keys", 1, 1, f_keys},
|
||||
@@ -9829,7 +9833,7 @@ f_ch_open(typval_T *argvars, typval_T *rettv)
|
||||
int port;
|
||||
int waittime = 0;
|
||||
int timeout = 2000;
|
||||
int json_mode = TRUE;
|
||||
ch_mode_T ch_mode = MODE_JSON;
|
||||
int ch_idx;
|
||||
|
||||
/* default: fail */
|
||||
@@ -9868,8 +9872,12 @@ f_ch_open(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
mode = get_dict_string(dict, (char_u *)"mode", FALSE);
|
||||
if (STRCMP(mode, "raw") == 0)
|
||||
json_mode = FALSE;
|
||||
else if (STRCMP(mode, "json") != 0)
|
||||
ch_mode = MODE_RAW;
|
||||
else if (STRCMP(mode, "js") == 0)
|
||||
ch_mode = MODE_JS;
|
||||
else if (STRCMP(mode, "json") == 0)
|
||||
ch_mode = MODE_JSON;
|
||||
else
|
||||
{
|
||||
EMSG2(_(e_invarg2), mode);
|
||||
return;
|
||||
@@ -9891,7 +9899,7 @@ f_ch_open(typval_T *argvars, typval_T *rettv)
|
||||
ch_idx = channel_open((char *)address, port, waittime, NULL);
|
||||
if (ch_idx >= 0)
|
||||
{
|
||||
channel_set_json_mode(ch_idx, json_mode);
|
||||
channel_set_json_mode(ch_idx, ch_mode);
|
||||
channel_set_timeout(ch_idx, timeout);
|
||||
if (callback != NULL && *callback != NUL)
|
||||
channel_set_callback(ch_idx, callback);
|
||||
@@ -9946,7 +9954,7 @@ f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
|
||||
rettv->vval.v_string = NULL;
|
||||
|
||||
id = channel_get_id();
|
||||
text = json_encode_nr_expr(id, &argvars[1]);
|
||||
text = json_encode_nr_expr(id, &argvars[1], 0);
|
||||
if (text == NULL)
|
||||
return;
|
||||
|
||||
@@ -14442,6 +14450,31 @@ f_join(typval_T *argvars, typval_T *rettv)
|
||||
rettv->vval.v_string = NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* "jsdecode()" function
|
||||
*/
|
||||
static void
|
||||
f_jsdecode(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
js_read_T reader;
|
||||
|
||||
reader.js_buf = get_tv_string(&argvars[0]);
|
||||
reader.js_fill = NULL;
|
||||
reader.js_used = 0;
|
||||
if (json_decode_all(&reader, rettv, JSON_JS) != OK)
|
||||
EMSG(_(e_invarg));
|
||||
}
|
||||
|
||||
/*
|
||||
* "jsencode()" function
|
||||
*/
|
||||
static void
|
||||
f_jsencode(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
rettv->v_type = VAR_STRING;
|
||||
rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
|
||||
}
|
||||
|
||||
/*
|
||||
* "jsondecode()" function
|
||||
*/
|
||||
@@ -14453,7 +14486,7 @@ f_jsondecode(typval_T *argvars, typval_T *rettv)
|
||||
reader.js_buf = get_tv_string(&argvars[0]);
|
||||
reader.js_fill = NULL;
|
||||
reader.js_used = 0;
|
||||
if (json_decode_all(&reader, rettv) != OK)
|
||||
if (json_decode_all(&reader, rettv, 0) != OK)
|
||||
EMSG(_(e_invarg));
|
||||
}
|
||||
|
||||
@@ -14464,7 +14497,7 @@ f_jsondecode(typval_T *argvars, typval_T *rettv)
|
||||
f_jsonencode(typval_T *argvars, typval_T *rettv)
|
||||
{
|
||||
rettv->v_type = VAR_STRING;
|
||||
rettv->vval.v_string = json_encode(&argvars[0]);
|
||||
rettv->vval.v_string = json_encode(&argvars[0], 0);
|
||||
}
|
||||
|
||||
/*
|
||||
|
Reference in New Issue
Block a user