summaryrefslogtreecommitdiff
path: root/src/eval.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2016-02-07 19:19:53 +0100
committerBram Moolenaar <Bram@vim.org>2016-02-07 19:19:53 +0100
commit595e64e259faefb330866852e1b9f6168544572a (patch)
tree87986bc108647e7c597195cea325ca130db69a40 /src/eval.c
parent55fab439a6f3bba6dbe780ac034b84d5822a1a96 (diff)
downloadvim-git-595e64e259faefb330866852e1b9f6168544572a.tar.gz
patch 7.4.1279v7.4.1279
Problem: jsonencode() is not producing strict JSON. Solution: Add jsencode() and jsdecode(). Make jsonencode() and jsondecode() strict.
Diffstat (limited to 'src/eval.c')
-rw-r--r--src/eval.c47
1 files changed, 40 insertions, 7 deletions
diff --git a/src/eval.c b/src/eval.c
index bd0040e66..4b1250b59 100644
--- a/src/eval.c
+++ b/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;
@@ -14443,6 +14451,31 @@ f_join(typval_T *argvars, typval_T *rettv)
}
/*
+ * "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
*/
static void
@@ -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);
}
/*