blob: faa35e576d0fb6b2ee4adcc2b329cb0d2c21ffad [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;
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100270 tuple_T *tuple;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100271 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100272 int i;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100273
274 switch (val->v_type)
275 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100276 case VAR_BOOL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100277 switch ((long)val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100278 {
279 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
280 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100281 }
282 break;
283
284 case VAR_SPECIAL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100285 switch ((long)val->vval.v_number)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100286 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100287 case VVAL_NONE: if ((options & JSON_JS) != 0
288 && (options & JSON_NO_NONE) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100289 // empty item
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100290 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100291 // FALLTHROUGH
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100292 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
293 }
294 break;
295
296 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200297 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100298 (varnumber_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100299 ga_concat(gap, numbuf);
300 break;
301
302 case VAR_STRING:
303 res = val->vval.v_string;
304 write_string(gap, res);
305 break;
306
307 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100308 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100309 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100310 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200311 case VAR_INSTR:
Bram Moolenaar00b28d62022-12-08 15:32:33 +0000312 case VAR_CLASS:
313 case VAR_OBJECT:
Yegappan Lakshmananec3cebb2023-10-27 19:35:26 +0200314 case VAR_TYPEALIAS:
Bram Moolenaara8530892021-02-08 21:53:09 +0100315 semsg(_(e_cannot_json_encode_str), vartype_name(val->v_type));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100316 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100317
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100318 case VAR_BLOB:
319 b = val->vval.v_blob;
320 if (b == NULL || b->bv_ga.ga_len == 0)
321 ga_concat(gap, (char_u *)"[]");
322 else
323 {
324 ga_append(gap, '[');
325 for (i = 0; i < b->bv_ga.ga_len; i++)
326 {
327 if (i > 0)
328 ga_concat(gap, (char_u *)",");
329 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000330 blob_get(b, i));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100331 ga_concat(gap, numbuf);
332 }
333 ga_append(gap, ']');
334 }
335 break;
336
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100337 case VAR_LIST:
338 l = val->vval.v_list;
339 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100340 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100341 else
342 {
343 if (l->lv_copyID == copyID)
344 ga_concat(gap, (char_u *)"[]");
345 else
346 {
347 listitem_T *li;
348
349 l->lv_copyID = copyID;
350 ga_append(gap, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200351 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100352 for (li = l->lv_first; li != NULL && !got_int; )
353 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100354 if (json_encode_item(gap, &li->li_tv, copyID,
355 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100356 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100357 if ((options & JSON_JS)
358 && li->li_next == NULL
359 && li->li_tv.v_type == VAR_SPECIAL
360 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100361 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100362 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100363 li = li->li_next;
364 if (li != NULL)
365 ga_append(gap, ',');
366 }
367 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100368 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100369 }
370 }
371 break;
372
Yegappan Lakshmanan9cb865e2025-03-23 16:42:16 +0100373 case VAR_TUPLE:
374 tuple = val->vval.v_tuple;
375 if (tuple == NULL)
376 ga_concat(gap, (char_u *)"[]");
377 else
378 {
379 if (tuple->tv_copyID == copyID)
380 ga_concat(gap, (char_u *)"[]");
381 else
382 {
383 int len = TUPLE_LEN(tuple);
384
385 tuple->tv_copyID = copyID;
386 ga_append(gap, '[');
387 for (i = 0; i < len && !got_int; i++)
388 {
389 typval_T *t_item = TUPLE_ITEM(tuple, i);
390 if (json_encode_item(gap, t_item, copyID,
391 options & JSON_JS) == FAIL)
392 return FAIL;
393
394 if ((options & JSON_JS)
395 && i == len - 1
396 && t_item->v_type == VAR_SPECIAL
397 && t_item->vval.v_number == VVAL_NONE)
398 // add an extra comma if the last item is v:none
399 ga_append(gap, ',');
400 if (i <= len - 2)
401 ga_append(gap, ',');
402 }
403 ga_append(gap, ']');
404 tuple->tv_copyID = 0;
405 }
406 }
407 break;
408
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100409 case VAR_DICT:
410 d = val->vval.v_dict;
411 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100412 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100413 else
414 {
415 if (d->dv_copyID == copyID)
416 ga_concat(gap, (char_u *)"{}");
417 else
418 {
419 int first = TRUE;
420 int todo = (int)d->dv_hashtab.ht_used;
421 hashitem_T *hi;
422
423 d->dv_copyID = copyID;
424 ga_append(gap, '{');
425
426 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
427 ++hi)
428 if (!HASHITEM_EMPTY(hi))
429 {
430 --todo;
431 if (first)
432 first = FALSE;
433 else
434 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100435 if ((options & JSON_JS)
436 && is_simple_key(hi->hi_key))
437 ga_concat(gap, hi->hi_key);
438 else
439 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100440 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100441 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100442 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100443 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100444 }
445 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100446 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100447 }
448 }
449 break;
450
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100451 case VAR_FLOAT:
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100452#if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100453 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100454 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100455 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100456 {
457 if (val->vval.v_float < 0.0)
458 ga_concat(gap, (char_u *)"-Infinity");
459 else
460 ga_concat(gap, (char_u *)"Infinity");
461 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100462 else
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100463#endif
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100464 {
465 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
466 val->vval.v_float);
467 ga_concat(gap, numbuf);
468 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100469 break;
Bram Moolenaar55fab432016-02-07 16:53:13 +0100470 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200471 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100472 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100473 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100474 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100475 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100476 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100477}
478
479/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100480 * When "reader" has less than NUMBUFLEN bytes available, call the fill
481 * callback to get more.
482 */
483 static void
484fill_numbuflen(js_read_T *reader)
485{
486 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
487 - reader->js_used < NUMBUFLEN)
488 {
489 if (reader->js_fill(reader))
490 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
491 }
492}
493
494/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100495 * Skip white space in "reader". All characters <= space are considered white
496 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100497 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100498 */
499 static void
500json_skip_white(js_read_T *reader)
501{
502 int c;
503
Bram Moolenaar56ead342016-02-02 18:20:08 +0100504 for (;;)
505 {
506 c = reader->js_buf[reader->js_used];
507 if (reader->js_fill != NULL && c == NUL)
508 {
509 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200510 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100511 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200512 continue;
513 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100514 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100515 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100516 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100517 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100518 }
519 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100520}
521
Bram Moolenaar56ead342016-02-02 18:20:08 +0100522 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100523json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100524{
525 garray_T ga;
526 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100527 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100528 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200529 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100530
Bram Moolenaar56ead342016-02-02 18:20:08 +0100531 if (res != NULL)
532 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100533
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100534 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100535 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100536 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100537 // The JSON is always expected to be utf-8, thus use utf functions
538 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100539 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100540 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100541 // Not enough bytes to make a character or end of the string. Get
542 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100543 if (reader->js_fill == NULL)
544 break;
545 len = (int)(reader->js_end - p);
546 reader->js_used = (int)(p - reader->js_buf);
547 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100548 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100549 p = reader->js_buf + reader->js_used;
550 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
551 continue;
552 }
553
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100554 if (*p == '\\')
555 {
556 c = -1;
557 switch (p[1])
558 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100559 case '\\': c = '\\'; break;
560 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100561 case 'b': c = BS; break;
562 case 't': c = TAB; break;
563 case 'n': c = NL; break;
564 case 'f': c = FF; break;
565 case 'r': c = CAR; break;
566 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100567 if (reader->js_fill != NULL
568 && (int)(reader->js_end - p) < NUMBUFLEN)
569 {
570 reader->js_used = (int)(p - reader->js_buf);
571 if (reader->js_fill(reader))
572 {
573 p = reader->js_buf + reader->js_used;
574 reader->js_end = reader->js_buf
575 + STRLEN(reader->js_buf);
576 }
577 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100578 nr = 0;
579 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100580 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000581 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200582 if (len == 0)
583 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200584 if (res != NULL)
585 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200586 return FAIL;
587 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100588 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100589 if (0xd800 <= nr && nr <= 0xdfff
590 && (int)(reader->js_end - p) >= 6
591 && *p == '\\' && *(p+1) == 'u')
592 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200593 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100594
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100595 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100596 len = 0;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000597 vim_str2nr(p + 2, NULL, &len, STR2NR_HEX + STR2NR_FORCE,
598 &nr2, NULL, 4, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200599 if (len == 0)
600 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200601 if (res != NULL)
602 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200603 return FAIL;
604 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100605 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
606 {
607 p += len + 2;
608 nr = (((nr - 0xd800) << 10) |
609 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
610 }
611 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100612 if (res != NULL)
613 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200614 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200615
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100616 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100617 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100618 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100619 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100620 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100621 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100622 ++p;
623 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100624 }
625 if (c > 0)
626 {
627 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100628 if (res != NULL)
629 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100630 }
631 }
632 else
633 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100634 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100635 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100636 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100637 if (ga_grow(&ga, len) == FAIL)
638 {
639 ga_clear(&ga);
640 return FAIL;
641 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100642 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
643 ga.ga_len += len;
644 }
645 p += len;
646 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100647 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100648
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100649 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100650 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100651 {
652 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100653 if (res != NULL)
654 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100655 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100656 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100657#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100658 if (!enc_utf8)
659 {
660 vimconv_T conv;
661
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100662 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100663 conv.vc_type = CONV_NONE;
664 convert_setup(&conv, (char_u*)"utf-8", p_enc);
665 if (conv.vc_type != CONV_NONE)
666 {
667 res->vval.v_string =
668 string_convert(&conv, ga.ga_data, NULL);
669 vim_free(ga.ga_data);
670 }
671 convert_setup(&conv, NULL, NULL);
672 }
673 else
674#endif
675 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100676 }
677 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100678 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100679 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100680 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100681 res->v_type = VAR_SPECIAL;
682 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100683 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100684 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100685 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100686}
687
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100688typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100689 JSON_ARRAY, // parsing items in an array
690 JSON_OBJECT_KEY, // parsing key of an object
691 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100692} json_decode_T;
693
694typedef struct {
695 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100696 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100697 typval_T jd_key_tv;
698 char_u *jd_key;
699} json_dec_item_T;
700
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100701/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100702 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100703 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100704 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100705 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100706 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100707 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100708 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100709json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100710{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100711 char_u *p;
Bram Moolenaar6d3a7212020-07-12 14:34:00 +0200712 int i;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100713 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100714 int retval;
715 garray_T stack;
716 typval_T item;
717 typval_T *cur_item;
718 json_dec_item_T *top_item;
719 char_u key_buf[NUMBUFLEN];
720
721 ga_init2(&stack, sizeof(json_dec_item_T), 100);
722 cur_item = res;
723 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100724 if (res != NULL)
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200725 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100726
Bram Moolenaar56ead342016-02-02 18:20:08 +0100727 fill_numbuflen(reader);
728 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100729 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100730 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100731 top_item = NULL;
732 if (stack.ga_len > 0)
733 {
734 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
735 json_skip_white(reader);
736 p = reader->js_buf + reader->js_used;
737 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100738 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100739 retval = MAYBE;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100740 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100741 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100742 if (top_item->jd_type == JSON_OBJECT_KEY
743 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100744 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100745 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100746 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100747 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100748 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100749 --stack.ga_len;
750 if (stack.ga_len == 0)
751 {
752 retval = OK;
753 goto theend;
754 }
755 if (cur_item != NULL)
756 cur_item = &top_item->jd_tv;
757 goto item_end;
758 }
759 }
760 }
761
762 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
763 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100764 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100765 && reader->js_buf[reader->js_used] != '\''
766 && reader->js_buf[reader->js_used] != '['
767 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100768 {
769 char_u *key;
770
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100771 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100772 key = p = reader->js_buf + reader->js_used;
773 while (*p != NUL && *p != ':' && *p > ' ')
774 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100775 if (cur_item != NULL)
776 {
777 cur_item->v_type = VAR_STRING;
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200778 cur_item->vval.v_string = vim_strnsave(key, p - key);
Bram Moolenaare2c60372017-01-22 15:56:26 +0100779 top_item->jd_key = cur_item->vval.v_string;
780 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100781 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100782 }
783 else
784 {
785 switch (*p)
786 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100787 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100788 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
789 {
790 retval = FAIL;
791 break;
792 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100793 if (ga_grow(&stack, 1) == FAIL)
794 {
795 retval = FAIL;
796 break;
797 }
798 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
799 {
800 cur_item->v_type = VAR_SPECIAL;
801 cur_item->vval.v_number = VVAL_NONE;
802 retval = FAIL;
803 break;
804 }
805
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100806 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100807 top_item = ((json_dec_item_T *)stack.ga_data)
808 + stack.ga_len;
809 top_item->jd_type = JSON_ARRAY;
810 ++stack.ga_len;
811 if (cur_item != NULL)
812 {
813 top_item->jd_tv = *cur_item;
814 cur_item = &item;
815 }
816 continue;
817
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100818 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100819 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
820 {
821 retval = FAIL;
822 break;
823 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100824 if (ga_grow(&stack, 1) == FAIL)
825 {
826 retval = FAIL;
827 break;
828 }
829 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
830 {
831 cur_item->v_type = VAR_SPECIAL;
832 cur_item->vval.v_number = VVAL_NONE;
833 retval = FAIL;
834 break;
835 }
836
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100837 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100838 top_item = ((json_dec_item_T *)stack.ga_data)
839 + stack.ga_len;
840 top_item->jd_type = JSON_OBJECT_KEY;
841 ++stack.ga_len;
842 if (cur_item != NULL)
843 {
844 top_item->jd_tv = *cur_item;
845 cur_item = &top_item->jd_key_tv;
846 }
847 continue;
848
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100849 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100850 retval = json_decode_string(reader, cur_item, *p);
851 break;
852
853 case '\'':
854 if (options & JSON_JS)
855 retval = json_decode_string(reader, cur_item, *p);
856 else
857 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000858 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100859 retval = FAIL;
860 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100861 break;
862
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100863 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100864 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100865 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000866 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100867 retval = FAIL;
868 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100869 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100870 // FALLTHROUGH
871 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100872 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100873 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100874 cur_item->v_type = VAR_SPECIAL;
875 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100876 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100877 retval = OK;
878 break;
879
880 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100881 if (VIM_ISDIGIT(*p) || (*p == '-'
882 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100883 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100884 char_u *sp = p;
885
886 if (*sp == '-')
887 {
888 ++sp;
889 if (*sp == NUL)
890 {
891 retval = MAYBE;
892 break;
893 }
894 if (!VIM_ISDIGIT(*sp))
895 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000896 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100897 retval = FAIL;
898 break;
899 }
900 }
901 sp = skipdigits(sp);
902 if (*sp == '.' || *sp == 'e' || *sp == 'E')
903 {
904 if (cur_item == NULL)
905 {
906 float_T f;
907
Bram Moolenaar29500652021-08-08 15:43:34 +0200908 len = string2float(p, &f, FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100909 }
910 else
911 {
912 cur_item->v_type = VAR_FLOAT;
Bram Moolenaar29500652021-08-08 15:43:34 +0200913 len = string2float(p, &cur_item->vval.v_float,
914 FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100915 }
916 }
917 else
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100918 {
919 varnumber_T nr;
920
921 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100922 NULL, &len, 0, // what
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000923 &nr, NULL, 0, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200924 if (len == 0)
925 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000926 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200927 retval = FAIL;
928 goto theend;
929 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100930 if (cur_item != NULL)
931 {
932 cur_item->v_type = VAR_NUMBER;
933 cur_item->vval.v_number = nr;
934 }
935 }
936 reader->js_used += len;
937 retval = OK;
938 break;
939 }
940 if (STRNICMP((char *)p, "false", 5) == 0)
941 {
942 reader->js_used += 5;
943 if (cur_item != NULL)
944 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100945 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100946 cur_item->vval.v_number = VVAL_FALSE;
947 }
948 retval = OK;
949 break;
950 }
951 if (STRNICMP((char *)p, "true", 4) == 0)
952 {
953 reader->js_used += 4;
954 if (cur_item != NULL)
955 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100956 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100957 cur_item->vval.v_number = VVAL_TRUE;
958 }
959 retval = OK;
960 break;
961 }
962 if (STRNICMP((char *)p, "null", 4) == 0)
963 {
964 reader->js_used += 4;
965 if (cur_item != NULL)
966 {
967 cur_item->v_type = VAR_SPECIAL;
968 cur_item->vval.v_number = VVAL_NULL;
969 }
970 retval = OK;
971 break;
972 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100973 if (STRNICMP((char *)p, "NaN", 3) == 0)
974 {
975 reader->js_used += 3;
976 if (cur_item != NULL)
977 {
978 cur_item->v_type = VAR_FLOAT;
979 cur_item->vval.v_float = NAN;
980 }
981 retval = OK;
982 break;
983 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100984 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
985 {
986 reader->js_used += 9;
987 if (cur_item != NULL)
988 {
989 cur_item->v_type = VAR_FLOAT;
990 cur_item->vval.v_float = -INFINITY;
991 }
992 retval = OK;
993 break;
994 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100995 if (STRNICMP((char *)p, "Infinity", 8) == 0)
996 {
997 reader->js_used += 8;
998 if (cur_item != NULL)
999 {
1000 cur_item->v_type = VAR_FLOAT;
1001 cur_item->vval.v_float = INFINITY;
1002 }
1003 retval = OK;
1004 break;
1005 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001006 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +01001007 len = (int)(reader->js_end
1008 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001009 if (
1010 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01001011 || (len < 9
1012 && STRNICMP((char *)p, "-Infinity", len) == 0)
1013 || (len < 8
1014 && STRNICMP((char *)p, "Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001015 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +01001016 || (len < 4
1017 && (STRNICMP((char *)p, "true", len) == 0
1018 || STRNICMP((char *)p, "null", len) == 0)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001019
1020 retval = MAYBE;
1021 else
1022 retval = FAIL;
1023 break;
1024 }
1025
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001026 // We are finished when retval is FAIL or MAYBE and when at the
1027 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001028 if (retval == FAIL)
1029 break;
1030 if (retval == MAYBE || stack.ga_len == 0)
1031 goto theend;
1032
1033 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
1034 && cur_item != NULL)
1035 {
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02001036 if (cur_item->v_type == VAR_FLOAT)
1037 {
1038 // cannot use a float as a key
Bram Moolenaar74409f62022-01-01 15:58:22 +00001039 emsg(_(e_using_float_as_string));
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02001040 retval = FAIL;
1041 goto theend;
1042 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001043 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +01001044 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001045 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001046 emsg(_(e_invalid_argument));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001047 retval = FAIL;
1048 goto theend;
1049 }
1050 }
1051 }
1052
1053item_end:
1054 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
1055 switch (top_item->jd_type)
1056 {
1057 case JSON_ARRAY:
1058 if (res != NULL)
1059 {
1060 listitem_T *li = listitem_alloc();
1061
1062 if (li == NULL)
1063 {
1064 clear_tv(cur_item);
1065 retval = FAIL;
1066 goto theend;
1067 }
1068 li->li_tv = *cur_item;
1069 list_append(top_item->jd_tv.vval.v_list, li);
1070 }
1071 if (cur_item != NULL)
1072 cur_item = &item;
1073
1074 json_skip_white(reader);
1075 p = reader->js_buf + reader->js_used;
1076 if (*p == ',')
1077 ++reader->js_used;
1078 else if (*p != ']')
1079 {
1080 if (*p == NUL)
1081 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001082 else
1083 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001084 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001085 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001086 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001087 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001088 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001089 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001090
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001091 case JSON_OBJECT_KEY:
1092 json_skip_white(reader);
1093 p = reader->js_buf + reader->js_used;
1094 if (*p != ':')
1095 {
1096 if (cur_item != NULL)
1097 clear_tv(cur_item);
1098 if (*p == NUL)
1099 retval = MAYBE;
1100 else
Bram Moolenaar56ead342016-02-02 18:20:08 +01001101 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001102 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001103 retval = FAIL;
1104 }
1105 goto theend;
1106 }
1107 ++reader->js_used;
1108 json_skip_white(reader);
1109 top_item->jd_type = JSON_OBJECT;
1110 if (cur_item != NULL)
1111 cur_item = &item;
1112 break;
1113
1114 case JSON_OBJECT:
1115 if (cur_item != NULL
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001116 && dict_has_key(top_item->jd_tv.vval.v_dict,
1117 (char *)top_item->jd_key))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001118 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001119 semsg(_(e_duplicate_key_in_json_str), top_item->jd_key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001120 clear_tv(cur_item);
1121 retval = FAIL;
1122 goto theend;
1123 }
1124
1125 if (cur_item != NULL)
1126 {
1127 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1128
1129 clear_tv(&top_item->jd_key_tv);
1130 if (di == NULL)
1131 {
1132 clear_tv(cur_item);
1133 retval = FAIL;
1134 goto theend;
1135 }
1136 di->di_tv = *cur_item;
1137 di->di_tv.v_lock = 0;
1138 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1139 {
1140 dictitem_free(di);
1141 retval = FAIL;
1142 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001143 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001144 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001145
1146 json_skip_white(reader);
1147 p = reader->js_buf + reader->js_used;
1148 if (*p == ',')
1149 ++reader->js_used;
1150 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001151 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001152 if (*p == NUL)
1153 retval = MAYBE;
1154 else
1155 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001156 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001157 retval = FAIL;
1158 }
1159 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001160 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001161 top_item->jd_type = JSON_OBJECT_KEY;
1162 if (cur_item != NULL)
1163 cur_item = &top_item->jd_key_tv;
1164 break;
1165 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001166 }
1167
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001168 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001169 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001170 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001171 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001172 res->v_type = VAR_SPECIAL;
1173 res->vval.v_number = VVAL_NONE;
1174 }
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001175 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001176
1177theend:
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001178 for (i = 0; i < stack.ga_len; i++)
1179 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001180 ga_clear(&stack);
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001181
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001182 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001183}
1184
1185/*
1186 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001187 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001188 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001189 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001190 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001191json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001192{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001193 int ret;
1194
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001195 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001196 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001197 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001198 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001199 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001200 {
1201 if (ret == MAYBE)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001202 semsg(_(e_json_decode_error_at_str), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001203 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001204 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001205 json_skip_white(reader);
1206 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001207 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001208 semsg(_(e_trailing_characters_str), reader->js_buf + reader->js_used);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001209 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001210 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001211 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001212}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001213
Bram Moolenaar113e1072019-01-20 15:30:40 +01001214#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001215/*
1216 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001217 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001218 * Return FAIL for a decoding error.
1219 * Return MAYBE for an incomplete message.
1220 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001221 */
1222 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001223json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001224{
1225 int ret;
1226
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001227 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001228 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1229 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001230 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001231 json_skip_white(reader);
1232
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001233 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001234}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001235#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001236
1237/*
1238 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001239 * "options" can be JSON_JS or zero.
1240 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001241 * Return FAIL if the message has a decoding error.
1242 * Return MAYBE if the message is truncated, need to read more.
1243 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001244 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001245 * Does not advance the reader.
1246 */
1247 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001248json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001249{
1250 int used_save = reader->js_used;
1251 int ret;
1252
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001253 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001254 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1255 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001256 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001257 reader->js_used = used_save;
1258 return ret;
1259}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001260
1261/*
1262 * "js_decode()" function
1263 */
1264 void
1265f_js_decode(typval_T *argvars, typval_T *rettv)
1266{
1267 js_read_T reader;
1268
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001269 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1270 return;
1271
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001272 reader.js_buf = tv_get_string(&argvars[0]);
1273 reader.js_fill = NULL;
1274 reader.js_used = 0;
1275 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001276 emsg(_(e_invalid_argument));
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001277}
1278
1279/*
1280 * "js_encode()" function
1281 */
1282 void
1283f_js_encode(typval_T *argvars, typval_T *rettv)
1284{
1285 rettv->v_type = VAR_STRING;
1286 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1287}
1288
1289/*
1290 * "json_decode()" function
1291 */
1292 void
1293f_json_decode(typval_T *argvars, typval_T *rettv)
1294{
1295 js_read_T reader;
1296
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001297 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1298 return;
1299
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001300 reader.js_buf = tv_get_string(&argvars[0]);
1301 reader.js_fill = NULL;
1302 reader.js_used = 0;
1303 json_decode_all(&reader, rettv, 0);
1304}
1305
1306/*
1307 * "json_encode()" function
1308 */
1309 void
1310f_json_encode(typval_T *argvars, typval_T *rettv)
1311{
1312 rettv->v_type = VAR_STRING;
1313 rettv->vval.v_string = json_encode(&argvars[0], 0);
1314}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001315#endif