blob: c1c6e85638846eedcb54fe2d1428b1c1836120e8 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * json.c: Encoding and decoding JSON.
12 *
Bram Moolenaar009d84a2016-01-28 14:12:00 +010013 * Follows this standard: https://tools.ietf.org/html/rfc7159.html
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010015#define USING_FLOAT_STUFF
Bram Moolenaar520e1e42016-01-23 19:46:28 +010016
17#include "vim.h"
18
19#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010020
Bram Moolenaar595e64e2016-02-07 19:19:53 +010021static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022
23/*
24 * Encode "val" into a JSON format string.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020025 * The result is added to "gap"
26 * Returns FAIL on failure and makes gap->ga_data empty.
27 */
28 static int
29json_encode_gap(garray_T *gap, typval_T *val, int options)
30{
31 if (json_encode_item(gap, val, get_copyID(), options) == FAIL)
32 {
33 ga_clear(gap);
34 gap->ga_data = vim_strsave((char_u *)"");
35 return FAIL;
36 }
37 return OK;
38}
39
40/*
41 * Encode "val" into a JSON format string.
Bram Moolenaar55fab432016-02-07 16:53:13 +010042 * The result is in allocated memory.
43 * The result is empty when encoding fails.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020044 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaar520e1e42016-01-23 19:46:28 +010045 */
46 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010047json_encode(typval_T *val, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010048{
49 garray_T ga;
50
Bram Moolenaar4ba37b52019-12-04 21:57:43 +010051 // Store bytes in the growarray.
Bram Moolenaar520e1e42016-01-23 19:46:28 +010052 ga_init2(&ga, 1, 4000);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020053 json_encode_gap(&ga, val, options);
Bram Moolenaar04af1962019-04-12 21:19:04 +020054 ga_append(&ga, NUL);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010055 return ga.ga_data;
56}
57
Bram Moolenaar113e1072019-01-20 15:30:40 +010058#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010059/*
Bram Moolenaar55fab432016-02-07 16:53:13 +010060 * Encode ["nr", "val"] into a JSON format string in allocated memory.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020061 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010062 * Returns NULL when out of memory.
63 */
64 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010065json_encode_nr_expr(int nr, typval_T *val, int options)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010066{
67 typval_T listtv;
68 typval_T nrtv;
Bram Moolenaarf1f07922016-08-26 17:58:53 +020069 garray_T ga;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010070
71 nrtv.v_type = VAR_NUMBER;
72 nrtv.vval.v_number = nr;
73 if (rettv_list_alloc(&listtv) == FAIL)
74 return NULL;
75 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
76 || list_append_tv(listtv.vval.v_list, val) == FAIL)
77 {
78 list_unref(listtv.vval.v_list);
79 return NULL;
80 }
81
Bram Moolenaarf1f07922016-08-26 17:58:53 +020082 ga_init2(&ga, 1, 4000);
83 if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL))
84 ga_append(&ga, '\n');
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010085 list_unref(listtv.vval.v_list);
Bram Moolenaar04af1962019-04-12 21:19:04 +020086 ga_append(&ga, NUL);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020087 return ga.ga_data;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010088}
Bram Moolenaar113e1072019-01-20 15:30:40 +010089#endif
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010090
Bram Moolenaar520e1e42016-01-23 19:46:28 +010091 static void
92write_string(garray_T *gap, char_u *str)
93{
94 char_u *res = str;
95 char_u numbuf[NUMBUFLEN];
96
97 if (res == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +010098 ga_concat(gap, (char_u *)"\"\"");
Bram Moolenaar520e1e42016-01-23 19:46:28 +010099 else
100 {
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100101#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100102 vimconv_T conv;
103 char_u *converted = NULL;
104
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100105 if (!enc_utf8)
106 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100107 // Convert the text from 'encoding' to utf-8, the JSON string is
108 // always utf-8.
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100109 conv.vc_type = CONV_NONE;
110 convert_setup(&conv, p_enc, (char_u*)"utf-8");
111 if (conv.vc_type != CONV_NONE)
112 converted = res = string_convert(&conv, res, NULL);
113 convert_setup(&conv, NULL, NULL);
114 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100115#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100116 ga_append(gap, '"');
117 while (*res != NUL)
118 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100119 int c;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100120 // always use utf-8 encoding, ignore 'encoding'
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100121 c = utf_ptr2char(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100122
123 switch (c)
124 {
125 case 0x08:
126 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
127 case 0x09:
128 ga_append(gap, '\\'); ga_append(gap, 't'); break;
129 case 0x0a:
130 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
131 case 0x0c:
132 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
133 case 0x0d:
134 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100135 case 0x22: // "
136 case 0x5c: // backslash
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100137 ga_append(gap, '\\');
138 ga_append(gap, c);
139 break;
140 default:
141 if (c >= 0x20)
142 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100143 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100144 ga_concat(gap, numbuf);
145 }
146 else
147 {
148 vim_snprintf((char *)numbuf, NUMBUFLEN,
149 "\\u%04lx", (long)c);
150 ga_concat(gap, numbuf);
151 }
152 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100153 res += utf_ptr2len(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100154 }
155 ga_append(gap, '"');
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100156#if defined(USE_ICONV)
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100157 vim_free(converted);
158#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100159 }
160}
161
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100162/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100163 * Return TRUE if "key" can be used without quotes.
164 * That is when it starts with a letter and only contains letters, digits and
165 * underscore.
166 */
167 static int
168is_simple_key(char_u *key)
169{
170 char_u *p;
171
172 if (!ASCII_ISALPHA(*key))
173 return FALSE;
174 for (p = key + 1; *p != NUL; ++p)
175 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
176 return FALSE;
177 return TRUE;
178}
179
180/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100181 * Encode "val" into "gap".
182 * Return FAIL or OK.
183 */
184 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100185json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100186{
187 char_u numbuf[NUMBUFLEN];
188 char_u *res;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100189 blob_T *b;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100190 list_T *l;
191 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100192 int i;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100193
194 switch (val->v_type)
195 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100196 case VAR_BOOL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100197 switch ((long)val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100198 {
199 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
200 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100201 }
202 break;
203
204 case VAR_SPECIAL:
Bram Moolenaarc593bec2020-02-25 21:26:49 +0100205 switch ((long)val->vval.v_number)
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100206 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100207 case VVAL_NONE: if ((options & JSON_JS) != 0
208 && (options & JSON_NO_NONE) == 0)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100209 // empty item
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100210 break;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100211 // FALLTHROUGH
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100212 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
213 }
214 break;
215
216 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200217 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaarf9706e92020-02-22 14:27:04 +0100218 (varnumber_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100219 ga_concat(gap, numbuf);
220 break;
221
222 case VAR_STRING:
223 res = val->vval.v_string;
224 write_string(gap, res);
225 break;
226
227 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100228 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100229 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100230 case VAR_CHANNEL:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100231 // no JSON equivalent TODO: better error
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100232 emsg(_(e_invarg));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100233 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100234
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100235 case VAR_BLOB:
236 b = val->vval.v_blob;
237 if (b == NULL || b->bv_ga.ga_len == 0)
238 ga_concat(gap, (char_u *)"[]");
239 else
240 {
241 ga_append(gap, '[');
242 for (i = 0; i < b->bv_ga.ga_len; i++)
243 {
244 if (i > 0)
245 ga_concat(gap, (char_u *)",");
246 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
247 (int)blob_get(b, i));
248 ga_concat(gap, numbuf);
249 }
250 ga_append(gap, ']');
251 }
252 break;
253
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100254 case VAR_LIST:
255 l = val->vval.v_list;
256 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100257 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100258 else
259 {
260 if (l->lv_copyID == copyID)
261 ga_concat(gap, (char_u *)"[]");
262 else
263 {
264 listitem_T *li;
265
266 l->lv_copyID = copyID;
267 ga_append(gap, '[');
Bram Moolenaar50985eb2020-01-27 22:09:39 +0100268 range_list_materialize(l);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100269 for (li = l->lv_first; li != NULL && !got_int; )
270 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100271 if (json_encode_item(gap, &li->li_tv, copyID,
272 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100273 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100274 if ((options & JSON_JS)
275 && li->li_next == NULL
276 && li->li_tv.v_type == VAR_SPECIAL
277 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100278 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100279 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100280 li = li->li_next;
281 if (li != NULL)
282 ga_append(gap, ',');
283 }
284 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100285 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100286 }
287 }
288 break;
289
290 case VAR_DICT:
291 d = val->vval.v_dict;
292 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100293 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100294 else
295 {
296 if (d->dv_copyID == copyID)
297 ga_concat(gap, (char_u *)"{}");
298 else
299 {
300 int first = TRUE;
301 int todo = (int)d->dv_hashtab.ht_used;
302 hashitem_T *hi;
303
304 d->dv_copyID = copyID;
305 ga_append(gap, '{');
306
307 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
308 ++hi)
309 if (!HASHITEM_EMPTY(hi))
310 {
311 --todo;
312 if (first)
313 first = FALSE;
314 else
315 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100316 if ((options & JSON_JS)
317 && is_simple_key(hi->hi_key))
318 ga_concat(gap, hi->hi_key);
319 else
320 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100321 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100322 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100323 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100324 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100325 }
326 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100327 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100328 }
329 }
330 break;
331
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100332 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100333#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100334# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100335 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100336 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100337 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100338 {
339 if (val->vval.v_float < 0.0)
340 ga_concat(gap, (char_u *)"-Infinity");
341 else
342 ga_concat(gap, (char_u *)"Infinity");
343 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100344 else
345# endif
346 {
347 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
348 val->vval.v_float);
349 ga_concat(gap, numbuf);
350 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100351 break;
352#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100353 case VAR_UNKNOWN:
Bram Moolenaar4c683752020-04-05 21:38:23 +0200354 case VAR_ANY:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100355 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100356 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100357 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100358 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100359 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100360}
361
362/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100363 * When "reader" has less than NUMBUFLEN bytes available, call the fill
364 * callback to get more.
365 */
366 static void
367fill_numbuflen(js_read_T *reader)
368{
369 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
370 - reader->js_used < NUMBUFLEN)
371 {
372 if (reader->js_fill(reader))
373 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
374 }
375}
376
377/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100378 * Skip white space in "reader". All characters <= space are considered white
379 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100380 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100381 */
382 static void
383json_skip_white(js_read_T *reader)
384{
385 int c;
386
Bram Moolenaar56ead342016-02-02 18:20:08 +0100387 for (;;)
388 {
389 c = reader->js_buf[reader->js_used];
390 if (reader->js_fill != NULL && c == NUL)
391 {
392 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200393 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100394 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200395 continue;
396 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100397 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100398 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100399 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100400 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100401 }
402 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100403}
404
Bram Moolenaar56ead342016-02-02 18:20:08 +0100405 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100406json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100407{
408 garray_T ga;
409 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100410 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100411 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200412 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100413
Bram Moolenaar56ead342016-02-02 18:20:08 +0100414 if (res != NULL)
415 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100416
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100417 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100418 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100419 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100420 // The JSON is always expected to be utf-8, thus use utf functions
421 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100422 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100423 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100424 // Not enough bytes to make a character or end of the string. Get
425 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100426 if (reader->js_fill == NULL)
427 break;
428 len = (int)(reader->js_end - p);
429 reader->js_used = (int)(p - reader->js_buf);
430 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100431 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100432 p = reader->js_buf + reader->js_used;
433 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
434 continue;
435 }
436
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100437 if (*p == '\\')
438 {
439 c = -1;
440 switch (p[1])
441 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100442 case '\\': c = '\\'; break;
443 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100444 case 'b': c = BS; break;
445 case 't': c = TAB; break;
446 case 'n': c = NL; break;
447 case 'f': c = FF; break;
448 case 'r': c = CAR; break;
449 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100450 if (reader->js_fill != NULL
451 && (int)(reader->js_end - p) < NUMBUFLEN)
452 {
453 reader->js_used = (int)(p - reader->js_buf);
454 if (reader->js_fill(reader))
455 {
456 p = reader->js_buf + reader->js_used;
457 reader->js_end = reader->js_buf
458 + STRLEN(reader->js_buf);
459 }
460 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100461 nr = 0;
462 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100463 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200464 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
465 if (len == 0)
466 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200467 if (res != NULL)
468 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200469 return FAIL;
470 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100471 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100472 if (0xd800 <= nr && nr <= 0xdfff
473 && (int)(reader->js_end - p) >= 6
474 && *p == '\\' && *(p+1) == 'u')
475 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200476 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100477
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100478 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100479 len = 0;
480 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200481 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
482 if (len == 0)
483 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200484 if (res != NULL)
485 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200486 return FAIL;
487 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100488 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
489 {
490 p += len + 2;
491 nr = (((nr - 0xd800) << 10) |
492 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
493 }
494 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100495 if (res != NULL)
496 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200497 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200498
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100499 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100500 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100501 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100502 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100503 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100504 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100505 ++p;
506 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100507 }
508 if (c > 0)
509 {
510 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100511 if (res != NULL)
512 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100513 }
514 }
515 else
516 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100517 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100518 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100519 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100520 if (ga_grow(&ga, len) == FAIL)
521 {
522 ga_clear(&ga);
523 return FAIL;
524 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100525 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
526 ga.ga_len += len;
527 }
528 p += len;
529 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100530 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100531
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100532 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100533 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100534 {
535 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100536 if (res != NULL)
537 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100538 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100539 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100540#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100541 if (!enc_utf8)
542 {
543 vimconv_T conv;
544
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100545 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100546 conv.vc_type = CONV_NONE;
547 convert_setup(&conv, (char_u*)"utf-8", p_enc);
548 if (conv.vc_type != CONV_NONE)
549 {
550 res->vval.v_string =
551 string_convert(&conv, ga.ga_data, NULL);
552 vim_free(ga.ga_data);
553 }
554 convert_setup(&conv, NULL, NULL);
555 }
556 else
557#endif
558 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100559 }
560 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100561 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100562 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100563 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100564 res->v_type = VAR_SPECIAL;
565 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100566 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100567 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100568 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100569}
570
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100571typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100572 JSON_ARRAY, // parsing items in an array
573 JSON_OBJECT_KEY, // parsing key of an object
574 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100575} json_decode_T;
576
577typedef struct {
578 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100579 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100580 typval_T jd_key_tv;
581 char_u *jd_key;
582} json_dec_item_T;
583
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100584/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100585 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100586 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100587 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100588 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100589 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100590 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100591 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100592json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100593{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100594 char_u *p;
595 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100596 int retval;
597 garray_T stack;
598 typval_T item;
599 typval_T *cur_item;
600 json_dec_item_T *top_item;
601 char_u key_buf[NUMBUFLEN];
602
603 ga_init2(&stack, sizeof(json_dec_item_T), 100);
604 cur_item = res;
605 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100606 if (res != NULL)
607 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100608
Bram Moolenaar56ead342016-02-02 18:20:08 +0100609 fill_numbuflen(reader);
610 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100611 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100612 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100613 top_item = NULL;
614 if (stack.ga_len > 0)
615 {
616 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
617 json_skip_white(reader);
618 p = reader->js_buf + reader->js_used;
619 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100620 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100621 retval = MAYBE;
622 if (top_item->jd_type == JSON_OBJECT)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100623 // did get the key, clear it
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100624 clear_tv(&top_item->jd_key_tv);
625 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;
663 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
664 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 Moolenaarf9e3e092019-01-13 23:38:42 +0100743 emsg(_(e_invarg));
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 Moolenaarf9e3e092019-01-13 23:38:42 +0100751 emsg(_(e_invarg));
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 Moolenaarf9e3e092019-01-13 23:38:42 +0100781 emsg(_(e_invarg));
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
794 len = string2float(p, &f);
795 }
796 else
797 {
798 cur_item->v_type = VAR_FLOAT;
799 len = string2float(p, &cur_item->vval.v_float);
800 }
801 }
802 else
803#endif
804 {
805 varnumber_T nr;
806
807 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100808 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200809 &nr, NULL, 0, TRUE);
810 if (len == 0)
811 {
812 emsg(_(e_invarg));
813 retval = FAIL;
814 goto theend;
815 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100816 if (cur_item != NULL)
817 {
818 cur_item->v_type = VAR_NUMBER;
819 cur_item->vval.v_number = nr;
820 }
821 }
822 reader->js_used += len;
823 retval = OK;
824 break;
825 }
826 if (STRNICMP((char *)p, "false", 5) == 0)
827 {
828 reader->js_used += 5;
829 if (cur_item != NULL)
830 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100831 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100832 cur_item->vval.v_number = VVAL_FALSE;
833 }
834 retval = OK;
835 break;
836 }
837 if (STRNICMP((char *)p, "true", 4) == 0)
838 {
839 reader->js_used += 4;
840 if (cur_item != NULL)
841 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100842 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100843 cur_item->vval.v_number = VVAL_TRUE;
844 }
845 retval = OK;
846 break;
847 }
848 if (STRNICMP((char *)p, "null", 4) == 0)
849 {
850 reader->js_used += 4;
851 if (cur_item != NULL)
852 {
853 cur_item->v_type = VAR_SPECIAL;
854 cur_item->vval.v_number = VVAL_NULL;
855 }
856 retval = OK;
857 break;
858 }
859#ifdef FEAT_FLOAT
860 if (STRNICMP((char *)p, "NaN", 3) == 0)
861 {
862 reader->js_used += 3;
863 if (cur_item != NULL)
864 {
865 cur_item->v_type = VAR_FLOAT;
866 cur_item->vval.v_float = NAN;
867 }
868 retval = OK;
869 break;
870 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100871 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
872 {
873 reader->js_used += 9;
874 if (cur_item != NULL)
875 {
876 cur_item->v_type = VAR_FLOAT;
877 cur_item->vval.v_float = -INFINITY;
878 }
879 retval = OK;
880 break;
881 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100882 if (STRNICMP((char *)p, "Infinity", 8) == 0)
883 {
884 reader->js_used += 8;
885 if (cur_item != NULL)
886 {
887 cur_item->v_type = VAR_FLOAT;
888 cur_item->vval.v_float = INFINITY;
889 }
890 retval = OK;
891 break;
892 }
893#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100894 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100895 len = (int)(reader->js_end
896 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100897 if (
898 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
899#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100900 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100901 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
902 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
903#endif
904 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
905 || STRNICMP((char *)p, "null", len) == 0)))
906
907 retval = MAYBE;
908 else
909 retval = FAIL;
910 break;
911 }
912
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100913 // We are finished when retval is FAIL or MAYBE and when at the
914 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100915 if (retval == FAIL)
916 break;
917 if (retval == MAYBE || stack.ga_len == 0)
918 goto theend;
919
920 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
921 && cur_item != NULL)
922 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100923 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100924 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100925 {
926 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100927 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100928 retval = FAIL;
929 goto theend;
930 }
931 }
932 }
933
934item_end:
935 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
936 switch (top_item->jd_type)
937 {
938 case JSON_ARRAY:
939 if (res != NULL)
940 {
941 listitem_T *li = listitem_alloc();
942
943 if (li == NULL)
944 {
945 clear_tv(cur_item);
946 retval = FAIL;
947 goto theend;
948 }
949 li->li_tv = *cur_item;
950 list_append(top_item->jd_tv.vval.v_list, li);
951 }
952 if (cur_item != NULL)
953 cur_item = &item;
954
955 json_skip_white(reader);
956 p = reader->js_buf + reader->js_used;
957 if (*p == ',')
958 ++reader->js_used;
959 else if (*p != ']')
960 {
961 if (*p == NUL)
962 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100963 else
964 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100965 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100966 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100967 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100968 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100969 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100970 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100971
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100972 case JSON_OBJECT_KEY:
973 json_skip_white(reader);
974 p = reader->js_buf + reader->js_used;
975 if (*p != ':')
976 {
977 if (cur_item != NULL)
978 clear_tv(cur_item);
979 if (*p == NUL)
980 retval = MAYBE;
981 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100982 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100983 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100984 retval = FAIL;
985 }
986 goto theend;
987 }
988 ++reader->js_used;
989 json_skip_white(reader);
990 top_item->jd_type = JSON_OBJECT;
991 if (cur_item != NULL)
992 cur_item = &item;
993 break;
994
995 case JSON_OBJECT:
996 if (cur_item != NULL
997 && dict_find(top_item->jd_tv.vval.v_dict,
998 top_item->jd_key, -1) != NULL)
999 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001000 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001001 top_item->jd_key);
1002 clear_tv(&top_item->jd_key_tv);
1003 clear_tv(cur_item);
1004 retval = FAIL;
1005 goto theend;
1006 }
1007
1008 if (cur_item != NULL)
1009 {
1010 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1011
1012 clear_tv(&top_item->jd_key_tv);
1013 if (di == NULL)
1014 {
1015 clear_tv(cur_item);
1016 retval = FAIL;
1017 goto theend;
1018 }
1019 di->di_tv = *cur_item;
1020 di->di_tv.v_lock = 0;
1021 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1022 {
1023 dictitem_free(di);
1024 retval = FAIL;
1025 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001026 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001027 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001028
1029 json_skip_white(reader);
1030 p = reader->js_buf + reader->js_used;
1031 if (*p == ',')
1032 ++reader->js_used;
1033 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001034 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001035 if (*p == NUL)
1036 retval = MAYBE;
1037 else
1038 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001039 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001040 retval = FAIL;
1041 }
1042 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001043 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001044 top_item->jd_type = JSON_OBJECT_KEY;
1045 if (cur_item != NULL)
1046 cur_item = &top_item->jd_key_tv;
1047 break;
1048 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001049 }
1050
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001051 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001052 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001053 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001054 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001055 res->v_type = VAR_SPECIAL;
1056 res->vval.v_number = VVAL_NONE;
1057 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001058 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001059
1060theend:
1061 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001062 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001063}
1064
1065/*
1066 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001067 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001068 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001069 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001070 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001071json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001072{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001073 int ret;
1074
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001075 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001076 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001077 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001078 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001079 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001080 {
1081 if (ret == MAYBE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001082 emsg(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001083 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001084 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001085 json_skip_white(reader);
1086 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001087 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001088 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001089 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001090 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001091 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001092}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001093
Bram Moolenaar113e1072019-01-20 15:30:40 +01001094#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001095/*
1096 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001097 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001098 * Return FAIL for a decoding error.
1099 * Return MAYBE for an incomplete message.
1100 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001101 */
1102 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001103json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001104{
1105 int ret;
1106
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001107 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001108 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1109 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001110 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001111 json_skip_white(reader);
1112
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001113 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001114}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001115#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001116
1117/*
1118 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001119 * "options" can be JSON_JS or zero.
1120 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001121 * Return FAIL if the message has a decoding error.
1122 * Return MAYBE if the message is truncated, need to read more.
1123 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001124 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001125 * Does not advance the reader.
1126 */
1127 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001128json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001129{
1130 int used_save = reader->js_used;
1131 int ret;
1132
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001133 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001134 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1135 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001136 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001137 reader->js_used = used_save;
1138 return ret;
1139}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001140
1141/*
1142 * "js_decode()" function
1143 */
1144 void
1145f_js_decode(typval_T *argvars, typval_T *rettv)
1146{
1147 js_read_T reader;
1148
1149 reader.js_buf = tv_get_string(&argvars[0]);
1150 reader.js_fill = NULL;
1151 reader.js_used = 0;
1152 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
1153 emsg(_(e_invarg));
1154}
1155
1156/*
1157 * "js_encode()" function
1158 */
1159 void
1160f_js_encode(typval_T *argvars, typval_T *rettv)
1161{
1162 rettv->v_type = VAR_STRING;
1163 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1164}
1165
1166/*
1167 * "json_decode()" function
1168 */
1169 void
1170f_json_decode(typval_T *argvars, typval_T *rettv)
1171{
1172 js_read_T reader;
1173
1174 reader.js_buf = tv_get_string(&argvars[0]);
1175 reader.js_fill = NULL;
1176 reader.js_used = 0;
1177 json_decode_all(&reader, rettv, 0);
1178}
1179
1180/*
1181 * "json_encode()" function
1182 */
1183 void
1184f_json_encode(typval_T *argvars, typval_T *rettv)
1185{
1186 rettv->v_type = VAR_STRING;
1187 rettv->vval.v_string = json_encode(&argvars[0], 0);
1188}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001189#endif