]> granicus.if.org Git - vim/commitdiff
patch 7.4.1166 v7.4.1166
authorBram Moolenaar <Bram@vim.org>
Sun, 24 Jan 2016 15:49:11 +0000 (16:49 +0100)
committerBram Moolenaar <Bram@vim.org>
Sun, 24 Jan 2016 15:49:11 +0000 (16:49 +0100)
Problem:    Can't encode a Funcref into JSON.  jsonencode() doesn't handle the
            same list or dict twice properly.  (Nikolay Pavlov)
Solution:   Give an error.  Reset copyID when the list or dict is finished.

src/json.c
src/proto/json.pro
src/testdir/test_json.vim
src/version.c

index e4473830a694c39f1d421acdf9257a463e3d49e8..da8beea874700ba58c7920d8bae6ebcaba0c5d2b 100644 (file)
@@ -16,6 +16,7 @@
 #include "vim.h"
 
 #if defined(FEAT_EVAL) || defined(PROTO)
+static int json_encode_item(garray_T *gap, typval_T *val, int copyID);
 static void json_decode_item(js_read_T *reader, typval_T *res);
 
 /*
@@ -83,7 +84,11 @@ write_string(garray_T *gap, char_u *str)
     }
 }
 
-    void
+/*
+ * Encode "val" into "gap".
+ * Return FAIL or OK.
+ */
+    static int
 json_encode_item(garray_T *gap, typval_T *val, int copyID)
 {
     char_u     numbuf[NUMBUFLEN];
@@ -94,7 +99,7 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
     switch (val->v_type)
     {
        case VAR_SPECIAL:
-           switch(val->vval.v_number)
+           switch (val->vval.v_number)
            {
                case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
                case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
@@ -115,8 +120,9 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
            break;
 
        case VAR_FUNC:
-           /* no JSON equivalent, skip */
-           break;
+           /* no JSON equivalent */
+           EMSG(_(e_invarg));
+           return FAIL;
 
        case VAR_LIST:
            l = val->vval.v_list;
@@ -134,12 +140,14 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
                    ga_append(gap, '[');
                    for (li = l->lv_first; li != NULL && !got_int; )
                    {
-                       json_encode_item(gap, &li->li_tv, copyID);
+                       if (json_encode_item(gap, &li->li_tv, copyID) == FAIL)
+                           return FAIL;
                        li = li->li_next;
                        if (li != NULL)
                            ga_append(gap, ',');
                    }
                    ga_append(gap, ']');
+                   l->lv_copyID = 0;
                }
            }
            break;
@@ -172,10 +180,12 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
                                ga_append(gap, ',');
                            write_string(gap, hi->hi_key);
                            ga_append(gap, ':');
-                           json_encode_item(gap, &dict_lookup(hi)->di_tv,
-                                                                     copyID);
+                           if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
+                                                             copyID) == FAIL)
+                               return FAIL;
                        }
                    ga_append(gap, '}');
+                   d->dv_copyID = 0;
                }
            }
            break;
@@ -187,7 +197,9 @@ json_encode_item(garray_T *gap, typval_T *val, int copyID)
            break;
 #endif
        default: EMSG2(_(e_intern2), "json_encode_item()"); break;
+                return FAIL;
     }
+    return OK;
 }
 
 /*
index 6920f516eb0e4e1ce42afb002d7c950ca7d32346..06635456b03754540ddeb5c03e8a5b10b78e485d 100644 (file)
@@ -1,5 +1,4 @@
 /* json.c */
 char_u *json_encode(typval_T *val);
-void json_encode_item(garray_T *gap, typval_T *val, int copyID);
 void json_decode(js_read_T *reader, typval_T *res);
 /* vim: set ft=c : */
index a1cfa93ac88652dd6a8970bbbaf7cd4319229f21..beabec883e0b6381c30cccf6f8beb5c3b0c8b358 100644 (file)
@@ -27,6 +27,9 @@ let s:varl2 = [1, 2, 3]
 let l2 = ['a', s:varl2, 'c']
 let s:varl2[1] = l2
 let s:varl2x = [1, ["a", [], "c"], 3]
+let s:jsonl3 = '[[1,2],[1,2]]'
+let l3 = [1, 2]
+let s:varl3 = [l3, l3]
 
 let s:jsond1 = '{"a":1,"b":"bee","c":[1,2]}'
 let s:vard1 = {"a": 1, "b": "bee","c": [1,2]}
@@ -36,6 +39,9 @@ let s:vard2 = {"1": 1, "2": 2, "3": 3}
 let d2 = {"a": "aa", "b": s:vard2, "c": "cc"}
 let s:vard2["2"] = d2
 let s:vard2x = {"1": 1, "2": {"a": "aa", "b": {}, "c": "cc"}, "3": 3}
+let d3 = {"a": 1, "b": 2}
+let s:vard3 = {"x": d3, "y": d3}
+let s:jsond3 = '{"x":{"a":1,"b":2},"y":{"a":1,"b":2}}'
 
 let s:jsonvals = '[true,false,,null]'
 let s:varvals = [v:true, v:false, v:none, v:null]
@@ -58,11 +64,16 @@ func Test_encode()
 
   call assert_equal(s:jsonl1, jsonencode(s:varl1))
   call assert_equal(s:jsonl2, jsonencode(s:varl2))
+  call assert_equal(s:jsonl3, jsonencode(s:varl3))
 
   call assert_equal(s:jsond1, jsonencode(s:vard1))
   call assert_equal(s:jsond2, jsonencode(s:vard2))
+  call assert_equal(s:jsond3, jsonencode(s:vard3))
 
   call assert_equal(s:jsonvals, jsonencode(s:varvals))
+
+  call assert_fails('echo jsonencode(function("tr"))', 'E474:')
+  call assert_fails('echo jsonencode([function("tr")])', 'E474:')
 endfunc
 
 func Test_decode()
@@ -84,9 +95,11 @@ func Test_decode()
   call assert_equal(s:varl1, jsondecode(s:jsonl1))
   call assert_equal(s:varl2x, jsondecode(s:jsonl2))
   call assert_equal(s:varl2x, jsondecode(s:jsonl2s))
+  call assert_equal(s:varl3, jsondecode(s:jsonl3))
 
   call assert_equal(s:vard1, jsondecode(s:jsond1))
   call assert_equal(s:vard2x, jsondecode(s:jsond2))
+  call assert_equal(s:vard3, jsondecode(s:jsond3))
 
   call assert_equal(s:varvals, jsondecode(s:jsonvals))
 
index 27496b9f30f176671c1455a354a3cfa4f9d4b34a..936fe0aa9229b877bc63a0fa815afe249d986c24 100644 (file)
@@ -741,6 +741,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    1166,
 /**/
     1165,
 /**/