blob: 6d7b1939e0ea5535874ce43be7e7b42a616e9973 [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 Moolenaarfcaaae62016-01-24 16:49:11 +0100197 switch (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:
205 switch (val->vval.v_number)
206 {
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 Moolenaar8a7d6542020-01-26 15:56:19 +0100218 (long_long_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, '[');
268 for (li = l->lv_first; li != NULL && !got_int; )
269 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100270 if (json_encode_item(gap, &li->li_tv, copyID,
271 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100272 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100273 if ((options & JSON_JS)
274 && li->li_next == NULL
275 && li->li_tv.v_type == VAR_SPECIAL
276 && li->li_tv.vval.v_number == VVAL_NONE)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100277 // add an extra comma if the last item is v:none
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100278 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100279 li = li->li_next;
280 if (li != NULL)
281 ga_append(gap, ',');
282 }
283 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100284 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100285 }
286 }
287 break;
288
289 case VAR_DICT:
290 d = val->vval.v_dict;
291 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100292 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100293 else
294 {
295 if (d->dv_copyID == copyID)
296 ga_concat(gap, (char_u *)"{}");
297 else
298 {
299 int first = TRUE;
300 int todo = (int)d->dv_hashtab.ht_used;
301 hashitem_T *hi;
302
303 d->dv_copyID = copyID;
304 ga_append(gap, '{');
305
306 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
307 ++hi)
308 if (!HASHITEM_EMPTY(hi))
309 {
310 --todo;
311 if (first)
312 first = FALSE;
313 else
314 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100315 if ((options & JSON_JS)
316 && is_simple_key(hi->hi_key))
317 ga_concat(gap, hi->hi_key);
318 else
319 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100320 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100321 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100322 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100323 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100324 }
325 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100326 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100327 }
328 }
329 break;
330
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100331 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100332#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100333# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100334 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100335 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100336 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100337 {
338 if (val->vval.v_float < 0.0)
339 ga_concat(gap, (char_u *)"-Infinity");
340 else
341 ga_concat(gap, (char_u *)"Infinity");
342 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100343 else
344# endif
345 {
346 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
347 val->vval.v_float);
348 ga_concat(gap, numbuf);
349 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100350 break;
351#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100352 case VAR_UNKNOWN:
Bram Moolenaar8a7d6542020-01-26 15:56:19 +0100353 case VAR_VOID:
Bram Moolenaar95f09602016-11-10 20:01:45 +0100354 internal_error("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100355 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100356 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100357 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100358}
359
360/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100361 * When "reader" has less than NUMBUFLEN bytes available, call the fill
362 * callback to get more.
363 */
364 static void
365fill_numbuflen(js_read_T *reader)
366{
367 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
368 - reader->js_used < NUMBUFLEN)
369 {
370 if (reader->js_fill(reader))
371 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
372 }
373}
374
375/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100376 * Skip white space in "reader". All characters <= space are considered white
377 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100378 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100379 */
380 static void
381json_skip_white(js_read_T *reader)
382{
383 int c;
384
Bram Moolenaar56ead342016-02-02 18:20:08 +0100385 for (;;)
386 {
387 c = reader->js_buf[reader->js_used];
388 if (reader->js_fill != NULL && c == NUL)
389 {
390 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200391 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100392 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200393 continue;
394 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100395 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100396 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100397 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100398 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100399 }
400 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100401}
402
Bram Moolenaar56ead342016-02-02 18:20:08 +0100403 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100404json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100405{
406 garray_T ga;
407 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100408 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100409 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200410 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100411
Bram Moolenaar56ead342016-02-02 18:20:08 +0100412 if (res != NULL)
413 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100414
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100415 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100416 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100417 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100418 // The JSON is always expected to be utf-8, thus use utf functions
419 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100420 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100421 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100422 // Not enough bytes to make a character or end of the string. Get
423 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100424 if (reader->js_fill == NULL)
425 break;
426 len = (int)(reader->js_end - p);
427 reader->js_used = (int)(p - reader->js_buf);
428 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100429 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100430 p = reader->js_buf + reader->js_used;
431 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
432 continue;
433 }
434
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100435 if (*p == '\\')
436 {
437 c = -1;
438 switch (p[1])
439 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100440 case '\\': c = '\\'; break;
441 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100442 case 'b': c = BS; break;
443 case 't': c = TAB; break;
444 case 'n': c = NL; break;
445 case 'f': c = FF; break;
446 case 'r': c = CAR; break;
447 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100448 if (reader->js_fill != NULL
449 && (int)(reader->js_end - p) < NUMBUFLEN)
450 {
451 reader->js_used = (int)(p - reader->js_buf);
452 if (reader->js_fill(reader))
453 {
454 p = reader->js_buf + reader->js_used;
455 reader->js_end = reader->js_buf
456 + STRLEN(reader->js_buf);
457 }
458 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100459 nr = 0;
460 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100461 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200462 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
463 if (len == 0)
464 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200465 if (res != NULL)
466 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200467 return FAIL;
468 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100469 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100470 if (0xd800 <= nr && nr <= 0xdfff
471 && (int)(reader->js_end - p) >= 6
472 && *p == '\\' && *(p+1) == 'u')
473 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200474 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100475
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100476 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100477 len = 0;
478 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200479 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
480 if (len == 0)
481 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200482 if (res != NULL)
483 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200484 return FAIL;
485 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100486 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
487 {
488 p += len + 2;
489 nr = (((nr - 0xd800) << 10) |
490 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
491 }
492 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100493 if (res != NULL)
494 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200495 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200496
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100497 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100498 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100499 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100500 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100501 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100502 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100503 ++p;
504 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100505 }
506 if (c > 0)
507 {
508 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100509 if (res != NULL)
510 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100511 }
512 }
513 else
514 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100515 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100516 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100517 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100518 if (ga_grow(&ga, len) == FAIL)
519 {
520 ga_clear(&ga);
521 return FAIL;
522 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100523 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
524 ga.ga_len += len;
525 }
526 p += len;
527 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100528 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100529
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100530 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100531 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100532 {
533 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100534 if (res != NULL)
535 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100536 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100537 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100538#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100539 if (!enc_utf8)
540 {
541 vimconv_T conv;
542
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100543 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100544 conv.vc_type = CONV_NONE;
545 convert_setup(&conv, (char_u*)"utf-8", p_enc);
546 if (conv.vc_type != CONV_NONE)
547 {
548 res->vval.v_string =
549 string_convert(&conv, ga.ga_data, NULL);
550 vim_free(ga.ga_data);
551 }
552 convert_setup(&conv, NULL, NULL);
553 }
554 else
555#endif
556 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100557 }
558 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100559 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100560 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100561 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100562 res->v_type = VAR_SPECIAL;
563 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100564 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100565 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100566 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100567}
568
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100569typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100570 JSON_ARRAY, // parsing items in an array
571 JSON_OBJECT_KEY, // parsing key of an object
572 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100573} json_decode_T;
574
575typedef struct {
576 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100577 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100578 typval_T jd_key_tv;
579 char_u *jd_key;
580} json_dec_item_T;
581
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100582/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100583 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100584 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100585 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100586 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100587 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100588 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100589 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100590json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100591{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100592 char_u *p;
593 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100594 int retval;
595 garray_T stack;
596 typval_T item;
597 typval_T *cur_item;
598 json_dec_item_T *top_item;
599 char_u key_buf[NUMBUFLEN];
600
601 ga_init2(&stack, sizeof(json_dec_item_T), 100);
602 cur_item = res;
603 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100604 if (res != NULL)
605 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100606
Bram Moolenaar56ead342016-02-02 18:20:08 +0100607 fill_numbuflen(reader);
608 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100609 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100610 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100611 top_item = NULL;
612 if (stack.ga_len > 0)
613 {
614 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
615 json_skip_white(reader);
616 p = reader->js_buf + reader->js_used;
617 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100618 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100619 retval = MAYBE;
620 if (top_item->jd_type == JSON_OBJECT)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100621 // did get the key, clear it
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100622 clear_tv(&top_item->jd_key_tv);
623 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100624 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100625 if (top_item->jd_type == JSON_OBJECT_KEY
626 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100627 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100628 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100629 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100630 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100631 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100632 --stack.ga_len;
633 if (stack.ga_len == 0)
634 {
635 retval = OK;
636 goto theend;
637 }
638 if (cur_item != NULL)
639 cur_item = &top_item->jd_tv;
640 goto item_end;
641 }
642 }
643 }
644
645 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
646 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100647 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100648 && reader->js_buf[reader->js_used] != '\''
649 && reader->js_buf[reader->js_used] != '['
650 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100651 {
652 char_u *key;
653
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100654 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100655 key = p = reader->js_buf + reader->js_used;
656 while (*p != NUL && *p != ':' && *p > ' ')
657 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100658 if (cur_item != NULL)
659 {
660 cur_item->v_type = VAR_STRING;
661 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
662 top_item->jd_key = cur_item->vval.v_string;
663 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100664 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100665 }
666 else
667 {
668 switch (*p)
669 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100670 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100671 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
672 {
673 retval = FAIL;
674 break;
675 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100676 if (ga_grow(&stack, 1) == FAIL)
677 {
678 retval = FAIL;
679 break;
680 }
681 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
682 {
683 cur_item->v_type = VAR_SPECIAL;
684 cur_item->vval.v_number = VVAL_NONE;
685 retval = FAIL;
686 break;
687 }
688
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100689 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100690 top_item = ((json_dec_item_T *)stack.ga_data)
691 + stack.ga_len;
692 top_item->jd_type = JSON_ARRAY;
693 ++stack.ga_len;
694 if (cur_item != NULL)
695 {
696 top_item->jd_tv = *cur_item;
697 cur_item = &item;
698 }
699 continue;
700
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100701 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100702 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
703 {
704 retval = FAIL;
705 break;
706 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100707 if (ga_grow(&stack, 1) == FAIL)
708 {
709 retval = FAIL;
710 break;
711 }
712 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
713 {
714 cur_item->v_type = VAR_SPECIAL;
715 cur_item->vval.v_number = VVAL_NONE;
716 retval = FAIL;
717 break;
718 }
719
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100720 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100721 top_item = ((json_dec_item_T *)stack.ga_data)
722 + stack.ga_len;
723 top_item->jd_type = JSON_OBJECT_KEY;
724 ++stack.ga_len;
725 if (cur_item != NULL)
726 {
727 top_item->jd_tv = *cur_item;
728 cur_item = &top_item->jd_key_tv;
729 }
730 continue;
731
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100732 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100733 retval = json_decode_string(reader, cur_item, *p);
734 break;
735
736 case '\'':
737 if (options & JSON_JS)
738 retval = json_decode_string(reader, cur_item, *p);
739 else
740 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100741 emsg(_(e_invarg));
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100742 retval = FAIL;
743 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100744 break;
745
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100746 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100747 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100748 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100749 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100750 retval = FAIL;
751 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100752 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100753 // FALLTHROUGH
754 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100755 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100756 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100757 cur_item->v_type = VAR_SPECIAL;
758 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100759 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100760 retval = OK;
761 break;
762
763 default:
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100764 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100765 {
766#ifdef FEAT_FLOAT
767 char_u *sp = p;
768
769 if (*sp == '-')
770 {
771 ++sp;
772 if (*sp == NUL)
773 {
774 retval = MAYBE;
775 break;
776 }
777 if (!VIM_ISDIGIT(*sp))
778 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100779 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100780 retval = FAIL;
781 break;
782 }
783 }
784 sp = skipdigits(sp);
785 if (*sp == '.' || *sp == 'e' || *sp == 'E')
786 {
787 if (cur_item == NULL)
788 {
789 float_T f;
790
791 len = string2float(p, &f);
792 }
793 else
794 {
795 cur_item->v_type = VAR_FLOAT;
796 len = string2float(p, &cur_item->vval.v_float);
797 }
798 }
799 else
800#endif
801 {
802 varnumber_T nr;
803
804 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100805 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200806 &nr, NULL, 0, TRUE);
807 if (len == 0)
808 {
809 emsg(_(e_invarg));
810 retval = FAIL;
811 goto theend;
812 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100813 if (cur_item != NULL)
814 {
815 cur_item->v_type = VAR_NUMBER;
816 cur_item->vval.v_number = nr;
817 }
818 }
819 reader->js_used += len;
820 retval = OK;
821 break;
822 }
823 if (STRNICMP((char *)p, "false", 5) == 0)
824 {
825 reader->js_used += 5;
826 if (cur_item != NULL)
827 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100828 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100829 cur_item->vval.v_number = VVAL_FALSE;
830 }
831 retval = OK;
832 break;
833 }
834 if (STRNICMP((char *)p, "true", 4) == 0)
835 {
836 reader->js_used += 4;
837 if (cur_item != NULL)
838 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100839 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100840 cur_item->vval.v_number = VVAL_TRUE;
841 }
842 retval = OK;
843 break;
844 }
845 if (STRNICMP((char *)p, "null", 4) == 0)
846 {
847 reader->js_used += 4;
848 if (cur_item != NULL)
849 {
850 cur_item->v_type = VAR_SPECIAL;
851 cur_item->vval.v_number = VVAL_NULL;
852 }
853 retval = OK;
854 break;
855 }
856#ifdef FEAT_FLOAT
857 if (STRNICMP((char *)p, "NaN", 3) == 0)
858 {
859 reader->js_used += 3;
860 if (cur_item != NULL)
861 {
862 cur_item->v_type = VAR_FLOAT;
863 cur_item->vval.v_float = NAN;
864 }
865 retval = OK;
866 break;
867 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100868 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
869 {
870 reader->js_used += 9;
871 if (cur_item != NULL)
872 {
873 cur_item->v_type = VAR_FLOAT;
874 cur_item->vval.v_float = -INFINITY;
875 }
876 retval = OK;
877 break;
878 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100879 if (STRNICMP((char *)p, "Infinity", 8) == 0)
880 {
881 reader->js_used += 8;
882 if (cur_item != NULL)
883 {
884 cur_item->v_type = VAR_FLOAT;
885 cur_item->vval.v_float = INFINITY;
886 }
887 retval = OK;
888 break;
889 }
890#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100891 // check for truncated name
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100892 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
893 if (
894 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
895#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100896 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100897 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
898 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
899#endif
900 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
901 || STRNICMP((char *)p, "null", len) == 0)))
902
903 retval = MAYBE;
904 else
905 retval = FAIL;
906 break;
907 }
908
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100909 // We are finished when retval is FAIL or MAYBE and when at the
910 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100911 if (retval == FAIL)
912 break;
913 if (retval == MAYBE || stack.ga_len == 0)
914 goto theend;
915
916 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
917 && cur_item != NULL)
918 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100919 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100920 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100921 {
922 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100923 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100924 retval = FAIL;
925 goto theend;
926 }
927 }
928 }
929
930item_end:
931 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
932 switch (top_item->jd_type)
933 {
934 case JSON_ARRAY:
935 if (res != NULL)
936 {
937 listitem_T *li = listitem_alloc();
938
939 if (li == NULL)
940 {
941 clear_tv(cur_item);
942 retval = FAIL;
943 goto theend;
944 }
945 li->li_tv = *cur_item;
946 list_append(top_item->jd_tv.vval.v_list, li);
947 }
948 if (cur_item != NULL)
949 cur_item = &item;
950
951 json_skip_white(reader);
952 p = reader->js_buf + reader->js_used;
953 if (*p == ',')
954 ++reader->js_used;
955 else if (*p != ']')
956 {
957 if (*p == NUL)
958 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100959 else
960 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100961 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100962 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100963 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100964 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100965 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100966 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100967
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100968 case JSON_OBJECT_KEY:
969 json_skip_white(reader);
970 p = reader->js_buf + reader->js_used;
971 if (*p != ':')
972 {
973 if (cur_item != NULL)
974 clear_tv(cur_item);
975 if (*p == NUL)
976 retval = MAYBE;
977 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100978 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100979 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100980 retval = FAIL;
981 }
982 goto theend;
983 }
984 ++reader->js_used;
985 json_skip_white(reader);
986 top_item->jd_type = JSON_OBJECT;
987 if (cur_item != NULL)
988 cur_item = &item;
989 break;
990
991 case JSON_OBJECT:
992 if (cur_item != NULL
993 && dict_find(top_item->jd_tv.vval.v_dict,
994 top_item->jd_key, -1) != NULL)
995 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100996 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100997 top_item->jd_key);
998 clear_tv(&top_item->jd_key_tv);
999 clear_tv(cur_item);
1000 retval = FAIL;
1001 goto theend;
1002 }
1003
1004 if (cur_item != NULL)
1005 {
1006 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1007
1008 clear_tv(&top_item->jd_key_tv);
1009 if (di == NULL)
1010 {
1011 clear_tv(cur_item);
1012 retval = FAIL;
1013 goto theend;
1014 }
1015 di->di_tv = *cur_item;
1016 di->di_tv.v_lock = 0;
1017 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1018 {
1019 dictitem_free(di);
1020 retval = FAIL;
1021 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001022 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001023 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001024
1025 json_skip_white(reader);
1026 p = reader->js_buf + reader->js_used;
1027 if (*p == ',')
1028 ++reader->js_used;
1029 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001030 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001031 if (*p == NUL)
1032 retval = MAYBE;
1033 else
1034 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001035 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001036 retval = FAIL;
1037 }
1038 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001039 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001040 top_item->jd_type = JSON_OBJECT_KEY;
1041 if (cur_item != NULL)
1042 cur_item = &top_item->jd_key_tv;
1043 break;
1044 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001045 }
1046
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001047 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001048 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001049 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001050 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001051 res->v_type = VAR_SPECIAL;
1052 res->vval.v_number = VVAL_NONE;
1053 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001054 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001055
1056theend:
1057 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001058 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001059}
1060
1061/*
1062 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001063 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001064 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001065 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001066 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001067json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001068{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001069 int ret;
1070
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001071 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001072 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001073 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001074 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001075 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001076 {
1077 if (ret == MAYBE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001078 emsg(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001079 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001080 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001081 json_skip_white(reader);
1082 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001083 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001084 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001085 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001086 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001087 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001088}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001089
Bram Moolenaar113e1072019-01-20 15:30:40 +01001090#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001091/*
1092 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001093 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001094 * Return FAIL for a decoding error.
1095 * Return MAYBE for an incomplete message.
1096 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001097 */
1098 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001099json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001100{
1101 int ret;
1102
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001103 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001104 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1105 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001106 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001107 json_skip_white(reader);
1108
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001109 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001110}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001111#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001112
1113/*
1114 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001115 * "options" can be JSON_JS or zero.
1116 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001117 * Return FAIL if the message has a decoding error.
1118 * Return MAYBE if the message is truncated, need to read more.
1119 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001120 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001121 * Does not advance the reader.
1122 */
1123 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001124json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001125{
1126 int used_save = reader->js_used;
1127 int ret;
1128
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001129 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001130 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1131 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001132 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001133 reader->js_used = used_save;
1134 return ret;
1135}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001136
1137/*
1138 * "js_decode()" function
1139 */
1140 void
1141f_js_decode(typval_T *argvars, typval_T *rettv)
1142{
1143 js_read_T reader;
1144
1145 reader.js_buf = tv_get_string(&argvars[0]);
1146 reader.js_fill = NULL;
1147 reader.js_used = 0;
1148 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
1149 emsg(_(e_invarg));
1150}
1151
1152/*
1153 * "js_encode()" function
1154 */
1155 void
1156f_js_encode(typval_T *argvars, typval_T *rettv)
1157{
1158 rettv->v_type = VAR_STRING;
1159 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1160}
1161
1162/*
1163 * "json_decode()" function
1164 */
1165 void
1166f_json_decode(typval_T *argvars, typval_T *rettv)
1167{
1168 js_read_T reader;
1169
1170 reader.js_buf = tv_get_string(&argvars[0]);
1171 reader.js_fill = NULL;
1172 reader.js_used = 0;
1173 json_decode_all(&reader, rettv, 0);
1174}
1175
1176/*
1177 * "json_encode()" function
1178 */
1179 void
1180f_json_encode(typval_T *argvars, typval_T *rettv)
1181{
1182 rettv->v_type = VAR_STRING;
1183 rettv->vval.v_string = json_encode(&argvars[0], 0);
1184}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001185#endif