blob: 4ec4411fb233b572b3c63e8f52c9606481789495 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * json.c: Encoding and decoding JSON.
12 *
Bram Moolenaar009d84a2016-01-28 14:12:00 +010013 * Follows this standard: https://tools.ietf.org/html/rfc7159.html
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010015#define USING_FLOAT_STUFF
Bram Moolenaar520e1e42016-01-23 19:46:28 +010016
17#include "vim.h"
18
19#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010020
Bram Moolenaar595e64e2016-02-07 19:19:53 +010021static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int options);
22static int json_decode_item(js_read_T *reader, typval_T *res, int options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023
24/*
25 * Encode "val" into a JSON format string.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020026 * The result is added to "gap"
27 * Returns FAIL on failure and makes gap->ga_data empty.
28 */
29 static int
30json_encode_gap(garray_T *gap, typval_T *val, int options)
31{
32 if (json_encode_item(gap, val, get_copyID(), options) == FAIL)
33 {
34 ga_clear(gap);
35 gap->ga_data = vim_strsave((char_u *)"");
36 return FAIL;
37 }
38 return OK;
39}
40
41/*
42 * Encode "val" into a JSON format string.
Bram Moolenaar55fab432016-02-07 16:53:13 +010043 * The result is in allocated memory.
44 * The result is empty when encoding fails.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020045 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaar520e1e42016-01-23 19:46:28 +010046 */
47 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010048json_encode(typval_T *val, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010049{
50 garray_T ga;
51
52 /* Store bytes in the growarray. */
53 ga_init2(&ga, 1, 4000);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020054 json_encode_gap(&ga, val, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010055 return ga.ga_data;
56}
57
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010058/*
Bram Moolenaar55fab432016-02-07 16:53:13 +010059 * Encode ["nr", "val"] into a JSON format string in allocated memory.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020060 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010061 * Returns NULL when out of memory.
62 */
63 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010064json_encode_nr_expr(int nr, typval_T *val, int options)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010065{
66 typval_T listtv;
67 typval_T nrtv;
Bram Moolenaarf1f07922016-08-26 17:58:53 +020068 garray_T ga;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010069
70 nrtv.v_type = VAR_NUMBER;
71 nrtv.vval.v_number = nr;
72 if (rettv_list_alloc(&listtv) == FAIL)
73 return NULL;
74 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
75 || list_append_tv(listtv.vval.v_list, val) == FAIL)
76 {
77 list_unref(listtv.vval.v_list);
78 return NULL;
79 }
80
Bram Moolenaarf1f07922016-08-26 17:58:53 +020081 ga_init2(&ga, 1, 4000);
82 if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL))
83 ga_append(&ga, '\n');
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010084 list_unref(listtv.vval.v_list);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020085 return ga.ga_data;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010086}
87
Bram Moolenaar520e1e42016-01-23 19:46:28 +010088 static void
89write_string(garray_T *gap, char_u *str)
90{
91 char_u *res = str;
92 char_u numbuf[NUMBUFLEN];
93
94 if (res == NULL)
95 ga_concat(gap, (char_u *)"null");
96 else
97 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +010098#if defined(FEAT_MBYTE) && defined(USE_ICONV)
99 vimconv_T conv;
100 char_u *converted = NULL;
101
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100102 if (!enc_utf8)
103 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100104 /* Convert the text from 'encoding' to utf-8, the JSON string is
105 * always utf-8. */
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100106 conv.vc_type = CONV_NONE;
107 convert_setup(&conv, p_enc, (char_u*)"utf-8");
108 if (conv.vc_type != CONV_NONE)
109 converted = res = string_convert(&conv, res, NULL);
110 convert_setup(&conv, NULL, NULL);
111 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100112#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100113 ga_append(gap, '"');
114 while (*res != NUL)
115 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100116 int c;
117#ifdef FEAT_MBYTE
118 /* always use utf-8 encoding, ignore 'encoding' */
119 c = utf_ptr2char(res);
120#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100121 c = *res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100122#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100123
124 switch (c)
125 {
126 case 0x08:
127 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
128 case 0x09:
129 ga_append(gap, '\\'); ga_append(gap, 't'); break;
130 case 0x0a:
131 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
132 case 0x0c:
133 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
134 case 0x0d:
135 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
136 case 0x22: /* " */
137 case 0x5c: /* \ */
138 ga_append(gap, '\\');
139 ga_append(gap, c);
140 break;
141 default:
142 if (c >= 0x20)
143 {
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100144#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100145 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100146#else
147 numbuf[0] = c;
148 numbuf[1] = NUL;
149#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100150 ga_concat(gap, numbuf);
151 }
152 else
153 {
154 vim_snprintf((char *)numbuf, NUMBUFLEN,
155 "\\u%04lx", (long)c);
156 ga_concat(gap, numbuf);
157 }
158 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100159#ifdef FEAT_MBYTE
160 res += utf_ptr2len(res);
161#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100162 ++res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100163#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100164 }
165 ga_append(gap, '"');
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100166#if defined(FEAT_MBYTE) && defined(USE_ICONV)
167 vim_free(converted);
168#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100169 }
170}
171
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100172/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100173 * Return TRUE if "key" can be used without quotes.
174 * That is when it starts with a letter and only contains letters, digits and
175 * underscore.
176 */
177 static int
178is_simple_key(char_u *key)
179{
180 char_u *p;
181
182 if (!ASCII_ISALPHA(*key))
183 return FALSE;
184 for (p = key + 1; *p != NUL; ++p)
185 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
186 return FALSE;
187 return TRUE;
188}
189
190/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100191 * Encode "val" into "gap".
192 * Return FAIL or OK.
193 */
194 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100195json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100196{
197 char_u numbuf[NUMBUFLEN];
198 char_u *res;
199 list_T *l;
200 dict_T *d;
201
202 switch (val->v_type)
203 {
204 case VAR_SPECIAL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100205 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100206 {
207 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
208 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100209 case VVAL_NONE: if ((options & JSON_JS) != 0
210 && (options & JSON_NO_NONE) == 0)
211 /* empty item */
212 break;
213 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100214 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
215 }
216 break;
217
218 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200219 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
220 val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100221 ga_concat(gap, numbuf);
222 break;
223
224 case VAR_STRING:
225 res = val->vval.v_string;
226 write_string(gap, res);
227 break;
228
229 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100230 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100231 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100232 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100233 /* no JSON equivalent TODO: better error */
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100234 EMSG(_(e_invarg));
235 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100236
237 case VAR_LIST:
238 l = val->vval.v_list;
239 if (l == NULL)
240 ga_concat(gap, (char_u *)"null");
241 else
242 {
243 if (l->lv_copyID == copyID)
244 ga_concat(gap, (char_u *)"[]");
245 else
246 {
247 listitem_T *li;
248
249 l->lv_copyID = copyID;
250 ga_append(gap, '[');
251 for (li = l->lv_first; li != NULL && !got_int; )
252 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100253 if (json_encode_item(gap, &li->li_tv, copyID,
254 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100255 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100256 if ((options & JSON_JS)
257 && li->li_next == NULL
258 && li->li_tv.v_type == VAR_SPECIAL
259 && li->li_tv.vval.v_number == VVAL_NONE)
260 /* add an extra comma if the last item is v:none */
261 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100262 li = li->li_next;
263 if (li != NULL)
264 ga_append(gap, ',');
265 }
266 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100267 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100268 }
269 }
270 break;
271
272 case VAR_DICT:
273 d = val->vval.v_dict;
274 if (d == NULL)
275 ga_concat(gap, (char_u *)"null");
276 else
277 {
278 if (d->dv_copyID == copyID)
279 ga_concat(gap, (char_u *)"{}");
280 else
281 {
282 int first = TRUE;
283 int todo = (int)d->dv_hashtab.ht_used;
284 hashitem_T *hi;
285
286 d->dv_copyID = copyID;
287 ga_append(gap, '{');
288
289 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
290 ++hi)
291 if (!HASHITEM_EMPTY(hi))
292 {
293 --todo;
294 if (first)
295 first = FALSE;
296 else
297 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100298 if ((options & JSON_JS)
299 && is_simple_key(hi->hi_key))
300 ga_concat(gap, hi->hi_key);
301 else
302 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100303 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100304 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100305 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100306 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100307 }
308 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100309 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100310 }
311 }
312 break;
313
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100314 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100315#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100316# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100317 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100318 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100319 else if (isinf(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100320 ga_concat(gap, (char_u *)"Infinity");
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100321 else
322# endif
323 {
324 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
325 val->vval.v_float);
326 ga_concat(gap, numbuf);
327 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100328 break;
329#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100330 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +0100331 internal_error("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100332 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100333 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100334 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100335}
336
337/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100338 * When "reader" has less than NUMBUFLEN bytes available, call the fill
339 * callback to get more.
340 */
341 static void
342fill_numbuflen(js_read_T *reader)
343{
344 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
345 - reader->js_used < NUMBUFLEN)
346 {
347 if (reader->js_fill(reader))
348 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
349 }
350}
351
352/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100353 * Skip white space in "reader". All characters <= space are considered white
354 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100355 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100356 */
357 static void
358json_skip_white(js_read_T *reader)
359{
360 int c;
361
Bram Moolenaar56ead342016-02-02 18:20:08 +0100362 for (;;)
363 {
364 c = reader->js_buf[reader->js_used];
365 if (reader->js_fill != NULL && c == NUL)
366 {
367 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200368 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100369 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200370 continue;
371 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100372 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100373 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100374 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100375 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100376 }
377 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100378}
379
Bram Moolenaar56ead342016-02-02 18:20:08 +0100380 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100381json_decode_array(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100382{
383 char_u *p;
384 typval_T item;
385 listitem_T *li;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100386 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100387
Bram Moolenaar56ead342016-02-02 18:20:08 +0100388 if (res != NULL && rettv_list_alloc(res) == FAIL)
389 {
390 res->v_type = VAR_SPECIAL;
391 res->vval.v_number = VVAL_NONE;
392 return FAIL;
393 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100394 ++reader->js_used; /* consume the '[' */
395
396 while (TRUE)
397 {
398 json_skip_white(reader);
399 p = reader->js_buf + reader->js_used;
400 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100401 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100402 if (*p == ']')
403 {
404 ++reader->js_used; /* consume the ']' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100405 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100406 }
407
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100408 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100409 if (ret != OK)
410 return ret;
411 if (res != NULL)
412 {
413 li = listitem_alloc();
414 if (li == NULL)
415 {
416 clear_tv(&item);
417 return FAIL;
418 }
419 li->li_tv = item;
420 list_append(res->vval.v_list, li);
421 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100422
423 json_skip_white(reader);
424 p = reader->js_buf + reader->js_used;
425 if (*p == ',')
426 ++reader->js_used;
427 else if (*p != ']')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100428 {
429 if (*p == NUL)
430 return MAYBE;
431 return FAIL;
432 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100433 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100434 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100435}
436
Bram Moolenaar56ead342016-02-02 18:20:08 +0100437 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100438json_decode_object(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100439{
440 char_u *p;
441 typval_T tvkey;
442 typval_T item;
443 dictitem_T *di;
444 char_u buf[NUMBUFLEN];
Bram Moolenaarfbf9c6b2016-02-02 19:43:57 +0100445 char_u *key = NULL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100446 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100447
Bram Moolenaar56ead342016-02-02 18:20:08 +0100448 if (res != NULL && rettv_dict_alloc(res) == FAIL)
449 {
450 res->v_type = VAR_SPECIAL;
451 res->vval.v_number = VVAL_NONE;
452 return FAIL;
453 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100454 ++reader->js_used; /* consume the '{' */
455
456 while (TRUE)
457 {
458 json_skip_white(reader);
459 p = reader->js_buf + reader->js_used;
460 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100461 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100462 if (*p == '}')
463 {
464 ++reader->js_used; /* consume the '}' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100465 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100466 }
467
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100468 if ((options & JSON_JS) && reader->js_buf[reader->js_used] != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100469 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100470 /* accept a key that is not in quotes */
471 key = p = reader->js_buf + reader->js_used;
472 while (*p != NUL && *p != ':' && *p > ' ')
473 ++p;
474 tvkey.v_type = VAR_STRING;
475 tvkey.vval.v_string = vim_strnsave(key, (int)(p - key));
476 reader->js_used += (int)(p - key);
477 key = tvkey.vval.v_string;
478 }
479 else
480 {
481 ret = json_decode_item(reader, res == NULL ? NULL : &tvkey,
482 options);
483 if (ret != OK)
484 return ret;
485 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100486 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100487 key = get_tv_string_buf_chk(&tvkey, buf);
488 if (key == NULL || *key == NUL)
489 {
490 clear_tv(&tvkey);
491 return FAIL;
492 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100493 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100494 }
495
496 json_skip_white(reader);
497 p = reader->js_buf + reader->js_used;
498 if (*p != ':')
499 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100500 if (res != NULL)
501 clear_tv(&tvkey);
502 if (*p == NUL)
503 return MAYBE;
504 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100505 }
506 ++reader->js_used;
507 json_skip_white(reader);
508
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100509 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100510 if (ret != OK)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100511 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100512 if (res != NULL)
513 clear_tv(&tvkey);
514 return ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100515 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100516
517 if (res != NULL)
518 {
519 di = dictitem_alloc(key);
520 clear_tv(&tvkey);
521 if (di == NULL)
522 {
523 clear_tv(&item);
524 return FAIL;
525 }
526 di->di_tv = item;
Bram Moolenaar2588b5a2016-03-05 23:23:02 +0100527 di->di_tv.v_lock = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100528 if (dict_add(res->vval.v_dict, di) == FAIL)
529 {
530 dictitem_free(di);
531 return FAIL;
532 }
533 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100534
535 json_skip_white(reader);
536 p = reader->js_buf + reader->js_used;
537 if (*p == ',')
538 ++reader->js_used;
539 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100540 {
541 if (*p == NUL)
542 return MAYBE;
543 return FAIL;
544 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100545 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100546 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100547}
548
Bram Moolenaar56ead342016-02-02 18:20:08 +0100549 static int
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100550json_decode_string(js_read_T *reader, typval_T *res)
551{
552 garray_T ga;
553 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100554 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100555 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200556 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100557
Bram Moolenaar56ead342016-02-02 18:20:08 +0100558 if (res != NULL)
559 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100560
Bram Moolenaar56ead342016-02-02 18:20:08 +0100561 p = reader->js_buf + reader->js_used + 1; /* skip over " */
562 while (*p != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100563 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100564 /* The JSON is always expected to be utf-8, thus use utf functions
565 * here. The string is converted below if needed. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100566 if (*p == NUL || p[1] == NUL
567#ifdef FEAT_MBYTE
568 || utf_ptr2len(p) < utf_byte2len(*p)
569#endif
570 )
571 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100572 /* Not enough bytes to make a character or end of the string. Get
573 * more if possible. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100574 if (reader->js_fill == NULL)
575 break;
576 len = (int)(reader->js_end - p);
577 reader->js_used = (int)(p - reader->js_buf);
578 if (!reader->js_fill(reader))
579 break; /* didn't get more */
580 p = reader->js_buf + reader->js_used;
581 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
582 continue;
583 }
584
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100585 if (*p == '\\')
586 {
587 c = -1;
588 switch (p[1])
589 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100590 case '\\': c = '\\'; break;
591 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100592 case 'b': c = BS; break;
593 case 't': c = TAB; break;
594 case 'n': c = NL; break;
595 case 'f': c = FF; break;
596 case 'r': c = CAR; break;
597 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100598 if (reader->js_fill != NULL
599 && (int)(reader->js_end - p) < NUMBUFLEN)
600 {
601 reader->js_used = (int)(p - reader->js_buf);
602 if (reader->js_fill(reader))
603 {
604 p = reader->js_buf + reader->js_used;
605 reader->js_end = reader->js_buf
606 + STRLEN(reader->js_buf);
607 }
608 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100609 nr = 0;
610 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100611 vim_str2nr(p + 2, NULL, &len,
612 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
613 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100614 if (0xd800 <= nr && nr <= 0xdfff
615 && (int)(reader->js_end - p) >= 6
616 && *p == '\\' && *(p+1) == 'u')
617 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200618 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100619
620 /* decode surrogate pair: \ud812\u3456 */
621 len = 0;
622 vim_str2nr(p + 2, NULL, &len,
623 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4);
624 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
625 {
626 p += len + 2;
627 nr = (((nr - 0xd800) << 10) |
628 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
629 }
630 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100631 if (res != NULL)
632 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100633#ifdef FEAT_MBYTE
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200634 char_u buf[NUMBUFLEN];
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100635 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100636 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100637#else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200638 ga_append(&ga, (int)nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100639#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100640 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100641 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100642 default:
643 /* not a special char, skip over \ */
644 ++p;
645 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100646 }
647 if (c > 0)
648 {
649 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100650 if (res != NULL)
651 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100652 }
653 }
654 else
655 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100656#ifdef FEAT_MBYTE
657 len = utf_ptr2len(p);
658#else
659 len = 1;
660#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100661 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100662 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100663 if (ga_grow(&ga, len) == FAIL)
664 {
665 ga_clear(&ga);
666 return FAIL;
667 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100668 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
669 ga.ga_len += len;
670 }
671 p += len;
672 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100673 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100674
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100675 reader->js_used = (int)(p - reader->js_buf);
676 if (*p == '"')
677 {
678 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100679 if (res != NULL)
680 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100681 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100682 res->v_type = VAR_STRING;
Bram Moolenaarb3628722016-02-28 14:56:39 +0100683#if defined(FEAT_MBYTE) && defined(USE_ICONV)
684 if (!enc_utf8)
685 {
686 vimconv_T conv;
687
688 /* Convert the utf-8 string to 'encoding'. */
689 conv.vc_type = CONV_NONE;
690 convert_setup(&conv, (char_u*)"utf-8", p_enc);
691 if (conv.vc_type != CONV_NONE)
692 {
693 res->vval.v_string =
694 string_convert(&conv, ga.ga_data, NULL);
695 vim_free(ga.ga_data);
696 }
697 convert_setup(&conv, NULL, NULL);
698 }
699 else
700#endif
701 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100702 }
703 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100704 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100705 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100706 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100707 res->v_type = VAR_SPECIAL;
708 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100709 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100710 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100711 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100712}
713
714/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100715 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100716 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100717 *
718 * Return FAIL for a decoding error.
719 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100720 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100721 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100722json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100723{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100724 char_u *p;
725 int len;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100726
Bram Moolenaar56ead342016-02-02 18:20:08 +0100727 fill_numbuflen(reader);
728 p = reader->js_buf + reader->js_used;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100729 switch (*p)
730 {
731 case '[': /* array */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100732 return json_decode_array(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100733
734 case '{': /* object */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100735 return json_decode_object(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100736
737 case '"': /* string */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100738 return json_decode_string(reader, res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100739
740 case ',': /* comma: empty item */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100741 if ((options & JSON_JS) == 0)
742 return FAIL;
743 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100744 case NUL: /* empty */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100745 if (res != NULL)
746 {
747 res->v_type = VAR_SPECIAL;
748 res->vval.v_number = VVAL_NONE;
749 }
750 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100751
752 default:
753 if (VIM_ISDIGIT(*p) || *p == '-')
754 {
Bram Moolenaar10b369f2016-02-29 23:12:49 +0100755#ifdef FEAT_FLOAT
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100756 char_u *sp = p;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100757
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100758 if (*sp == '-')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100759 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100760 ++sp;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100761 if (*sp == NUL)
762 return MAYBE;
763 if (!VIM_ISDIGIT(*sp))
764 return FAIL;
765 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100766 sp = skipdigits(sp);
767 if (*sp == '.' || *sp == 'e' || *sp == 'E')
768 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100769 if (res == NULL)
770 {
771 float_T f;
772
773 len = string2float(p, &f);
774 }
775 else
776 {
777 res->v_type = VAR_FLOAT;
778 len = string2float(p, &res->vval.v_float);
779 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100780 }
781 else
782#endif
783 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200784 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100785
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100786 vim_str2nr(reader->js_buf + reader->js_used,
787 NULL, &len, 0, /* what */
788 &nr, NULL, 0);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100789 if (res != NULL)
790 {
791 res->v_type = VAR_NUMBER;
792 res->vval.v_number = nr;
793 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100794 }
795 reader->js_used += len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100796 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100797 }
798 if (STRNICMP((char *)p, "false", 5) == 0)
799 {
800 reader->js_used += 5;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100801 if (res != NULL)
802 {
803 res->v_type = VAR_SPECIAL;
804 res->vval.v_number = VVAL_FALSE;
805 }
806 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100807 }
808 if (STRNICMP((char *)p, "true", 4) == 0)
809 {
810 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100811 if (res != NULL)
812 {
813 res->v_type = VAR_SPECIAL;
814 res->vval.v_number = VVAL_TRUE;
815 }
816 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100817 }
818 if (STRNICMP((char *)p, "null", 4) == 0)
819 {
820 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100821 if (res != NULL)
822 {
823 res->v_type = VAR_SPECIAL;
824 res->vval.v_number = VVAL_NULL;
825 }
826 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100827 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100828#ifdef FEAT_FLOAT
829 if (STRNICMP((char *)p, "NaN", 3) == 0)
830 {
831 reader->js_used += 3;
832 if (res != NULL)
833 {
834 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100835 res->vval.v_float = NAN;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100836 }
837 return OK;
838 }
839 if (STRNICMP((char *)p, "Infinity", 8) == 0)
840 {
841 reader->js_used += 8;
842 if (res != NULL)
843 {
844 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100845 res->vval.v_float = INFINITY;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100846 }
847 return OK;
848 }
849#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100850 /* check for truncated name */
851 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100852 if (
853 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
854#ifdef FEAT_FLOAT
855 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
856 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
857#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100858 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
859 || STRNICMP((char *)p, "null", len) == 0)))
860 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100861 break;
862 }
863
Bram Moolenaar7756e742016-10-21 20:35:37 +0200864 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100865 {
866 res->v_type = VAR_SPECIAL;
867 res->vval.v_number = VVAL_NONE;
868 }
869 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100870}
871
872/*
873 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100874 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100875 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100876 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100877 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100878json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100879{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100880 int ret;
881
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100882 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100883 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100884 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100885 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100886 if (ret != OK)
887 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100888 json_skip_white(reader);
889 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100890 return FAIL;
891 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100892}
Bram Moolenaar56ead342016-02-02 18:20:08 +0100893
894/*
895 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100896 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100897 * Return FAIL for a decoding error.
898 * Return MAYBE for an incomplete message.
899 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100900 */
901 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100902json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100903{
904 int ret;
905
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100906 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100907 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
908 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100909 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100910 json_skip_white(reader);
911
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100912 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100913}
914
915/*
916 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100917 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100918 * Return FAIL if the message has a decoding error.
919 * Return MAYBE if the message is truncated, need to read more.
920 * This only works reliable if the message contains an object, array or
921 * string. A number might be trucated without knowing.
922 * Does not advance the reader.
923 */
924 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100925json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100926{
927 int used_save = reader->js_used;
928 int ret;
929
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100930 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100931 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
932 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100933 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100934 reader->js_used = used_save;
935 return ret;
936}
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100937#endif