blob: a8d3a090fc77f6f77f308debf5a848fadd578e46 [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
Bram Moolenaara09195f2020-05-19 22:38:59 +020023static char e_json_error[] = N_("E491: json decode error at '%s'");
24
Bram Moolenaar520e1e42016-01-23 19:46:28 +010025/*
26 * Encode "val" into a JSON format string.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020027 * The result is added to "gap"
28 * Returns FAIL on failure and makes gap->ga_data empty.
29 */
30 static int
31json_encode_gap(garray_T *gap, typval_T *val, int options)
32{
33 if (json_encode_item(gap, val, get_copyID(), options) == FAIL)
34 {
35 ga_clear(gap);
36 gap->ga_data = vim_strsave((char_u *)"");
37 return FAIL;
38 }
39 return OK;
40}
41
42/*
43 * Encode "val" into a JSON format string.
Bram Moolenaar55fab432016-02-07 16:53:13 +010044 * The result is in allocated memory.
45 * The result is empty when encoding fails.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020046 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaar520e1e42016-01-23 19:46:28 +010047 */
48 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010049json_encode(typval_T *val, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010050{
51 garray_T ga;
52
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010053 // Store bytes in the growarray.
Bram Moolenaar520e1e42016-01-23 19:46:28 +010054 ga_init2(&ga, 1, 4000);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020055 json_encode_gap(&ga, val, options);
Bram Moolenaar04af1962019-04-12 21:19:04 +020056 ga_append(&ga, NUL);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010057 return ga.ga_data;
58}
59
Bram Moolenaar113e1072019-01-20 15:30:40 +010060#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010061/*
Bram Moolenaar55fab432016-02-07 16:53:13 +010062 * Encode ["nr", "val"] into a JSON format string in allocated memory.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020063 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010064 * Returns NULL when out of memory.
65 */
66 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010067json_encode_nr_expr(int nr, typval_T *val, int options)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010068{
69 typval_T listtv;
70 typval_T nrtv;
Bram Moolenaarf1f07922016-08-26 17:58:53 +020071 garray_T ga;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010072
73 nrtv.v_type = VAR_NUMBER;
74 nrtv.vval.v_number = nr;
75 if (rettv_list_alloc(&listtv) == FAIL)
76 return NULL;
77 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
78 || list_append_tv(listtv.vval.v_list, val) == FAIL)
79 {
80 list_unref(listtv.vval.v_list);
81 return NULL;
82 }
83
Bram Moolenaarf1f07922016-08-26 17:58:53 +020084 ga_init2(&ga, 1, 4000);
85 if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL))
86 ga_append(&ga, '\n');
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010087 list_unref(listtv.vval.v_list);
Bram Moolenaar04af1962019-04-12 21:19:04 +020088 ga_append(&ga, NUL);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020089 return ga.ga_data;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010090}
Bram Moolenaar113e1072019-01-20 15:30:40 +010091#endif
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010092
Bram Moolenaar520e1e42016-01-23 19:46:28 +010093 static void
94write_string(garray_T *gap, char_u *str)
95{
96 char_u *res = str;
97 char_u numbuf[NUMBUFLEN];
98
99 if (res == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100100 ga_concat(gap, (char_u *)"\"\"");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100101 else
102 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100103#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100104 vimconv_T conv;
105 char_u *converted = NULL;
106
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100107 if (!enc_utf8)
108 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100109 // Convert the text from 'encoding' to utf-8, the JSON string is
110 // always utf-8.
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100111 conv.vc_type = CONV_NONE;
112 convert_setup(&conv, p_enc, (char_u*)"utf-8");
113 if (conv.vc_type != CONV_NONE)
114 converted = res = string_convert(&conv, res, NULL);
115 convert_setup(&conv, NULL, NULL);
116 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100117#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100118 ga_append(gap, '"');
119 while (*res != NUL)
120 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100121 int c;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100122 // always use utf-8 encoding, ignore 'encoding'
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100123 c = utf_ptr2char(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100124
125 switch (c)
126 {
127 case 0x08:
128 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
129 case 0x09:
130 ga_append(gap, '\\'); ga_append(gap, 't'); break;
131 case 0x0a:
132 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
133 case 0x0c:
134 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
135 case 0x0d:
136 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100137 case 0x22: // "
138 case 0x5c: // backslash
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100139 ga_append(gap, '\\');
140 ga_append(gap, c);
141 break;
142 default:
143 if (c >= 0x20)
144 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100145 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100146 ga_concat(gap, numbuf);
147 }
148 else
149 {
150 vim_snprintf((char *)numbuf, NUMBUFLEN,
151 "\\u%04lx", (long)c);
152 ga_concat(gap, numbuf);
153 }
154 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100155 res += utf_ptr2len(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100156 }
157 ga_append(gap, '"');
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100158#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100159 vim_free(converted);
160#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100161 }
162}
163
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100164/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100165 * Return TRUE if "key" can be used without quotes.
166 * That is when it starts with a letter and only contains letters, digits and
167 * underscore.
168 */
169 static int
170is_simple_key(char_u *key)
171{
172 char_u *p;
173
174 if (!ASCII_ISALPHA(*key))
175 return FALSE;
176 for (p = key + 1; *p != NUL; ++p)
177 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
178 return FALSE;
179 return TRUE;
180}
181
182/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100183 * Encode "val" into "gap".
184 * Return FAIL or OK.
185 */
186 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100187json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100188{
189 char_u numbuf[NUMBUFLEN];
190 char_u *res;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100191 blob_T *b;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100192 list_T *l;
193 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100194 int i;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100195
196 switch (val->v_type)
197 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100198 case VAR_BOOL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100199 switch ((long)val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100200 {
201 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
202 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100203 }
204 break;
205
206 case VAR_SPECIAL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100207 switch ((long)val->vval.v_number)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100208 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100209 case VVAL_NONE: if ((options & JSON_JS) != 0
210 && (options & JSON_NO_NONE) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100211 // empty item
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100212 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100213 // FALLTHROUGH
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100214 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
215 }
216 break;
217
218 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200219 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100220 (varnumber_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100221 ga_concat(gap, numbuf);
222 break;
223
224 case VAR_STRING:
225 res = val->vval.v_string;
226 write_string(gap, res);
227 break;
228
229 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100230 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100231 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100232 case VAR_CHANNEL:
Bram Moolenaarf18332f2021-05-07 17:55:55 +0200233 case VAR_INSTR:
Bram Moolenaara8530892021-02-08 21:53:09 +0100234 semsg(_(e_cannot_json_encode_str), vartype_name(val->v_type));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100235 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100236
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100237 case VAR_BLOB:
238 b = val->vval.v_blob;
239 if (b == NULL || b->bv_ga.ga_len == 0)
240 ga_concat(gap, (char_u *)"[]");
241 else
242 {
243 ga_append(gap, '[');
244 for (i = 0; i < b->bv_ga.ga_len; i++)
245 {
246 if (i > 0)
247 ga_concat(gap, (char_u *)",");
248 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
249 (int)blob_get(b, i));
250 ga_concat(gap, numbuf);
251 }
252 ga_append(gap, ']');
253 }
254 break;
255
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100256 case VAR_LIST:
257 l = val->vval.v_list;
258 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100259 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100260 else
261 {
262 if (l->lv_copyID == copyID)
263 ga_concat(gap, (char_u *)"[]");
264 else
265 {
266 listitem_T *li;
267
268 l->lv_copyID = copyID;
269 ga_append(gap, '[');
Bram Moolenaar7e9f3512020-05-13 22:44:22 +0200270 CHECK_LIST_MATERIALIZE(l);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100271 for (li = l->lv_first; li != NULL && !got_int; )
272 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100273 if (json_encode_item(gap, &li->li_tv, copyID,
274 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100275 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100276 if ((options & JSON_JS)
277 && li->li_next == NULL
278 && li->li_tv.v_type == VAR_SPECIAL
279 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100280 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100281 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100282 li = li->li_next;
283 if (li != NULL)
284 ga_append(gap, ',');
285 }
286 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100287 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100288 }
289 }
290 break;
291
292 case VAR_DICT:
293 d = val->vval.v_dict;
294 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100295 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100296 else
297 {
298 if (d->dv_copyID == copyID)
299 ga_concat(gap, (char_u *)"{}");
300 else
301 {
302 int first = TRUE;
303 int todo = (int)d->dv_hashtab.ht_used;
304 hashitem_T *hi;
305
306 d->dv_copyID = copyID;
307 ga_append(gap, '{');
308
309 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
310 ++hi)
311 if (!HASHITEM_EMPTY(hi))
312 {
313 --todo;
314 if (first)
315 first = FALSE;
316 else
317 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100318 if ((options & JSON_JS)
319 && is_simple_key(hi->hi_key))
320 ga_concat(gap, hi->hi_key);
321 else
322 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100323 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100324 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100325 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100326 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100327 }
328 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100329 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100330 }
331 }
332 break;
333
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100334 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100335#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100336# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100337 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100338 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100339 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100340 {
341 if (val->vval.v_float < 0.0)
342 ga_concat(gap, (char_u *)"-Infinity");
343 else
344 ga_concat(gap, (char_u *)"Infinity");
345 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100346 else
347# endif
348 {
349 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
350 val->vval.v_float);
351 ga_concat(gap, numbuf);
352 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100353 break;
354#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100355 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200356 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100357 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100358 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100359 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100360 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100361 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100362}
363
364/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100365 * When "reader" has less than NUMBUFLEN bytes available, call the fill
366 * callback to get more.
367 */
368 static void
369fill_numbuflen(js_read_T *reader)
370{
371 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
372 - reader->js_used < NUMBUFLEN)
373 {
374 if (reader->js_fill(reader))
375 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
376 }
377}
378
379/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100380 * Skip white space in "reader". All characters <= space are considered white
381 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100382 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100383 */
384 static void
385json_skip_white(js_read_T *reader)
386{
387 int c;
388
Bram Moolenaar56ead342016-02-02 18:20:08 +0100389 for (;;)
390 {
391 c = reader->js_buf[reader->js_used];
392 if (reader->js_fill != NULL && c == NUL)
393 {
394 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200395 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100396 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200397 continue;
398 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100399 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100400 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100401 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100402 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100403 }
404 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100405}
406
Bram Moolenaar56ead342016-02-02 18:20:08 +0100407 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100408json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100409{
410 garray_T ga;
411 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100412 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100413 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200414 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100415
Bram Moolenaar56ead342016-02-02 18:20:08 +0100416 if (res != NULL)
417 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100418
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100419 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100420 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100421 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // The JSON is always expected to be utf-8, thus use utf functions
423 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100424 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100425 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100426 // Not enough bytes to make a character or end of the string. Get
427 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100428 if (reader->js_fill == NULL)
429 break;
430 len = (int)(reader->js_end - p);
431 reader->js_used = (int)(p - reader->js_buf);
432 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100433 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100434 p = reader->js_buf + reader->js_used;
435 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
436 continue;
437 }
438
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100439 if (*p == '\\')
440 {
441 c = -1;
442 switch (p[1])
443 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100444 case '\\': c = '\\'; break;
445 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100446 case 'b': c = BS; break;
447 case 't': c = TAB; break;
448 case 'n': c = NL; break;
449 case 'f': c = FF; break;
450 case 'r': c = CAR; break;
451 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100452 if (reader->js_fill != NULL
453 && (int)(reader->js_end - p) < NUMBUFLEN)
454 {
455 reader->js_used = (int)(p - reader->js_buf);
456 if (reader->js_fill(reader))
457 {
458 p = reader->js_buf + reader->js_used;
459 reader->js_end = reader->js_buf
460 + STRLEN(reader->js_buf);
461 }
462 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100463 nr = 0;
464 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100465 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200466 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
467 if (len == 0)
468 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200469 if (res != NULL)
470 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200471 return FAIL;
472 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100473 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100474 if (0xd800 <= nr && nr <= 0xdfff
475 && (int)(reader->js_end - p) >= 6
476 && *p == '\\' && *(p+1) == 'u')
477 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200478 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100479
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100480 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100481 len = 0;
482 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200483 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
484 if (len == 0)
485 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200486 if (res != NULL)
487 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200488 return FAIL;
489 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100490 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
491 {
492 p += len + 2;
493 nr = (((nr - 0xd800) << 10) |
494 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
495 }
496 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100497 if (res != NULL)
498 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200499 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200500
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100501 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100502 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100503 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100504 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100505 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100506 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100507 ++p;
508 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100509 }
510 if (c > 0)
511 {
512 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100513 if (res != NULL)
514 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100515 }
516 }
517 else
518 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100519 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100520 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100521 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100522 if (ga_grow(&ga, len) == FAIL)
523 {
524 ga_clear(&ga);
525 return FAIL;
526 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100527 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
528 ga.ga_len += len;
529 }
530 p += len;
531 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100532 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100533
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100534 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100535 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100536 {
537 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100538 if (res != NULL)
539 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100540 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100541 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100542#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100543 if (!enc_utf8)
544 {
545 vimconv_T conv;
546
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100547 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100548 conv.vc_type = CONV_NONE;
549 convert_setup(&conv, (char_u*)"utf-8", p_enc);
550 if (conv.vc_type != CONV_NONE)
551 {
552 res->vval.v_string =
553 string_convert(&conv, ga.ga_data, NULL);
554 vim_free(ga.ga_data);
555 }
556 convert_setup(&conv, NULL, NULL);
557 }
558 else
559#endif
560 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100561 }
562 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100563 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100564 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100565 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100566 res->v_type = VAR_SPECIAL;
567 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100568 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100569 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100570 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100571}
572
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100573typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100574 JSON_ARRAY, // parsing items in an array
575 JSON_OBJECT_KEY, // parsing key of an object
576 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100577} json_decode_T;
578
579typedef struct {
580 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100581 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100582 typval_T jd_key_tv;
583 char_u *jd_key;
584} json_dec_item_T;
585
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100586/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100587 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100588 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100589 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100590 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100591 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100592 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100593 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100594json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100595{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100596 char_u *p;
Bram Moolenaar6d3a7212020-07-12 14:34:00 +0200597 int i;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100598 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100599 int retval;
600 garray_T stack;
601 typval_T item;
602 typval_T *cur_item;
603 json_dec_item_T *top_item;
604 char_u key_buf[NUMBUFLEN];
605
606 ga_init2(&stack, sizeof(json_dec_item_T), 100);
607 cur_item = res;
608 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100609 if (res != NULL)
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200610 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100611
Bram Moolenaar56ead342016-02-02 18:20:08 +0100612 fill_numbuflen(reader);
613 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100614 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100615 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100616 top_item = NULL;
617 if (stack.ga_len > 0)
618 {
619 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
620 json_skip_white(reader);
621 p = reader->js_buf + reader->js_used;
622 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100623 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100624 retval = MAYBE;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100625 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100626 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100627 if (top_item->jd_type == JSON_OBJECT_KEY
628 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100629 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100630 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100631 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100632 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100633 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100634 --stack.ga_len;
635 if (stack.ga_len == 0)
636 {
637 retval = OK;
638 goto theend;
639 }
640 if (cur_item != NULL)
641 cur_item = &top_item->jd_tv;
642 goto item_end;
643 }
644 }
645 }
646
647 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
648 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100649 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100650 && reader->js_buf[reader->js_used] != '\''
651 && reader->js_buf[reader->js_used] != '['
652 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100653 {
654 char_u *key;
655
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100656 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100657 key = p = reader->js_buf + reader->js_used;
658 while (*p != NUL && *p != ':' && *p > ' ')
659 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100660 if (cur_item != NULL)
661 {
662 cur_item->v_type = VAR_STRING;
Bram Moolenaar71ccd032020-06-12 22:59:11 +0200663 cur_item->vval.v_string = vim_strnsave(key, p - key);
Bram Moolenaare2c60372017-01-22 15:56:26 +0100664 top_item->jd_key = cur_item->vval.v_string;
665 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100666 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100667 }
668 else
669 {
670 switch (*p)
671 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100672 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100673 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
674 {
675 retval = FAIL;
676 break;
677 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100678 if (ga_grow(&stack, 1) == FAIL)
679 {
680 retval = FAIL;
681 break;
682 }
683 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
684 {
685 cur_item->v_type = VAR_SPECIAL;
686 cur_item->vval.v_number = VVAL_NONE;
687 retval = FAIL;
688 break;
689 }
690
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100691 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100692 top_item = ((json_dec_item_T *)stack.ga_data)
693 + stack.ga_len;
694 top_item->jd_type = JSON_ARRAY;
695 ++stack.ga_len;
696 if (cur_item != NULL)
697 {
698 top_item->jd_tv = *cur_item;
699 cur_item = &item;
700 }
701 continue;
702
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100703 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100704 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
705 {
706 retval = FAIL;
707 break;
708 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100709 if (ga_grow(&stack, 1) == FAIL)
710 {
711 retval = FAIL;
712 break;
713 }
714 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
715 {
716 cur_item->v_type = VAR_SPECIAL;
717 cur_item->vval.v_number = VVAL_NONE;
718 retval = FAIL;
719 break;
720 }
721
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100722 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100723 top_item = ((json_dec_item_T *)stack.ga_data)
724 + stack.ga_len;
725 top_item->jd_type = JSON_OBJECT_KEY;
726 ++stack.ga_len;
727 if (cur_item != NULL)
728 {
729 top_item->jd_tv = *cur_item;
730 cur_item = &top_item->jd_key_tv;
731 }
732 continue;
733
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100734 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100735 retval = json_decode_string(reader, cur_item, *p);
736 break;
737
738 case '\'':
739 if (options & JSON_JS)
740 retval = json_decode_string(reader, cur_item, *p);
741 else
742 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200743 semsg(_(e_json_error), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100744 retval = FAIL;
745 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100746 break;
747
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100748 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100749 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100750 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200751 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100752 retval = FAIL;
753 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100754 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100755 // FALLTHROUGH
756 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100757 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100758 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100759 cur_item->v_type = VAR_SPECIAL;
760 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100761 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100762 retval = OK;
763 break;
764
765 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100766 if (VIM_ISDIGIT(*p) || (*p == '-'
767 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100768 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100769 char_u *sp = p;
770
771 if (*sp == '-')
772 {
773 ++sp;
774 if (*sp == NUL)
775 {
776 retval = MAYBE;
777 break;
778 }
779 if (!VIM_ISDIGIT(*sp))
780 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200781 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100782 retval = FAIL;
783 break;
784 }
785 }
786 sp = skipdigits(sp);
Bram Moolenaara5d59532020-01-26 21:42:03 +0100787#ifdef FEAT_FLOAT
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100788 if (*sp == '.' || *sp == 'e' || *sp == 'E')
789 {
790 if (cur_item == NULL)
791 {
792 float_T f;
793
Bram Moolenaar29500652021-08-08 15:43:34 +0200794 len = string2float(p, &f, FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100795 }
796 else
797 {
798 cur_item->v_type = VAR_FLOAT;
Bram Moolenaar29500652021-08-08 15:43:34 +0200799 len = string2float(p, &cur_item->vval.v_float,
800 FALSE);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100801 }
802 }
803 else
804#endif
805 {
806 varnumber_T nr;
807
808 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100809 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200810 &nr, NULL, 0, TRUE);
811 if (len == 0)
812 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200813 semsg(_(e_json_error), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200814 retval = FAIL;
815 goto theend;
816 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100817 if (cur_item != NULL)
818 {
819 cur_item->v_type = VAR_NUMBER;
820 cur_item->vval.v_number = nr;
821 }
822 }
823 reader->js_used += len;
824 retval = OK;
825 break;
826 }
827 if (STRNICMP((char *)p, "false", 5) == 0)
828 {
829 reader->js_used += 5;
830 if (cur_item != NULL)
831 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100832 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100833 cur_item->vval.v_number = VVAL_FALSE;
834 }
835 retval = OK;
836 break;
837 }
838 if (STRNICMP((char *)p, "true", 4) == 0)
839 {
840 reader->js_used += 4;
841 if (cur_item != NULL)
842 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100843 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100844 cur_item->vval.v_number = VVAL_TRUE;
845 }
846 retval = OK;
847 break;
848 }
849 if (STRNICMP((char *)p, "null", 4) == 0)
850 {
851 reader->js_used += 4;
852 if (cur_item != NULL)
853 {
854 cur_item->v_type = VAR_SPECIAL;
855 cur_item->vval.v_number = VVAL_NULL;
856 }
857 retval = OK;
858 break;
859 }
860#ifdef FEAT_FLOAT
861 if (STRNICMP((char *)p, "NaN", 3) == 0)
862 {
863 reader->js_used += 3;
864 if (cur_item != NULL)
865 {
866 cur_item->v_type = VAR_FLOAT;
867 cur_item->vval.v_float = NAN;
868 }
869 retval = OK;
870 break;
871 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100872 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
873 {
874 reader->js_used += 9;
875 if (cur_item != NULL)
876 {
877 cur_item->v_type = VAR_FLOAT;
878 cur_item->vval.v_float = -INFINITY;
879 }
880 retval = OK;
881 break;
882 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100883 if (STRNICMP((char *)p, "Infinity", 8) == 0)
884 {
885 reader->js_used += 8;
886 if (cur_item != NULL)
887 {
888 cur_item->v_type = VAR_FLOAT;
889 cur_item->vval.v_float = INFINITY;
890 }
891 retval = OK;
892 break;
893 }
894#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100895 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100896 len = (int)(reader->js_end
897 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100898 if (
899 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
900#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100901 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100902 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
903 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
904#endif
905 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
906 || STRNICMP((char *)p, "null", len) == 0)))
907
908 retval = MAYBE;
909 else
910 retval = FAIL;
911 break;
912 }
913
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100914 // We are finished when retval is FAIL or MAYBE and when at the
915 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100916 if (retval == FAIL)
917 break;
918 if (retval == MAYBE || stack.ga_len == 0)
919 goto theend;
920
921 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
922 && cur_item != NULL)
923 {
Bram Moolenaar3cfa5b12021-06-06 14:14:39 +0200924#ifdef FEAT_FLOAT
925 if (cur_item->v_type == VAR_FLOAT)
926 {
927 // cannot use a float as a key
928 emsg(_(e_float_as_string));
929 retval = FAIL;
930 goto theend;
931 }
932#endif
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100933 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100934 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100935 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100936 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100937 retval = FAIL;
938 goto theend;
939 }
940 }
941 }
942
943item_end:
944 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
945 switch (top_item->jd_type)
946 {
947 case JSON_ARRAY:
948 if (res != NULL)
949 {
950 listitem_T *li = listitem_alloc();
951
952 if (li == NULL)
953 {
954 clear_tv(cur_item);
955 retval = FAIL;
956 goto theend;
957 }
958 li->li_tv = *cur_item;
959 list_append(top_item->jd_tv.vval.v_list, li);
960 }
961 if (cur_item != NULL)
962 cur_item = &item;
963
964 json_skip_white(reader);
965 p = reader->js_buf + reader->js_used;
966 if (*p == ',')
967 ++reader->js_used;
968 else if (*p != ']')
969 {
970 if (*p == NUL)
971 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100972 else
973 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200974 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100975 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100976 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100977 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100978 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100979 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100980
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100981 case JSON_OBJECT_KEY:
982 json_skip_white(reader);
983 p = reader->js_buf + reader->js_used;
984 if (*p != ':')
985 {
986 if (cur_item != NULL)
987 clear_tv(cur_item);
988 if (*p == NUL)
989 retval = MAYBE;
990 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100991 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200992 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100993 retval = FAIL;
994 }
995 goto theend;
996 }
997 ++reader->js_used;
998 json_skip_white(reader);
999 top_item->jd_type = JSON_OBJECT;
1000 if (cur_item != NULL)
1001 cur_item = &item;
1002 break;
1003
1004 case JSON_OBJECT:
1005 if (cur_item != NULL
1006 && dict_find(top_item->jd_tv.vval.v_dict,
1007 top_item->jd_key, -1) != NULL)
1008 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001009 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001010 top_item->jd_key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001011 clear_tv(cur_item);
1012 retval = FAIL;
1013 goto theend;
1014 }
1015
1016 if (cur_item != NULL)
1017 {
1018 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1019
1020 clear_tv(&top_item->jd_key_tv);
1021 if (di == NULL)
1022 {
1023 clear_tv(cur_item);
1024 retval = FAIL;
1025 goto theend;
1026 }
1027 di->di_tv = *cur_item;
1028 di->di_tv.v_lock = 0;
1029 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1030 {
1031 dictitem_free(di);
1032 retval = FAIL;
1033 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001034 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001035 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001036
1037 json_skip_white(reader);
1038 p = reader->js_buf + reader->js_used;
1039 if (*p == ',')
1040 ++reader->js_used;
1041 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001042 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001043 if (*p == NUL)
1044 retval = MAYBE;
1045 else
1046 {
Bram Moolenaara09195f2020-05-19 22:38:59 +02001047 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001048 retval = FAIL;
1049 }
1050 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001051 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001052 top_item->jd_type = JSON_OBJECT_KEY;
1053 if (cur_item != NULL)
1054 cur_item = &top_item->jd_key_tv;
1055 break;
1056 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001057 }
1058
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001059 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001060 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001061 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001062 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001063 res->v_type = VAR_SPECIAL;
1064 res->vval.v_number = VVAL_NONE;
1065 }
Bram Moolenaara09195f2020-05-19 22:38:59 +02001066 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001067
1068theend:
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001069 for (i = 0; i < stack.ga_len; i++)
1070 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001071 ga_clear(&stack);
Bram Moolenaar6d3a7212020-07-12 14:34:00 +02001072
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001073 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001074}
1075
1076/*
1077 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001078 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001079 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001080 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001081 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001082json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001083{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001084 int ret;
1085
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001086 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001087 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001088 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001089 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001090 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001091 {
1092 if (ret == MAYBE)
Bram Moolenaara09195f2020-05-19 22:38:59 +02001093 semsg(_(e_json_error), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001094 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001095 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001096 json_skip_white(reader);
1097 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001098 {
Bram Moolenaar2d06bfd2020-07-23 17:16:18 +02001099 semsg(_(e_trailing_arg), reader->js_buf + reader->js_used);
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001100 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001101 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001102 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001103}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001104
Bram Moolenaar113e1072019-01-20 15:30:40 +01001105#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001106/*
1107 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001108 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001109 * Return FAIL for a decoding error.
1110 * Return MAYBE for an incomplete message.
1111 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001112 */
1113 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001114json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001115{
1116 int ret;
1117
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001118 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001119 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1120 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001121 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001122 json_skip_white(reader);
1123
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001124 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001125}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001126#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001127
1128/*
1129 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001130 * "options" can be JSON_JS or zero.
1131 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001132 * Return FAIL if the message has a decoding error.
1133 * Return MAYBE if the message is truncated, need to read more.
1134 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001135 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001136 * Does not advance the reader.
1137 */
1138 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001139json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001140{
1141 int used_save = reader->js_used;
1142 int ret;
1143
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001144 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001145 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1146 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001147 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001148 reader->js_used = used_save;
1149 return ret;
1150}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001151
1152/*
1153 * "js_decode()" function
1154 */
1155 void
1156f_js_decode(typval_T *argvars, typval_T *rettv)
1157{
1158 js_read_T reader;
1159
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001160 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1161 return;
1162
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001163 reader.js_buf = tv_get_string(&argvars[0]);
1164 reader.js_fill = NULL;
1165 reader.js_used = 0;
1166 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
1167 emsg(_(e_invarg));
1168}
1169
1170/*
1171 * "js_encode()" function
1172 */
1173 void
1174f_js_encode(typval_T *argvars, typval_T *rettv)
1175{
1176 rettv->v_type = VAR_STRING;
1177 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1178}
1179
1180/*
1181 * "json_decode()" function
1182 */
1183 void
1184f_json_decode(typval_T *argvars, typval_T *rettv)
1185{
1186 js_read_T reader;
1187
Yegappan Lakshmanan4490ec42021-07-27 22:00:44 +02001188 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
1189 return;
1190
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001191 reader.js_buf = tv_get_string(&argvars[0]);
1192 reader.js_fill = NULL;
1193 reader.js_used = 0;
1194 json_decode_all(&reader, rettv, 0);
1195}
1196
1197/*
1198 * "json_encode()" function
1199 */
1200 void
1201f_json_encode(typval_T *argvars, typval_T *rettv)
1202{
1203 rettv->v_type = VAR_STRING;
1204 rettv->vval.v_string = json_encode(&argvars[0], 0);
1205}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001206#endif