diff src/json.c @ 9969:176e34b0d678 v7.4.2258

commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e Author: Bram Moolenaar <Bram@vim.org> Date: Fri Aug 26 17:58:53 2016 +0200 patch 7.4.2258 Problem: Two JSON messages are sent without a separator. Solution: Separate messages with a NL. (closes https://github.com/vim/vim/issues/1001)
author Christian Brabandt <cb@256bit.org>
date Fri, 26 Aug 2016 18:00:08 +0200
parents 32e34e574716
children 4aead6a9b7a9
line wrap: on
line diff
--- a/src/json.c
+++ b/src/json.c
@@ -23,9 +23,26 @@ static int json_decode_item(js_read_T *r
 
 /*
  * Encode "val" into a JSON format string.
+ * The result is added to "gap"
+ * Returns FAIL on failure and makes gap->ga_data empty.
+ */
+    static int
+json_encode_gap(garray_T *gap, typval_T *val, int options)
+{
+    if (json_encode_item(gap, val, get_copyID(), options) == FAIL)
+    {
+	ga_clear(gap);
+	gap->ga_data = vim_strsave((char_u *)"");
+	return FAIL;
+    }
+    return OK;
+}
+
+/*
+ * Encode "val" into a JSON format string.
  * The result is in allocated memory.
  * The result is empty when encoding fails.
- * "options" can be JSON_JS or zero;
+ * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
  */
     char_u *
 json_encode(typval_T *val, int options)
@@ -34,17 +51,13 @@ json_encode(typval_T *val, int options)
 
     /* Store bytes in the growarray. */
     ga_init2(&ga, 1, 4000);
-    if (json_encode_item(&ga, val, get_copyID(), options) == FAIL)
-    {
-	vim_free(ga.ga_data);
-	return vim_strsave((char_u *)"");
-    }
+    json_encode_gap(&ga, val, options);
     return ga.ga_data;
 }
 
 /*
  * Encode ["nr", "val"] into a JSON format string in allocated memory.
- * "options" can be JSON_JS or zero;
+ * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
  * Returns NULL when out of memory.
  */
     char_u *
@@ -52,7 +65,7 @@ json_encode_nr_expr(int nr, typval_T *va
 {
     typval_T	listtv;
     typval_T	nrtv;
-    char_u	*text;
+    garray_T	ga;
 
     nrtv.v_type = VAR_NUMBER;
     nrtv.vval.v_number = nr;
@@ -65,9 +78,11 @@ json_encode_nr_expr(int nr, typval_T *va
 	return NULL;
     }
 
-    text = json_encode(&listtv, options);
+    ga_init2(&ga, 1, 4000);
+    if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL))
+	ga_append(&ga, '\n');
     list_unref(listtv.vval.v_list);
-    return text;
+    return ga.ga_data;
 }
 
     static void