blob: b23bfa089582372d1df04d927ad7cbc1398585f0 [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
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100117 static void
118write_string(garray_T *gap, char_u *str)
119{
120 char_u *res = str;
121 char_u numbuf[NUMBUFLEN];
122
123 if (res == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100124 ga_concat(gap, (char_u *)"\"\"");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100125 else
126 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100127#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100128 vimconv_T conv;
129 char_u *converted = NULL;
130
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100131 if (!enc_utf8)
132 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100133 // Convert the text from 'encoding' to utf-8, the JSON string is
134 // always utf-8.
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100135 conv.vc_type = CONV_NONE;
136 convert_setup(&conv, p_enc, (char_u*)"utf-8");
137 if (conv.vc_type != CONV_NONE)
138 converted = res = string_convert(&conv, res, NULL);
139 convert_setup(&conv, NULL, NULL);
140 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100141#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100142 ga_append(gap, '"');
143 while (*res != NUL)
144 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100145 int c;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100146 // always use utf-8 encoding, ignore 'encoding'
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100147 c = utf_ptr2char(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100148
149 switch (c)
150 {
151 case 0x08:
152 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
153 case 0x09:
154 ga_append(gap, '\\'); ga_append(gap, 't'); break;
155 case 0x0a:
156 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
157 case 0x0c:
158 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
159 case 0x0d:
160 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100161 case 0x22: // "
162 case 0x5c: // backslash
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100163 ga_append(gap, '\\');
164 ga_append(gap, c);
165 break;
166 default:
167 if (c >= 0x20)
168 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100169 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100170 ga_concat(gap, numbuf);
171 }
172 else
173 {
174 vim_snprintf((char *)numbuf, NUMBUFLEN,
175 "\\u%04lx", (long)c);
176 ga_concat(gap, numbuf);
177 }
178 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100179 res += utf_ptr2len(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100180 }
181 ga_append(gap, '"');
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100182#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100183 vim_free(converted);
184#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100185 }
186}
187
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100188/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100189 * Return TRUE if "key" can be used without quotes.
190 * That is when it starts with a letter and only contains letters, digits and
191 * underscore.
192 */
193 static int
194is_simple_key(char_u *key)
195{
196 char_u *p;
197
198 if (!ASCII_ISALPHA(*key))
199 return FALSE;
200 for (p = key + 1; *p != NUL; ++p)
201 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
202 return FALSE;
203 return TRUE;
204}
205
206/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100207 * Encode "val" into "gap".
208 * Return FAIL or OK.
209 */
210 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100211json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100212{
213 char_u numbuf[NUMBUFLEN];
214 char_u *res;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100215 blob_T *b;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100216 list_T *l;
217 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100218 int i;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100219
220 switch (val->v_type)
221 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100222 case VAR_BOOL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100223 switch ((long)val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100224 {
225 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
226 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100227 }
228 break;
229
230 case VAR_SPECIAL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100231 switch ((long)val->vval.v_number)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100232 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100233 case VVAL_NONE: if ((options & JSON_JS) != 0
234 && (options & JSON_NO_NONE) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100235 // empty item
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100236 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100237 // FALLTHROUGH
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100238 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
239 }
240 break;
241
242 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200243 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100244 (varnumber_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100245 ga_concat(gap, numbuf);
246 break;
247
248 case VAR_STRING:
249 res = val->vval.v_string;
250 write_string(gap, res);
251 break;
252
253 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100254 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100255 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100256 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200257 case VAR_INSTR:
Bram Moolenaara8530892021-02-08 21:53:09 +0100258 semsg(_(e_cannot_json_encode_str), vartype_name(val->v_type));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100259 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100260
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100261 case VAR_BLOB:
262 b = val->vval.v_blob;
263 if (b == NULL || b->bv_ga.ga_len == 0)
264 ga_concat(gap, (char_u *)"[]");
265 else
266 {
267 ga_append(gap, '[');
268 for (i = 0; i < b->bv_ga.ga_len; i++)
269 {
270 if (i > 0)
271 ga_concat(gap, (char_u *)",");
272 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
=?UTF-8?q?Dundar=20G=C3=B6c?=420fabc2022-01-28 15:28:04 +0000273 blob_get(b, i));
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100274 ga_concat(gap, numbuf);
275 }
276 ga_append(gap, ']');
277 }
278 break;
279
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100280 case VAR_LIST:
281 l = val->vval.v_list;
282 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100283 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100284 else
285 {
286 if (l->lv_copyID == copyID)
287 ga_concat(gap, (char_u *)"[]");
288 else
289 {
290 listitem_T *li;
291
292 l->lv_copyID = copyID;
293 ga_append(gap, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200294 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100295 for (li = l->lv_first; li != NULL && !got_int; )
296 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100297 if (json_encode_item(gap, &li->li_tv, copyID,
298 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100299 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100300 if ((options & JSON_JS)
301 && li->li_next == NULL
302 && li->li_tv.v_type == VAR_SPECIAL
303 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100304 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100305 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100306 li = li->li_next;
307 if (li != NULL)
308 ga_append(gap, ',');
309 }
310 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100311 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100312 }
313 }
314 break;
315
316 case VAR_DICT:
317 d = val->vval.v_dict;
318 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100319 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100320 else
321 {
322 if (d->dv_copyID == copyID)
323 ga_concat(gap, (char_u *)"{}");
324 else
325 {
326 int first = TRUE;
327 int todo = (int)d->dv_hashtab.ht_used;
328 hashitem_T *hi;
329
330 d->dv_copyID = copyID;
331 ga_append(gap, '{');
332
333 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
334 ++hi)
335 if (!HASHITEM_EMPTY(hi))
336 {
337 --todo;
338 if (first)
339 first = FALSE;
340 else
341 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100342 if ((options & JSON_JS)
343 && is_simple_key(hi->hi_key))
344 ga_concat(gap, hi->hi_key);
345 else
346 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100347 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100348 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100349 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100350 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100351 }
352 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100353 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100354 }
355 }
356 break;
357
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100358 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100359#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100360# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100361 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100362 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100363 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100364 {
365 if (val->vval.v_float < 0.0)
366 ga_concat(gap, (char_u *)"-Infinity");
367 else
368 ga_concat(gap, (char_u *)"Infinity");
369 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100370 else
371# endif
372 {
373 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
374 val->vval.v_float);
375 ga_concat(gap, numbuf);
376 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100377 break;
378#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100379 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200380 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100381 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100382 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100383 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100384 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100385 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100386}
387
388/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100389 * When "reader" has less than NUMBUFLEN bytes available, call the fill
390 * callback to get more.
391 */
392 static void
393fill_numbuflen(js_read_T *reader)
394{
395 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
396 - reader->js_used < NUMBUFLEN)
397 {
398 if (reader->js_fill(reader))
399 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
400 }
401}
402
403/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100404 * Skip white space in "reader". All characters <= space are considered white
405 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100406 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100407 */
408 static void
409json_skip_white(js_read_T *reader)
410{
411 int c;
412
Bram Moolenaar56ead342016-02-02 18:20:08 +0100413 for (;;)
414 {
415 c = reader->js_buf[reader->js_used];
416 if (reader->js_fill != NULL && c == NUL)
417 {
418 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200419 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100420 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200421 continue;
422 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100423 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100424 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100425 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100426 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100427 }
428 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100429}
430
Bram Moolenaar56ead342016-02-02 18:20:08 +0100431 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100432json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100433{
434 garray_T ga;
435 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100436 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100437 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200438 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100439
Bram Moolenaar56ead342016-02-02 18:20:08 +0100440 if (res != NULL)
441 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100442
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100443 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100444 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100445 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100446 // The JSON is always expected to be utf-8, thus use utf functions
447 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100448 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100449 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100450 // Not enough bytes to make a character or end of the string. Get
451 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100452 if (reader->js_fill == NULL)
453 break;
454 len = (int)(reader->js_end - p);
455 reader->js_used = (int)(p - reader->js_buf);
456 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100457 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100458 p = reader->js_buf + reader->js_used;
459 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
460 continue;
461 }
462
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100463 if (*p == '\\')
464 {
465 c = -1;
466 switch (p[1])
467 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100468 case '\\': c = '\\'; break;
469 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100470 case 'b': c = BS; break;
471 case 't': c = TAB; break;
472 case 'n': c = NL; break;
473 case 'f': c = FF; break;
474 case 'r': c = CAR; break;
475 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100476 if (reader->js_fill != NULL
477 && (int)(reader->js_end - p) < NUMBUFLEN)
478 {
479 reader->js_used = (int)(p - reader->js_buf);
480 if (reader->js_fill(reader))
481 {
482 p = reader->js_buf + reader->js_used;
483 reader->js_end = reader->js_buf
484 + STRLEN(reader->js_buf);
485 }
486 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100487 nr = 0;
488 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100489 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200490 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
491 if (len == 0)
492 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200493 if (res != NULL)
494 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200495 return FAIL;
496 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100497 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100498 if (0xd800 <= nr && nr <= 0xdfff
499 && (int)(reader->js_end - p) >= 6
500 && *p == '\\' && *(p+1) == 'u')
501 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200502 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100503
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100504 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100505 len = 0;
506 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200507 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
508 if (len == 0)
509 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200510 if (res != NULL)
511 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200512 return FAIL;
513 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100514 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
515 {
516 p += len + 2;
517 nr = (((nr - 0xd800) << 10) |
518 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
519 }
520 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100521 if (res != NULL)
522 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200523 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200524
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100525 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100526 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100527 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100528 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100529 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100530 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100531 ++p;
532 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100533 }
534 if (c > 0)
535 {
536 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100537 if (res != NULL)
538 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100539 }
540 }
541 else
542 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100543 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100544 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100545 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100546 if (ga_grow(&ga, len) == FAIL)
547 {
548 ga_clear(&ga);
549 return FAIL;
550 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100551 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
552 ga.ga_len += len;
553 }
554 p += len;
555 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100556 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100557
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100558 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100559 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100560 {
561 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100562 if (res != NULL)
563 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100564 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100565 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100566#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100567 if (!enc_utf8)
568 {
569 vimconv_T conv;
570
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100571 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100572 conv.vc_type = CONV_NONE;
573 convert_setup(&conv, (char_u*)"utf-8", p_enc);
574 if (conv.vc_type != CONV_NONE)
575 {
576 res->vval.v_string =
577 string_convert(&conv, ga.ga_data, NULL);
578 vim_free(ga.ga_data);
579 }
580 convert_setup(&conv, NULL, NULL);
581 }
582 else
583#endif
584 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100585 }
586 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100587 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100588 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100589 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100590 res->v_type = VAR_SPECIAL;
591 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100592 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100593 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100594 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100595}
596
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100597typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100598 JSON_ARRAY, // parsing items in an array
599 JSON_OBJECT_KEY, // parsing key of an object
600 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100601} json_decode_T;
602
603typedef struct {
604 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100605 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100606 typval_T jd_key_tv;
607 char_u *jd_key;
608} json_dec_item_T;
609
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100610/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100611 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100612 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100613 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100614 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100615 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100616 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100617 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100618json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100619{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100620 char_u *p;
Bram Moolenaar6d3a7212020-07-12 14:34:00 +0200621 int i;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100622 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100623 int retval;
624 garray_T stack;
625 typval_T item;
626 typval_T *cur_item;
627 json_dec_item_T *top_item;
628 char_u key_buf[NUMBUFLEN];
629
630 ga_init2(&stack, sizeof(json_dec_item_T), 100);
631 cur_item = res;
632 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100633 if (res != NULL)
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200634 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100635
Bram Moolenaar56ead342016-02-02 18:20:08 +0100636 fill_numbuflen(reader);
637 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100638 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100639 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100640 top_item = NULL;
641 if (stack.ga_len > 0)
642 {
643 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
644 json_skip_white(reader);
645 p = reader->js_buf + reader->js_used;
646 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100647 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100648 retval = MAYBE;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100649 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100650 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100651 if (top_item->jd_type == JSON_OBJECT_KEY
652 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100653 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100654 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100655 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100656 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100657 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100658 --stack.ga_len;
659 if (stack.ga_len == 0)
660 {
661 retval = OK;
662 goto theend;
663 }
664 if (cur_item != NULL)
665 cur_item = &top_item->jd_tv;
666 goto item_end;
667 }
668 }
669 }
670
671 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
672 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100673 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100674 && reader->js_buf[reader->js_used] != '\''
675 && reader->js_buf[reader->js_used] != '['
676 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100677 {
678 char_u *key;
679
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100680 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100681 key = p = reader->js_buf + reader->js_used;
682 while (*p != NUL && *p != ':' && *p > ' ')
683 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100684 if (cur_item != NULL)
685 {
686 cur_item->v_type = VAR_STRING;
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200687 cur_item->vval.v_string = vim_strnsave(key, p - key);
Bram Moolenaare2c60372017-01-22 15:56:26 +0100688 top_item->jd_key = cur_item->vval.v_string;
689 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100690 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100691 }
692 else
693 {
694 switch (*p)
695 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100696 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100697 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
698 {
699 retval = FAIL;
700 break;
701 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100702 if (ga_grow(&stack, 1) == FAIL)
703 {
704 retval = FAIL;
705 break;
706 }
707 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
708 {
709 cur_item->v_type = VAR_SPECIAL;
710 cur_item->vval.v_number = VVAL_NONE;
711 retval = FAIL;
712 break;
713 }
714
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100715 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100716 top_item = ((json_dec_item_T *)stack.ga_data)
717 + stack.ga_len;
718 top_item->jd_type = JSON_ARRAY;
719 ++stack.ga_len;
720 if (cur_item != NULL)
721 {
722 top_item->jd_tv = *cur_item;
723 cur_item = &item;
724 }
725 continue;
726
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100727 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100728 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
729 {
730 retval = FAIL;
731 break;
732 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100733 if (ga_grow(&stack, 1) == FAIL)
734 {
735 retval = FAIL;
736 break;
737 }
738 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
739 {
740 cur_item->v_type = VAR_SPECIAL;
741 cur_item->vval.v_number = VVAL_NONE;
742 retval = FAIL;
743 break;
744 }
745
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100746 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100747 top_item = ((json_dec_item_T *)stack.ga_data)
748 + stack.ga_len;
749 top_item->jd_type = JSON_OBJECT_KEY;
750 ++stack.ga_len;
751 if (cur_item != NULL)
752 {
753 top_item->jd_tv = *cur_item;
754 cur_item = &top_item->jd_key_tv;
755 }
756 continue;
757
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100758 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100759 retval = json_decode_string(reader, cur_item, *p);
760 break;
761
762 case '\'':
763 if (options & JSON_JS)
764 retval = json_decode_string(reader, cur_item, *p);
765 else
766 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000767 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100768 retval = FAIL;
769 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100770 break;
771
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100772 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100773 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100774 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000775 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100776 retval = FAIL;
777 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100778 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100779 // FALLTHROUGH
780 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100781 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100782 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100783 cur_item->v_type = VAR_SPECIAL;
784 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100785 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100786 retval = OK;
787 break;
788
789 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100790 if (VIM_ISDIGIT(*p) || (*p == '-'
791 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100792 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100793 char_u *sp = p;
794
795 if (*sp == '-')
796 {
797 ++sp;
798 if (*sp == NUL)
799 {
800 retval = MAYBE;
801 break;
802 }
803 if (!VIM_ISDIGIT(*sp))
804 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000805 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100806 retval = FAIL;
807 break;
808 }
809 }
810 sp = skipdigits(sp);
Bram Moolenaara5d59532020-01-26 21:42:03 +0100811#ifdef FEAT_FLOAT
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100812 if (*sp == '.' || *sp == 'e' || *sp == 'E')
813 {
814 if (cur_item == NULL)
815 {
816 float_T f;
817
Bram Moolenaar29500652021-08-08 15:43:34 +0200818 len = string2float(p, &f, FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100819 }
820 else
821 {
822 cur_item->v_type = VAR_FLOAT;
Bram Moolenaar29500652021-08-08 15:43:34 +0200823 len = string2float(p, &cur_item->vval.v_float,
824 FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100825 }
826 }
827 else
828#endif
829 {
830 varnumber_T nr;
831
832 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100833 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200834 &nr, NULL, 0, TRUE);
835 if (len == 0)
836 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000837 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200838 retval = FAIL;
839 goto theend;
840 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100841 if (cur_item != NULL)
842 {
843 cur_item->v_type = VAR_NUMBER;
844 cur_item->vval.v_number = nr;
845 }
846 }
847 reader->js_used += len;
848 retval = OK;
849 break;
850 }
851 if (STRNICMP((char *)p, "false", 5) == 0)
852 {
853 reader->js_used += 5;
854 if (cur_item != NULL)
855 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100856 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100857 cur_item->vval.v_number = VVAL_FALSE;
858 }
859 retval = OK;
860 break;
861 }
862 if (STRNICMP((char *)p, "true", 4) == 0)
863 {
864 reader->js_used += 4;
865 if (cur_item != NULL)
866 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100867 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100868 cur_item->vval.v_number = VVAL_TRUE;
869 }
870 retval = OK;
871 break;
872 }
873 if (STRNICMP((char *)p, "null", 4) == 0)
874 {
875 reader->js_used += 4;
876 if (cur_item != NULL)
877 {
878 cur_item->v_type = VAR_SPECIAL;
879 cur_item->vval.v_number = VVAL_NULL;
880 }
881 retval = OK;
882 break;
883 }
884#ifdef FEAT_FLOAT
885 if (STRNICMP((char *)p, "NaN", 3) == 0)
886 {
887 reader->js_used += 3;
888 if (cur_item != NULL)
889 {
890 cur_item->v_type = VAR_FLOAT;
891 cur_item->vval.v_float = NAN;
892 }
893 retval = OK;
894 break;
895 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100896 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
897 {
898 reader->js_used += 9;
899 if (cur_item != NULL)
900 {
901 cur_item->v_type = VAR_FLOAT;
902 cur_item->vval.v_float = -INFINITY;
903 }
904 retval = OK;
905 break;
906 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100907 if (STRNICMP((char *)p, "Infinity", 8) == 0)
908 {
909 reader->js_used += 8;
910 if (cur_item != NULL)
911 {
912 cur_item->v_type = VAR_FLOAT;
913 cur_item->vval.v_float = INFINITY;
914 }
915 retval = OK;
916 break;
917 }
918#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100919 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100920 len = (int)(reader->js_end
921 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100922 if (
923 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
924#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100925 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100926 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
927 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
928#endif
929 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
930 || STRNICMP((char *)p, "null", len) == 0)))
931
932 retval = MAYBE;
933 else
934 retval = FAIL;
935 break;
936 }
937
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100938 // We are finished when retval is FAIL or MAYBE and when at the
939 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100940 if (retval == FAIL)
941 break;
942 if (retval == MAYBE || stack.ga_len == 0)
943 goto theend;
944
945 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
946 && cur_item != NULL)
947 {
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200948#ifdef FEAT_FLOAT
949 if (cur_item->v_type == VAR_FLOAT)
950 {
951 // cannot use a float as a key
Bram Moolenaar74409f62022-01-01 15:58:22 +0000952 emsg(_(e_using_float_as_string));
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200953 retval = FAIL;
954 goto theend;
955 }
956#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100957 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100958 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100959 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000960 emsg(_(e_invalid_argument));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100961 retval = FAIL;
962 goto theend;
963 }
964 }
965 }
966
967item_end:
968 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
969 switch (top_item->jd_type)
970 {
971 case JSON_ARRAY:
972 if (res != NULL)
973 {
974 listitem_T *li = listitem_alloc();
975
976 if (li == NULL)
977 {
978 clear_tv(cur_item);
979 retval = FAIL;
980 goto theend;
981 }
982 li->li_tv = *cur_item;
983 list_append(top_item->jd_tv.vval.v_list, li);
984 }
985 if (cur_item != NULL)
986 cur_item = &item;
987
988 json_skip_white(reader);
989 p = reader->js_buf + reader->js_used;
990 if (*p == ',')
991 ++reader->js_used;
992 else if (*p != ']')
993 {
994 if (*p == NUL)
995 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100996 else
997 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000998 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100999 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001000 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001001 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001002 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001003 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001004
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001005 case JSON_OBJECT_KEY:
1006 json_skip_white(reader);
1007 p = reader->js_buf + reader->js_used;
1008 if (*p != ':')
1009 {
1010 if (cur_item != NULL)
1011 clear_tv(cur_item);
1012 if (*p == NUL)
1013 retval = MAYBE;
1014 else
Bram Moolenaar56ead342016-02-02 18:20:08 +01001015 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001016 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001017 retval = FAIL;
1018 }
1019 goto theend;
1020 }
1021 ++reader->js_used;
1022 json_skip_white(reader);
1023 top_item->jd_type = JSON_OBJECT;
1024 if (cur_item != NULL)
1025 cur_item = &item;
1026 break;
1027
1028 case JSON_OBJECT:
1029 if (cur_item != NULL
1030 && dict_find(top_item->jd_tv.vval.v_dict,
1031 top_item->jd_key, -1) != NULL)
1032 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001033 semsg(_(e_duplicate_key_in_json_str), top_item->jd_key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001034 clear_tv(cur_item);
1035 retval = FAIL;
1036 goto theend;
1037 }
1038
1039 if (cur_item != NULL)
1040 {
1041 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1042
1043 clear_tv(&top_item->jd_key_tv);
1044 if (di == NULL)
1045 {
1046 clear_tv(cur_item);
1047 retval = FAIL;
1048 goto theend;
1049 }
1050 di->di_tv = *cur_item;
1051 di->di_tv.v_lock = 0;
1052 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1053 {
1054 dictitem_free(di);
1055 retval = FAIL;
1056 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001057 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001058 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001059
1060 json_skip_white(reader);
1061 p = reader->js_buf + reader->js_used;
1062 if (*p == ',')
1063 ++reader->js_used;
1064 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001065 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001066 if (*p == NUL)
1067 retval = MAYBE;
1068 else
1069 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001070 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001071 retval = FAIL;
1072 }
1073 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001074 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001075 top_item->jd_type = JSON_OBJECT_KEY;
1076 if (cur_item != NULL)
1077 cur_item = &top_item->jd_key_tv;
1078 break;
1079 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001080 }
1081
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001082 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001083 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001084 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001085 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001086 res->v_type = VAR_SPECIAL;
1087 res->vval.v_number = VVAL_NONE;
1088 }
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001089 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001090
1091theend:
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001092 for (i = 0; i < stack.ga_len; i++)
1093 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001094 ga_clear(&stack);
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001095
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001096 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001097}
1098
1099/*
1100 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001101 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001102 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001103 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001104 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001105json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001106{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001107 int ret;
1108
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001109 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001110 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001111 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001112 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001113 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001114 {
1115 if (ret == MAYBE)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001116 semsg(_(e_json_decode_error_at_str), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001117 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001118 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001119 json_skip_white(reader);
1120 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001121 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001122 semsg(_(e_trailing_characters_str), reader->js_buf + reader->js_used);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001123 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001124 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001125 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001126}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001127
Bram Moolenaar113e1072019-01-20 15:30:40 +01001128#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001129/*
1130 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001131 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001132 * Return FAIL for a decoding error.
1133 * Return MAYBE for an incomplete message.
1134 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001135 */
1136 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001137json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001138{
1139 int ret;
1140
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001141 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001142 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1143 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001144 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001145 json_skip_white(reader);
1146
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001147 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001148}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001149#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001150
1151/*
1152 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001153 * "options" can be JSON_JS or zero.
1154 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001155 * Return FAIL if the message has a decoding error.
1156 * Return MAYBE if the message is truncated, need to read more.
1157 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001158 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001159 * Does not advance the reader.
1160 */
1161 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001162json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001163{
1164 int used_save = reader->js_used;
1165 int ret;
1166
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001167 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001168 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1169 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001170 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001171 reader->js_used = used_save;
1172 return ret;
1173}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001174
1175/*
1176 * "js_decode()" function
1177 */
1178 void
1179f_js_decode(typval_T *argvars, typval_T *rettv)
1180{
1181 js_read_T reader;
1182
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001183 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1184 return;
1185
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001186 reader.js_buf = tv_get_string(&argvars[0]);
1187 reader.js_fill = NULL;
1188 reader.js_used = 0;
1189 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001190 emsg(_(e_invalid_argument));
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001191}
1192
1193/*
1194 * "js_encode()" function
1195 */
1196 void
1197f_js_encode(typval_T *argvars, typval_T *rettv)
1198{
1199 rettv->v_type = VAR_STRING;
1200 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1201}
1202
1203/*
1204 * "json_decode()" function
1205 */
1206 void
1207f_json_decode(typval_T *argvars, typval_T *rettv)
1208{
1209 js_read_T reader;
1210
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001211 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1212 return;
1213
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001214 reader.js_buf = tv_get_string(&argvars[0]);
1215 reader.js_fill = NULL;
1216 reader.js_used = 0;
1217 json_decode_all(&reader, rettv, 0);
1218}
1219
1220/*
1221 * "json_encode()" function
1222 */
1223 void
1224f_json_encode(typval_T *argvars, typval_T *rettv)
1225{
1226 rettv->v_type = VAR_STRING;
1227 rettv->vval.v_string = json_encode(&argvars[0], 0);
1228}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001229#endif