blob: acf7ac57cfeaa22b2be44396973349e99c248242 [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,
108 "Content-Length: %u\r\n"
Yegappan Lakshmananc3eddd22023-04-25 14:54:54 +0100109 "Content-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n",
Yegappan Lakshmanan9247a222022-03-30 10:16:05 +0100110 ga.ga_len - 1);
111 ga_concat(&lspga, IObuff);
112 ga_concat_len(&lspga, ga.ga_data, ga.ga_len);
113 ga_clear(&ga);
114 return lspga.ga_data;
115}
Bram Moolenaar113e1072019-01-20 15:30:40 +0100116#endif
Bram Moolenaarfb1f6262016-01-31 20:24:32 +0100117
LemonBoybeb0ef12022-04-05 15:07:32 +0100118/*
119 * Lookup table to quickly know if the given ASCII character must be escaped.
120 */
121static const char ascii_needs_escape[128] = {
122 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x0.
123 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x1.
124 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x2.
125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x3.
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x4.
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // 0x5.
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x6.
129 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x7.
130};
131
132/*
133 * Encode the utf-8 encoded string "str" into "gap".
134 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100135 static void
136write_string(garray_T *gap, char_u *str)
137{
138 char_u *res = str;
139 char_u numbuf[NUMBUFLEN];
LemonBoybeb0ef12022-04-05 15:07:32 +0100140 char_u *from;
141#if defined(USE_ICONV)
142 vimconv_T conv;
143 char_u *converted = NULL;
144#endif
145 int c;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100146
147 if (res == NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100148 {
LemonBoybeb0ef12022-04-05 15:07:32 +0100149 ga_concat(gap, (char_u *)"\"\"");
150 return;
151 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100152
LemonBoybeb0ef12022-04-05 15:07:32 +0100153#if defined(USE_ICONV)
154 if (!enc_utf8)
155 {
156 // Convert the text from 'encoding' to utf-8, because a JSON string is
157 // always utf-8.
158 conv.vc_type = CONV_NONE;
159 convert_setup(&conv, p_enc, (char_u*)"utf-8");
160 if (conv.vc_type != CONV_NONE)
161 converted = res = string_convert(&conv, res, NULL);
162 convert_setup(&conv, NULL, NULL);
163 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100164#endif
LemonBoybeb0ef12022-04-05 15:07:32 +0100165 ga_append(gap, '"');
166 // `from` is the beginning of a sequence of bytes we can directly copy from
167 // the input string, avoiding the overhead associated to decoding/encoding
168 // them.
169 from = res;
170 while ((c = *res) != NUL)
171 {
172 // always use utf-8 encoding, ignore 'encoding'
173 if (c < 0x80)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100174 {
LemonBoybeb0ef12022-04-05 15:07:32 +0100175 if (!ascii_needs_escape[c])
176 {
177 res += 1;
178 continue;
179 }
180
181 if (res != from)
182 ga_concat_len(gap, from, res - from);
183 from = res + 1;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100184
185 switch (c)
186 {
187 case 0x08:
188 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
189 case 0x09:
190 ga_append(gap, '\\'); ga_append(gap, 't'); break;
191 case 0x0a:
192 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
193 case 0x0c:
194 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
195 case 0x0d:
196 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100197 case 0x22: // "
198 case 0x5c: // backslash
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100199 ga_append(gap, '\\');
200 ga_append(gap, c);
201 break;
202 default:
LemonBoybeb0ef12022-04-05 15:07:32 +0100203 vim_snprintf((char *)numbuf, NUMBUFLEN, "\\u%04lx",
204 (long)c);
205 ga_concat(gap, numbuf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100206 }
LemonBoybeb0ef12022-04-05 15:07:32 +0100207
208 res += 1;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100209 }
LemonBoybeb0ef12022-04-05 15:07:32 +0100210 else
211 {
212 int l = utf_ptr2len(res);
213
214 if (l > 1)
215 {
216 res += l;
217 continue;
218 }
219
220 // Invalid utf-8 sequence, replace it with the Unicode replacement
221 // character U+FFFD.
222 if (res != from)
223 ga_concat_len(gap, from, res - from);
224 from = res + 1;
225
226 numbuf[utf_char2bytes(0xFFFD, numbuf)] = NUL;
227 ga_concat(gap, numbuf);
228
229 res += l;
230 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100231 }
LemonBoybeb0ef12022-04-05 15:07:32 +0100232
233 if (res != from)
234 ga_concat_len(gap, from, res - from);
235
236 ga_append(gap, '"');
237#if defined(USE_ICONV)
238 vim_free(converted);
239#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100240}
241
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100242/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100243 * Return TRUE if "key" can be used without quotes.
244 * That is when it starts with a letter and only contains letters, digits and
245 * underscore.
246 */
247 static int
248is_simple_key(char_u *key)
249{
250 char_u *p;
251
252 if (!ASCII_ISALPHA(*key))
253 return FALSE;
254 for (p = key + 1; *p != NUL; ++p)
255 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
256 return FALSE;
257 return TRUE;
258}
259
260/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100261 * Encode "val" into "gap".
262 * Return FAIL or OK.
263 */
264 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100265json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100266{
267 char_u numbuf[NUMBUFLEN];
268 char_u *res;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100269 blob_T *b;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100270 list_T *l;
271 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:
Bram Moolenaara8530892021-02-08 21:53:09 +0100314 semsg(_(e_cannot_json_encode_str), vartype_name(val->v_type));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100315 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100316
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100317 case VAR_BLOB:
318 b = val->vval.v_blob;
319 if (b == NULL || b->bv_ga.ga_len == 0)
320 ga_concat(gap, (char_u *)"[]");
321 else
322 {
323 ga_append(gap, '[');
324 for (i = 0; i < b->bv_ga.ga_len; i++)
325 {
326 if (i > 0)
327 ga_concat(gap, (char_u *)",");
328 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000329 blob_get(b, i));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100330 ga_concat(gap, numbuf);
331 }
332 ga_append(gap, ']');
333 }
334 break;
335
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100336 case VAR_LIST:
337 l = val->vval.v_list;
338 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100339 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100340 else
341 {
342 if (l->lv_copyID == copyID)
343 ga_concat(gap, (char_u *)"[]");
344 else
345 {
346 listitem_T *li;
347
348 l->lv_copyID = copyID;
349 ga_append(gap, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200350 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100351 for (li = l->lv_first; li != NULL && !got_int; )
352 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100353 if (json_encode_item(gap, &li->li_tv, copyID,
354 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100355 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100356 if ((options & JSON_JS)
357 && li->li_next == NULL
358 && li->li_tv.v_type == VAR_SPECIAL
359 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100360 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100361 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100362 li = li->li_next;
363 if (li != NULL)
364 ga_append(gap, ',');
365 }
366 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100367 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100368 }
369 }
370 break;
371
372 case VAR_DICT:
373 d = val->vval.v_dict;
374 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100375 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 else
377 {
378 if (d->dv_copyID == copyID)
379 ga_concat(gap, (char_u *)"{}");
380 else
381 {
382 int first = TRUE;
383 int todo = (int)d->dv_hashtab.ht_used;
384 hashitem_T *hi;
385
386 d->dv_copyID = copyID;
387 ga_append(gap, '{');
388
389 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
390 ++hi)
391 if (!HASHITEM_EMPTY(hi))
392 {
393 --todo;
394 if (first)
395 first = FALSE;
396 else
397 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100398 if ((options & JSON_JS)
399 && is_simple_key(hi->hi_key))
400 ga_concat(gap, hi->hi_key);
401 else
402 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100403 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100404 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100405 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100406 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100407 }
408 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100409 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100410 }
411 }
412 break;
413
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100414 case VAR_FLOAT:
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100415#if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100416 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100417 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100418 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100419 {
420 if (val->vval.v_float < 0.0)
421 ga_concat(gap, (char_u *)"-Infinity");
422 else
423 ga_concat(gap, (char_u *)"Infinity");
424 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100425 else
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100426#endif
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100427 {
428 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
429 val->vval.v_float);
430 ga_concat(gap, numbuf);
431 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100432 break;
Bram Moolenaar55fab432016-02-07 16:53:13 +0100433 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200434 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100435 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100436 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100437 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100438 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100439 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100440}
441
442/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100443 * When "reader" has less than NUMBUFLEN bytes available, call the fill
444 * callback to get more.
445 */
446 static void
447fill_numbuflen(js_read_T *reader)
448{
449 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
450 - reader->js_used < NUMBUFLEN)
451 {
452 if (reader->js_fill(reader))
453 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
454 }
455}
456
457/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100458 * Skip white space in "reader". All characters <= space are considered white
459 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100460 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100461 */
462 static void
463json_skip_white(js_read_T *reader)
464{
465 int c;
466
Bram Moolenaar56ead342016-02-02 18:20:08 +0100467 for (;;)
468 {
469 c = reader->js_buf[reader->js_used];
470 if (reader->js_fill != NULL && c == NUL)
471 {
472 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200473 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100474 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200475 continue;
476 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100477 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100478 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100479 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100480 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100481 }
482 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100483}
484
Bram Moolenaar56ead342016-02-02 18:20:08 +0100485 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100486json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100487{
488 garray_T ga;
489 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100490 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100491 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200492 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100493
Bram Moolenaar56ead342016-02-02 18:20:08 +0100494 if (res != NULL)
495 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100496
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100497 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100498 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100499 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100500 // The JSON is always expected to be utf-8, thus use utf functions
501 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100502 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100503 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100504 // Not enough bytes to make a character or end of the string. Get
505 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100506 if (reader->js_fill == NULL)
507 break;
508 len = (int)(reader->js_end - p);
509 reader->js_used = (int)(p - reader->js_buf);
510 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100511 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100512 p = reader->js_buf + reader->js_used;
513 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
514 continue;
515 }
516
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100517 if (*p == '\\')
518 {
519 c = -1;
520 switch (p[1])
521 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100522 case '\\': c = '\\'; break;
523 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100524 case 'b': c = BS; break;
525 case 't': c = TAB; break;
526 case 'n': c = NL; break;
527 case 'f': c = FF; break;
528 case 'r': c = CAR; break;
529 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100530 if (reader->js_fill != NULL
531 && (int)(reader->js_end - p) < NUMBUFLEN)
532 {
533 reader->js_used = (int)(p - reader->js_buf);
534 if (reader->js_fill(reader))
535 {
536 p = reader->js_buf + reader->js_used;
537 reader->js_end = reader->js_buf
538 + STRLEN(reader->js_buf);
539 }
540 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100541 nr = 0;
542 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100543 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000544 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200545 if (len == 0)
546 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200547 if (res != NULL)
548 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200549 return FAIL;
550 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100551 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100552 if (0xd800 <= nr && nr <= 0xdfff
553 && (int)(reader->js_end - p) >= 6
554 && *p == '\\' && *(p+1) == 'u')
555 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200556 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100557
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100558 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100559 len = 0;
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000560 vim_str2nr(p + 2, NULL, &len, STR2NR_HEX + STR2NR_FORCE,
561 &nr2, NULL, 4, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200562 if (len == 0)
563 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200564 if (res != NULL)
565 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200566 return FAIL;
567 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100568 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
569 {
570 p += len + 2;
571 nr = (((nr - 0xd800) << 10) |
572 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
573 }
574 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100575 if (res != NULL)
576 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200577 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200578
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100579 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100580 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100581 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100582 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100583 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100584 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100585 ++p;
586 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100587 }
588 if (c > 0)
589 {
590 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100591 if (res != NULL)
592 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100593 }
594 }
595 else
596 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100597 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100598 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100599 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100600 if (ga_grow(&ga, len) == FAIL)
601 {
602 ga_clear(&ga);
603 return FAIL;
604 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100605 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
606 ga.ga_len += len;
607 }
608 p += len;
609 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100610 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100611
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100612 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100613 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100614 {
615 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100616 if (res != NULL)
617 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100618 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100619 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100620#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100621 if (!enc_utf8)
622 {
623 vimconv_T conv;
624
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100625 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100626 conv.vc_type = CONV_NONE;
627 convert_setup(&conv, (char_u*)"utf-8", p_enc);
628 if (conv.vc_type != CONV_NONE)
629 {
630 res->vval.v_string =
631 string_convert(&conv, ga.ga_data, NULL);
632 vim_free(ga.ga_data);
633 }
634 convert_setup(&conv, NULL, NULL);
635 }
636 else
637#endif
638 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100639 }
640 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100641 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100642 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100643 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100644 res->v_type = VAR_SPECIAL;
645 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100646 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100647 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100648 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100649}
650
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100651typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100652 JSON_ARRAY, // parsing items in an array
653 JSON_OBJECT_KEY, // parsing key of an object
654 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100655} json_decode_T;
656
657typedef struct {
658 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100659 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100660 typval_T jd_key_tv;
661 char_u *jd_key;
662} json_dec_item_T;
663
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100664/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100665 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100666 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100667 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100668 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100669 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100670 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100671 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100672json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100673{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100674 char_u *p;
Bram Moolenaar6d3a7212020-07-12 14:34:00 +0200675 int i;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100676 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100677 int retval;
678 garray_T stack;
679 typval_T item;
680 typval_T *cur_item;
681 json_dec_item_T *top_item;
682 char_u key_buf[NUMBUFLEN];
683
684 ga_init2(&stack, sizeof(json_dec_item_T), 100);
685 cur_item = res;
686 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100687 if (res != NULL)
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200688 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100689
Bram Moolenaar56ead342016-02-02 18:20:08 +0100690 fill_numbuflen(reader);
691 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100692 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100693 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100694 top_item = NULL;
695 if (stack.ga_len > 0)
696 {
697 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
698 json_skip_white(reader);
699 p = reader->js_buf + reader->js_used;
700 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100701 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100702 retval = MAYBE;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100703 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100704 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100705 if (top_item->jd_type == JSON_OBJECT_KEY
706 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100707 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100708 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100709 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100710 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100711 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100712 --stack.ga_len;
713 if (stack.ga_len == 0)
714 {
715 retval = OK;
716 goto theend;
717 }
718 if (cur_item != NULL)
719 cur_item = &top_item->jd_tv;
720 goto item_end;
721 }
722 }
723 }
724
725 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
726 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100727 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100728 && reader->js_buf[reader->js_used] != '\''
729 && reader->js_buf[reader->js_used] != '['
730 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100731 {
732 char_u *key;
733
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100734 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100735 key = p = reader->js_buf + reader->js_used;
736 while (*p != NUL && *p != ':' && *p > ' ')
737 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100738 if (cur_item != NULL)
739 {
740 cur_item->v_type = VAR_STRING;
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200741 cur_item->vval.v_string = vim_strnsave(key, p - key);
Bram Moolenaare2c60372017-01-22 15:56:26 +0100742 top_item->jd_key = cur_item->vval.v_string;
743 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100744 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100745 }
746 else
747 {
748 switch (*p)
749 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100750 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100751 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
752 {
753 retval = FAIL;
754 break;
755 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100756 if (ga_grow(&stack, 1) == FAIL)
757 {
758 retval = FAIL;
759 break;
760 }
761 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
762 {
763 cur_item->v_type = VAR_SPECIAL;
764 cur_item->vval.v_number = VVAL_NONE;
765 retval = FAIL;
766 break;
767 }
768
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100769 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100770 top_item = ((json_dec_item_T *)stack.ga_data)
771 + stack.ga_len;
772 top_item->jd_type = JSON_ARRAY;
773 ++stack.ga_len;
774 if (cur_item != NULL)
775 {
776 top_item->jd_tv = *cur_item;
777 cur_item = &item;
778 }
779 continue;
780
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100781 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100782 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
783 {
784 retval = FAIL;
785 break;
786 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100787 if (ga_grow(&stack, 1) == FAIL)
788 {
789 retval = FAIL;
790 break;
791 }
792 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
793 {
794 cur_item->v_type = VAR_SPECIAL;
795 cur_item->vval.v_number = VVAL_NONE;
796 retval = FAIL;
797 break;
798 }
799
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100800 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100801 top_item = ((json_dec_item_T *)stack.ga_data)
802 + stack.ga_len;
803 top_item->jd_type = JSON_OBJECT_KEY;
804 ++stack.ga_len;
805 if (cur_item != NULL)
806 {
807 top_item->jd_tv = *cur_item;
808 cur_item = &top_item->jd_key_tv;
809 }
810 continue;
811
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100812 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100813 retval = json_decode_string(reader, cur_item, *p);
814 break;
815
816 case '\'':
817 if (options & JSON_JS)
818 retval = json_decode_string(reader, cur_item, *p);
819 else
820 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000821 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100822 retval = FAIL;
823 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100824 break;
825
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100826 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100827 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100828 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000829 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100830 retval = FAIL;
831 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100832 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100833 // FALLTHROUGH
834 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100835 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100836 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100837 cur_item->v_type = VAR_SPECIAL;
838 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100839 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100840 retval = OK;
841 break;
842
843 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100844 if (VIM_ISDIGIT(*p) || (*p == '-'
845 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100846 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100847 char_u *sp = p;
848
849 if (*sp == '-')
850 {
851 ++sp;
852 if (*sp == NUL)
853 {
854 retval = MAYBE;
855 break;
856 }
857 if (!VIM_ISDIGIT(*sp))
858 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000859 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100860 retval = FAIL;
861 break;
862 }
863 }
864 sp = skipdigits(sp);
865 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
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100881 {
882 varnumber_T nr;
883
884 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100885 NULL, &len, 0, // what
Bram Moolenaar5fb78c32023-03-04 20:47:39 +0000886 &nr, NULL, 0, TRUE, NULL);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200887 if (len == 0)
888 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000889 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200890 retval = FAIL;
891 goto theend;
892 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100893 if (cur_item != NULL)
894 {
895 cur_item->v_type = VAR_NUMBER;
896 cur_item->vval.v_number = nr;
897 }
898 }
899 reader->js_used += len;
900 retval = OK;
901 break;
902 }
903 if (STRNICMP((char *)p, "false", 5) == 0)
904 {
905 reader->js_used += 5;
906 if (cur_item != NULL)
907 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100908 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100909 cur_item->vval.v_number = VVAL_FALSE;
910 }
911 retval = OK;
912 break;
913 }
914 if (STRNICMP((char *)p, "true", 4) == 0)
915 {
916 reader->js_used += 4;
917 if (cur_item != NULL)
918 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100919 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100920 cur_item->vval.v_number = VVAL_TRUE;
921 }
922 retval = OK;
923 break;
924 }
925 if (STRNICMP((char *)p, "null", 4) == 0)
926 {
927 reader->js_used += 4;
928 if (cur_item != NULL)
929 {
930 cur_item->v_type = VAR_SPECIAL;
931 cur_item->vval.v_number = VVAL_NULL;
932 }
933 retval = OK;
934 break;
935 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100936 if (STRNICMP((char *)p, "NaN", 3) == 0)
937 {
938 reader->js_used += 3;
939 if (cur_item != NULL)
940 {
941 cur_item->v_type = VAR_FLOAT;
942 cur_item->vval.v_float = NAN;
943 }
944 retval = OK;
945 break;
946 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100947 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
948 {
949 reader->js_used += 9;
950 if (cur_item != NULL)
951 {
952 cur_item->v_type = VAR_FLOAT;
953 cur_item->vval.v_float = -INFINITY;
954 }
955 retval = OK;
956 break;
957 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100958 if (STRNICMP((char *)p, "Infinity", 8) == 0)
959 {
960 reader->js_used += 8;
961 if (cur_item != NULL)
962 {
963 cur_item->v_type = VAR_FLOAT;
964 cur_item->vval.v_float = INFINITY;
965 }
966 retval = OK;
967 break;
968 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100969 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100970 len = (int)(reader->js_end
971 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100972 if (
973 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100974 || (len < 9
975 && STRNICMP((char *)p, "-Infinity", len) == 0)
976 || (len < 8
977 && STRNICMP((char *)p, "Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100978 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
Bram Moolenaar73e28dc2022-09-17 21:08:33 +0100979 || (len < 4
980 && (STRNICMP((char *)p, "true", len) == 0
981 || STRNICMP((char *)p, "null", len) == 0)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100982
983 retval = MAYBE;
984 else
985 retval = FAIL;
986 break;
987 }
988
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100989 // We are finished when retval is FAIL or MAYBE and when at the
990 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100991 if (retval == FAIL)
992 break;
993 if (retval == MAYBE || stack.ga_len == 0)
994 goto theend;
995
996 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
997 && cur_item != NULL)
998 {
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200999 if (cur_item->v_type == VAR_FLOAT)
1000 {
1001 // cannot use a float as a key
Bram Moolenaar74409f62022-01-01 15:58:22 +00001002 emsg(_(e_using_float_as_string));
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +02001003 retval = FAIL;
1004 goto theend;
1005 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001006 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +01001007 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001008 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001009 emsg(_(e_invalid_argument));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001010 retval = FAIL;
1011 goto theend;
1012 }
1013 }
1014 }
1015
1016item_end:
1017 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
1018 switch (top_item->jd_type)
1019 {
1020 case JSON_ARRAY:
1021 if (res != NULL)
1022 {
1023 listitem_T *li = listitem_alloc();
1024
1025 if (li == NULL)
1026 {
1027 clear_tv(cur_item);
1028 retval = FAIL;
1029 goto theend;
1030 }
1031 li->li_tv = *cur_item;
1032 list_append(top_item->jd_tv.vval.v_list, li);
1033 }
1034 if (cur_item != NULL)
1035 cur_item = &item;
1036
1037 json_skip_white(reader);
1038 p = reader->js_buf + reader->js_used;
1039 if (*p == ',')
1040 ++reader->js_used;
1041 else if (*p != ']')
1042 {
1043 if (*p == NUL)
1044 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001045 else
1046 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001047 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001048 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001049 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001050 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001051 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001052 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001053
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001054 case JSON_OBJECT_KEY:
1055 json_skip_white(reader);
1056 p = reader->js_buf + reader->js_used;
1057 if (*p != ':')
1058 {
1059 if (cur_item != NULL)
1060 clear_tv(cur_item);
1061 if (*p == NUL)
1062 retval = MAYBE;
1063 else
Bram Moolenaar56ead342016-02-02 18:20:08 +01001064 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001065 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001066 retval = FAIL;
1067 }
1068 goto theend;
1069 }
1070 ++reader->js_used;
1071 json_skip_white(reader);
1072 top_item->jd_type = JSON_OBJECT;
1073 if (cur_item != NULL)
1074 cur_item = &item;
1075 break;
1076
1077 case JSON_OBJECT:
1078 if (cur_item != NULL
Yegappan Lakshmanan4829c1c2022-04-04 15:16:54 +01001079 && dict_has_key(top_item->jd_tv.vval.v_dict,
1080 (char *)top_item->jd_key))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001081 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001082 semsg(_(e_duplicate_key_in_json_str), top_item->jd_key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001083 clear_tv(cur_item);
1084 retval = FAIL;
1085 goto theend;
1086 }
1087
1088 if (cur_item != NULL)
1089 {
1090 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1091
1092 clear_tv(&top_item->jd_key_tv);
1093 if (di == NULL)
1094 {
1095 clear_tv(cur_item);
1096 retval = FAIL;
1097 goto theend;
1098 }
1099 di->di_tv = *cur_item;
1100 di->di_tv.v_lock = 0;
1101 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1102 {
1103 dictitem_free(di);
1104 retval = FAIL;
1105 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001106 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001107 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001108
1109 json_skip_white(reader);
1110 p = reader->js_buf + reader->js_used;
1111 if (*p == ',')
1112 ++reader->js_used;
1113 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001114 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001115 if (*p == NUL)
1116 retval = MAYBE;
1117 else
1118 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001119 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001120 retval = FAIL;
1121 }
1122 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001123 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001124 top_item->jd_type = JSON_OBJECT_KEY;
1125 if (cur_item != NULL)
1126 cur_item = &top_item->jd_key_tv;
1127 break;
1128 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001129 }
1130
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001131 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001132 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001133 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001134 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001135 res->v_type = VAR_SPECIAL;
1136 res->vval.v_number = VVAL_NONE;
1137 }
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001138 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001139
1140theend:
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001141 for (i = 0; i < stack.ga_len; i++)
1142 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001143 ga_clear(&stack);
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001144
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001145 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001146}
1147
1148/*
1149 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001150 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001151 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001152 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001153 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001154json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001155{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001156 int ret;
1157
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001158 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001159 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001160 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001161 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001162 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001163 {
1164 if (ret == MAYBE)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001165 semsg(_(e_json_decode_error_at_str), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001166 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001167 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001168 json_skip_white(reader);
1169 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001170 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001171 semsg(_(e_trailing_characters_str), reader->js_buf + reader->js_used);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001172 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001173 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001174 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001175}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001176
Bram Moolenaar113e1072019-01-20 15:30:40 +01001177#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001178/*
1179 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001180 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001181 * Return FAIL for a decoding error.
1182 * Return MAYBE for an incomplete message.
1183 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001184 */
1185 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001186json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001187{
1188 int ret;
1189
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001190 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001191 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1192 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001193 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001194 json_skip_white(reader);
1195
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001196 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001197}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001198#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001199
1200/*
1201 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001202 * "options" can be JSON_JS or zero.
1203 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001204 * Return FAIL if the message has a decoding error.
1205 * Return MAYBE if the message is truncated, need to read more.
1206 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001207 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001208 * Does not advance the reader.
1209 */
1210 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001211json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001212{
1213 int used_save = reader->js_used;
1214 int ret;
1215
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001216 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001217 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1218 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001219 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001220 reader->js_used = used_save;
1221 return ret;
1222}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001223
1224/*
1225 * "js_decode()" function
1226 */
1227 void
1228f_js_decode(typval_T *argvars, typval_T *rettv)
1229{
1230 js_read_T reader;
1231
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001232 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1233 return;
1234
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001235 reader.js_buf = tv_get_string(&argvars[0]);
1236 reader.js_fill = NULL;
1237 reader.js_used = 0;
1238 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001239 emsg(_(e_invalid_argument));
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001240}
1241
1242/*
1243 * "js_encode()" function
1244 */
1245 void
1246f_js_encode(typval_T *argvars, typval_T *rettv)
1247{
1248 rettv->v_type = VAR_STRING;
1249 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1250}
1251
1252/*
1253 * "json_decode()" function
1254 */
1255 void
1256f_json_decode(typval_T *argvars, typval_T *rettv)
1257{
1258 js_read_T reader;
1259
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001260 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1261 return;
1262
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001263 reader.js_buf = tv_get_string(&argvars[0]);
1264 reader.js_fill = NULL;
1265 reader.js_used = 0;
1266 json_decode_all(&reader, rettv, 0);
1267}
1268
1269/*
1270 * "json_encode()" function
1271 */
1272 void
1273f_json_encode(typval_T *argvars, typval_T *rettv)
1274{
1275 rettv->v_type = VAR_STRING;
1276 rettv->vval.v_string = json_encode(&argvars[0], 0);
1277}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001278#endif