blob: 80120acb303235c223095d7ec2eaea5256e19668 [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);
106 vim_snprintf((char *)IObuff, IOSIZE,
107 "Content-Length: %u\r\n"
108 "Content-Type: application/vim-jsonrpc; charset=utf-8\r\n\r\n",
109 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 Moolenaara8530892021-02-08 21:53:09 +0100311 semsg(_(e_cannot_json_encode_str), vartype_name(val->v_type));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100312 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100313
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100314 case VAR_BLOB:
315 b = val->vval.v_blob;
316 if (b == NULL || b->bv_ga.ga_len == 0)
317 ga_concat(gap, (char_u *)"[]");
318 else
319 {
320 ga_append(gap, '[');
321 for (i = 0; i < b->bv_ga.ga_len; i++)
322 {
323 if (i > 0)
324 ga_concat(gap, (char_u *)",");
325 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000326 blob_get(b, i));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100327 ga_concat(gap, numbuf);
328 }
329 ga_append(gap, ']');
330 }
331 break;
332
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100333 case VAR_LIST:
334 l = val->vval.v_list;
335 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100336 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100337 else
338 {
339 if (l->lv_copyID == copyID)
340 ga_concat(gap, (char_u *)"[]");
341 else
342 {
343 listitem_T *li;
344
345 l->lv_copyID = copyID;
346 ga_append(gap, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200347 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100348 for (li = l->lv_first; li != NULL && !got_int; )
349 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100350 if (json_encode_item(gap, &li->li_tv, copyID,
351 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100352 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100353 if ((options & JSON_JS)
354 && li->li_next == NULL
355 && li->li_tv.v_type == VAR_SPECIAL
356 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100357 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100358 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100359 li = li->li_next;
360 if (li != NULL)
361 ga_append(gap, ',');
362 }
363 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100364 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100365 }
366 }
367 break;
368
369 case VAR_DICT:
370 d = val->vval.v_dict;
371 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100372 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100373 else
374 {
375 if (d->dv_copyID == copyID)
376 ga_concat(gap, (char_u *)"{}");
377 else
378 {
379 int first = TRUE;
380 int todo = (int)d->dv_hashtab.ht_used;
381 hashitem_T *hi;
382
383 d->dv_copyID = copyID;
384 ga_append(gap, '{');
385
386 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
387 ++hi)
388 if (!HASHITEM_EMPTY(hi))
389 {
390 --todo;
391 if (first)
392 first = FALSE;
393 else
394 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100395 if ((options & JSON_JS)
396 && is_simple_key(hi->hi_key))
397 ga_concat(gap, hi->hi_key);
398 else
399 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100400 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100401 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100402 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100403 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100404 }
405 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100406 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100407 }
408 }
409 break;
410
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100411 case VAR_FLOAT:
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100412#if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100413 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100414 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100415 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100416 {
417 if (val->vval.v_float < 0.0)
418 ga_concat(gap, (char_u *)"-Infinity");
419 else
420 ga_concat(gap, (char_u *)"Infinity");
421 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100422 else
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100423#endif
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100424 {
425 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
426 val->vval.v_float);
427 ga_concat(gap, numbuf);
428 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100429 break;
Bram Moolenaar55fab432016-02-07 16:53:13 +0100430 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200431 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100432 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100433 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100434 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100435 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100436 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100437}
438
439/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100440 * When "reader" has less than NUMBUFLEN bytes available, call the fill
441 * callback to get more.
442 */
443 static void
444fill_numbuflen(js_read_T *reader)
445{
446 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
447 - reader->js_used < NUMBUFLEN)
448 {
449 if (reader->js_fill(reader))
450 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
451 }
452}
453
454/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100455 * Skip white space in "reader". All characters <= space are considered white
456 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100457 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100458 */
459 static void
460json_skip_white(js_read_T *reader)
461{
462 int c;
463
Bram Moolenaar56ead342016-02-02 18:20:08 +0100464 for (;;)
465 {
466 c = reader->js_buf[reader->js_used];
467 if (reader->js_fill != NULL && c == NUL)
468 {
469 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200470 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100471 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200472 continue;
473 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100474 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100475 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100476 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100477 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100478 }
479 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100480}
481
Bram Moolenaar56ead342016-02-02 18:20:08 +0100482 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100483json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100484{
485 garray_T ga;
486 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100487 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100488 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200489 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100490
Bram Moolenaar56ead342016-02-02 18:20:08 +0100491 if (res != NULL)
492 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100493
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100494 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100495 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100496 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100497 // The JSON is always expected to be utf-8, thus use utf functions
498 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100499 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100500 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100501 // Not enough bytes to make a character or end of the string. Get
502 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100503 if (reader->js_fill == NULL)
504 break;
505 len = (int)(reader->js_end - p);
506 reader->js_used = (int)(p - reader->js_buf);
507 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100508 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100509 p = reader->js_buf + reader->js_used;
510 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
511 continue;
512 }
513
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100514 if (*p == '\\')
515 {
516 c = -1;
517 switch (p[1])
518 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100519 case '\\': c = '\\'; break;
520 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100521 case 'b': c = BS; break;
522 case 't': c = TAB; break;
523 case 'n': c = NL; break;
524 case 'f': c = FF; break;
525 case 'r': c = CAR; break;
526 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100527 if (reader->js_fill != NULL
528 && (int)(reader->js_end - p) < NUMBUFLEN)
529 {
530 reader->js_used = (int)(p - reader->js_buf);
531 if (reader->js_fill(reader))
532 {
533 p = reader->js_buf + reader->js_used;
534 reader->js_end = reader->js_buf
535 + STRLEN(reader->js_buf);
536 }
537 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100538 nr = 0;
539 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100540 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200541 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
542 if (len == 0)
543 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200544 if (res != NULL)
545 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200546 return FAIL;
547 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100548 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100549 if (0xd800 <= nr && nr <= 0xdfff
550 && (int)(reader->js_end - p) >= 6
551 && *p == '\\' && *(p+1) == 'u')
552 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200553 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100554
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100555 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100556 len = 0;
557 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200558 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
559 if (len == 0)
560 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200561 if (res != NULL)
562 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200563 return FAIL;
564 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100565 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
566 {
567 p += len + 2;
568 nr = (((nr - 0xd800) << 10) |
569 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
570 }
571 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100572 if (res != NULL)
573 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200574 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200575
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100576 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100577 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100578 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100579 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100580 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100581 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100582 ++p;
583 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100584 }
585 if (c > 0)
586 {
587 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100588 if (res != NULL)
589 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100590 }
591 }
592 else
593 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100594 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100595 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100596 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100597 if (ga_grow(&ga, len) == FAIL)
598 {
599 ga_clear(&ga);
600 return FAIL;
601 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100602 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
603 ga.ga_len += len;
604 }
605 p += len;
606 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100607 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100608
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100609 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100610 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100611 {
612 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100613 if (res != NULL)
614 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100615 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100616 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100617#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100618 if (!enc_utf8)
619 {
620 vimconv_T conv;
621
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100622 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100623 conv.vc_type = CONV_NONE;
624 convert_setup(&conv, (char_u*)"utf-8", p_enc);
625 if (conv.vc_type != CONV_NONE)
626 {
627 res->vval.v_string =
628 string_convert(&conv, ga.ga_data, NULL);
629 vim_free(ga.ga_data);
630 }
631 convert_setup(&conv, NULL, NULL);
632 }
633 else
634#endif
635 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100636 }
637 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100638 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100639 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100640 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100641 res->v_type = VAR_SPECIAL;
642 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100643 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100644 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100645 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100646}
647
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100648typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100649 JSON_ARRAY, // parsing items in an array
650 JSON_OBJECT_KEY, // parsing key of an object
651 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100652} json_decode_T;
653
654typedef struct {
655 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100656 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100657 typval_T jd_key_tv;
658 char_u *jd_key;
659} json_dec_item_T;
660
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100661/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100662 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100663 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100664 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100665 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100666 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100667 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100668 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100669json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100670{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100671 char_u *p;
Bram Moolenaar6d3a7212020-07-12 14:34:00 +0200672 int i;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100673 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100674 int retval;
675 garray_T stack;
676 typval_T item;
677 typval_T *cur_item;
678 json_dec_item_T *top_item;
679 char_u key_buf[NUMBUFLEN];
680
681 ga_init2(&stack, sizeof(json_dec_item_T), 100);
682 cur_item = res;
683 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100684 if (res != NULL)
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200685 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100686
Bram Moolenaar56ead342016-02-02 18:20:08 +0100687 fill_numbuflen(reader);
688 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100689 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100690 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100691 top_item = NULL;
692 if (stack.ga_len > 0)
693 {
694 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
695 json_skip_white(reader);
696 p = reader->js_buf + reader->js_used;
697 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100698 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100699 retval = MAYBE;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100700 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100701 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100702 if (top_item->jd_type == JSON_OBJECT_KEY
703 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100704 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100705 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100706 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100707 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100708 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100709 --stack.ga_len;
710 if (stack.ga_len == 0)
711 {
712 retval = OK;
713 goto theend;
714 }
715 if (cur_item != NULL)
716 cur_item = &top_item->jd_tv;
717 goto item_end;
718 }
719 }
720 }
721
722 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
723 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100724 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100725 && reader->js_buf[reader->js_used] != '\''
726 && reader->js_buf[reader->js_used] != '['
727 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100728 {
729 char_u *key;
730
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100731 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100732 key = p = reader->js_buf + reader->js_used;
733 while (*p != NUL && *p != ':' && *p > ' ')
734 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100735 if (cur_item != NULL)
736 {
737 cur_item->v_type = VAR_STRING;
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200738 cur_item->vval.v_string = vim_strnsave(key, p - key);
Bram Moolenaare2c60372017-01-22 15:56:26 +0100739 top_item->jd_key = cur_item->vval.v_string;
740 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100741 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100742 }
743 else
744 {
745 switch (*p)
746 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100747 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100748 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
749 {
750 retval = FAIL;
751 break;
752 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100753 if (ga_grow(&stack, 1) == FAIL)
754 {
755 retval = FAIL;
756 break;
757 }
758 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
759 {
760 cur_item->v_type = VAR_SPECIAL;
761 cur_item->vval.v_number = VVAL_NONE;
762 retval = FAIL;
763 break;
764 }
765
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100766 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100767 top_item = ((json_dec_item_T *)stack.ga_data)
768 + stack.ga_len;
769 top_item->jd_type = JSON_ARRAY;
770 ++stack.ga_len;
771 if (cur_item != NULL)
772 {
773 top_item->jd_tv = *cur_item;
774 cur_item = &item;
775 }
776 continue;
777
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100778 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100779 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
780 {
781 retval = FAIL;
782 break;
783 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100784 if (ga_grow(&stack, 1) == FAIL)
785 {
786 retval = FAIL;
787 break;
788 }
789 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
790 {
791 cur_item->v_type = VAR_SPECIAL;
792 cur_item->vval.v_number = VVAL_NONE;
793 retval = FAIL;
794 break;
795 }
796
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100797 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100798 top_item = ((json_dec_item_T *)stack.ga_data)
799 + stack.ga_len;
800 top_item->jd_type = JSON_OBJECT_KEY;
801 ++stack.ga_len;
802 if (cur_item != NULL)
803 {
804 top_item->jd_tv = *cur_item;
805 cur_item = &top_item->jd_key_tv;
806 }
807 continue;
808
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100809 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100810 retval = json_decode_string(reader, cur_item, *p);
811 break;
812
813 case '\'':
814 if (options & JSON_JS)
815 retval = json_decode_string(reader, cur_item, *p);
816 else
817 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000818 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100819 retval = FAIL;
820 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100821 break;
822
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100823 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100824 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100825 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000826 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100827 retval = FAIL;
828 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100829 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100830 // FALLTHROUGH
831 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100832 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100833 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100834 cur_item->v_type = VAR_SPECIAL;
835 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100836 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100837 retval = OK;
838 break;
839
840 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100841 if (VIM_ISDIGIT(*p) || (*p == '-'
842 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100843 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100844 char_u *sp = p;
845
846 if (*sp == '-')
847 {
848 ++sp;
849 if (*sp == NUL)
850 {
851 retval = MAYBE;
852 break;
853 }
854 if (!VIM_ISDIGIT(*sp))
855 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000856 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100857 retval = FAIL;
858 break;
859 }
860 }
861 sp = skipdigits(sp);
862 if (*sp == '.' || *sp == 'e' || *sp == 'E')
863 {
864 if (cur_item == NULL)
865 {
866 float_T f;
867
Bram Moolenaar29500652021-08-08 15:43:34 +0200868 len = string2float(p, &f, FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100869 }
870 else
871 {
872 cur_item->v_type = VAR_FLOAT;
Bram Moolenaar29500652021-08-08 15:43:34 +0200873 len = string2float(p, &cur_item->vval.v_float,
874 FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100875 }
876 }
877 else
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100878 {
879 varnumber_T nr;
880
881 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100882 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200883 &nr, NULL, 0, TRUE);
884 if (len == 0)
885 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000886 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200887 retval = FAIL;
888 goto theend;
889 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100890 if (cur_item != NULL)
891 {
892 cur_item->v_type = VAR_NUMBER;
893 cur_item->vval.v_number = nr;
894 }
895 }
896 reader->js_used += len;
897 retval = OK;
898 break;
899 }
900 if (STRNICMP((char *)p, "false", 5) == 0)
901 {
902 reader->js_used += 5;
903 if (cur_item != NULL)
904 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100905 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100906 cur_item->vval.v_number = VVAL_FALSE;
907 }
908 retval = OK;
909 break;
910 }
911 if (STRNICMP((char *)p, "true", 4) == 0)
912 {
913 reader->js_used += 4;
914 if (cur_item != NULL)
915 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100916 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100917 cur_item->vval.v_number = VVAL_TRUE;
918 }
919 retval = OK;
920 break;
921 }
922 if (STRNICMP((char *)p, "null", 4) == 0)
923 {
924 reader->js_used += 4;
925 if (cur_item != NULL)
926 {
927 cur_item->v_type = VAR_SPECIAL;
928 cur_item->vval.v_number = VVAL_NULL;
929 }
930 retval = OK;
931 break;
932 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100933 if (STRNICMP((char *)p, "NaN", 3) == 0)
934 {
935 reader->js_used += 3;
936 if (cur_item != NULL)
937 {
938 cur_item->v_type = VAR_FLOAT;
939 cur_item->vval.v_float = NAN;
940 }
941 retval = OK;
942 break;
943 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100944 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
945 {
946 reader->js_used += 9;
947 if (cur_item != NULL)
948 {
949 cur_item->v_type = VAR_FLOAT;
950 cur_item->vval.v_float = -INFINITY;
951 }
952 retval = OK;
953 break;
954 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100955 if (STRNICMP((char *)p, "Infinity", 8) == 0)
956 {
957 reader->js_used += 8;
958 if (cur_item != NULL)
959 {
960 cur_item->v_type = VAR_FLOAT;
961 cur_item->vval.v_float = INFINITY;
962 }
963 retval = OK;
964 break;
965 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100966 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100967 len = (int)(reader->js_end
968 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100969 if (
970 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100971 || (len < 9
972 && STRNICMP((char *)p, "-Infinity", len) == 0)
973 || (len < 8
974 && STRNICMP((char *)p, "Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100975 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100976 || (len < 4
977 && (STRNICMP((char *)p, "true", len) == 0
978 || STRNICMP((char *)p, "null", len) == 0)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100979
980 retval = MAYBE;
981 else
982 retval = FAIL;
983 break;
984 }
985
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100986 // We are finished when retval is FAIL or MAYBE and when at the
987 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100988 if (retval == FAIL)
989 break;
990 if (retval == MAYBE || stack.ga_len == 0)
991 goto theend;
992
993 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
994 && cur_item != NULL)
995 {
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200996 if (cur_item->v_type == VAR_FLOAT)
997 {
998 // cannot use a float as a key
Bram Moolenaar74409f62022-01-01 15:58:22 +0000999 emsg(_(e_using_float_as_string));
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02001000 retval = FAIL;
1001 goto theend;
1002 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001003 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +01001004 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001005 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001006 emsg(_(e_invalid_argument));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001007 retval = FAIL;
1008 goto theend;
1009 }
1010 }
1011 }
1012
1013item_end:
1014 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
1015 switch (top_item->jd_type)
1016 {
1017 case JSON_ARRAY:
1018 if (res != NULL)
1019 {
1020 listitem_T *li = listitem_alloc();
1021
1022 if (li == NULL)
1023 {
1024 clear_tv(cur_item);
1025 retval = FAIL;
1026 goto theend;
1027 }
1028 li->li_tv = *cur_item;
1029 list_append(top_item->jd_tv.vval.v_list, li);
1030 }
1031 if (cur_item != NULL)
1032 cur_item = &item;
1033
1034 json_skip_white(reader);
1035 p = reader->js_buf + reader->js_used;
1036 if (*p == ',')
1037 ++reader->js_used;
1038 else if (*p != ']')
1039 {
1040 if (*p == NUL)
1041 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001042 else
1043 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001044 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001045 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001046 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001047 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001048 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001049 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001050
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001051 case JSON_OBJECT_KEY:
1052 json_skip_white(reader);
1053 p = reader->js_buf + reader->js_used;
1054 if (*p != ':')
1055 {
1056 if (cur_item != NULL)
1057 clear_tv(cur_item);
1058 if (*p == NUL)
1059 retval = MAYBE;
1060 else
Bram Moolenaar56ead342016-02-02 18:20:08 +01001061 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001062 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001063 retval = FAIL;
1064 }
1065 goto theend;
1066 }
1067 ++reader->js_used;
1068 json_skip_white(reader);
1069 top_item->jd_type = JSON_OBJECT;
1070 if (cur_item != NULL)
1071 cur_item = &item;
1072 break;
1073
1074 case JSON_OBJECT:
1075 if (cur_item != NULL
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001076 && dict_has_key(top_item->jd_tv.vval.v_dict,
1077 (char *)top_item->jd_key))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001078 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001079 semsg(_(e_duplicate_key_in_json_str), top_item->jd_key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001080 clear_tv(cur_item);
1081 retval = FAIL;
1082 goto theend;
1083 }
1084
1085 if (cur_item != NULL)
1086 {
1087 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1088
1089 clear_tv(&top_item->jd_key_tv);
1090 if (di == NULL)
1091 {
1092 clear_tv(cur_item);
1093 retval = FAIL;
1094 goto theend;
1095 }
1096 di->di_tv = *cur_item;
1097 di->di_tv.v_lock = 0;
1098 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1099 {
1100 dictitem_free(di);
1101 retval = FAIL;
1102 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001103 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001104 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001105
1106 json_skip_white(reader);
1107 p = reader->js_buf + reader->js_used;
1108 if (*p == ',')
1109 ++reader->js_used;
1110 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001111 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001112 if (*p == NUL)
1113 retval = MAYBE;
1114 else
1115 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001116 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001117 retval = FAIL;
1118 }
1119 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001120 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001121 top_item->jd_type = JSON_OBJECT_KEY;
1122 if (cur_item != NULL)
1123 cur_item = &top_item->jd_key_tv;
1124 break;
1125 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001126 }
1127
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001128 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001129 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001130 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001131 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001132 res->v_type = VAR_SPECIAL;
1133 res->vval.v_number = VVAL_NONE;
1134 }
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001135 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001136
1137theend:
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001138 for (i = 0; i < stack.ga_len; i++)
1139 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001140 ga_clear(&stack);
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001141
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001142 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001143}
1144
1145/*
1146 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001147 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001148 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001149 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001150 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001151json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001152{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001153 int ret;
1154
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001155 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001156 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001157 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001158 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001159 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001160 {
1161 if (ret == MAYBE)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001162 semsg(_(e_json_decode_error_at_str), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001163 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001164 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001165 json_skip_white(reader);
1166 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001167 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001168 semsg(_(e_trailing_characters_str), reader->js_buf + reader->js_used);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001169 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001170 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001171 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001172}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001173
Bram Moolenaar113e1072019-01-20 15:30:40 +01001174#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001175/*
1176 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001177 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001178 * Return FAIL for a decoding error.
1179 * Return MAYBE for an incomplete message.
1180 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001181 */
1182 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001183json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001184{
1185 int ret;
1186
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001187 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001188 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1189 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001190 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001191 json_skip_white(reader);
1192
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001193 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001194}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001195#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001196
1197/*
1198 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001199 * "options" can be JSON_JS or zero.
1200 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001201 * Return FAIL if the message has a decoding error.
1202 * Return MAYBE if the message is truncated, need to read more.
1203 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001204 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001205 * Does not advance the reader.
1206 */
1207 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001208json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001209{
1210 int used_save = reader->js_used;
1211 int ret;
1212
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001213 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001214 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1215 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001216 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001217 reader->js_used = used_save;
1218 return ret;
1219}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001220
1221/*
1222 * "js_decode()" function
1223 */
1224 void
1225f_js_decode(typval_T *argvars, typval_T *rettv)
1226{
1227 js_read_T reader;
1228
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001229 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1230 return;
1231
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001232 reader.js_buf = tv_get_string(&argvars[0]);
1233 reader.js_fill = NULL;
1234 reader.js_used = 0;
1235 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001236 emsg(_(e_invalid_argument));
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001237}
1238
1239/*
1240 * "js_encode()" function
1241 */
1242 void
1243f_js_encode(typval_T *argvars, typval_T *rettv)
1244{
1245 rettv->v_type = VAR_STRING;
1246 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1247}
1248
1249/*
1250 * "json_decode()" function
1251 */
1252 void
1253f_json_decode(typval_T *argvars, typval_T *rettv)
1254{
1255 js_read_T reader;
1256
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001257 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1258 return;
1259
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001260 reader.js_buf = tv_get_string(&argvars[0]);
1261 reader.js_fill = NULL;
1262 reader.js_used = 0;
1263 json_decode_all(&reader, rettv, 0);
1264}
1265
1266/*
1267 * "json_encode()" function
1268 */
1269 void
1270f_json_encode(typval_T *argvars, typval_T *rettv)
1271{
1272 rettv->v_type = VAR_STRING;
1273 rettv->vval.v_string = json_encode(&argvars[0], 0);
1274}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001275#endif