comparison src/json.c @ 7947:b2922673917a v7.4.1269

commit https://github.com/vim/vim/commit/4f8b8faec31a934920a723053e8dcf47b6fac08c Author: Bram Moolenaar <Bram@vim.org> Date: Sat Feb 6 18:42:07 2016 +0100 patch 7.4.1269 Problem: Encoding {'key':} to JSON doesn't give an error (Tyru) Solution: Give an error.
author Christian Brabandt <cb@256bit.org>
date Sat, 06 Feb 2016 18:45:04 +0100
parents 2679e636e862
children 646d5148fee2
comparison
equal deleted inserted replaced
7946:b91049e93f0b 7947:b2922673917a
14 */ 14 */
15 15
16 #include "vim.h" 16 #include "vim.h"
17 17
18 #if defined(FEAT_EVAL) || defined(PROTO) 18 #if defined(FEAT_EVAL) || defined(PROTO)
19 static int json_encode_item(garray_T *gap, typval_T *val, int copyID); 19 static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int allow_none);
20 static int json_decode_item(js_read_T *reader, typval_T *res); 20 static int json_decode_item(js_read_T *reader, typval_T *res);
21 21
22 /* 22 /*
23 * Encode "val" into a JSON format string. 23 * Encode "val" into a JSON format string.
24 */ 24 */
27 { 27 {
28 garray_T ga; 28 garray_T ga;
29 29
30 /* Store bytes in the growarray. */ 30 /* Store bytes in the growarray. */
31 ga_init2(&ga, 1, 4000); 31 ga_init2(&ga, 1, 4000);
32 json_encode_item(&ga, val, get_copyID()); 32 json_encode_item(&ga, val, get_copyID(), TRUE);
33 return ga.ga_data; 33 return ga.ga_data;
34 } 34 }
35 35
36 /* 36 /*
37 * Encode ["nr", "val"] into a JSON format string. 37 * Encode ["nr", "val"] into a JSON format string.
119 /* 119 /*
120 * Encode "val" into "gap". 120 * Encode "val" into "gap".
121 * Return FAIL or OK. 121 * Return FAIL or OK.
122 */ 122 */
123 static int 123 static int
124 json_encode_item(garray_T *gap, typval_T *val, int copyID) 124 json_encode_item(garray_T *gap, typval_T *val, int copyID, int allow_none)
125 { 125 {
126 char_u numbuf[NUMBUFLEN]; 126 char_u numbuf[NUMBUFLEN];
127 char_u *res; 127 char_u *res;
128 list_T *l; 128 list_T *l;
129 dict_T *d; 129 dict_T *d;
133 case VAR_SPECIAL: 133 case VAR_SPECIAL:
134 switch (val->vval.v_number) 134 switch (val->vval.v_number)
135 { 135 {
136 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break; 136 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
137 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break; 137 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
138 case VVAL_NONE: break; 138 case VVAL_NONE: if (!allow_none)
139 /* TODO: better error */
140 EMSG(_(e_invarg));
141 break;
139 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break; 142 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
140 } 143 }
141 break; 144 break;
142 145
143 case VAR_NUMBER: 146 case VAR_NUMBER:
150 res = val->vval.v_string; 153 res = val->vval.v_string;
151 write_string(gap, res); 154 write_string(gap, res);
152 break; 155 break;
153 156
154 case VAR_FUNC: 157 case VAR_FUNC:
155 /* no JSON equivalent */ 158 /* no JSON equivalent TODO: better error */
156 EMSG(_(e_invarg)); 159 EMSG(_(e_invarg));
157 return FAIL; 160 return FAIL;
158 161
159 case VAR_LIST: 162 case VAR_LIST:
160 l = val->vval.v_list; 163 l = val->vval.v_list;
170 173
171 l->lv_copyID = copyID; 174 l->lv_copyID = copyID;
172 ga_append(gap, '['); 175 ga_append(gap, '[');
173 for (li = l->lv_first; li != NULL && !got_int; ) 176 for (li = l->lv_first; li != NULL && !got_int; )
174 { 177 {
175 if (json_encode_item(gap, &li->li_tv, copyID) == FAIL) 178 if (json_encode_item(gap, &li->li_tv, copyID, TRUE)
179 == FAIL)
176 return FAIL; 180 return FAIL;
177 li = li->li_next; 181 li = li->li_next;
178 if (li != NULL) 182 if (li != NULL)
179 ga_append(gap, ','); 183 ga_append(gap, ',');
180 } 184 }
211 else 215 else
212 ga_append(gap, ','); 216 ga_append(gap, ',');
213 write_string(gap, hi->hi_key); 217 write_string(gap, hi->hi_key);
214 ga_append(gap, ':'); 218 ga_append(gap, ':');
215 if (json_encode_item(gap, &dict_lookup(hi)->di_tv, 219 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
216 copyID) == FAIL) 220 copyID, FALSE) == FAIL)
217 return FAIL; 221 return FAIL;
218 } 222 }
219 ga_append(gap, '}'); 223 ga_append(gap, '}');
220 d->dv_copyID = 0; 224 d->dv_copyID = 0;
221 } 225 }