blob: e19bc9742c4a8c4ae81d64fa61bec92410ec2fed [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}
Bram Moolenaar113e1072019-01-20 15:30:40 +010089#endif
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010090
Bram Moolenaar520e1e42016-01-23 19:46:28 +010091 static void
92write_string(garray_T *gap, char_u *str)
93{
94 char_u *res = str;
95 char_u numbuf[NUMBUFLEN];
96
97 if (res == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +010098 ga_concat(gap, (char_u *)"\"\"");
Bram Moolenaar520e1e42016-01-23 19:46:28 +010099 else
100 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100101#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100102 vimconv_T conv;
103 char_u *converted = NULL;
104
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100105 if (!enc_utf8)
106 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100107 // Convert the text from 'encoding' to utf-8, the JSON string is
108 // always utf-8.
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100109 conv.vc_type = CONV_NONE;
110 convert_setup(&conv, p_enc, (char_u*)"utf-8");
111 if (conv.vc_type != CONV_NONE)
112 converted = res = string_convert(&conv, res, NULL);
113 convert_setup(&conv, NULL, NULL);
114 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100115#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100116 ga_append(gap, '"');
117 while (*res != NUL)
118 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100119 int c;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100120 // always use utf-8 encoding, ignore 'encoding'
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100121 c = utf_ptr2char(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100122
123 switch (c)
124 {
125 case 0x08:
126 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
127 case 0x09:
128 ga_append(gap, '\\'); ga_append(gap, 't'); break;
129 case 0x0a:
130 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
131 case 0x0c:
132 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
133 case 0x0d:
134 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100135 case 0x22: // "
136 case 0x5c: // backslash
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100137 ga_append(gap, '\\');
138 ga_append(gap, c);
139 break;
140 default:
141 if (c >= 0x20)
142 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100143 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100144 ga_concat(gap, numbuf);
145 }
146 else
147 {
148 vim_snprintf((char *)numbuf, NUMBUFLEN,
149 "\\u%04lx", (long)c);
150 ga_concat(gap, numbuf);
151 }
152 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100153 res += utf_ptr2len(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100154 }
155 ga_append(gap, '"');
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100156#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100157 vim_free(converted);
158#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100159 }
160}
161
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100162/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100163 * Return TRUE if "key" can be used without quotes.
164 * That is when it starts with a letter and only contains letters, digits and
165 * underscore.
166 */
167 static int
168is_simple_key(char_u *key)
169{
170 char_u *p;
171
172 if (!ASCII_ISALPHA(*key))
173 return FALSE;
174 for (p = key + 1; *p != NUL; ++p)
175 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
176 return FALSE;
177 return TRUE;
178}
179
180/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100181 * Encode "val" into "gap".
182 * Return FAIL or OK.
183 */
184 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100185json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100186{
187 char_u numbuf[NUMBUFLEN];
188 char_u *res;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100189 blob_T *b;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100190 list_T *l;
191 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 int i;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100193
194 switch (val->v_type)
195 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100196 case VAR_BOOL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100197 switch ((long)val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100198 {
199 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
200 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100201 }
202 break;
203
204 case VAR_SPECIAL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100205 switch ((long)val->vval.v_number)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100206 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100207 case VVAL_NONE: if ((options & JSON_JS) != 0
208 && (options & JSON_NO_NONE) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100209 // empty item
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100210 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100211 // FALLTHROUGH
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100212 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
213 }
214 break;
215
216 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200217 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100218 (varnumber_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100219 ga_concat(gap, numbuf);
220 break;
221
222 case VAR_STRING:
223 res = val->vval.v_string;
224 write_string(gap, res);
225 break;
226
227 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100228 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100229 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100230 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200231 case VAR_INSTR:
Bram Moolenaara8530892021-02-08 21:53:09 +0100232 semsg(_(e_cannot_json_encode_str), vartype_name(val->v_type));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100233 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100234
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100235 case VAR_BLOB:
236 b = val->vval.v_blob;
237 if (b == NULL || b->bv_ga.ga_len == 0)
238 ga_concat(gap, (char_u *)"[]");
239 else
240 {
241 ga_append(gap, '[');
242 for (i = 0; i < b->bv_ga.ga_len; i++)
243 {
244 if (i > 0)
245 ga_concat(gap, (char_u *)",");
246 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
247 (int)blob_get(b, i));
248 ga_concat(gap, numbuf);
249 }
250 ga_append(gap, ']');
251 }
252 break;
253
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100254 case VAR_LIST:
255 l = val->vval.v_list;
256 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100257 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100258 else
259 {
260 if (l->lv_copyID == copyID)
261 ga_concat(gap, (char_u *)"[]");
262 else
263 {
264 listitem_T *li;
265
266 l->lv_copyID = copyID;
267 ga_append(gap, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200268 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100269 for (li = l->lv_first; li != NULL && !got_int; )
270 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100271 if (json_encode_item(gap, &li->li_tv, copyID,
272 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100273 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100274 if ((options & JSON_JS)
275 && li->li_next == NULL
276 && li->li_tv.v_type == VAR_SPECIAL
277 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100278 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100279 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100280 li = li->li_next;
281 if (li != NULL)
282 ga_append(gap, ',');
283 }
284 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100285 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100286 }
287 }
288 break;
289
290 case VAR_DICT:
291 d = val->vval.v_dict;
292 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100293 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100294 else
295 {
296 if (d->dv_copyID == copyID)
297 ga_concat(gap, (char_u *)"{}");
298 else
299 {
300 int first = TRUE;
301 int todo = (int)d->dv_hashtab.ht_used;
302 hashitem_T *hi;
303
304 d->dv_copyID = copyID;
305 ga_append(gap, '{');
306
307 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
308 ++hi)
309 if (!HASHITEM_EMPTY(hi))
310 {
311 --todo;
312 if (first)
313 first = FALSE;
314 else
315 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100316 if ((options & JSON_JS)
317 && is_simple_key(hi->hi_key))
318 ga_concat(gap, hi->hi_key);
319 else
320 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100321 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100322 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100323 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100324 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100325 }
326 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100327 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100328 }
329 }
330 break;
331
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100332 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100333#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100334# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100335 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100336 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100337 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100338 {
339 if (val->vval.v_float < 0.0)
340 ga_concat(gap, (char_u *)"-Infinity");
341 else
342 ga_concat(gap, (char_u *)"Infinity");
343 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100344 else
345# endif
346 {
347 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
348 val->vval.v_float);
349 ga_concat(gap, numbuf);
350 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100351 break;
352#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100353 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200354 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100356 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100357 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100358 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100359 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100360}
361
362/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100363 * When "reader" has less than NUMBUFLEN bytes available, call the fill
364 * callback to get more.
365 */
366 static void
367fill_numbuflen(js_read_T *reader)
368{
369 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
370 - reader->js_used < NUMBUFLEN)
371 {
372 if (reader->js_fill(reader))
373 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
374 }
375}
376
377/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100378 * Skip white space in "reader". All characters <= space are considered white
379 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100380 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100381 */
382 static void
383json_skip_white(js_read_T *reader)
384{
385 int c;
386
Bram Moolenaar56ead342016-02-02 18:20:08 +0100387 for (;;)
388 {
389 c = reader->js_buf[reader->js_used];
390 if (reader->js_fill != NULL && c == NUL)
391 {
392 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200393 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100394 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200395 continue;
396 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100397 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100398 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100399 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100400 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100401 }
402 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100403}
404
Bram Moolenaar56ead342016-02-02 18:20:08 +0100405 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100406json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100407{
408 garray_T ga;
409 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100410 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100411 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200412 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100413
Bram Moolenaar56ead342016-02-02 18:20:08 +0100414 if (res != NULL)
415 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100418 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100419 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100420 // The JSON is always expected to be utf-8, thus use utf functions
421 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100422 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100423 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100424 // Not enough bytes to make a character or end of the string. Get
425 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100426 if (reader->js_fill == NULL)
427 break;
428 len = (int)(reader->js_end - p);
429 reader->js_used = (int)(p - reader->js_buf);
430 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100431 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100432 p = reader->js_buf + reader->js_used;
433 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
434 continue;
435 }
436
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100437 if (*p == '\\')
438 {
439 c = -1;
440 switch (p[1])
441 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100442 case '\\': c = '\\'; break;
443 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100444 case 'b': c = BS; break;
445 case 't': c = TAB; break;
446 case 'n': c = NL; break;
447 case 'f': c = FF; break;
448 case 'r': c = CAR; break;
449 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100450 if (reader->js_fill != NULL
451 && (int)(reader->js_end - p) < NUMBUFLEN)
452 {
453 reader->js_used = (int)(p - reader->js_buf);
454 if (reader->js_fill(reader))
455 {
456 p = reader->js_buf + reader->js_used;
457 reader->js_end = reader->js_buf
458 + STRLEN(reader->js_buf);
459 }
460 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100461 nr = 0;
462 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100463 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200464 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
465 if (len == 0)
466 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200467 if (res != NULL)
468 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200469 return FAIL;
470 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100471 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100472 if (0xd800 <= nr && nr <= 0xdfff
473 && (int)(reader->js_end - p) >= 6
474 && *p == '\\' && *(p+1) == 'u')
475 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200476 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100477
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100478 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100479 len = 0;
480 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200481 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
482 if (len == 0)
483 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200484 if (res != NULL)
485 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200486 return FAIL;
487 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100488 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
489 {
490 p += len + 2;
491 nr = (((nr - 0xd800) << 10) |
492 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
493 }
494 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100495 if (res != NULL)
496 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200497 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200498
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100499 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100500 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100501 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100502 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100503 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100504 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100505 ++p;
506 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100507 }
508 if (c > 0)
509 {
510 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100511 if (res != NULL)
512 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100513 }
514 }
515 else
516 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100517 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100518 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100519 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100520 if (ga_grow(&ga, len) == FAIL)
521 {
522 ga_clear(&ga);
523 return FAIL;
524 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100525 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
526 ga.ga_len += len;
527 }
528 p += len;
529 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100530 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100531
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100532 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100533 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100534 {
535 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100536 if (res != NULL)
537 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100538 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100539 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100540#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100541 if (!enc_utf8)
542 {
543 vimconv_T conv;
544
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100545 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100546 conv.vc_type = CONV_NONE;
547 convert_setup(&conv, (char_u*)"utf-8", p_enc);
548 if (conv.vc_type != CONV_NONE)
549 {
550 res->vval.v_string =
551 string_convert(&conv, ga.ga_data, NULL);
552 vim_free(ga.ga_data);
553 }
554 convert_setup(&conv, NULL, NULL);
555 }
556 else
557#endif
558 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100559 }
560 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100561 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100562 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100563 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100564 res->v_type = VAR_SPECIAL;
565 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100566 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100567 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100568 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100569}
570
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100571typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100572 JSON_ARRAY, // parsing items in an array
573 JSON_OBJECT_KEY, // parsing key of an object
574 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100575} json_decode_T;
576
577typedef struct {
578 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100579 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100580 typval_T jd_key_tv;
581 char_u *jd_key;
582} json_dec_item_T;
583
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100584/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100585 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100586 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100587 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100588 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100589 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100590 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100591 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100592json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100593{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100594 char_u *p;
Bram Moolenaar6d3a7212020-07-12 14:34:00 +0200595 int i;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100596 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100597 int retval;
598 garray_T stack;
599 typval_T item;
600 typval_T *cur_item;
601 json_dec_item_T *top_item;
602 char_u key_buf[NUMBUFLEN];
603
604 ga_init2(&stack, sizeof(json_dec_item_T), 100);
605 cur_item = res;
606 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100607 if (res != NULL)
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200608 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100609
Bram Moolenaar56ead342016-02-02 18:20:08 +0100610 fill_numbuflen(reader);
611 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100612 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100613 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100614 top_item = NULL;
615 if (stack.ga_len > 0)
616 {
617 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
618 json_skip_white(reader);
619 p = reader->js_buf + reader->js_used;
620 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100621 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100622 retval = MAYBE;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100623 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100624 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100625 if (top_item->jd_type == JSON_OBJECT_KEY
626 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100627 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100628 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100629 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100630 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100631 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100632 --stack.ga_len;
633 if (stack.ga_len == 0)
634 {
635 retval = OK;
636 goto theend;
637 }
638 if (cur_item != NULL)
639 cur_item = &top_item->jd_tv;
640 goto item_end;
641 }
642 }
643 }
644
645 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
646 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100647 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100648 && reader->js_buf[reader->js_used] != '\''
649 && reader->js_buf[reader->js_used] != '['
650 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100651 {
652 char_u *key;
653
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100654 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100655 key = p = reader->js_buf + reader->js_used;
656 while (*p != NUL && *p != ':' && *p > ' ')
657 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100658 if (cur_item != NULL)
659 {
660 cur_item->v_type = VAR_STRING;
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200661 cur_item->vval.v_string = vim_strnsave(key, p - key);
Bram Moolenaare2c60372017-01-22 15:56:26 +0100662 top_item->jd_key = cur_item->vval.v_string;
663 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100664 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100665 }
666 else
667 {
668 switch (*p)
669 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100670 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100671 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
672 {
673 retval = FAIL;
674 break;
675 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100676 if (ga_grow(&stack, 1) == FAIL)
677 {
678 retval = FAIL;
679 break;
680 }
681 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
682 {
683 cur_item->v_type = VAR_SPECIAL;
684 cur_item->vval.v_number = VVAL_NONE;
685 retval = FAIL;
686 break;
687 }
688
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100689 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100690 top_item = ((json_dec_item_T *)stack.ga_data)
691 + stack.ga_len;
692 top_item->jd_type = JSON_ARRAY;
693 ++stack.ga_len;
694 if (cur_item != NULL)
695 {
696 top_item->jd_tv = *cur_item;
697 cur_item = &item;
698 }
699 continue;
700
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100701 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100702 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
703 {
704 retval = FAIL;
705 break;
706 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100707 if (ga_grow(&stack, 1) == FAIL)
708 {
709 retval = FAIL;
710 break;
711 }
712 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
713 {
714 cur_item->v_type = VAR_SPECIAL;
715 cur_item->vval.v_number = VVAL_NONE;
716 retval = FAIL;
717 break;
718 }
719
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100720 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100721 top_item = ((json_dec_item_T *)stack.ga_data)
722 + stack.ga_len;
723 top_item->jd_type = JSON_OBJECT_KEY;
724 ++stack.ga_len;
725 if (cur_item != NULL)
726 {
727 top_item->jd_tv = *cur_item;
728 cur_item = &top_item->jd_key_tv;
729 }
730 continue;
731
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100732 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100733 retval = json_decode_string(reader, cur_item, *p);
734 break;
735
736 case '\'':
737 if (options & JSON_JS)
738 retval = json_decode_string(reader, cur_item, *p);
739 else
740 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000741 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100742 retval = FAIL;
743 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100744 break;
745
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100746 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100747 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100748 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000749 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100750 retval = FAIL;
751 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100752 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100753 // FALLTHROUGH
754 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100755 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100756 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100757 cur_item->v_type = VAR_SPECIAL;
758 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100759 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100760 retval = OK;
761 break;
762
763 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100764 if (VIM_ISDIGIT(*p) || (*p == '-'
765 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100766 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100767 char_u *sp = p;
768
769 if (*sp == '-')
770 {
771 ++sp;
772 if (*sp == NUL)
773 {
774 retval = MAYBE;
775 break;
776 }
777 if (!VIM_ISDIGIT(*sp))
778 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000779 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100780 retval = FAIL;
781 break;
782 }
783 }
784 sp = skipdigits(sp);
Bram Moolenaara5d59532020-01-26 21:42:03 +0100785#ifdef FEAT_FLOAT
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100786 if (*sp == '.' || *sp == 'e' || *sp == 'E')
787 {
788 if (cur_item == NULL)
789 {
790 float_T f;
791
Bram Moolenaar29500652021-08-08 15:43:34 +0200792 len = string2float(p, &f, FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100793 }
794 else
795 {
796 cur_item->v_type = VAR_FLOAT;
Bram Moolenaar29500652021-08-08 15:43:34 +0200797 len = string2float(p, &cur_item->vval.v_float,
798 FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100799 }
800 }
801 else
802#endif
803 {
804 varnumber_T nr;
805
806 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100807 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200808 &nr, NULL, 0, TRUE);
809 if (len == 0)
810 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000811 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200812 retval = FAIL;
813 goto theend;
814 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100815 if (cur_item != NULL)
816 {
817 cur_item->v_type = VAR_NUMBER;
818 cur_item->vval.v_number = nr;
819 }
820 }
821 reader->js_used += len;
822 retval = OK;
823 break;
824 }
825 if (STRNICMP((char *)p, "false", 5) == 0)
826 {
827 reader->js_used += 5;
828 if (cur_item != NULL)
829 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100830 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100831 cur_item->vval.v_number = VVAL_FALSE;
832 }
833 retval = OK;
834 break;
835 }
836 if (STRNICMP((char *)p, "true", 4) == 0)
837 {
838 reader->js_used += 4;
839 if (cur_item != NULL)
840 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100841 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100842 cur_item->vval.v_number = VVAL_TRUE;
843 }
844 retval = OK;
845 break;
846 }
847 if (STRNICMP((char *)p, "null", 4) == 0)
848 {
849 reader->js_used += 4;
850 if (cur_item != NULL)
851 {
852 cur_item->v_type = VAR_SPECIAL;
853 cur_item->vval.v_number = VVAL_NULL;
854 }
855 retval = OK;
856 break;
857 }
858#ifdef FEAT_FLOAT
859 if (STRNICMP((char *)p, "NaN", 3) == 0)
860 {
861 reader->js_used += 3;
862 if (cur_item != NULL)
863 {
864 cur_item->v_type = VAR_FLOAT;
865 cur_item->vval.v_float = NAN;
866 }
867 retval = OK;
868 break;
869 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100870 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
871 {
872 reader->js_used += 9;
873 if (cur_item != NULL)
874 {
875 cur_item->v_type = VAR_FLOAT;
876 cur_item->vval.v_float = -INFINITY;
877 }
878 retval = OK;
879 break;
880 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100881 if (STRNICMP((char *)p, "Infinity", 8) == 0)
882 {
883 reader->js_used += 8;
884 if (cur_item != NULL)
885 {
886 cur_item->v_type = VAR_FLOAT;
887 cur_item->vval.v_float = INFINITY;
888 }
889 retval = OK;
890 break;
891 }
892#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100893 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100894 len = (int)(reader->js_end
895 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100896 if (
897 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
898#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100899 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100900 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
901 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
902#endif
903 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
904 || STRNICMP((char *)p, "null", len) == 0)))
905
906 retval = MAYBE;
907 else
908 retval = FAIL;
909 break;
910 }
911
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100912 // We are finished when retval is FAIL or MAYBE and when at the
913 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100914 if (retval == FAIL)
915 break;
916 if (retval == MAYBE || stack.ga_len == 0)
917 goto theend;
918
919 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
920 && cur_item != NULL)
921 {
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200922#ifdef FEAT_FLOAT
923 if (cur_item->v_type == VAR_FLOAT)
924 {
925 // cannot use a float as a key
Bram Moolenaar74409f62022-01-01 15:58:22 +0000926 emsg(_(e_using_float_as_string));
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200927 retval = FAIL;
928 goto theend;
929 }
930#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100931 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100932 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100933 {
Bram Moolenaar436b5ad2021-12-31 22:49:24 +0000934 emsg(_(e_invalid_argument));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100935 retval = FAIL;
936 goto theend;
937 }
938 }
939 }
940
941item_end:
942 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
943 switch (top_item->jd_type)
944 {
945 case JSON_ARRAY:
946 if (res != NULL)
947 {
948 listitem_T *li = listitem_alloc();
949
950 if (li == NULL)
951 {
952 clear_tv(cur_item);
953 retval = FAIL;
954 goto theend;
955 }
956 li->li_tv = *cur_item;
957 list_append(top_item->jd_tv.vval.v_list, li);
958 }
959 if (cur_item != NULL)
960 cur_item = &item;
961
962 json_skip_white(reader);
963 p = reader->js_buf + reader->js_used;
964 if (*p == ',')
965 ++reader->js_used;
966 else if (*p != ']')
967 {
968 if (*p == NUL)
969 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100970 else
971 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000972 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100973 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100974 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100975 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100976 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100977 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100978
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100979 case JSON_OBJECT_KEY:
980 json_skip_white(reader);
981 p = reader->js_buf + reader->js_used;
982 if (*p != ':')
983 {
984 if (cur_item != NULL)
985 clear_tv(cur_item);
986 if (*p == NUL)
987 retval = MAYBE;
988 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100989 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +0000990 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100991 retval = FAIL;
992 }
993 goto theend;
994 }
995 ++reader->js_used;
996 json_skip_white(reader);
997 top_item->jd_type = JSON_OBJECT;
998 if (cur_item != NULL)
999 cur_item = &item;
1000 break;
1001
1002 case JSON_OBJECT:
1003 if (cur_item != NULL
1004 && dict_find(top_item->jd_tv.vval.v_dict,
1005 top_item->jd_key, -1) != NULL)
1006 {
Bram Moolenaard82a47d2022-01-05 20:24:39 +00001007 semsg(_(e_duplicate_key_in_json_str), top_item->jd_key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001008 clear_tv(cur_item);
1009 retval = FAIL;
1010 goto theend;
1011 }
1012
1013 if (cur_item != NULL)
1014 {
1015 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1016
1017 clear_tv(&top_item->jd_key_tv);
1018 if (di == NULL)
1019 {
1020 clear_tv(cur_item);
1021 retval = FAIL;
1022 goto theend;
1023 }
1024 di->di_tv = *cur_item;
1025 di->di_tv.v_lock = 0;
1026 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1027 {
1028 dictitem_free(di);
1029 retval = FAIL;
1030 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001031 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001032 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001033
1034 json_skip_white(reader);
1035 p = reader->js_buf + reader->js_used;
1036 if (*p == ',')
1037 ++reader->js_used;
1038 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001039 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001040 if (*p == NUL)
1041 retval = MAYBE;
1042 else
1043 {
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001044 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001045 retval = FAIL;
1046 }
1047 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001048 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001049 top_item->jd_type = JSON_OBJECT_KEY;
1050 if (cur_item != NULL)
1051 cur_item = &top_item->jd_key_tv;
1052 break;
1053 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001054 }
1055
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001056 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001057 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001058 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001059 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001060 res->v_type = VAR_SPECIAL;
1061 res->vval.v_number = VVAL_NONE;
1062 }
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001063 semsg(_(e_json_decode_error_at_str), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001064
1065theend:
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001066 for (i = 0; i < stack.ga_len; i++)
1067 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001068 ga_clear(&stack);
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001069
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001070 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001071}
1072
1073/*
1074 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001075 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001076 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001077 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001078 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001079json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001080{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001081 int ret;
1082
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001083 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001084 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001085 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001086 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001087 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001088 {
1089 if (ret == MAYBE)
Bram Moolenaarb09feaa2022-01-02 20:20:45 +00001090 semsg(_(e_json_decode_error_at_str), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001091 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001092 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001093 json_skip_white(reader);
1094 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001095 {
Bram Moolenaar74409f62022-01-01 15:58:22 +00001096 semsg(_(e_trailing_characters_str), reader->js_buf + reader->js_used);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001097 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001098 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001099 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001100}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001101
Bram Moolenaar113e1072019-01-20 15:30:40 +01001102#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001103/*
1104 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001105 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001106 * Return FAIL for a decoding error.
1107 * Return MAYBE for an incomplete message.
1108 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001109 */
1110 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001111json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001112{
1113 int ret;
1114
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001115 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001116 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1117 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001118 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001119 json_skip_white(reader);
1120
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001121 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001122}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001123#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001124
1125/*
1126 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001127 * "options" can be JSON_JS or zero.
1128 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001129 * Return FAIL if the message has a decoding error.
1130 * Return MAYBE if the message is truncated, need to read more.
1131 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001132 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001133 * Does not advance the reader.
1134 */
1135 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001136json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001137{
1138 int used_save = reader->js_used;
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, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001145 reader->js_used = used_save;
1146 return ret;
1147}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001148
1149/*
1150 * "js_decode()" function
1151 */
1152 void
1153f_js_decode(typval_T *argvars, typval_T *rettv)
1154{
1155 js_read_T reader;
1156
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001157 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1158 return;
1159
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001160 reader.js_buf = tv_get_string(&argvars[0]);
1161 reader.js_fill = NULL;
1162 reader.js_used = 0;
1163 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
Bram Moolenaar436b5ad2021-12-31 22:49:24 +00001164 emsg(_(e_invalid_argument));
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001165}
1166
1167/*
1168 * "js_encode()" function
1169 */
1170 void
1171f_js_encode(typval_T *argvars, typval_T *rettv)
1172{
1173 rettv->v_type = VAR_STRING;
1174 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1175}
1176
1177/*
1178 * "json_decode()" function
1179 */
1180 void
1181f_json_decode(typval_T *argvars, typval_T *rettv)
1182{
1183 js_read_T reader;
1184
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001185 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1186 return;
1187
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001188 reader.js_buf = tv_get_string(&argvars[0]);
1189 reader.js_fill = NULL;
1190 reader.js_used = 0;
1191 json_decode_all(&reader, rettv, 0);
1192}
1193
1194/*
1195 * "json_encode()" function
1196 */
1197 void
1198f_json_encode(typval_T *argvars, typval_T *rettv)
1199{
1200 rettv->v_type = VAR_STRING;
1201 rettv->vval.v_string = json_encode(&argvars[0], 0);
1202}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001203#endif