blob: 64ef93fabb4731a848d77640a47dc7371882af82 [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);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022
23/*
24 * Encode "val" into a JSON format string.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020025 * The result is added to "gap"
26 * Returns FAIL on failure and makes gap->ga_data empty.
27 */
28 static int
29json_encode_gap(garray_T *gap, typval_T *val, int options)
30{
31 if (json_encode_item(gap, val, get_copyID(), options) == FAIL)
32 {
33 ga_clear(gap);
34 gap->ga_data = vim_strsave((char_u *)"");
35 return FAIL;
36 }
37 return OK;
38}
39
40/*
41 * Encode "val" into a JSON format string.
Bram Moolenaar55fab432016-02-07 16:53:13 +010042 * The result is in allocated memory.
43 * The result is empty when encoding fails.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020044 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaar520e1e42016-01-23 19:46:28 +010045 */
46 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010047json_encode(typval_T *val, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010048{
49 garray_T ga;
50
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010051 // Store bytes in the growarray.
Bram Moolenaar520e1e42016-01-23 19:46:28 +010052 ga_init2(&ga, 1, 4000);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020053 json_encode_gap(&ga, val, options);
Bram Moolenaar04af1962019-04-12 21:19:04 +020054 ga_append(&ga, NUL);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010055 return ga.ga_data;
56}
57
Bram Moolenaar113e1072019-01-20 15:30:40 +010058#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010059/*
Bram Moolenaar55fab432016-02-07 16:53:13 +010060 * Encode ["nr", "val"] into a JSON format string in allocated memory.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020061 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010062 * Returns NULL when out of memory.
63 */
64 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010065json_encode_nr_expr(int nr, typval_T *val, int options)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010066{
67 typval_T listtv;
68 typval_T nrtv;
Bram Moolenaarf1f07922016-08-26 17:58:53 +020069 garray_T ga;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010070
71 nrtv.v_type = VAR_NUMBER;
72 nrtv.vval.v_number = nr;
73 if (rettv_list_alloc(&listtv) == FAIL)
74 return NULL;
75 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
76 || list_append_tv(listtv.vval.v_list, val) == FAIL)
77 {
78 list_unref(listtv.vval.v_list);
79 return NULL;
80 }
81
Bram Moolenaarf1f07922016-08-26 17:58:53 +020082 ga_init2(&ga, 1, 4000);
83 if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL))
84 ga_append(&ga, '\n');
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010085 list_unref(listtv.vval.v_list);
Bram Moolenaar04af1962019-04-12 21:19:04 +020086 ga_append(&ga, NUL);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020087 return ga.ga_data;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010088}
Bram Moolenaar113e1072019-01-20 15:30:40 +010089#endif
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010090
Bram Moolenaar520e1e42016-01-23 19:46:28 +010091 static void
92write_string(garray_T *gap, char_u *str)
93{
94 char_u *res = str;
95 char_u numbuf[NUMBUFLEN];
96
97 if (res == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +010098 ga_concat(gap, (char_u *)"\"\"");
Bram Moolenaar520e1e42016-01-23 19:46:28 +010099 else
100 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100101#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100102 vimconv_T conv;
103 char_u *converted = NULL;
104
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100105 if (!enc_utf8)
106 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100107 // Convert the text from 'encoding' to utf-8, the JSON string is
108 // always utf-8.
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100109 conv.vc_type = CONV_NONE;
110 convert_setup(&conv, p_enc, (char_u*)"utf-8");
111 if (conv.vc_type != CONV_NONE)
112 converted = res = string_convert(&conv, res, NULL);
113 convert_setup(&conv, NULL, NULL);
114 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100115#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100116 ga_append(gap, '"');
117 while (*res != NUL)
118 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100119 int c;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100120 // always use utf-8 encoding, ignore 'encoding'
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100121 c = utf_ptr2char(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100122
123 switch (c)
124 {
125 case 0x08:
126 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
127 case 0x09:
128 ga_append(gap, '\\'); ga_append(gap, 't'); break;
129 case 0x0a:
130 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
131 case 0x0c:
132 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
133 case 0x0d:
134 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100135 case 0x22: // "
136 case 0x5c: // backslash
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100137 ga_append(gap, '\\');
138 ga_append(gap, c);
139 break;
140 default:
141 if (c >= 0x20)
142 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100143 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100144 ga_concat(gap, numbuf);
145 }
146 else
147 {
148 vim_snprintf((char *)numbuf, NUMBUFLEN,
149 "\\u%04lx", (long)c);
150 ga_concat(gap, numbuf);
151 }
152 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100153 res += utf_ptr2len(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100154 }
155 ga_append(gap, '"');
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100156#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100157 vim_free(converted);
158#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100159 }
160}
161
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100162/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100163 * Return TRUE if "key" can be used without quotes.
164 * That is when it starts with a letter and only contains letters, digits and
165 * underscore.
166 */
167 static int
168is_simple_key(char_u *key)
169{
170 char_u *p;
171
172 if (!ASCII_ISALPHA(*key))
173 return FALSE;
174 for (p = key + 1; *p != NUL; ++p)
175 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
176 return FALSE;
177 return TRUE;
178}
179
180/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100181 * Encode "val" into "gap".
182 * Return FAIL or OK.
183 */
184 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100185json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100186{
187 char_u numbuf[NUMBUFLEN];
188 char_u *res;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100189 blob_T *b;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100190 list_T *l;
191 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 int i;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100193
194 switch (val->v_type)
195 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100196 case VAR_BOOL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100197 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100198 {
199 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
200 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100201 }
202 break;
203
204 case VAR_SPECIAL:
205 switch (val->vval.v_number)
206 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100207 case VVAL_NONE: if ((options & JSON_JS) != 0
208 && (options & JSON_NO_NONE) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100209 // empty item
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100210 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100211 // FALLTHROUGH
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100212 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
213 }
214 break;
215
216 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200217 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +0100218 (long_long_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100219 ga_concat(gap, numbuf);
220 break;
221
222 case VAR_STRING:
223 res = val->vval.v_string;
224 write_string(gap, res);
225 break;
226
227 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100228 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100229 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100230 case VAR_CHANNEL:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100231 // no JSON equivalent TODO: better error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100232 emsg(_(e_invarg));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100233 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100234
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100235 case VAR_BLOB:
236 b = val->vval.v_blob;
237 if (b == NULL || b->bv_ga.ga_len == 0)
238 ga_concat(gap, (char_u *)"[]");
239 else
240 {
241 ga_append(gap, '[');
242 for (i = 0; i < b->bv_ga.ga_len; i++)
243 {
244 if (i > 0)
245 ga_concat(gap, (char_u *)",");
246 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
247 (int)blob_get(b, i));
248 ga_concat(gap, numbuf);
249 }
250 ga_append(gap, ']');
251 }
252 break;
253
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100254 case VAR_LIST:
255 l = val->vval.v_list;
256 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100257 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100258 else
259 {
260 if (l->lv_copyID == copyID)
261 ga_concat(gap, (char_u *)"[]");
262 else
263 {
264 listitem_T *li;
265
266 l->lv_copyID = copyID;
267 ga_append(gap, '[');
268 for (li = l->lv_first; li != NULL && !got_int; )
269 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100270 if (json_encode_item(gap, &li->li_tv, copyID,
271 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100272 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100273 if ((options & JSON_JS)
274 && li->li_next == NULL
275 && li->li_tv.v_type == VAR_SPECIAL
276 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100277 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100278 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100279 li = li->li_next;
280 if (li != NULL)
281 ga_append(gap, ',');
282 }
283 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100284 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100285 }
286 }
287 break;
288
289 case VAR_DICT:
290 d = val->vval.v_dict;
291 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100292 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100293 else
294 {
295 if (d->dv_copyID == copyID)
296 ga_concat(gap, (char_u *)"{}");
297 else
298 {
299 int first = TRUE;
300 int todo = (int)d->dv_hashtab.ht_used;
301 hashitem_T *hi;
302
303 d->dv_copyID = copyID;
304 ga_append(gap, '{');
305
306 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
307 ++hi)
308 if (!HASHITEM_EMPTY(hi))
309 {
310 --todo;
311 if (first)
312 first = FALSE;
313 else
314 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100315 if ((options & JSON_JS)
316 && is_simple_key(hi->hi_key))
317 ga_concat(gap, hi->hi_key);
318 else
319 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100320 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100321 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100322 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100323 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100324 }
325 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100326 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100327 }
328 }
329 break;
330
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100331 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100332#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100333# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100334 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100335 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100336 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100337 {
338 if (val->vval.v_float < 0.0)
339 ga_concat(gap, (char_u *)"-Infinity");
340 else
341 ga_concat(gap, (char_u *)"Infinity");
342 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100343 else
344# endif
345 {
346 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
347 val->vval.v_float);
348 ga_concat(gap, numbuf);
349 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100350 break;
351#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100352 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +0100353 internal_error("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100354 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100355 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100356 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100357}
358
359/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100360 * When "reader" has less than NUMBUFLEN bytes available, call the fill
361 * callback to get more.
362 */
363 static void
364fill_numbuflen(js_read_T *reader)
365{
366 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
367 - reader->js_used < NUMBUFLEN)
368 {
369 if (reader->js_fill(reader))
370 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
371 }
372}
373
374/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100375 * Skip white space in "reader". All characters <= space are considered white
376 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100377 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100378 */
379 static void
380json_skip_white(js_read_T *reader)
381{
382 int c;
383
Bram Moolenaar56ead342016-02-02 18:20:08 +0100384 for (;;)
385 {
386 c = reader->js_buf[reader->js_used];
387 if (reader->js_fill != NULL && c == NUL)
388 {
389 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200390 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100391 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200392 continue;
393 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100394 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100395 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100396 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100397 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100398 }
399 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100400}
401
Bram Moolenaar56ead342016-02-02 18:20:08 +0100402 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100403json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100404{
405 garray_T ga;
406 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100407 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100408 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200409 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100410
Bram Moolenaar56ead342016-02-02 18:20:08 +0100411 if (res != NULL)
412 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100413
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100414 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100415 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100416 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 // The JSON is always expected to be utf-8, thus use utf functions
418 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100419 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100420 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100421 // Not enough bytes to make a character or end of the string. Get
422 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100423 if (reader->js_fill == NULL)
424 break;
425 len = (int)(reader->js_end - p);
426 reader->js_used = (int)(p - reader->js_buf);
427 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100428 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100429 p = reader->js_buf + reader->js_used;
430 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
431 continue;
432 }
433
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100434 if (*p == '\\')
435 {
436 c = -1;
437 switch (p[1])
438 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100439 case '\\': c = '\\'; break;
440 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100441 case 'b': c = BS; break;
442 case 't': c = TAB; break;
443 case 'n': c = NL; break;
444 case 'f': c = FF; break;
445 case 'r': c = CAR; break;
446 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100447 if (reader->js_fill != NULL
448 && (int)(reader->js_end - p) < NUMBUFLEN)
449 {
450 reader->js_used = (int)(p - reader->js_buf);
451 if (reader->js_fill(reader))
452 {
453 p = reader->js_buf + reader->js_used;
454 reader->js_end = reader->js_buf
455 + STRLEN(reader->js_buf);
456 }
457 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100458 nr = 0;
459 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100460 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200461 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
462 if (len == 0)
463 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200464 if (res != NULL)
465 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200466 return FAIL;
467 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100468 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100469 if (0xd800 <= nr && nr <= 0xdfff
470 && (int)(reader->js_end - p) >= 6
471 && *p == '\\' && *(p+1) == 'u')
472 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200473 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100474
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100475 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100476 len = 0;
477 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200478 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
479 if (len == 0)
480 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200481 if (res != NULL)
482 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200483 return FAIL;
484 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100485 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
486 {
487 p += len + 2;
488 nr = (((nr - 0xd800) << 10) |
489 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
490 }
491 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100492 if (res != NULL)
493 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200494 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200495
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100496 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100497 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100498 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100499 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100500 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100501 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100502 ++p;
503 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100504 }
505 if (c > 0)
506 {
507 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100508 if (res != NULL)
509 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100510 }
511 }
512 else
513 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100514 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100515 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100516 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100517 if (ga_grow(&ga, len) == FAIL)
518 {
519 ga_clear(&ga);
520 return FAIL;
521 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100522 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
523 ga.ga_len += len;
524 }
525 p += len;
526 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100527 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100528
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100529 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100530 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100531 {
532 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100533 if (res != NULL)
534 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100535 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100536 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100537#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100538 if (!enc_utf8)
539 {
540 vimconv_T conv;
541
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100542 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100543 conv.vc_type = CONV_NONE;
544 convert_setup(&conv, (char_u*)"utf-8", p_enc);
545 if (conv.vc_type != CONV_NONE)
546 {
547 res->vval.v_string =
548 string_convert(&conv, ga.ga_data, NULL);
549 vim_free(ga.ga_data);
550 }
551 convert_setup(&conv, NULL, NULL);
552 }
553 else
554#endif
555 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100556 }
557 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100558 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100559 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100560 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100561 res->v_type = VAR_SPECIAL;
562 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100563 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100564 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100565 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100566}
567
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100568typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100569 JSON_ARRAY, // parsing items in an array
570 JSON_OBJECT_KEY, // parsing key of an object
571 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100572} json_decode_T;
573
574typedef struct {
575 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100576 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100577 typval_T jd_key_tv;
578 char_u *jd_key;
579} json_dec_item_T;
580
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100581/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100582 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100583 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100584 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100585 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100586 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100587 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100588 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100589json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100590{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100591 char_u *p;
592 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100593 int retval;
594 garray_T stack;
595 typval_T item;
596 typval_T *cur_item;
597 json_dec_item_T *top_item;
598 char_u key_buf[NUMBUFLEN];
599
600 ga_init2(&stack, sizeof(json_dec_item_T), 100);
601 cur_item = res;
602 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100603 if (res != NULL)
604 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100605
Bram Moolenaar56ead342016-02-02 18:20:08 +0100606 fill_numbuflen(reader);
607 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100608 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100609 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100610 top_item = NULL;
611 if (stack.ga_len > 0)
612 {
613 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
614 json_skip_white(reader);
615 p = reader->js_buf + reader->js_used;
616 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100617 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100618 retval = MAYBE;
619 if (top_item->jd_type == JSON_OBJECT)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100620 // did get the key, clear it
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100621 clear_tv(&top_item->jd_key_tv);
622 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100623 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100624 if (top_item->jd_type == JSON_OBJECT_KEY
625 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100626 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100627 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100628 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100629 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100630 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100631 --stack.ga_len;
632 if (stack.ga_len == 0)
633 {
634 retval = OK;
635 goto theend;
636 }
637 if (cur_item != NULL)
638 cur_item = &top_item->jd_tv;
639 goto item_end;
640 }
641 }
642 }
643
644 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
645 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100646 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100647 && reader->js_buf[reader->js_used] != '\''
648 && reader->js_buf[reader->js_used] != '['
649 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100650 {
651 char_u *key;
652
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100653 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100654 key = p = reader->js_buf + reader->js_used;
655 while (*p != NUL && *p != ':' && *p > ' ')
656 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100657 if (cur_item != NULL)
658 {
659 cur_item->v_type = VAR_STRING;
660 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
661 top_item->jd_key = cur_item->vval.v_string;
662 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100663 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100664 }
665 else
666 {
667 switch (*p)
668 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100669 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100670 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
671 {
672 retval = FAIL;
673 break;
674 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100675 if (ga_grow(&stack, 1) == FAIL)
676 {
677 retval = FAIL;
678 break;
679 }
680 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
681 {
682 cur_item->v_type = VAR_SPECIAL;
683 cur_item->vval.v_number = VVAL_NONE;
684 retval = FAIL;
685 break;
686 }
687
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100688 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100689 top_item = ((json_dec_item_T *)stack.ga_data)
690 + stack.ga_len;
691 top_item->jd_type = JSON_ARRAY;
692 ++stack.ga_len;
693 if (cur_item != NULL)
694 {
695 top_item->jd_tv = *cur_item;
696 cur_item = &item;
697 }
698 continue;
699
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100700 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100701 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
702 {
703 retval = FAIL;
704 break;
705 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100706 if (ga_grow(&stack, 1) == FAIL)
707 {
708 retval = FAIL;
709 break;
710 }
711 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
712 {
713 cur_item->v_type = VAR_SPECIAL;
714 cur_item->vval.v_number = VVAL_NONE;
715 retval = FAIL;
716 break;
717 }
718
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100719 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100720 top_item = ((json_dec_item_T *)stack.ga_data)
721 + stack.ga_len;
722 top_item->jd_type = JSON_OBJECT_KEY;
723 ++stack.ga_len;
724 if (cur_item != NULL)
725 {
726 top_item->jd_tv = *cur_item;
727 cur_item = &top_item->jd_key_tv;
728 }
729 continue;
730
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100731 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100732 retval = json_decode_string(reader, cur_item, *p);
733 break;
734
735 case '\'':
736 if (options & JSON_JS)
737 retval = json_decode_string(reader, cur_item, *p);
738 else
739 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100740 emsg(_(e_invarg));
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100741 retval = FAIL;
742 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100743 break;
744
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100745 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100746 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100747 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100748 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100749 retval = FAIL;
750 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100751 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100752 // FALLTHROUGH
753 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100754 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100755 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100756 cur_item->v_type = VAR_SPECIAL;
757 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100758 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100759 retval = OK;
760 break;
761
762 default:
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100763 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100764 {
765#ifdef FEAT_FLOAT
766 char_u *sp = p;
767
768 if (*sp == '-')
769 {
770 ++sp;
771 if (*sp == NUL)
772 {
773 retval = MAYBE;
774 break;
775 }
776 if (!VIM_ISDIGIT(*sp))
777 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100778 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100779 retval = FAIL;
780 break;
781 }
782 }
783 sp = skipdigits(sp);
784 if (*sp == '.' || *sp == 'e' || *sp == 'E')
785 {
786 if (cur_item == NULL)
787 {
788 float_T f;
789
790 len = string2float(p, &f);
791 }
792 else
793 {
794 cur_item->v_type = VAR_FLOAT;
795 len = string2float(p, &cur_item->vval.v_float);
796 }
797 }
798 else
799#endif
800 {
801 varnumber_T nr;
802
803 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100804 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200805 &nr, NULL, 0, TRUE);
806 if (len == 0)
807 {
808 emsg(_(e_invarg));
809 retval = FAIL;
810 goto theend;
811 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100812 if (cur_item != NULL)
813 {
814 cur_item->v_type = VAR_NUMBER;
815 cur_item->vval.v_number = nr;
816 }
817 }
818 reader->js_used += len;
819 retval = OK;
820 break;
821 }
822 if (STRNICMP((char *)p, "false", 5) == 0)
823 {
824 reader->js_used += 5;
825 if (cur_item != NULL)
826 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100827 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100828 cur_item->vval.v_number = VVAL_FALSE;
829 }
830 retval = OK;
831 break;
832 }
833 if (STRNICMP((char *)p, "true", 4) == 0)
834 {
835 reader->js_used += 4;
836 if (cur_item != NULL)
837 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100838 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100839 cur_item->vval.v_number = VVAL_TRUE;
840 }
841 retval = OK;
842 break;
843 }
844 if (STRNICMP((char *)p, "null", 4) == 0)
845 {
846 reader->js_used += 4;
847 if (cur_item != NULL)
848 {
849 cur_item->v_type = VAR_SPECIAL;
850 cur_item->vval.v_number = VVAL_NULL;
851 }
852 retval = OK;
853 break;
854 }
855#ifdef FEAT_FLOAT
856 if (STRNICMP((char *)p, "NaN", 3) == 0)
857 {
858 reader->js_used += 3;
859 if (cur_item != NULL)
860 {
861 cur_item->v_type = VAR_FLOAT;
862 cur_item->vval.v_float = NAN;
863 }
864 retval = OK;
865 break;
866 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100867 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
868 {
869 reader->js_used += 9;
870 if (cur_item != NULL)
871 {
872 cur_item->v_type = VAR_FLOAT;
873 cur_item->vval.v_float = -INFINITY;
874 }
875 retval = OK;
876 break;
877 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100878 if (STRNICMP((char *)p, "Infinity", 8) == 0)
879 {
880 reader->js_used += 8;
881 if (cur_item != NULL)
882 {
883 cur_item->v_type = VAR_FLOAT;
884 cur_item->vval.v_float = INFINITY;
885 }
886 retval = OK;
887 break;
888 }
889#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100890 // check for truncated name
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100891 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
892 if (
893 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
894#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100895 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100896 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
897 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
898#endif
899 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
900 || STRNICMP((char *)p, "null", len) == 0)))
901
902 retval = MAYBE;
903 else
904 retval = FAIL;
905 break;
906 }
907
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100908 // We are finished when retval is FAIL or MAYBE and when at the
909 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100910 if (retval == FAIL)
911 break;
912 if (retval == MAYBE || stack.ga_len == 0)
913 goto theend;
914
915 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
916 && cur_item != NULL)
917 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100918 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100919 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100920 {
921 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100922 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100923 retval = FAIL;
924 goto theend;
925 }
926 }
927 }
928
929item_end:
930 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
931 switch (top_item->jd_type)
932 {
933 case JSON_ARRAY:
934 if (res != NULL)
935 {
936 listitem_T *li = listitem_alloc();
937
938 if (li == NULL)
939 {
940 clear_tv(cur_item);
941 retval = FAIL;
942 goto theend;
943 }
944 li->li_tv = *cur_item;
945 list_append(top_item->jd_tv.vval.v_list, li);
946 }
947 if (cur_item != NULL)
948 cur_item = &item;
949
950 json_skip_white(reader);
951 p = reader->js_buf + reader->js_used;
952 if (*p == ',')
953 ++reader->js_used;
954 else if (*p != ']')
955 {
956 if (*p == NUL)
957 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100958 else
959 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100960 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100961 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100962 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100963 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100964 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100965 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100966
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100967 case JSON_OBJECT_KEY:
968 json_skip_white(reader);
969 p = reader->js_buf + reader->js_used;
970 if (*p != ':')
971 {
972 if (cur_item != NULL)
973 clear_tv(cur_item);
974 if (*p == NUL)
975 retval = MAYBE;
976 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100977 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100978 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100979 retval = FAIL;
980 }
981 goto theend;
982 }
983 ++reader->js_used;
984 json_skip_white(reader);
985 top_item->jd_type = JSON_OBJECT;
986 if (cur_item != NULL)
987 cur_item = &item;
988 break;
989
990 case JSON_OBJECT:
991 if (cur_item != NULL
992 && dict_find(top_item->jd_tv.vval.v_dict,
993 top_item->jd_key, -1) != NULL)
994 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100995 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100996 top_item->jd_key);
997 clear_tv(&top_item->jd_key_tv);
998 clear_tv(cur_item);
999 retval = FAIL;
1000 goto theend;
1001 }
1002
1003 if (cur_item != NULL)
1004 {
1005 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1006
1007 clear_tv(&top_item->jd_key_tv);
1008 if (di == NULL)
1009 {
1010 clear_tv(cur_item);
1011 retval = FAIL;
1012 goto theend;
1013 }
1014 di->di_tv = *cur_item;
1015 di->di_tv.v_lock = 0;
1016 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1017 {
1018 dictitem_free(di);
1019 retval = FAIL;
1020 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001021 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001022 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001023
1024 json_skip_white(reader);
1025 p = reader->js_buf + reader->js_used;
1026 if (*p == ',')
1027 ++reader->js_used;
1028 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001029 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001030 if (*p == NUL)
1031 retval = MAYBE;
1032 else
1033 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001034 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001035 retval = FAIL;
1036 }
1037 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001038 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001039 top_item->jd_type = JSON_OBJECT_KEY;
1040 if (cur_item != NULL)
1041 cur_item = &top_item->jd_key_tv;
1042 break;
1043 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001044 }
1045
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001046 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001047 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001048 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001049 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001050 res->v_type = VAR_SPECIAL;
1051 res->vval.v_number = VVAL_NONE;
1052 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001053 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001054
1055theend:
1056 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001057 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001058}
1059
1060/*
1061 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001062 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001063 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001064 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001065 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001066json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001067{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001068 int ret;
1069
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001070 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001071 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001072 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001073 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001074 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001075 {
1076 if (ret == MAYBE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001077 emsg(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001078 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001079 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001080 json_skip_white(reader);
1081 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001082 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001083 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001084 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001085 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001086 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001087}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001088
Bram Moolenaar113e1072019-01-20 15:30:40 +01001089#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001090/*
1091 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001092 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001093 * Return FAIL for a decoding error.
1094 * Return MAYBE for an incomplete message.
1095 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001096 */
1097 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001098json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001099{
1100 int ret;
1101
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001102 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001103 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1104 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001105 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001106 json_skip_white(reader);
1107
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001108 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001109}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001110#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001111
1112/*
1113 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001114 * "options" can be JSON_JS or zero.
1115 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001116 * Return FAIL if the message has a decoding error.
1117 * Return MAYBE if the message is truncated, need to read more.
1118 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001119 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001120 * Does not advance the reader.
1121 */
1122 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001123json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001124{
1125 int used_save = reader->js_used;
1126 int ret;
1127
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001128 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001129 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1130 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001131 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001132 reader->js_used = used_save;
1133 return ret;
1134}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001135
1136/*
1137 * "js_decode()" function
1138 */
1139 void
1140f_js_decode(typval_T *argvars, typval_T *rettv)
1141{
1142 js_read_T reader;
1143
1144 reader.js_buf = tv_get_string(&argvars[0]);
1145 reader.js_fill = NULL;
1146 reader.js_used = 0;
1147 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
1148 emsg(_(e_invarg));
1149}
1150
1151/*
1152 * "js_encode()" function
1153 */
1154 void
1155f_js_encode(typval_T *argvars, typval_T *rettv)
1156{
1157 rettv->v_type = VAR_STRING;
1158 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1159}
1160
1161/*
1162 * "json_decode()" function
1163 */
1164 void
1165f_json_decode(typval_T *argvars, typval_T *rettv)
1166{
1167 js_read_T reader;
1168
1169 reader.js_buf = tv_get_string(&argvars[0]);
1170 reader.js_fill = NULL;
1171 reader.js_used = 0;
1172 json_decode_all(&reader, rettv, 0);
1173}
1174
1175/*
1176 * "json_encode()" function
1177 */
1178 void
1179f_json_encode(typval_T *argvars, typval_T *rettv)
1180{
1181 rettv->v_type = VAR_STRING;
1182 rettv->vval.v_string = json_encode(&argvars[0], 0);
1183}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001184#endif