blob: 1a3dea1e5bbf330a75d3391d4528ce1d410736fa [file] [log] [blame]
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
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 */
15
16#include "vim.h"
17
18#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010019
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +010020#if defined(FEAT_FLOAT)
21# include <float.h>
22# if defined(HAVE_MATH_H)
23 /* for isnan() and isinf() */
24# include <math.h>
25# endif
26# if defined(WIN32) && !defined(isnan)
27# define isnan(x) _isnan(x)
28# define isinf(x) (!_finite(x) && !_isnan(x))
29# endif
30# if defined(_MSC_VER) && !defined(INFINITY)
31# define INFINITY (DBL_MAX+DBL_MAX)
32# define NAN (INFINITY-INFINITY)
33# endif
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010034#endif
35
Bram Moolenaar595e64e2016-02-07 19:19:53 +010036static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int options);
37static int json_decode_item(js_read_T *reader, typval_T *res, int options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010038
39/*
40 * Encode "val" into a JSON format string.
Bram Moolenaar55fab432016-02-07 16:53:13 +010041 * The result is in allocated memory.
42 * The result is empty when encoding fails.
Bram Moolenaar595e64e2016-02-07 19:19:53 +010043 * "options" can be JSON_JS or zero;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010044 */
45 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010046json_encode(typval_T *val, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010047{
48 garray_T ga;
49
50 /* Store bytes in the growarray. */
51 ga_init2(&ga, 1, 4000);
Bram Moolenaar595e64e2016-02-07 19:19:53 +010052 if (json_encode_item(&ga, val, get_copyID(), options) == FAIL)
Bram Moolenaar55fab432016-02-07 16:53:13 +010053 {
54 vim_free(ga.ga_data);
55 return vim_strsave((char_u *)"");
56 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +010057 return ga.ga_data;
58}
59
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010060/*
Bram Moolenaar55fab432016-02-07 16:53:13 +010061 * Encode ["nr", "val"] into a JSON format string in allocated memory.
Bram Moolenaar595e64e2016-02-07 19:19:53 +010062 * "options" can be JSON_JS or zero;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010063 * Returns NULL when out of memory.
64 */
65 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010066json_encode_nr_expr(int nr, typval_T *val, int options)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010067{
68 typval_T listtv;
69 typval_T nrtv;
70 char_u *text;
71
72 nrtv.v_type = VAR_NUMBER;
73 nrtv.vval.v_number = nr;
74 if (rettv_list_alloc(&listtv) == FAIL)
75 return NULL;
76 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
77 || list_append_tv(listtv.vval.v_list, val) == FAIL)
78 {
79 list_unref(listtv.vval.v_list);
80 return NULL;
81 }
82
Bram Moolenaar595e64e2016-02-07 19:19:53 +010083 text = json_encode(&listtv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010084 list_unref(listtv.vval.v_list);
85 return text;
86}
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 {
98 ga_append(gap, '"');
99 while (*res != NUL)
100 {
101 int c = PTR2CHAR(res);
102
103 switch (c)
104 {
105 case 0x08:
106 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
107 case 0x09:
108 ga_append(gap, '\\'); ga_append(gap, 't'); break;
109 case 0x0a:
110 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
111 case 0x0c:
112 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
113 case 0x0d:
114 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
115 case 0x22: /* " */
116 case 0x5c: /* \ */
117 ga_append(gap, '\\');
118 ga_append(gap, c);
119 break;
120 default:
121 if (c >= 0x20)
122 {
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100123#ifdef FEAT_MBYTE
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100124 numbuf[mb_char2bytes(c, numbuf)] = NUL;
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100125#else
126 numbuf[0] = c;
127 numbuf[1] = NUL;
128#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100129 ga_concat(gap, numbuf);
130 }
131 else
132 {
133 vim_snprintf((char *)numbuf, NUMBUFLEN,
134 "\\u%04lx", (long)c);
135 ga_concat(gap, numbuf);
136 }
137 }
138 mb_cptr_adv(res);
139 }
140 ga_append(gap, '"');
141 }
142}
143
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100144/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100145 * Return TRUE if "key" can be used without quotes.
146 * That is when it starts with a letter and only contains letters, digits and
147 * underscore.
148 */
149 static int
150is_simple_key(char_u *key)
151{
152 char_u *p;
153
154 if (!ASCII_ISALPHA(*key))
155 return FALSE;
156 for (p = key + 1; *p != NUL; ++p)
157 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
158 return FALSE;
159 return TRUE;
160}
161
162/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100163 * Encode "val" into "gap".
164 * Return FAIL or OK.
165 */
166 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100167json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100168{
169 char_u numbuf[NUMBUFLEN];
170 char_u *res;
171 list_T *l;
172 dict_T *d;
173
174 switch (val->v_type)
175 {
176 case VAR_SPECIAL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100177 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100178 {
179 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
180 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100181 case VVAL_NONE: if ((options & JSON_JS) != 0
182 && (options & JSON_NO_NONE) == 0)
183 /* empty item */
184 break;
185 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100186 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
187 }
188 break;
189
190 case VAR_NUMBER:
191 vim_snprintf((char *)numbuf, NUMBUFLEN, "%ld",
192 (long)val->vval.v_number);
193 ga_concat(gap, numbuf);
194 break;
195
196 case VAR_STRING:
197 res = val->vval.v_string;
198 write_string(gap, res);
199 break;
200
201 case VAR_FUNC:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100202 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100203 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100204 /* no JSON equivalent TODO: better error */
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100205 EMSG(_(e_invarg));
206 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100207
208 case VAR_LIST:
209 l = val->vval.v_list;
210 if (l == NULL)
211 ga_concat(gap, (char_u *)"null");
212 else
213 {
214 if (l->lv_copyID == copyID)
215 ga_concat(gap, (char_u *)"[]");
216 else
217 {
218 listitem_T *li;
219
220 l->lv_copyID = copyID;
221 ga_append(gap, '[');
222 for (li = l->lv_first; li != NULL && !got_int; )
223 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100224 if (json_encode_item(gap, &li->li_tv, copyID,
225 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100226 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100227 if ((options & JSON_JS)
228 && li->li_next == NULL
229 && li->li_tv.v_type == VAR_SPECIAL
230 && li->li_tv.vval.v_number == VVAL_NONE)
231 /* add an extra comma if the last item is v:none */
232 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100233 li = li->li_next;
234 if (li != NULL)
235 ga_append(gap, ',');
236 }
237 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100238 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100239 }
240 }
241 break;
242
243 case VAR_DICT:
244 d = val->vval.v_dict;
245 if (d == NULL)
246 ga_concat(gap, (char_u *)"null");
247 else
248 {
249 if (d->dv_copyID == copyID)
250 ga_concat(gap, (char_u *)"{}");
251 else
252 {
253 int first = TRUE;
254 int todo = (int)d->dv_hashtab.ht_used;
255 hashitem_T *hi;
256
257 d->dv_copyID = copyID;
258 ga_append(gap, '{');
259
260 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
261 ++hi)
262 if (!HASHITEM_EMPTY(hi))
263 {
264 --todo;
265 if (first)
266 first = FALSE;
267 else
268 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100269 if ((options & JSON_JS)
270 && is_simple_key(hi->hi_key))
271 ga_concat(gap, hi->hi_key);
272 else
273 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100274 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100275 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100276 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100277 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100278 }
279 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100280 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100281 }
282 }
283 break;
284
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100285 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100286#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100287# if defined(HAVE_MATH_H)
288 if ((options & JSON_JS) && isnan(val->vval.v_float))
289 ga_concat(gap, (char_u *)"NaN");
290 else if ((options & JSON_JS) && isinf(val->vval.v_float))
291 ga_concat(gap, (char_u *)"Infinity");
292 else if (isnan(val->vval.v_float) || isinf(val->vval.v_float))
293 ga_concat(gap, (char_u *)"null");
294 else
295# endif
296 {
297 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
298 val->vval.v_float);
299 ga_concat(gap, numbuf);
300 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100301 break;
302#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100303 case VAR_UNKNOWN:
Bram Moolenaarc6b14f02016-02-20 15:26:42 +0100304 EMSG2(_(e_intern2), "json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100305 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100306 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100307 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100308}
309
310/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100311 * When "reader" has less than NUMBUFLEN bytes available, call the fill
312 * callback to get more.
313 */
314 static void
315fill_numbuflen(js_read_T *reader)
316{
317 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
318 - reader->js_used < NUMBUFLEN)
319 {
320 if (reader->js_fill(reader))
321 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
322 }
323}
324
325/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100326 * Skip white space in "reader". All characters <= space are considered white
327 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100328 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100329 */
330 static void
331json_skip_white(js_read_T *reader)
332{
333 int c;
334
Bram Moolenaar56ead342016-02-02 18:20:08 +0100335 for (;;)
336 {
337 c = reader->js_buf[reader->js_used];
338 if (reader->js_fill != NULL && c == NUL)
339 {
340 if (reader->js_fill(reader))
341 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
342 continue;
343 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100344 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100345 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100346 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100347 }
348 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100349}
350
Bram Moolenaar56ead342016-02-02 18:20:08 +0100351 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100352json_decode_array(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100353{
354 char_u *p;
355 typval_T item;
356 listitem_T *li;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100357 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100358
Bram Moolenaar56ead342016-02-02 18:20:08 +0100359 if (res != NULL && rettv_list_alloc(res) == FAIL)
360 {
361 res->v_type = VAR_SPECIAL;
362 res->vval.v_number = VVAL_NONE;
363 return FAIL;
364 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100365 ++reader->js_used; /* consume the '[' */
366
367 while (TRUE)
368 {
369 json_skip_white(reader);
370 p = reader->js_buf + reader->js_used;
371 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100372 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 if (*p == ']')
374 {
375 ++reader->js_used; /* consume the ']' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100376 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100377 }
378
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100379 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100380 if (ret != OK)
381 return ret;
382 if (res != NULL)
383 {
384 li = listitem_alloc();
385 if (li == NULL)
386 {
387 clear_tv(&item);
388 return FAIL;
389 }
390 li->li_tv = item;
391 list_append(res->vval.v_list, li);
392 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100393
394 json_skip_white(reader);
395 p = reader->js_buf + reader->js_used;
396 if (*p == ',')
397 ++reader->js_used;
398 else if (*p != ']')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100399 {
400 if (*p == NUL)
401 return MAYBE;
402 return FAIL;
403 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100404 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100405 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100406}
407
Bram Moolenaar56ead342016-02-02 18:20:08 +0100408 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100409json_decode_object(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100410{
411 char_u *p;
412 typval_T tvkey;
413 typval_T item;
414 dictitem_T *di;
415 char_u buf[NUMBUFLEN];
Bram Moolenaarfbf9c6b2016-02-02 19:43:57 +0100416 char_u *key = NULL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100417 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100418
Bram Moolenaar56ead342016-02-02 18:20:08 +0100419 if (res != NULL && rettv_dict_alloc(res) == FAIL)
420 {
421 res->v_type = VAR_SPECIAL;
422 res->vval.v_number = VVAL_NONE;
423 return FAIL;
424 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100425 ++reader->js_used; /* consume the '{' */
426
427 while (TRUE)
428 {
429 json_skip_white(reader);
430 p = reader->js_buf + reader->js_used;
431 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100432 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100433 if (*p == '}')
434 {
435 ++reader->js_used; /* consume the '}' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100436 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100437 }
438
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100439 if ((options & JSON_JS) && reader->js_buf[reader->js_used] != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100440 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100441 /* accept a key that is not in quotes */
442 key = p = reader->js_buf + reader->js_used;
443 while (*p != NUL && *p != ':' && *p > ' ')
444 ++p;
445 tvkey.v_type = VAR_STRING;
446 tvkey.vval.v_string = vim_strnsave(key, (int)(p - key));
447 reader->js_used += (int)(p - key);
448 key = tvkey.vval.v_string;
449 }
450 else
451 {
452 ret = json_decode_item(reader, res == NULL ? NULL : &tvkey,
453 options);
454 if (ret != OK)
455 return ret;
456 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100457 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100458 key = get_tv_string_buf_chk(&tvkey, buf);
459 if (key == NULL || *key == NUL)
460 {
461 clear_tv(&tvkey);
462 return FAIL;
463 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100464 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100465 }
466
467 json_skip_white(reader);
468 p = reader->js_buf + reader->js_used;
469 if (*p != ':')
470 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100471 if (res != NULL)
472 clear_tv(&tvkey);
473 if (*p == NUL)
474 return MAYBE;
475 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100476 }
477 ++reader->js_used;
478 json_skip_white(reader);
479
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100480 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100481 if (ret != OK)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100482 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100483 if (res != NULL)
484 clear_tv(&tvkey);
485 return ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100486 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100487
488 if (res != NULL)
489 {
490 di = dictitem_alloc(key);
491 clear_tv(&tvkey);
492 if (di == NULL)
493 {
494 clear_tv(&item);
495 return FAIL;
496 }
497 di->di_tv = item;
498 if (dict_add(res->vval.v_dict, di) == FAIL)
499 {
500 dictitem_free(di);
501 return FAIL;
502 }
503 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100504
505 json_skip_white(reader);
506 p = reader->js_buf + reader->js_used;
507 if (*p == ',')
508 ++reader->js_used;
509 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100510 {
511 if (*p == NUL)
512 return MAYBE;
513 return FAIL;
514 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100515 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100516 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100517}
518
Bram Moolenaar56ead342016-02-02 18:20:08 +0100519 static int
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100520json_decode_string(js_read_T *reader, typval_T *res)
521{
522 garray_T ga;
523 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100524 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100525 int c;
526 long nr;
527 char_u buf[NUMBUFLEN];
528
Bram Moolenaar56ead342016-02-02 18:20:08 +0100529 if (res != NULL)
530 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100531
Bram Moolenaar56ead342016-02-02 18:20:08 +0100532 p = reader->js_buf + reader->js_used + 1; /* skip over " */
533 while (*p != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100534 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100535 if (*p == NUL || p[1] == NUL
536#ifdef FEAT_MBYTE
537 || utf_ptr2len(p) < utf_byte2len(*p)
538#endif
539 )
540 {
541 if (reader->js_fill == NULL)
542 break;
543 len = (int)(reader->js_end - p);
544 reader->js_used = (int)(p - reader->js_buf);
545 if (!reader->js_fill(reader))
546 break; /* didn't get more */
547 p = reader->js_buf + reader->js_used;
548 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
549 continue;
550 }
551
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100552 if (*p == '\\')
553 {
554 c = -1;
555 switch (p[1])
556 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100557 case '\\': c = '\\'; break;
558 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100559 case 'b': c = BS; break;
560 case 't': c = TAB; break;
561 case 'n': c = NL; break;
562 case 'f': c = FF; break;
563 case 'r': c = CAR; break;
564 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100565 if (reader->js_fill != NULL
566 && (int)(reader->js_end - p) < NUMBUFLEN)
567 {
568 reader->js_used = (int)(p - reader->js_buf);
569 if (reader->js_fill(reader))
570 {
571 p = reader->js_buf + reader->js_used;
572 reader->js_end = reader->js_buf
573 + STRLEN(reader->js_buf);
574 }
575 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100576 vim_str2nr(p + 2, NULL, &len,
577 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
578 p += len + 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100579 if (res != NULL)
580 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100581#ifdef FEAT_MBYTE
Bram Moolenaar56ead342016-02-02 18:20:08 +0100582 buf[(*mb_char2bytes)((int)nr, buf)] = NUL;
583 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100584#else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100585 ga_append(&ga, nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100586#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100587 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100588 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100589 default:
590 /* not a special char, skip over \ */
591 ++p;
592 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100593 }
594 if (c > 0)
595 {
596 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100597 if (res != NULL)
598 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100599 }
600 }
601 else
602 {
603 len = MB_PTR2LEN(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100604 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100605 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100606 if (ga_grow(&ga, len) == FAIL)
607 {
608 ga_clear(&ga);
609 return FAIL;
610 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100611 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
612 ga.ga_len += len;
613 }
614 p += len;
615 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100616 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100617
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100618 reader->js_used = (int)(p - reader->js_buf);
619 if (*p == '"')
620 {
621 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100622 if (res != NULL)
623 {
624 res->v_type = VAR_STRING;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +0100625 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100626 }
627 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100628 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100629 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100630 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100631 res->v_type = VAR_SPECIAL;
632 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100633 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100634 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100635 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100636}
637
638/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100639 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100640 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100641 *
642 * Return FAIL for a decoding error.
643 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100644 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100645 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100646json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100647{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100648 char_u *p;
649 int len;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100650
Bram Moolenaar56ead342016-02-02 18:20:08 +0100651 fill_numbuflen(reader);
652 p = reader->js_buf + reader->js_used;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100653 switch (*p)
654 {
655 case '[': /* array */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100656 return json_decode_array(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100657
658 case '{': /* object */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100659 return json_decode_object(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100660
661 case '"': /* string */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100662 return json_decode_string(reader, res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100663
664 case ',': /* comma: empty item */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100665 if ((options & JSON_JS) == 0)
666 return FAIL;
667 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100668 case NUL: /* empty */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100669 if (res != NULL)
670 {
671 res->v_type = VAR_SPECIAL;
672 res->vval.v_number = VVAL_NONE;
673 }
674 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100675
676 default:
677 if (VIM_ISDIGIT(*p) || *p == '-')
678 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100679 char_u *sp = p;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100680
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100681#ifdef FEAT_FLOAT
682 if (*sp == '-')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100683 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100684 ++sp;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100685 if (*sp == NUL)
686 return MAYBE;
687 if (!VIM_ISDIGIT(*sp))
688 return FAIL;
689 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100690 sp = skipdigits(sp);
691 if (*sp == '.' || *sp == 'e' || *sp == 'E')
692 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100693 if (res == NULL)
694 {
695 float_T f;
696
697 len = string2float(p, &f);
698 }
699 else
700 {
701 res->v_type = VAR_FLOAT;
702 len = string2float(p, &res->vval.v_float);
703 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100704 }
705 else
706#endif
707 {
708 long nr;
709
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100710 vim_str2nr(reader->js_buf + reader->js_used,
711 NULL, &len, 0, /* what */
712 &nr, NULL, 0);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100713 if (res != NULL)
714 {
715 res->v_type = VAR_NUMBER;
716 res->vval.v_number = nr;
717 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100718 }
719 reader->js_used += len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100720 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100721 }
722 if (STRNICMP((char *)p, "false", 5) == 0)
723 {
724 reader->js_used += 5;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100725 if (res != NULL)
726 {
727 res->v_type = VAR_SPECIAL;
728 res->vval.v_number = VVAL_FALSE;
729 }
730 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100731 }
732 if (STRNICMP((char *)p, "true", 4) == 0)
733 {
734 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100735 if (res != NULL)
736 {
737 res->v_type = VAR_SPECIAL;
738 res->vval.v_number = VVAL_TRUE;
739 }
740 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100741 }
742 if (STRNICMP((char *)p, "null", 4) == 0)
743 {
744 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100745 if (res != NULL)
746 {
747 res->v_type = VAR_SPECIAL;
748 res->vval.v_number = VVAL_NULL;
749 }
750 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100751 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100752#ifdef FEAT_FLOAT
753 if (STRNICMP((char *)p, "NaN", 3) == 0)
754 {
755 reader->js_used += 3;
756 if (res != NULL)
757 {
758 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100759 res->vval.v_float = NAN;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100760 }
761 return OK;
762 }
763 if (STRNICMP((char *)p, "Infinity", 8) == 0)
764 {
765 reader->js_used += 8;
766 if (res != NULL)
767 {
768 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100769 res->vval.v_float = INFINITY;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100770 }
771 return OK;
772 }
773#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100774 /* check for truncated name */
775 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100776 if (
777 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
778#ifdef FEAT_FLOAT
779 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
780 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
781#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100782 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
783 || STRNICMP((char *)p, "null", len) == 0)))
784 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100785 break;
786 }
787
Bram Moolenaar56ead342016-02-02 18:20:08 +0100788 if (res != NUL)
789 {
790 res->v_type = VAR_SPECIAL;
791 res->vval.v_number = VVAL_NONE;
792 }
793 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100794}
795
796/*
797 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100798 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100799 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100800 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100801 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100802json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100803{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100804 int ret;
805
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100806 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100807 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100808 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100809 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100810 if (ret != OK)
811 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100812 json_skip_white(reader);
813 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100814 return FAIL;
815 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100816}
Bram Moolenaar56ead342016-02-02 18:20:08 +0100817
818/*
819 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100820 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100821 * Return FAIL if the message has a decoding error or the message is
822 * truncated. Consumes the message anyway.
823 */
824 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100825json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100826{
827 int ret;
828
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100829 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100830 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
831 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100832 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100833 json_skip_white(reader);
834
835 return ret == OK ? OK : FAIL;
836}
837
838/*
839 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100840 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100841 * Return FAIL if the message has a decoding error.
842 * Return MAYBE if the message is truncated, need to read more.
843 * This only works reliable if the message contains an object, array or
844 * string. A number might be trucated without knowing.
845 * Does not advance the reader.
846 */
847 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100848json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100849{
850 int used_save = reader->js_used;
851 int ret;
852
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100853 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100854 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
855 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100856 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100857 reader->js_used = used_save;
858 return ret;
859}
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100860#endif