blob: 47bf9904a312a589938a358e382d0c24b4a7a231 [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 Moolenaar55fab432016-02-07 16:53:13 +0100412#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100413# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100414 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100415 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100416 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100417 {
418 if (val->vval.v_float < 0.0)
419 ga_concat(gap, (char_u *)"-Infinity");
420 else
421 ga_concat(gap, (char_u *)"Infinity");
422 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100423 else
424# endif
425 {
426 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
427 val->vval.v_float);
428 ga_concat(gap, numbuf);
429 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100430 break;
431#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100432 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200433 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100434 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100435 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100436 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100437 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100438 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100439}
440
441/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100442 * When "reader" has less than NUMBUFLEN bytes available, call the fill
443 * callback to get more.
444 */
445 static void
446fill_numbuflen(js_read_T *reader)
447{
448 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
449 - reader->js_used < NUMBUFLEN)
450 {
451 if (reader->js_fill(reader))
452 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
453 }
454}
455
456/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100457 * Skip white space in "reader". All characters <= space are considered white
458 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100459 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100460 */
461 static void
462json_skip_white(js_read_T *reader)
463{
464 int c;
465
Bram Moolenaar56ead342016-02-02 18:20:08 +0100466 for (;;)
467 {
468 c = reader->js_buf[reader->js_used];
469 if (reader->js_fill != NULL && c == NUL)
470 {
471 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200472 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100473 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200474 continue;
475 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100476 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100477 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100478 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100479 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100480 }
481 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100482}
483
Bram Moolenaar56ead342016-02-02 18:20:08 +0100484 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100485json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100486{
487 garray_T ga;
488 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100489 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100490 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200491 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100492
Bram Moolenaar56ead342016-02-02 18:20:08 +0100493 if (res != NULL)
494 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100495
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100496 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100497 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100498 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100499 // The JSON is always expected to be utf-8, thus use utf functions
500 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100501 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100502 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100503 // Not enough bytes to make a character or end of the string. Get
504 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100505 if (reader->js_fill == NULL)
506 break;
507 len = (int)(reader->js_end - p);
508 reader->js_used = (int)(p - reader->js_buf);
509 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100510 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100511 p = reader->js_buf + reader->js_used;
512 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
513 continue;
514 }
515
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100516 if (*p == '\\')
517 {
518 c = -1;
519 switch (p[1])
520 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100521 case '\\': c = '\\'; break;
522 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100523 case 'b': c = BS; break;
524 case 't': c = TAB; break;
525 case 'n': c = NL; break;
526 case 'f': c = FF; break;
527 case 'r': c = CAR; break;
528 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100529 if (reader->js_fill != NULL
530 && (int)(reader->js_end - p) < NUMBUFLEN)
531 {
532 reader->js_used = (int)(p - reader->js_buf);
533 if (reader->js_fill(reader))
534 {
535 p = reader->js_buf + reader->js_used;
536 reader->js_end = reader->js_buf
537 + STRLEN(reader->js_buf);
538 }
539 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100540 nr = 0;
541 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100542 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200543 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
544 if (len == 0)
545 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200546 if (res != NULL)
547 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200548 return FAIL;
549 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100550 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100551 if (0xd800 <= nr && nr <= 0xdfff
552 && (int)(reader->js_end - p) >= 6
553 && *p == '\\' && *(p+1) == 'u')
554 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200555 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100556
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100557 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100558 len = 0;
559 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200560 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
561 if (len == 0)
562 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200563 if (res != NULL)
564 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200565 return FAIL;
566 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100567 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
568 {
569 p += len + 2;
570 nr = (((nr - 0xd800) << 10) |
571 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
572 }
573 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100574 if (res != NULL)
575 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200576 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200577
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100578 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100579 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100580 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100581 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100582 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100583 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100584 ++p;
585 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100586 }
587 if (c > 0)
588 {
589 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100590 if (res != NULL)
591 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100592 }
593 }
594 else
595 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100596 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100597 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100598 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100599 if (ga_grow(&ga, len) == FAIL)
600 {
601 ga_clear(&ga);
602 return FAIL;
603 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100604 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
605 ga.ga_len += len;
606 }
607 p += len;
608 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100609 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100610
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100611 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100612 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100613 {
614 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100615 if (res != NULL)
616 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100617 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100618 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100619#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100620 if (!enc_utf8)
621 {
622 vimconv_T conv;
623
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100624 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100625 conv.vc_type = CONV_NONE;
626 convert_setup(&conv, (char_u*)"utf-8", p_enc);
627 if (conv.vc_type != CONV_NONE)
628 {
629 res->vval.v_string =
630 string_convert(&conv, ga.ga_data, NULL);
631 vim_free(ga.ga_data);
632 }
633 convert_setup(&conv, NULL, NULL);
634 }
635 else
636#endif
637 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100638 }
639 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100640 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100641 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100642 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100643 res->v_type = VAR_SPECIAL;
644 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100645 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100646 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100647 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100648}
649
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100650typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100651 JSON_ARRAY, // parsing items in an array
652 JSON_OBJECT_KEY, // parsing key of an object
653 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100654} json_decode_T;
655
656typedef struct {
657 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100658 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100659 typval_T jd_key_tv;
660 char_u *jd_key;
661} json_dec_item_T;
662
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100663/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100664 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100665 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100666 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100667 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100668 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100669 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100670 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100671json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100672{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100673 char_u *p;
Bram Moolenaar6d3a7212020-07-12 14:34:00 +0200674 int i;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100675 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100676 int retval;
677 garray_T stack;
678 typval_T item;
679 typval_T *cur_item;
680 json_dec_item_T *top_item;
681 char_u key_buf[NUMBUFLEN];
682
683 ga_init2(&stack, sizeof(json_dec_item_T), 100);
684 cur_item = res;
685 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100686 if (res != NULL)
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200687 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100688
Bram Moolenaar56ead342016-02-02 18:20:08 +0100689 fill_numbuflen(reader);
690 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100691 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100692 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100693 top_item = NULL;
694 if (stack.ga_len > 0)
695 {
696 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
697 json_skip_white(reader);
698 p = reader->js_buf + reader->js_used;
699 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100700 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100701 retval = MAYBE;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100702 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100703 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100704 if (top_item->jd_type == JSON_OBJECT_KEY
705 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100706 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100707 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100708 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100709 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100710 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100711 --stack.ga_len;
712 if (stack.ga_len == 0)
713 {
714 retval = OK;
715 goto theend;
716 }
717 if (cur_item != NULL)
718 cur_item = &top_item->jd_tv;
719 goto item_end;
720 }
721 }
722 }
723
724 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
725 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100726 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100727 && reader->js_buf[reader->js_used] != '\''
728 && reader->js_buf[reader->js_used] != '['
729 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100730 {
731 char_u *key;
732
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100733 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100734 key = p = reader->js_buf + reader->js_used;
735 while (*p != NUL && *p != ':' && *p > ' ')
736 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100737 if (cur_item != NULL)
738 {
739 cur_item->v_type = VAR_STRING;
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200740 cur_item->vval.v_string = vim_strnsave(key, p - key);
Bram Moolenaare2c60372017-01-22 15:56:26 +0100741 top_item->jd_key = cur_item->vval.v_string;
742 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100743 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100744 }
745 else
746 {
747 switch (*p)
748 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100749 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100750 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
751 {
752 retval = FAIL;
753 break;
754 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100755 if (ga_grow(&stack, 1) == FAIL)
756 {
757 retval = FAIL;
758 break;
759 }
760 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
761 {
762 cur_item->v_type = VAR_SPECIAL;
763 cur_item->vval.v_number = VVAL_NONE;
764 retval = FAIL;
765 break;
766 }
767
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100768 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100769 top_item = ((json_dec_item_T *)stack.ga_data)
770 + stack.ga_len;
771 top_item->jd_type = JSON_ARRAY;
772 ++stack.ga_len;
773 if (cur_item != NULL)
774 {
775 top_item->jd_tv = *cur_item;
776 cur_item = &item;
777 }
778 continue;
779
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100780 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100781 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
782 {
783 retval = FAIL;
784 break;
785 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100786 if (ga_grow(&stack, 1) == FAIL)
787 {
788 retval = FAIL;
789 break;
790 }
791 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
792 {
793 cur_item->v_type = VAR_SPECIAL;
794 cur_item->vval.v_number = VVAL_NONE;
795 retval = FAIL;
796 break;
797 }
798
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100799 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100800 top_item = ((json_dec_item_T *)stack.ga_data)
801 + stack.ga_len;
802 top_item->jd_type = JSON_OBJECT_KEY;
803 ++stack.ga_len;
804 if (cur_item != NULL)
805 {
806 top_item->jd_tv = *cur_item;
807 cur_item = &top_item->jd_key_tv;
808 }
809 continue;
810
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100811 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100812 retval = json_decode_string(reader, cur_item, *p);
813 break;
814
815 case '\'':
816 if (options & JSON_JS)
817 retval = json_decode_string(reader, cur_item, *p);
818 else
819 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000820 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100821 retval = FAIL;
822 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100823 break;
824
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100825 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100826 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100827 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000828 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100829 retval = FAIL;
830 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100831 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100832 // FALLTHROUGH
833 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100834 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100835 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100836 cur_item->v_type = VAR_SPECIAL;
837 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100838 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100839 retval = OK;
840 break;
841
842 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100843 if (VIM_ISDIGIT(*p) || (*p == '-'
844 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100845 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100846 char_u *sp = p;
847
848 if (*sp == '-')
849 {
850 ++sp;
851 if (*sp == NUL)
852 {
853 retval = MAYBE;
854 break;
855 }
856 if (!VIM_ISDIGIT(*sp))
857 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000858 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100859 retval = FAIL;
860 break;
861 }
862 }
863 sp = skipdigits(sp);
Bram Moolenaara5d59532020-01-26 21:42:03 +0100864#ifdef FEAT_FLOAT
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100865 if (*sp == '.' || *sp == 'e' || *sp == 'E')
866 {
867 if (cur_item == NULL)
868 {
869 float_T f;
870
Bram Moolenaar29500652021-08-08 15:43:34 +0200871 len = string2float(p, &f, FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100872 }
873 else
874 {
875 cur_item->v_type = VAR_FLOAT;
Bram Moolenaar29500652021-08-08 15:43:34 +0200876 len = string2float(p, &cur_item->vval.v_float,
877 FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100878 }
879 }
880 else
881#endif
882 {
883 varnumber_T nr;
884
885 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100886 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200887 &nr, NULL, 0, TRUE);
888 if (len == 0)
889 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000890 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200891 retval = FAIL;
892 goto theend;
893 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100894 if (cur_item != NULL)
895 {
896 cur_item->v_type = VAR_NUMBER;
897 cur_item->vval.v_number = nr;
898 }
899 }
900 reader->js_used += len;
901 retval = OK;
902 break;
903 }
904 if (STRNICMP((char *)p, "false", 5) == 0)
905 {
906 reader->js_used += 5;
907 if (cur_item != NULL)
908 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100909 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100910 cur_item->vval.v_number = VVAL_FALSE;
911 }
912 retval = OK;
913 break;
914 }
915 if (STRNICMP((char *)p, "true", 4) == 0)
916 {
917 reader->js_used += 4;
918 if (cur_item != NULL)
919 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100920 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100921 cur_item->vval.v_number = VVAL_TRUE;
922 }
923 retval = OK;
924 break;
925 }
926 if (STRNICMP((char *)p, "null", 4) == 0)
927 {
928 reader->js_used += 4;
929 if (cur_item != NULL)
930 {
931 cur_item->v_type = VAR_SPECIAL;
932 cur_item->vval.v_number = VVAL_NULL;
933 }
934 retval = OK;
935 break;
936 }
937#ifdef FEAT_FLOAT
938 if (STRNICMP((char *)p, "NaN", 3) == 0)
939 {
940 reader->js_used += 3;
941 if (cur_item != NULL)
942 {
943 cur_item->v_type = VAR_FLOAT;
944 cur_item->vval.v_float = NAN;
945 }
946 retval = OK;
947 break;
948 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100949 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
950 {
951 reader->js_used += 9;
952 if (cur_item != NULL)
953 {
954 cur_item->v_type = VAR_FLOAT;
955 cur_item->vval.v_float = -INFINITY;
956 }
957 retval = OK;
958 break;
959 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100960 if (STRNICMP((char *)p, "Infinity", 8) == 0)
961 {
962 reader->js_used += 8;
963 if (cur_item != NULL)
964 {
965 cur_item->v_type = VAR_FLOAT;
966 cur_item->vval.v_float = INFINITY;
967 }
968 retval = OK;
969 break;
970 }
971#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100972 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100973 len = (int)(reader->js_end
974 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100975 if (
976 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
977#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100978 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100979 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
980 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
981#endif
982 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
983 || STRNICMP((char *)p, "null", len) == 0)))
984
985 retval = MAYBE;
986 else
987 retval = FAIL;
988 break;
989 }
990
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100991 // We are finished when retval is FAIL or MAYBE and when at the
992 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100993 if (retval == FAIL)
994 break;
995 if (retval == MAYBE || stack.ga_len == 0)
996 goto theend;
997
998 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
999 && cur_item != NULL)
1000 {
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02001001#ifdef FEAT_FLOAT
1002 if (cur_item->v_type == VAR_FLOAT)
1003 {
1004 // cannot use a float as a key
Bram Moolenaar74409f62022-01-01 15:58:22 +00001005 emsg(_(e_using_float_as_string));
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02001006 retval = FAIL;
1007 goto theend;
1008 }
1009#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001010 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +01001011 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001012 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001013 emsg(_(e_invalid_argument));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001014 retval = FAIL;
1015 goto theend;
1016 }
1017 }
1018 }
1019
1020item_end:
1021 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
1022 switch (top_item->jd_type)
1023 {
1024 case JSON_ARRAY:
1025 if (res != NULL)
1026 {
1027 listitem_T *li = listitem_alloc();
1028
1029 if (li == NULL)
1030 {
1031 clear_tv(cur_item);
1032 retval = FAIL;
1033 goto theend;
1034 }
1035 li->li_tv = *cur_item;
1036 list_append(top_item->jd_tv.vval.v_list, li);
1037 }
1038 if (cur_item != NULL)
1039 cur_item = &item;
1040
1041 json_skip_white(reader);
1042 p = reader->js_buf + reader->js_used;
1043 if (*p == ',')
1044 ++reader->js_used;
1045 else if (*p != ']')
1046 {
1047 if (*p == NUL)
1048 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001049 else
1050 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001051 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001052 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001053 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001054 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001055 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001056 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001057
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001058 case JSON_OBJECT_KEY:
1059 json_skip_white(reader);
1060 p = reader->js_buf + reader->js_used;
1061 if (*p != ':')
1062 {
1063 if (cur_item != NULL)
1064 clear_tv(cur_item);
1065 if (*p == NUL)
1066 retval = MAYBE;
1067 else
Bram Moolenaar56ead342016-02-02 18:20:08 +01001068 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001069 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001070 retval = FAIL;
1071 }
1072 goto theend;
1073 }
1074 ++reader->js_used;
1075 json_skip_white(reader);
1076 top_item->jd_type = JSON_OBJECT;
1077 if (cur_item != NULL)
1078 cur_item = &item;
1079 break;
1080
1081 case JSON_OBJECT:
1082 if (cur_item != NULL
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001083 && dict_has_key(top_item->jd_tv.vval.v_dict,
1084 (char *)top_item->jd_key))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001085 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001086 semsg(_(e_duplicate_key_in_json_str), top_item->jd_key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001087 clear_tv(cur_item);
1088 retval = FAIL;
1089 goto theend;
1090 }
1091
1092 if (cur_item != NULL)
1093 {
1094 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1095
1096 clear_tv(&top_item->jd_key_tv);
1097 if (di == NULL)
1098 {
1099 clear_tv(cur_item);
1100 retval = FAIL;
1101 goto theend;
1102 }
1103 di->di_tv = *cur_item;
1104 di->di_tv.v_lock = 0;
1105 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1106 {
1107 dictitem_free(di);
1108 retval = FAIL;
1109 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001110 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001111 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001112
1113 json_skip_white(reader);
1114 p = reader->js_buf + reader->js_used;
1115 if (*p == ',')
1116 ++reader->js_used;
1117 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001118 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001119 if (*p == NUL)
1120 retval = MAYBE;
1121 else
1122 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001123 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001124 retval = FAIL;
1125 }
1126 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001127 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001128 top_item->jd_type = JSON_OBJECT_KEY;
1129 if (cur_item != NULL)
1130 cur_item = &top_item->jd_key_tv;
1131 break;
1132 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001133 }
1134
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001135 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001136 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001137 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001138 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001139 res->v_type = VAR_SPECIAL;
1140 res->vval.v_number = VVAL_NONE;
1141 }
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001142 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001143
1144theend:
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001145 for (i = 0; i < stack.ga_len; i++)
1146 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001147 ga_clear(&stack);
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001148
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001149 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001150}
1151
1152/*
1153 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001154 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001155 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001156 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001157 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001158json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001159{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001160 int ret;
1161
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001162 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001163 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001164 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001165 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001166 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001167 {
1168 if (ret == MAYBE)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001169 semsg(_(e_json_decode_error_at_str), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001170 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001171 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001172 json_skip_white(reader);
1173 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001174 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001175 semsg(_(e_trailing_characters_str), reader->js_buf + reader->js_used);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001176 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001177 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001178 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001179}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001180
Bram Moolenaar113e1072019-01-20 15:30:40 +01001181#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001182/*
1183 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001184 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001185 * Return FAIL for a decoding error.
1186 * Return MAYBE for an incomplete message.
1187 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001188 */
1189 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001190json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001191{
1192 int ret;
1193
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001194 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001195 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1196 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001197 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001198 json_skip_white(reader);
1199
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001200 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001201}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001202#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001203
1204/*
1205 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001206 * "options" can be JSON_JS or zero.
1207 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001208 * Return FAIL if the message has a decoding error.
1209 * Return MAYBE if the message is truncated, need to read more.
1210 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001211 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001212 * Does not advance the reader.
1213 */
1214 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001215json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001216{
1217 int used_save = reader->js_used;
1218 int ret;
1219
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001220 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001221 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1222 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001223 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001224 reader->js_used = used_save;
1225 return ret;
1226}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001227
1228/*
1229 * "js_decode()" function
1230 */
1231 void
1232f_js_decode(typval_T *argvars, typval_T *rettv)
1233{
1234 js_read_T reader;
1235
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001236 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1237 return;
1238
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001239 reader.js_buf = tv_get_string(&argvars[0]);
1240 reader.js_fill = NULL;
1241 reader.js_used = 0;
1242 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001243 emsg(_(e_invalid_argument));
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001244}
1245
1246/*
1247 * "js_encode()" function
1248 */
1249 void
1250f_js_encode(typval_T *argvars, typval_T *rettv)
1251{
1252 rettv->v_type = VAR_STRING;
1253 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1254}
1255
1256/*
1257 * "json_decode()" function
1258 */
1259 void
1260f_json_decode(typval_T *argvars, typval_T *rettv)
1261{
1262 js_read_T reader;
1263
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001264 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1265 return;
1266
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001267 reader.js_buf = tv_get_string(&argvars[0]);
1268 reader.js_fill = NULL;
1269 reader.js_used = 0;
1270 json_decode_all(&reader, rettv, 0);
1271}
1272
1273/*
1274 * "json_encode()" function
1275 */
1276 void
1277f_json_encode(typval_T *argvars, typval_T *rettv)
1278{
1279 rettv->v_type = VAR_STRING;
1280 rettv->vval.v_string = json_encode(&argvars[0], 0);
1281}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001282#endif