blob: 2720abfca49990a94c2746e112b63b71792ca249 [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 Moolenaar4ba37b52019-12-04 21:57:43 +0100233 // no JSON equivalent TODO: better error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100234 emsg(_(e_invarg));
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;
597 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100598 int retval;
599 garray_T stack;
600 typval_T item;
601 typval_T *cur_item;
602 json_dec_item_T *top_item;
603 char_u key_buf[NUMBUFLEN];
604
605 ga_init2(&stack, sizeof(json_dec_item_T), 100);
606 cur_item = res;
607 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100608 if (res != NULL)
609 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100610
Bram Moolenaar56ead342016-02-02 18:20:08 +0100611 fill_numbuflen(reader);
612 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100613 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100614 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100615 top_item = NULL;
616 if (stack.ga_len > 0)
617 {
618 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
619 json_skip_white(reader);
620 p = reader->js_buf + reader->js_used;
621 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100622 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100623 retval = MAYBE;
624 if (top_item->jd_type == JSON_OBJECT)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100625 // did get the key, clear it
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100626 clear_tv(&top_item->jd_key_tv);
627 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100628 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100629 if (top_item->jd_type == JSON_OBJECT_KEY
630 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100631 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100632 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100633 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100634 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100635 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100636 --stack.ga_len;
637 if (stack.ga_len == 0)
638 {
639 retval = OK;
640 goto theend;
641 }
642 if (cur_item != NULL)
643 cur_item = &top_item->jd_tv;
644 goto item_end;
645 }
646 }
647 }
648
649 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
650 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100651 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100652 && reader->js_buf[reader->js_used] != '\''
653 && reader->js_buf[reader->js_used] != '['
654 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100655 {
656 char_u *key;
657
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100658 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100659 key = p = reader->js_buf + reader->js_used;
660 while (*p != NUL && *p != ':' && *p > ' ')
661 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100662 if (cur_item != NULL)
663 {
664 cur_item->v_type = VAR_STRING;
665 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
666 top_item->jd_key = cur_item->vval.v_string;
667 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100668 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100669 }
670 else
671 {
672 switch (*p)
673 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100674 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100675 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
676 {
677 retval = FAIL;
678 break;
679 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100680 if (ga_grow(&stack, 1) == FAIL)
681 {
682 retval = FAIL;
683 break;
684 }
685 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
686 {
687 cur_item->v_type = VAR_SPECIAL;
688 cur_item->vval.v_number = VVAL_NONE;
689 retval = FAIL;
690 break;
691 }
692
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100693 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100694 top_item = ((json_dec_item_T *)stack.ga_data)
695 + stack.ga_len;
696 top_item->jd_type = JSON_ARRAY;
697 ++stack.ga_len;
698 if (cur_item != NULL)
699 {
700 top_item->jd_tv = *cur_item;
701 cur_item = &item;
702 }
703 continue;
704
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100705 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100706 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
707 {
708 retval = FAIL;
709 break;
710 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100711 if (ga_grow(&stack, 1) == FAIL)
712 {
713 retval = FAIL;
714 break;
715 }
716 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
717 {
718 cur_item->v_type = VAR_SPECIAL;
719 cur_item->vval.v_number = VVAL_NONE;
720 retval = FAIL;
721 break;
722 }
723
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100724 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100725 top_item = ((json_dec_item_T *)stack.ga_data)
726 + stack.ga_len;
727 top_item->jd_type = JSON_OBJECT_KEY;
728 ++stack.ga_len;
729 if (cur_item != NULL)
730 {
731 top_item->jd_tv = *cur_item;
732 cur_item = &top_item->jd_key_tv;
733 }
734 continue;
735
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100736 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100737 retval = json_decode_string(reader, cur_item, *p);
738 break;
739
740 case '\'':
741 if (options & JSON_JS)
742 retval = json_decode_string(reader, cur_item, *p);
743 else
744 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200745 semsg(_(e_json_error), p);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100746 retval = FAIL;
747 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100748 break;
749
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100750 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100751 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100752 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200753 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100754 retval = FAIL;
755 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100756 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100757 // FALLTHROUGH
758 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100759 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100760 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100761 cur_item->v_type = VAR_SPECIAL;
762 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100763 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100764 retval = OK;
765 break;
766
767 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100768 if (VIM_ISDIGIT(*p) || (*p == '-'
769 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100770 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100771 char_u *sp = p;
772
773 if (*sp == '-')
774 {
775 ++sp;
776 if (*sp == NUL)
777 {
778 retval = MAYBE;
779 break;
780 }
781 if (!VIM_ISDIGIT(*sp))
782 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200783 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100784 retval = FAIL;
785 break;
786 }
787 }
788 sp = skipdigits(sp);
Bram Moolenaara5d59532020-01-26 21:42:03 +0100789#ifdef FEAT_FLOAT
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100790 if (*sp == '.' || *sp == 'e' || *sp == 'E')
791 {
792 if (cur_item == NULL)
793 {
794 float_T f;
795
796 len = string2float(p, &f);
797 }
798 else
799 {
800 cur_item->v_type = VAR_FLOAT;
801 len = string2float(p, &cur_item->vval.v_float);
802 }
803 }
804 else
805#endif
806 {
807 varnumber_T nr;
808
809 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100810 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200811 &nr, NULL, 0, TRUE);
812 if (len == 0)
813 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200814 semsg(_(e_json_error), p);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200815 retval = FAIL;
816 goto theend;
817 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100818 if (cur_item != NULL)
819 {
820 cur_item->v_type = VAR_NUMBER;
821 cur_item->vval.v_number = nr;
822 }
823 }
824 reader->js_used += len;
825 retval = OK;
826 break;
827 }
828 if (STRNICMP((char *)p, "false", 5) == 0)
829 {
830 reader->js_used += 5;
831 if (cur_item != NULL)
832 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100833 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100834 cur_item->vval.v_number = VVAL_FALSE;
835 }
836 retval = OK;
837 break;
838 }
839 if (STRNICMP((char *)p, "true", 4) == 0)
840 {
841 reader->js_used += 4;
842 if (cur_item != NULL)
843 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100844 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100845 cur_item->vval.v_number = VVAL_TRUE;
846 }
847 retval = OK;
848 break;
849 }
850 if (STRNICMP((char *)p, "null", 4) == 0)
851 {
852 reader->js_used += 4;
853 if (cur_item != NULL)
854 {
855 cur_item->v_type = VAR_SPECIAL;
856 cur_item->vval.v_number = VVAL_NULL;
857 }
858 retval = OK;
859 break;
860 }
861#ifdef FEAT_FLOAT
862 if (STRNICMP((char *)p, "NaN", 3) == 0)
863 {
864 reader->js_used += 3;
865 if (cur_item != NULL)
866 {
867 cur_item->v_type = VAR_FLOAT;
868 cur_item->vval.v_float = NAN;
869 }
870 retval = OK;
871 break;
872 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100873 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
874 {
875 reader->js_used += 9;
876 if (cur_item != NULL)
877 {
878 cur_item->v_type = VAR_FLOAT;
879 cur_item->vval.v_float = -INFINITY;
880 }
881 retval = OK;
882 break;
883 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100884 if (STRNICMP((char *)p, "Infinity", 8) == 0)
885 {
886 reader->js_used += 8;
887 if (cur_item != NULL)
888 {
889 cur_item->v_type = VAR_FLOAT;
890 cur_item->vval.v_float = INFINITY;
891 }
892 retval = OK;
893 break;
894 }
895#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100896 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100897 len = (int)(reader->js_end
898 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100899 if (
900 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
901#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100902 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100903 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
904 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
905#endif
906 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
907 || STRNICMP((char *)p, "null", len) == 0)))
908
909 retval = MAYBE;
910 else
911 retval = FAIL;
912 break;
913 }
914
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100915 // We are finished when retval is FAIL or MAYBE and when at the
916 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100917 if (retval == FAIL)
918 break;
919 if (retval == MAYBE || stack.ga_len == 0)
920 goto theend;
921
922 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
923 && cur_item != NULL)
924 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100925 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100926 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100927 {
928 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100929 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100930 retval = FAIL;
931 goto theend;
932 }
933 }
934 }
935
936item_end:
937 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
938 switch (top_item->jd_type)
939 {
940 case JSON_ARRAY:
941 if (res != NULL)
942 {
943 listitem_T *li = listitem_alloc();
944
945 if (li == NULL)
946 {
947 clear_tv(cur_item);
948 retval = FAIL;
949 goto theend;
950 }
951 li->li_tv = *cur_item;
952 list_append(top_item->jd_tv.vval.v_list, li);
953 }
954 if (cur_item != NULL)
955 cur_item = &item;
956
957 json_skip_white(reader);
958 p = reader->js_buf + reader->js_used;
959 if (*p == ',')
960 ++reader->js_used;
961 else if (*p != ']')
962 {
963 if (*p == NUL)
964 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100965 else
966 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200967 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100968 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100969 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100970 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100971 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100972 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100973
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100974 case JSON_OBJECT_KEY:
975 json_skip_white(reader);
976 p = reader->js_buf + reader->js_used;
977 if (*p != ':')
978 {
979 if (cur_item != NULL)
980 clear_tv(cur_item);
981 if (*p == NUL)
982 retval = MAYBE;
983 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100984 {
Bram Moolenaara09195f2020-05-19 22:38:59 +0200985 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100986 retval = FAIL;
987 }
988 goto theend;
989 }
990 ++reader->js_used;
991 json_skip_white(reader);
992 top_item->jd_type = JSON_OBJECT;
993 if (cur_item != NULL)
994 cur_item = &item;
995 break;
996
997 case JSON_OBJECT:
998 if (cur_item != NULL
999 && dict_find(top_item->jd_tv.vval.v_dict,
1000 top_item->jd_key, -1) != NULL)
1001 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001002 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001003 top_item->jd_key);
1004 clear_tv(&top_item->jd_key_tv);
1005 clear_tv(cur_item);
1006 retval = FAIL;
1007 goto theend;
1008 }
1009
1010 if (cur_item != NULL)
1011 {
1012 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1013
1014 clear_tv(&top_item->jd_key_tv);
1015 if (di == NULL)
1016 {
1017 clear_tv(cur_item);
1018 retval = FAIL;
1019 goto theend;
1020 }
1021 di->di_tv = *cur_item;
1022 di->di_tv.v_lock = 0;
1023 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1024 {
1025 dictitem_free(di);
1026 retval = FAIL;
1027 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001028 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001029 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001030
1031 json_skip_white(reader);
1032 p = reader->js_buf + reader->js_used;
1033 if (*p == ',')
1034 ++reader->js_used;
1035 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001036 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001037 if (*p == NUL)
1038 retval = MAYBE;
1039 else
1040 {
Bram Moolenaara09195f2020-05-19 22:38:59 +02001041 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001042 retval = FAIL;
1043 }
1044 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001045 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001046 top_item->jd_type = JSON_OBJECT_KEY;
1047 if (cur_item != NULL)
1048 cur_item = &top_item->jd_key_tv;
1049 break;
1050 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001051 }
1052
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001053 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001054 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001055 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001056 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001057 res->v_type = VAR_SPECIAL;
1058 res->vval.v_number = VVAL_NONE;
1059 }
Bram Moolenaara09195f2020-05-19 22:38:59 +02001060 semsg(_(e_json_error), p);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001061
1062theend:
1063 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001064 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001065}
1066
1067/*
1068 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001069 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001070 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001071 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001072 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001073json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001074{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001075 int ret;
1076
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001077 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001078 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001079 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001080 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001081 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001082 {
1083 if (ret == MAYBE)
Bram Moolenaara09195f2020-05-19 22:38:59 +02001084 semsg(_(e_json_error), reader->js_buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001085 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001086 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001087 json_skip_white(reader);
1088 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001089 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001090 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001091 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001092 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001093 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001094}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001095
Bram Moolenaar113e1072019-01-20 15:30:40 +01001096#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001097/*
1098 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001099 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001100 * Return FAIL for a decoding error.
1101 * Return MAYBE for an incomplete message.
1102 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001103 */
1104 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001105json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001106{
1107 int ret;
1108
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001109 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001110 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1111 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001112 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001113 json_skip_white(reader);
1114
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001115 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001116}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001117#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001118
1119/*
1120 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001121 * "options" can be JSON_JS or zero.
1122 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001123 * Return FAIL if the message has a decoding error.
1124 * Return MAYBE if the message is truncated, need to read more.
1125 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001126 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001127 * Does not advance the reader.
1128 */
1129 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001130json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001131{
1132 int used_save = reader->js_used;
1133 int ret;
1134
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001135 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001136 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1137 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001138 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001139 reader->js_used = used_save;
1140 return ret;
1141}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001142
1143/*
1144 * "js_decode()" function
1145 */
1146 void
1147f_js_decode(typval_T *argvars, typval_T *rettv)
1148{
1149 js_read_T reader;
1150
1151 reader.js_buf = tv_get_string(&argvars[0]);
1152 reader.js_fill = NULL;
1153 reader.js_used = 0;
1154 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
1155 emsg(_(e_invarg));
1156}
1157
1158/*
1159 * "js_encode()" function
1160 */
1161 void
1162f_js_encode(typval_T *argvars, typval_T *rettv)
1163{
1164 rettv->v_type = VAR_STRING;
1165 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1166}
1167
1168/*
1169 * "json_decode()" function
1170 */
1171 void
1172f_json_decode(typval_T *argvars, typval_T *rettv)
1173{
1174 js_read_T reader;
1175
1176 reader.js_buf = tv_get_string(&argvars[0]);
1177 reader.js_fill = NULL;
1178 reader.js_used = 0;
1179 json_decode_all(&reader, rettv, 0);
1180}
1181
1182/*
1183 * "json_encode()" function
1184 */
1185 void
1186f_json_encode(typval_T *argvars, typval_T *rettv)
1187{
1188 rettv->v_type = VAR_STRING;
1189 rettv->vval.v_string = json_encode(&argvars[0], 0);
1190}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001191#endif