blob: e2a011309c32986912a3ffd754dad3980f58846c [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}
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +010089
90/*
91 * Encode "val" into a JSON format string prefixed by the LSP HTTP header.
92 * Returns NULL when out of memory.
93 */
94 char_u *
95json_encode_lsp_msg(typval_T *val)
96{
97 garray_T ga;
98 garray_T lspga;
99
100 ga_init2(&ga, 1, 4000);
101 if (json_encode_gap(&ga, val, 0) == FAIL)
102 return NULL;
103 ga_append(&ga, NUL);
104
105 ga_init2(&lspga, 1, 4000);
Yegappan Lakshmananc3eddd22023-04-25 14:54:54 +0100106 // Header according to LSP specification.
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100107 vim_snprintf((char *)IObuff, IOSIZE,
Magnus Groß8fbd9442023-08-27 00:49:51 +0200108 "Content-Length: %u\r\n\r\n",
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100109 ga.ga_len - 1);
110 ga_concat(&lspga, IObuff);
111 ga_concat_len(&lspga, ga.ga_data, ga.ga_len);
112 ga_clear(&ga);
113 return lspga.ga_data;
114}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100115#endif
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100116
LemonBoybeb0ef12022-04-05 15:07:32 +0100117/*
118 * Lookup table to quickly know if the given ASCII character must be escaped.
119 */
120static const char ascii_needs_escape[128] = {
121 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x0.
122 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x1.
123 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x2.
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x3.
125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x4.
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // 0x5.
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x6.
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x7.
129};
130
131/*
132 * Encode the utf-8 encoded string "str" into "gap".
133 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100134 static void
135write_string(garray_T *gap, char_u *str)
136{
137 char_u *res = str;
138 char_u numbuf[NUMBUFLEN];
LemonBoybeb0ef12022-04-05 15:07:32 +0100139 char_u *from;
140#if defined(USE_ICONV)
141 vimconv_T conv;
142 char_u *converted = NULL;
143#endif
144 int c;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100145
146 if (res == NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100147 {
LemonBoybeb0ef12022-04-05 15:07:32 +0100148 ga_concat(gap, (char_u *)"\"\"");
149 return;
150 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100151
LemonBoybeb0ef12022-04-05 15:07:32 +0100152#if defined(USE_ICONV)
153 if (!enc_utf8)
154 {
155 // Convert the text from 'encoding' to utf-8, because a JSON string is
156 // always utf-8.
157 conv.vc_type = CONV_NONE;
158 convert_setup(&conv, p_enc, (char_u*)"utf-8");
159 if (conv.vc_type != CONV_NONE)
160 converted = res = string_convert(&conv, res, NULL);
161 convert_setup(&conv, NULL, NULL);
162 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100163#endif
LemonBoybeb0ef12022-04-05 15:07:32 +0100164 ga_append(gap, '"');
165 // `from` is the beginning of a sequence of bytes we can directly copy from
166 // the input string, avoiding the overhead associated to decoding/encoding
167 // them.
168 from = res;
169 while ((c = *res) != NUL)
170 {
171 // always use utf-8 encoding, ignore 'encoding'
172 if (c < 0x80)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100173 {
LemonBoybeb0ef12022-04-05 15:07:32 +0100174 if (!ascii_needs_escape[c])
175 {
176 res += 1;
177 continue;
178 }
179
180 if (res != from)
181 ga_concat_len(gap, from, res - from);
182 from = res + 1;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100183
184 switch (c)
185 {
186 case 0x08:
187 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
188 case 0x09:
189 ga_append(gap, '\\'); ga_append(gap, 't'); break;
190 case 0x0a:
191 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
192 case 0x0c:
193 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
194 case 0x0d:
195 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100196 case 0x22: // "
197 case 0x5c: // backslash
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100198 ga_append(gap, '\\');
199 ga_append(gap, c);
200 break;
201 default:
LemonBoybeb0ef12022-04-05 15:07:32 +0100202 vim_snprintf((char *)numbuf, NUMBUFLEN, "\\u%04lx",
203 (long)c);
204 ga_concat(gap, numbuf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100205 }
LemonBoybeb0ef12022-04-05 15:07:32 +0100206
207 res += 1;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100208 }
LemonBoybeb0ef12022-04-05 15:07:32 +0100209 else
210 {
211 int l = utf_ptr2len(res);
212
213 if (l > 1)
214 {
215 res += l;
216 continue;
217 }
218
219 // Invalid utf-8 sequence, replace it with the Unicode replacement
220 // character U+FFFD.
221 if (res != from)
222 ga_concat_len(gap, from, res - from);
223 from = res + 1;
224
225 numbuf[utf_char2bytes(0xFFFD, numbuf)] = NUL;
226 ga_concat(gap, numbuf);
227
228 res += l;
229 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100230 }
LemonBoybeb0ef12022-04-05 15:07:32 +0100231
232 if (res != from)
233 ga_concat_len(gap, from, res - from);
234
235 ga_append(gap, '"');
236#if defined(USE_ICONV)
237 vim_free(converted);
238#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100239}
240
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100241/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100242 * Return TRUE if "key" can be used without quotes.
243 * That is when it starts with a letter and only contains letters, digits and
244 * underscore.
245 */
246 static int
247is_simple_key(char_u *key)
248{
249 char_u *p;
250
251 if (!ASCII_ISALPHA(*key))
252 return FALSE;
253 for (p = key + 1; *p != NUL; ++p)
254 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
255 return FALSE;
256 return TRUE;
257}
258
259/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100260 * Encode "val" into "gap".
261 * Return FAIL or OK.
262 */
263 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100264json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100265{
266 char_u numbuf[NUMBUFLEN];
267 char_u *res;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100268 blob_T *b;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100269 list_T *l;
270 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100271 int i;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100272
273 switch (val->v_type)
274 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100275 case VAR_BOOL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100276 switch ((long)val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100277 {
278 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
279 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100280 }
281 break;
282
283 case VAR_SPECIAL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100284 switch ((long)val->vval.v_number)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100285 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100286 case VVAL_NONE: if ((options & JSON_JS) != 0
287 && (options & JSON_NO_NONE) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100288 // empty item
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100289 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100290 // FALLTHROUGH
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100291 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
292 }
293 break;
294
295 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200296 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100297 (varnumber_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100298 ga_concat(gap, numbuf);
299 break;
300
301 case VAR_STRING:
302 res = val->vval.v_string;
303 write_string(gap, res);
304 break;
305
306 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100307 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100308 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100309 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200310 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000311 case VAR_CLASS:
312 case VAR_OBJECT:
Bram Moolenaara8530892021-02-08 21:53:09 +0100313 semsg(_(e_cannot_json_encode_str), vartype_name(val->v_type));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100314 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100315
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100316 case VAR_BLOB:
317 b = val->vval.v_blob;
318 if (b == NULL || b->bv_ga.ga_len == 0)
319 ga_concat(gap, (char_u *)"[]");
320 else
321 {
322 ga_append(gap, '[');
323 for (i = 0; i < b->bv_ga.ga_len; i++)
324 {
325 if (i > 0)
326 ga_concat(gap, (char_u *)",");
327 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000328 blob_get(b, i));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100329 ga_concat(gap, numbuf);
330 }
331 ga_append(gap, ']');
332 }
333 break;
334
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100335 case VAR_LIST:
336 l = val->vval.v_list;
337 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100338 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100339 else
340 {
341 if (l->lv_copyID == copyID)
342 ga_concat(gap, (char_u *)"[]");
343 else
344 {
345 listitem_T *li;
346
347 l->lv_copyID = copyID;
348 ga_append(gap, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200349 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100350 for (li = l->lv_first; li != NULL && !got_int; )
351 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100352 if (json_encode_item(gap, &li->li_tv, copyID,
353 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100354 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100355 if ((options & JSON_JS)
356 && li->li_next == NULL
357 && li->li_tv.v_type == VAR_SPECIAL
358 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100359 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100360 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100361 li = li->li_next;
362 if (li != NULL)
363 ga_append(gap, ',');
364 }
365 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100366 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100367 }
368 }
369 break;
370
371 case VAR_DICT:
372 d = val->vval.v_dict;
373 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100374 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100375 else
376 {
377 if (d->dv_copyID == copyID)
378 ga_concat(gap, (char_u *)"{}");
379 else
380 {
381 int first = TRUE;
382 int todo = (int)d->dv_hashtab.ht_used;
383 hashitem_T *hi;
384
385 d->dv_copyID = copyID;
386 ga_append(gap, '{');
387
388 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
389 ++hi)
390 if (!HASHITEM_EMPTY(hi))
391 {
392 --todo;
393 if (first)
394 first = FALSE;
395 else
396 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100397 if ((options & JSON_JS)
398 && is_simple_key(hi->hi_key))
399 ga_concat(gap, hi->hi_key);
400 else
401 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100402 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100403 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100404 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100405 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100406 }
407 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100408 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100409 }
410 }
411 break;
412
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100413 case VAR_FLOAT:
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100414#if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100415 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100416 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100417 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100418 {
419 if (val->vval.v_float < 0.0)
420 ga_concat(gap, (char_u *)"-Infinity");
421 else
422 ga_concat(gap, (char_u *)"Infinity");
423 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100424 else
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100425#endif
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100426 {
427 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
428 val->vval.v_float);
429 ga_concat(gap, numbuf);
430 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100431 break;
Bram Moolenaar55fab432016-02-07 16:53:13 +0100432 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200433 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100435 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100436 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100437 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100438 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100439}
440
441/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100442 * When "reader" has less than NUMBUFLEN bytes available, call the fill
443 * callback to get more.
444 */
445 static void
446fill_numbuflen(js_read_T *reader)
447{
448 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
449 - reader->js_used < NUMBUFLEN)
450 {
451 if (reader->js_fill(reader))
452 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
453 }
454}
455
456/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100457 * Skip white space in "reader". All characters <= space are considered white
458 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100459 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100460 */
461 static void
462json_skip_white(js_read_T *reader)
463{
464 int c;
465
Bram Moolenaar56ead342016-02-02 18:20:08 +0100466 for (;;)
467 {
468 c = reader->js_buf[reader->js_used];
469 if (reader->js_fill != NULL && c == NUL)
470 {
471 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200472 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100473 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200474 continue;
475 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100476 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100477 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100478 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100479 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100480 }
481 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100482}
483
Bram Moolenaar56ead342016-02-02 18:20:08 +0100484 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100485json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100486{
487 garray_T ga;
488 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100489 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100490 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200491 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100492
Bram Moolenaar56ead342016-02-02 18:20:08 +0100493 if (res != NULL)
494 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100495
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100496 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100497 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100498 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100499 // The JSON is always expected to be utf-8, thus use utf functions
500 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100501 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100502 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100503 // Not enough bytes to make a character or end of the string. Get
504 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100505 if (reader->js_fill == NULL)
506 break;
507 len = (int)(reader->js_end - p);
508 reader->js_used = (int)(p - reader->js_buf);
509 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100510 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100511 p = reader->js_buf + reader->js_used;
512 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
513 continue;
514 }
515
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100516 if (*p == '\\')
517 {
518 c = -1;
519 switch (p[1])
520 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100521 case '\\': c = '\\'; break;
522 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100523 case 'b': c = BS; break;
524 case 't': c = TAB; break;
525 case 'n': c = NL; break;
526 case 'f': c = FF; break;
527 case 'r': c = CAR; break;
528 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100529 if (reader->js_fill != NULL
530 && (int)(reader->js_end - p) < NUMBUFLEN)
531 {
532 reader->js_used = (int)(p - reader->js_buf);
533 if (reader->js_fill(reader))
534 {
535 p = reader->js_buf + reader->js_used;
536 reader->js_end = reader->js_buf
537 + STRLEN(reader->js_buf);
538 }
539 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100540 nr = 0;
541 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100542 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000543 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200544 if (len == 0)
545 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200546 if (res != NULL)
547 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200548 return FAIL;
549 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100550 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100551 if (0xd800 <= nr && nr <= 0xdfff
552 && (int)(reader->js_end - p) >= 6
553 && *p == '\\' && *(p+1) == 'u')
554 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200555 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100556
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100557 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100558 len = 0;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000559 vim_str2nr(p + 2, NULL, &len, STR2NR_HEX + STR2NR_FORCE,
560 &nr2, NULL, 4, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200561 if (len == 0)
562 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200563 if (res != NULL)
564 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200565 return FAIL;
566 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100567 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
568 {
569 p += len + 2;
570 nr = (((nr - 0xd800) << 10) |
571 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
572 }
573 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100574 if (res != NULL)
575 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200576 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200577
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100578 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100579 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100580 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100581 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100582 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100583 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100584 ++p;
585 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100586 }
587 if (c > 0)
588 {
589 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100590 if (res != NULL)
591 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100592 }
593 }
594 else
595 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100596 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100597 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100598 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100599 if (ga_grow(&ga, len) == FAIL)
600 {
601 ga_clear(&ga);
602 return FAIL;
603 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100604 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
605 ga.ga_len += len;
606 }
607 p += len;
608 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100609 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100610
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100611 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100612 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100613 {
614 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100615 if (res != NULL)
616 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100617 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100618 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100619#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100620 if (!enc_utf8)
621 {
622 vimconv_T conv;
623
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100624 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100625 conv.vc_type = CONV_NONE;
626 convert_setup(&conv, (char_u*)"utf-8", p_enc);
627 if (conv.vc_type != CONV_NONE)
628 {
629 res->vval.v_string =
630 string_convert(&conv, ga.ga_data, NULL);
631 vim_free(ga.ga_data);
632 }
633 convert_setup(&conv, NULL, NULL);
634 }
635 else
636#endif
637 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100638 }
639 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100640 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100641 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100642 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100643 res->v_type = VAR_SPECIAL;
644 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100645 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100646 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100647 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100648}
649
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100650typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100651 JSON_ARRAY, // parsing items in an array
652 JSON_OBJECT_KEY, // parsing key of an object
653 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100654} json_decode_T;
655
656typedef struct {
657 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100658 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100659 typval_T jd_key_tv;
660 char_u *jd_key;
661} json_dec_item_T;
662
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100663/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100664 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100665 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100666 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100667 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100668 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100669 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100670 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100671json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100672{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100673 char_u *p;
Bram Moolenaar6d3a7212020-07-12 14:34:00 +0200674 int i;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100675 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100676 int retval;
677 garray_T stack;
678 typval_T item;
679 typval_T *cur_item;
680 json_dec_item_T *top_item;
681 char_u key_buf[NUMBUFLEN];
682
683 ga_init2(&stack, sizeof(json_dec_item_T), 100);
684 cur_item = res;
685 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100686 if (res != NULL)
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200687 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100688
Bram Moolenaar56ead342016-02-02 18:20:08 +0100689 fill_numbuflen(reader);
690 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100691 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100692 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100693 top_item = NULL;
694 if (stack.ga_len > 0)
695 {
696 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
697 json_skip_white(reader);
698 p = reader->js_buf + reader->js_used;
699 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100700 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100701 retval = MAYBE;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100702 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100703 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100704 if (top_item->jd_type == JSON_OBJECT_KEY
705 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100706 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100707 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100708 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100709 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100710 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100711 --stack.ga_len;
712 if (stack.ga_len == 0)
713 {
714 retval = OK;
715 goto theend;
716 }
717 if (cur_item != NULL)
718 cur_item = &top_item->jd_tv;
719 goto item_end;
720 }
721 }
722 }
723
724 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
725 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100726 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100727 && reader->js_buf[reader->js_used] != '\''
728 && reader->js_buf[reader->js_used] != '['
729 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100730 {
731 char_u *key;
732
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100733 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100734 key = p = reader->js_buf + reader->js_used;
735 while (*p != NUL && *p != ':' && *p > ' ')
736 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100737 if (cur_item != NULL)
738 {
739 cur_item->v_type = VAR_STRING;
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200740 cur_item->vval.v_string = vim_strnsave(key, p - key);
Bram Moolenaare2c60372017-01-22 15:56:26 +0100741 top_item->jd_key = cur_item->vval.v_string;
742 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100743 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100744 }
745 else
746 {
747 switch (*p)
748 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100749 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100750 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
751 {
752 retval = FAIL;
753 break;
754 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100755 if (ga_grow(&stack, 1) == FAIL)
756 {
757 retval = FAIL;
758 break;
759 }
760 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
761 {
762 cur_item->v_type = VAR_SPECIAL;
763 cur_item->vval.v_number = VVAL_NONE;
764 retval = FAIL;
765 break;
766 }
767
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100768 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100769 top_item = ((json_dec_item_T *)stack.ga_data)
770 + stack.ga_len;
771 top_item->jd_type = JSON_ARRAY;
772 ++stack.ga_len;
773 if (cur_item != NULL)
774 {
775 top_item->jd_tv = *cur_item;
776 cur_item = &item;
777 }
778 continue;
779
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100780 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100781 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
782 {
783 retval = FAIL;
784 break;
785 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100786 if (ga_grow(&stack, 1) == FAIL)
787 {
788 retval = FAIL;
789 break;
790 }
791 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
792 {
793 cur_item->v_type = VAR_SPECIAL;
794 cur_item->vval.v_number = VVAL_NONE;
795 retval = FAIL;
796 break;
797 }
798
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100799 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100800 top_item = ((json_dec_item_T *)stack.ga_data)
801 + stack.ga_len;
802 top_item->jd_type = JSON_OBJECT_KEY;
803 ++stack.ga_len;
804 if (cur_item != NULL)
805 {
806 top_item->jd_tv = *cur_item;
807 cur_item = &top_item->jd_key_tv;
808 }
809 continue;
810
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100811 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100812 retval = json_decode_string(reader, cur_item, *p);
813 break;
814
815 case '\'':
816 if (options & JSON_JS)
817 retval = json_decode_string(reader, cur_item, *p);
818 else
819 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000820 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100821 retval = FAIL;
822 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100823 break;
824
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100825 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100826 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100827 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000828 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100829 retval = FAIL;
830 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100831 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100832 // FALLTHROUGH
833 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100834 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100835 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100836 cur_item->v_type = VAR_SPECIAL;
837 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100838 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100839 retval = OK;
840 break;
841
842 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100843 if (VIM_ISDIGIT(*p) || (*p == '-'
844 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100845 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100846 char_u *sp = p;
847
848 if (*sp == '-')
849 {
850 ++sp;
851 if (*sp == NUL)
852 {
853 retval = MAYBE;
854 break;
855 }
856 if (!VIM_ISDIGIT(*sp))
857 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000858 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100859 retval = FAIL;
860 break;
861 }
862 }
863 sp = skipdigits(sp);
864 if (*sp == '.' || *sp == 'e' || *sp == 'E')
865 {
866 if (cur_item == NULL)
867 {
868 float_T f;
869
Bram Moolenaar29500652021-08-08 15:43:34 +0200870 len = string2float(p, &f, FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100871 }
872 else
873 {
874 cur_item->v_type = VAR_FLOAT;
Bram Moolenaar29500652021-08-08 15:43:34 +0200875 len = string2float(p, &cur_item->vval.v_float,
876 FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100877 }
878 }
879 else
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100880 {
881 varnumber_T nr;
882
883 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100884 NULL, &len, 0, // what
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000885 &nr, NULL, 0, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200886 if (len == 0)
887 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000888 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200889 retval = FAIL;
890 goto theend;
891 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100892 if (cur_item != NULL)
893 {
894 cur_item->v_type = VAR_NUMBER;
895 cur_item->vval.v_number = nr;
896 }
897 }
898 reader->js_used += len;
899 retval = OK;
900 break;
901 }
902 if (STRNICMP((char *)p, "false", 5) == 0)
903 {
904 reader->js_used += 5;
905 if (cur_item != NULL)
906 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100907 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100908 cur_item->vval.v_number = VVAL_FALSE;
909 }
910 retval = OK;
911 break;
912 }
913 if (STRNICMP((char *)p, "true", 4) == 0)
914 {
915 reader->js_used += 4;
916 if (cur_item != NULL)
917 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100918 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100919 cur_item->vval.v_number = VVAL_TRUE;
920 }
921 retval = OK;
922 break;
923 }
924 if (STRNICMP((char *)p, "null", 4) == 0)
925 {
926 reader->js_used += 4;
927 if (cur_item != NULL)
928 {
929 cur_item->v_type = VAR_SPECIAL;
930 cur_item->vval.v_number = VVAL_NULL;
931 }
932 retval = OK;
933 break;
934 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100935 if (STRNICMP((char *)p, "NaN", 3) == 0)
936 {
937 reader->js_used += 3;
938 if (cur_item != NULL)
939 {
940 cur_item->v_type = VAR_FLOAT;
941 cur_item->vval.v_float = NAN;
942 }
943 retval = OK;
944 break;
945 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100946 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
947 {
948 reader->js_used += 9;
949 if (cur_item != NULL)
950 {
951 cur_item->v_type = VAR_FLOAT;
952 cur_item->vval.v_float = -INFINITY;
953 }
954 retval = OK;
955 break;
956 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100957 if (STRNICMP((char *)p, "Infinity", 8) == 0)
958 {
959 reader->js_used += 8;
960 if (cur_item != NULL)
961 {
962 cur_item->v_type = VAR_FLOAT;
963 cur_item->vval.v_float = INFINITY;
964 }
965 retval = OK;
966 break;
967 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100968 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100969 len = (int)(reader->js_end
970 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100971 if (
972 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100973 || (len < 9
974 && STRNICMP((char *)p, "-Infinity", len) == 0)
975 || (len < 8
976 && STRNICMP((char *)p, "Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100977 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100978 || (len < 4
979 && (STRNICMP((char *)p, "true", len) == 0
980 || STRNICMP((char *)p, "null", len) == 0)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100981
982 retval = MAYBE;
983 else
984 retval = FAIL;
985 break;
986 }
987
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100988 // We are finished when retval is FAIL or MAYBE and when at the
989 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100990 if (retval == FAIL)
991 break;
992 if (retval == MAYBE || stack.ga_len == 0)
993 goto theend;
994
995 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
996 && cur_item != NULL)
997 {
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200998 if (cur_item->v_type == VAR_FLOAT)
999 {
1000 // cannot use a float as a key
Bram Moolenaar74409f62022-01-01 15:58:22 +00001001 emsg(_(e_using_float_as_string));
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02001002 retval = FAIL;
1003 goto theend;
1004 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001005 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +01001006 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001007 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001008 emsg(_(e_invalid_argument));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001009 retval = FAIL;
1010 goto theend;
1011 }
1012 }
1013 }
1014
1015item_end:
1016 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
1017 switch (top_item->jd_type)
1018 {
1019 case JSON_ARRAY:
1020 if (res != NULL)
1021 {
1022 listitem_T *li = listitem_alloc();
1023
1024 if (li == NULL)
1025 {
1026 clear_tv(cur_item);
1027 retval = FAIL;
1028 goto theend;
1029 }
1030 li->li_tv = *cur_item;
1031 list_append(top_item->jd_tv.vval.v_list, li);
1032 }
1033 if (cur_item != NULL)
1034 cur_item = &item;
1035
1036 json_skip_white(reader);
1037 p = reader->js_buf + reader->js_used;
1038 if (*p == ',')
1039 ++reader->js_used;
1040 else if (*p != ']')
1041 {
1042 if (*p == NUL)
1043 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001044 else
1045 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001046 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001047 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001048 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001049 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001050 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001051 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001052
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001053 case JSON_OBJECT_KEY:
1054 json_skip_white(reader);
1055 p = reader->js_buf + reader->js_used;
1056 if (*p != ':')
1057 {
1058 if (cur_item != NULL)
1059 clear_tv(cur_item);
1060 if (*p == NUL)
1061 retval = MAYBE;
1062 else
Bram Moolenaar56ead342016-02-02 18:20:08 +01001063 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001064 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001065 retval = FAIL;
1066 }
1067 goto theend;
1068 }
1069 ++reader->js_used;
1070 json_skip_white(reader);
1071 top_item->jd_type = JSON_OBJECT;
1072 if (cur_item != NULL)
1073 cur_item = &item;
1074 break;
1075
1076 case JSON_OBJECT:
1077 if (cur_item != NULL
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001078 && dict_has_key(top_item->jd_tv.vval.v_dict,
1079 (char *)top_item->jd_key))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001080 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001081 semsg(_(e_duplicate_key_in_json_str), top_item->jd_key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001082 clear_tv(cur_item);
1083 retval = FAIL;
1084 goto theend;
1085 }
1086
1087 if (cur_item != NULL)
1088 {
1089 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1090
1091 clear_tv(&top_item->jd_key_tv);
1092 if (di == NULL)
1093 {
1094 clear_tv(cur_item);
1095 retval = FAIL;
1096 goto theend;
1097 }
1098 di->di_tv = *cur_item;
1099 di->di_tv.v_lock = 0;
1100 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1101 {
1102 dictitem_free(di);
1103 retval = FAIL;
1104 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001105 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001106 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001107
1108 json_skip_white(reader);
1109 p = reader->js_buf + reader->js_used;
1110 if (*p == ',')
1111 ++reader->js_used;
1112 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001113 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001114 if (*p == NUL)
1115 retval = MAYBE;
1116 else
1117 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001118 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001119 retval = FAIL;
1120 }
1121 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001122 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001123 top_item->jd_type = JSON_OBJECT_KEY;
1124 if (cur_item != NULL)
1125 cur_item = &top_item->jd_key_tv;
1126 break;
1127 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001128 }
1129
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001130 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001131 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001132 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001133 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001134 res->v_type = VAR_SPECIAL;
1135 res->vval.v_number = VVAL_NONE;
1136 }
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001137 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001138
1139theend:
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001140 for (i = 0; i < stack.ga_len; i++)
1141 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001142 ga_clear(&stack);
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001143
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001144 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001145}
1146
1147/*
1148 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001149 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001150 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001151 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001152 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001153json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001154{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001155 int ret;
1156
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001157 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001158 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001159 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001160 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001161 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001162 {
1163 if (ret == MAYBE)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001164 semsg(_(e_json_decode_error_at_str), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001165 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001166 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001167 json_skip_white(reader);
1168 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001169 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001170 semsg(_(e_trailing_characters_str), reader->js_buf + reader->js_used);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001171 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001172 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001173 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001174}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001175
Bram Moolenaar113e1072019-01-20 15:30:40 +01001176#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001177/*
1178 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001179 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001180 * Return FAIL for a decoding error.
1181 * Return MAYBE for an incomplete message.
1182 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001183 */
1184 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001185json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001186{
1187 int ret;
1188
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001189 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001190 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1191 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001192 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001193 json_skip_white(reader);
1194
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001195 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001196}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001197#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001198
1199/*
1200 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001201 * "options" can be JSON_JS or zero.
1202 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001203 * Return FAIL if the message has a decoding error.
1204 * Return MAYBE if the message is truncated, need to read more.
1205 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001206 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001207 * Does not advance the reader.
1208 */
1209 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001210json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001211{
1212 int used_save = reader->js_used;
1213 int ret;
1214
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001215 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001216 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1217 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001218 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001219 reader->js_used = used_save;
1220 return ret;
1221}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001222
1223/*
1224 * "js_decode()" function
1225 */
1226 void
1227f_js_decode(typval_T *argvars, typval_T *rettv)
1228{
1229 js_read_T reader;
1230
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001231 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1232 return;
1233
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001234 reader.js_buf = tv_get_string(&argvars[0]);
1235 reader.js_fill = NULL;
1236 reader.js_used = 0;
1237 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001238 emsg(_(e_invalid_argument));
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001239}
1240
1241/*
1242 * "js_encode()" function
1243 */
1244 void
1245f_js_encode(typval_T *argvars, typval_T *rettv)
1246{
1247 rettv->v_type = VAR_STRING;
1248 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1249}
1250
1251/*
1252 * "json_decode()" function
1253 */
1254 void
1255f_json_decode(typval_T *argvars, typval_T *rettv)
1256{
1257 js_read_T reader;
1258
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001259 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1260 return;
1261
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001262 reader.js_buf = tv_get_string(&argvars[0]);
1263 reader.js_fill = NULL;
1264 reader.js_used = 0;
1265 json_decode_all(&reader, rettv, 0);
1266}
1267
1268/*
1269 * "json_encode()" function
1270 */
1271 void
1272f_json_encode(typval_T *argvars, typval_T *rettv)
1273{
1274 rettv->v_type = VAR_STRING;
1275 rettv->vval.v_string = json_encode(&argvars[0], 0);
1276}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001277#endif