diff src/eval.c @ 7967:45ea5ebf3a98 v7.4.1279

commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a Author: Bram Moolenaar <Bram@vim.org> Date: Sun Feb 7 19:19:53 2016 +0100 patch 7.4.1279 Problem: jsonencode() is not producing strict JSON. Solution: Add jsencode() and jsdecode(). Make jsonencode() and jsondecode() strict.
author Christian Brabandt <cb@256bit.org>
date Sun, 07 Feb 2016 19:30:05 +0100
parents d96e38f35e2d
children 7224f5e9c36a
line wrap: on
line diff
--- a/src/eval.c
+++ b/src/eval.c
@@ -628,6 +628,8 @@ static void f_job_stop(typval_T *argvars
 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 *r
     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 *r
 	{
 	    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 *r
     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_
     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 *rett
 }
 
 /*
+ * "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
     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
 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);
 }
 
 /*