blob: f4a03e61ad6771cb7690c1ac5f2adfb5426cf007 [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 Moolenaar8a7d6542020-01-26 15:56:19 +0100354 case VAR_VOID:
Bram Moolenaardd589232020-02-29 17:38:12 +0100355 internal_error_no_abort("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100356 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100357 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100358 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100359}
360
361/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100362 * When "reader" has less than NUMBUFLEN bytes available, call the fill
363 * callback to get more.
364 */
365 static void
366fill_numbuflen(js_read_T *reader)
367{
368 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
369 - reader->js_used < NUMBUFLEN)
370 {
371 if (reader->js_fill(reader))
372 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
373 }
374}
375
376/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100377 * Skip white space in "reader". All characters <= space are considered white
378 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100379 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100380 */
381 static void
382json_skip_white(js_read_T *reader)
383{
384 int c;
385
Bram Moolenaar56ead342016-02-02 18:20:08 +0100386 for (;;)
387 {
388 c = reader->js_buf[reader->js_used];
389 if (reader->js_fill != NULL && c == NUL)
390 {
391 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200392 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100393 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200394 continue;
395 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100396 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100397 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100398 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100399 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100400 }
401 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100402}
403
Bram Moolenaar56ead342016-02-02 18:20:08 +0100404 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100405json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100406{
407 garray_T ga;
408 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100409 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100410 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200411 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100412
Bram Moolenaar56ead342016-02-02 18:20:08 +0100413 if (res != NULL)
414 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100415
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100416 p = reader->js_buf + reader->js_used + 1; // skip over " or '
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100417 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100418 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100419 // The JSON is always expected to be utf-8, thus use utf functions
420 // here. The string is converted below if needed.
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100421 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100422 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100423 // Not enough bytes to make a character or end of the string. Get
424 // more if possible.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100425 if (reader->js_fill == NULL)
426 break;
427 len = (int)(reader->js_end - p);
428 reader->js_used = (int)(p - reader->js_buf);
429 if (!reader->js_fill(reader))
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100430 break; // didn't get more
Bram Moolenaar56ead342016-02-02 18:20:08 +0100431 p = reader->js_buf + reader->js_used;
432 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
433 continue;
434 }
435
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100436 if (*p == '\\')
437 {
438 c = -1;
439 switch (p[1])
440 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100441 case '\\': c = '\\'; break;
442 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100443 case 'b': c = BS; break;
444 case 't': c = TAB; break;
445 case 'n': c = NL; break;
446 case 'f': c = FF; break;
447 case 'r': c = CAR; break;
448 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100449 if (reader->js_fill != NULL
450 && (int)(reader->js_end - p) < NUMBUFLEN)
451 {
452 reader->js_used = (int)(p - reader->js_buf);
453 if (reader->js_fill(reader))
454 {
455 p = reader->js_buf + reader->js_used;
456 reader->js_end = reader->js_buf
457 + STRLEN(reader->js_buf);
458 }
459 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100460 nr = 0;
461 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100462 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200463 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
464 if (len == 0)
465 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200466 if (res != NULL)
467 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200468 return FAIL;
469 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100470 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100471 if (0xd800 <= nr && nr <= 0xdfff
472 && (int)(reader->js_end - p) >= 6
473 && *p == '\\' && *(p+1) == 'u')
474 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200475 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100476
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100477 // decode surrogate pair: \ud812\u3456
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100478 len = 0;
479 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200480 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
481 if (len == 0)
482 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200483 if (res != NULL)
484 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200485 return FAIL;
486 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100487 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
488 {
489 p += len + 2;
490 nr = (((nr - 0xd800) << 10) |
491 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
492 }
493 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100494 if (res != NULL)
495 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200496 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200497
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100498 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100499 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100500 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100501 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100502 default:
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100503 // not a special char, skip over backslash
Bram Moolenaar56ead342016-02-02 18:20:08 +0100504 ++p;
505 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100506 }
507 if (c > 0)
508 {
509 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100510 if (res != NULL)
511 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100512 }
513 }
514 else
515 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100516 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100517 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100518 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100519 if (ga_grow(&ga, len) == FAIL)
520 {
521 ga_clear(&ga);
522 return FAIL;
523 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100524 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
525 ga.ga_len += len;
526 }
527 p += len;
528 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100529 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100530
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100531 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100532 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100533 {
534 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100535 if (res != NULL)
536 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100537 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100538 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100539#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100540 if (!enc_utf8)
541 {
542 vimconv_T conv;
543
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100544 // Convert the utf-8 string to 'encoding'.
Bram Moolenaarb3628722016-02-28 14:56:39 +0100545 conv.vc_type = CONV_NONE;
546 convert_setup(&conv, (char_u*)"utf-8", p_enc);
547 if (conv.vc_type != CONV_NONE)
548 {
549 res->vval.v_string =
550 string_convert(&conv, ga.ga_data, NULL);
551 vim_free(ga.ga_data);
552 }
553 convert_setup(&conv, NULL, NULL);
554 }
555 else
556#endif
557 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100558 }
559 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100560 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100561 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100562 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100563 res->v_type = VAR_SPECIAL;
564 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100565 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100566 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100567 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100568}
569
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100570typedef enum {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100571 JSON_ARRAY, // parsing items in an array
572 JSON_OBJECT_KEY, // parsing key of an object
573 JSON_OBJECT // parsing item in an object, after the key
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100574} json_decode_T;
575
576typedef struct {
577 json_decode_T jd_type;
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100578 typval_T jd_tv; // the list or dict
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100579 typval_T jd_key_tv;
580 char_u *jd_key;
581} json_dec_item_T;
582
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100583/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100584 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100585 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100586 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100587 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100588 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100589 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100590 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100591json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100592{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100593 char_u *p;
594 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100595 int retval;
596 garray_T stack;
597 typval_T item;
598 typval_T *cur_item;
599 json_dec_item_T *top_item;
600 char_u key_buf[NUMBUFLEN];
601
602 ga_init2(&stack, sizeof(json_dec_item_T), 100);
603 cur_item = res;
604 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100605 if (res != NULL)
606 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100607
Bram Moolenaar56ead342016-02-02 18:20:08 +0100608 fill_numbuflen(reader);
609 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100610 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100611 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100612 top_item = NULL;
613 if (stack.ga_len > 0)
614 {
615 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
616 json_skip_white(reader);
617 p = reader->js_buf + reader->js_used;
618 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100619 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100620 retval = MAYBE;
621 if (top_item->jd_type == JSON_OBJECT)
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100622 // did get the key, clear it
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100623 clear_tv(&top_item->jd_key_tv);
624 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100625 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100626 if (top_item->jd_type == JSON_OBJECT_KEY
627 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100628 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100629 // Check for end of object or array.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100630 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100631 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100632 ++reader->js_used; // consume the ']' or '}'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100633 --stack.ga_len;
634 if (stack.ga_len == 0)
635 {
636 retval = OK;
637 goto theend;
638 }
639 if (cur_item != NULL)
640 cur_item = &top_item->jd_tv;
641 goto item_end;
642 }
643 }
644 }
645
646 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
647 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100648 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100649 && reader->js_buf[reader->js_used] != '\''
650 && reader->js_buf[reader->js_used] != '['
651 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100652 {
653 char_u *key;
654
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100655 // accept an object key that is not in quotes
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100656 key = p = reader->js_buf + reader->js_used;
657 while (*p != NUL && *p != ':' && *p > ' ')
658 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100659 if (cur_item != NULL)
660 {
661 cur_item->v_type = VAR_STRING;
662 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
663 top_item->jd_key = cur_item->vval.v_string;
664 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100665 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100666 }
667 else
668 {
669 switch (*p)
670 {
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100671 case '[': // start of array
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100672 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
673 {
674 retval = FAIL;
675 break;
676 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100677 if (ga_grow(&stack, 1) == FAIL)
678 {
679 retval = FAIL;
680 break;
681 }
682 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
683 {
684 cur_item->v_type = VAR_SPECIAL;
685 cur_item->vval.v_number = VVAL_NONE;
686 retval = FAIL;
687 break;
688 }
689
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100690 ++reader->js_used; // consume the '['
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100691 top_item = ((json_dec_item_T *)stack.ga_data)
692 + stack.ga_len;
693 top_item->jd_type = JSON_ARRAY;
694 ++stack.ga_len;
695 if (cur_item != NULL)
696 {
697 top_item->jd_tv = *cur_item;
698 cur_item = &item;
699 }
700 continue;
701
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100702 case '{': // start of object
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100703 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
704 {
705 retval = FAIL;
706 break;
707 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100708 if (ga_grow(&stack, 1) == FAIL)
709 {
710 retval = FAIL;
711 break;
712 }
713 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
714 {
715 cur_item->v_type = VAR_SPECIAL;
716 cur_item->vval.v_number = VVAL_NONE;
717 retval = FAIL;
718 break;
719 }
720
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100721 ++reader->js_used; // consume the '{'
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100722 top_item = ((json_dec_item_T *)stack.ga_data)
723 + stack.ga_len;
724 top_item->jd_type = JSON_OBJECT_KEY;
725 ++stack.ga_len;
726 if (cur_item != NULL)
727 {
728 top_item->jd_tv = *cur_item;
729 cur_item = &top_item->jd_key_tv;
730 }
731 continue;
732
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100733 case '"': // string
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100734 retval = json_decode_string(reader, cur_item, *p);
735 break;
736
737 case '\'':
738 if (options & JSON_JS)
739 retval = json_decode_string(reader, cur_item, *p);
740 else
741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100742 emsg(_(e_invarg));
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100743 retval = FAIL;
744 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100745 break;
746
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100747 case ',': // comma: empty item
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100748 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100749 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100750 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100751 retval = FAIL;
752 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100753 }
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100754 // FALLTHROUGH
755 case NUL: // empty
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100756 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100757 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100758 cur_item->v_type = VAR_SPECIAL;
759 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100760 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100761 retval = OK;
762 break;
763
764 default:
Bram Moolenaara5d59532020-01-26 21:42:03 +0100765 if (VIM_ISDIGIT(*p) || (*p == '-'
766 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100767 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100768 char_u *sp = p;
769
770 if (*sp == '-')
771 {
772 ++sp;
773 if (*sp == NUL)
774 {
775 retval = MAYBE;
776 break;
777 }
778 if (!VIM_ISDIGIT(*sp))
779 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100780 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100781 retval = FAIL;
782 break;
783 }
784 }
785 sp = skipdigits(sp);
Bram Moolenaara5d59532020-01-26 21:42:03 +0100786#ifdef FEAT_FLOAT
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100787 if (*sp == '.' || *sp == 'e' || *sp == 'E')
788 {
789 if (cur_item == NULL)
790 {
791 float_T f;
792
793 len = string2float(p, &f);
794 }
795 else
796 {
797 cur_item->v_type = VAR_FLOAT;
798 len = string2float(p, &cur_item->vval.v_float);
799 }
800 }
801 else
802#endif
803 {
804 varnumber_T nr;
805
806 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100807 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200808 &nr, NULL, 0, TRUE);
809 if (len == 0)
810 {
811 emsg(_(e_invarg));
812 retval = FAIL;
813 goto theend;
814 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100815 if (cur_item != NULL)
816 {
817 cur_item->v_type = VAR_NUMBER;
818 cur_item->vval.v_number = nr;
819 }
820 }
821 reader->js_used += len;
822 retval = OK;
823 break;
824 }
825 if (STRNICMP((char *)p, "false", 5) == 0)
826 {
827 reader->js_used += 5;
828 if (cur_item != NULL)
829 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100830 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100831 cur_item->vval.v_number = VVAL_FALSE;
832 }
833 retval = OK;
834 break;
835 }
836 if (STRNICMP((char *)p, "true", 4) == 0)
837 {
838 reader->js_used += 4;
839 if (cur_item != NULL)
840 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100841 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100842 cur_item->vval.v_number = VVAL_TRUE;
843 }
844 retval = OK;
845 break;
846 }
847 if (STRNICMP((char *)p, "null", 4) == 0)
848 {
849 reader->js_used += 4;
850 if (cur_item != NULL)
851 {
852 cur_item->v_type = VAR_SPECIAL;
853 cur_item->vval.v_number = VVAL_NULL;
854 }
855 retval = OK;
856 break;
857 }
858#ifdef FEAT_FLOAT
859 if (STRNICMP((char *)p, "NaN", 3) == 0)
860 {
861 reader->js_used += 3;
862 if (cur_item != NULL)
863 {
864 cur_item->v_type = VAR_FLOAT;
865 cur_item->vval.v_float = NAN;
866 }
867 retval = OK;
868 break;
869 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100870 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
871 {
872 reader->js_used += 9;
873 if (cur_item != NULL)
874 {
875 cur_item->v_type = VAR_FLOAT;
876 cur_item->vval.v_float = -INFINITY;
877 }
878 retval = OK;
879 break;
880 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100881 if (STRNICMP((char *)p, "Infinity", 8) == 0)
882 {
883 reader->js_used += 8;
884 if (cur_item != NULL)
885 {
886 cur_item->v_type = VAR_FLOAT;
887 cur_item->vval.v_float = INFINITY;
888 }
889 retval = OK;
890 break;
891 }
892#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100893 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100894 len = (int)(reader->js_end
895 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100896 if (
897 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
898#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100899 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100900 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
901 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
902#endif
903 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
904 || STRNICMP((char *)p, "null", len) == 0)))
905
906 retval = MAYBE;
907 else
908 retval = FAIL;
909 break;
910 }
911
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100912 // We are finished when retval is FAIL or MAYBE and when at the
913 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100914 if (retval == FAIL)
915 break;
916 if (retval == MAYBE || stack.ga_len == 0)
917 goto theend;
918
919 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
920 && cur_item != NULL)
921 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100922 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100923 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100924 {
925 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100926 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100927 retval = FAIL;
928 goto theend;
929 }
930 }
931 }
932
933item_end:
934 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
935 switch (top_item->jd_type)
936 {
937 case JSON_ARRAY:
938 if (res != NULL)
939 {
940 listitem_T *li = listitem_alloc();
941
942 if (li == NULL)
943 {
944 clear_tv(cur_item);
945 retval = FAIL;
946 goto theend;
947 }
948 li->li_tv = *cur_item;
949 list_append(top_item->jd_tv.vval.v_list, li);
950 }
951 if (cur_item != NULL)
952 cur_item = &item;
953
954 json_skip_white(reader);
955 p = reader->js_buf + reader->js_used;
956 if (*p == ',')
957 ++reader->js_used;
958 else if (*p != ']')
959 {
960 if (*p == NUL)
961 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100962 else
963 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100964 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100965 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100966 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100967 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100968 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100969 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100970
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100971 case JSON_OBJECT_KEY:
972 json_skip_white(reader);
973 p = reader->js_buf + reader->js_used;
974 if (*p != ':')
975 {
976 if (cur_item != NULL)
977 clear_tv(cur_item);
978 if (*p == NUL)
979 retval = MAYBE;
980 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100981 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100982 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100983 retval = FAIL;
984 }
985 goto theend;
986 }
987 ++reader->js_used;
988 json_skip_white(reader);
989 top_item->jd_type = JSON_OBJECT;
990 if (cur_item != NULL)
991 cur_item = &item;
992 break;
993
994 case JSON_OBJECT:
995 if (cur_item != NULL
996 && dict_find(top_item->jd_tv.vval.v_dict,
997 top_item->jd_key, -1) != NULL)
998 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100999 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001000 top_item->jd_key);
1001 clear_tv(&top_item->jd_key_tv);
1002 clear_tv(cur_item);
1003 retval = FAIL;
1004 goto theend;
1005 }
1006
1007 if (cur_item != NULL)
1008 {
1009 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1010
1011 clear_tv(&top_item->jd_key_tv);
1012 if (di == NULL)
1013 {
1014 clear_tv(cur_item);
1015 retval = FAIL;
1016 goto theend;
1017 }
1018 di->di_tv = *cur_item;
1019 di->di_tv.v_lock = 0;
1020 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1021 {
1022 dictitem_free(di);
1023 retval = FAIL;
1024 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001025 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001026 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001027
1028 json_skip_white(reader);
1029 p = reader->js_buf + reader->js_used;
1030 if (*p == ',')
1031 ++reader->js_used;
1032 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001033 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001034 if (*p == NUL)
1035 retval = MAYBE;
1036 else
1037 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001038 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001039 retval = FAIL;
1040 }
1041 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001042 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001043 top_item->jd_type = JSON_OBJECT_KEY;
1044 if (cur_item != NULL)
1045 cur_item = &top_item->jd_key_tv;
1046 break;
1047 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001048 }
1049
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001050 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001051 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001052 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001053 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001054 res->v_type = VAR_SPECIAL;
1055 res->vval.v_number = VVAL_NONE;
1056 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001057 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001058
1059theend:
1060 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001061 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001062}
1063
1064/*
1065 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001066 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001067 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001068 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001069 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001070json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001071{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001072 int ret;
1073
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001074 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001075 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001076 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001077 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001078 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001079 {
1080 if (ret == MAYBE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001081 emsg(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001082 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001083 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001084 json_skip_white(reader);
1085 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001086 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001087 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001088 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001089 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001090 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001091}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001092
Bram Moolenaar113e1072019-01-20 15:30:40 +01001093#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001094/*
1095 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001096 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001097 * Return FAIL for a decoding error.
1098 * Return MAYBE for an incomplete message.
1099 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001100 */
1101 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001102json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001103{
1104 int ret;
1105
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001106 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001107 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1108 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001109 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001110 json_skip_white(reader);
1111
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001112 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001113}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001114#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001115
1116/*
1117 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001118 * "options" can be JSON_JS or zero.
1119 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001120 * Return FAIL if the message has a decoding error.
1121 * Return MAYBE if the message is truncated, need to read more.
1122 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001123 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001124 * Does not advance the reader.
1125 */
1126 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001127json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001128{
1129 int used_save = reader->js_used;
1130 int ret;
1131
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001132 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001133 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1134 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001135 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001136 reader->js_used = used_save;
1137 return ret;
1138}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001139
1140/*
1141 * "js_decode()" function
1142 */
1143 void
1144f_js_decode(typval_T *argvars, typval_T *rettv)
1145{
1146 js_read_T reader;
1147
1148 reader.js_buf = tv_get_string(&argvars[0]);
1149 reader.js_fill = NULL;
1150 reader.js_used = 0;
1151 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
1152 emsg(_(e_invarg));
1153}
1154
1155/*
1156 * "js_encode()" function
1157 */
1158 void
1159f_js_encode(typval_T *argvars, typval_T *rettv)
1160{
1161 rettv->v_type = VAR_STRING;
1162 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1163}
1164
1165/*
1166 * "json_decode()" function
1167 */
1168 void
1169f_json_decode(typval_T *argvars, typval_T *rettv)
1170{
1171 js_read_T reader;
1172
1173 reader.js_buf = tv_get_string(&argvars[0]);
1174 reader.js_fill = NULL;
1175 reader.js_used = 0;
1176 json_decode_all(&reader, rettv, 0);
1177}
1178
1179/*
1180 * "json_encode()" function
1181 */
1182 void
1183f_json_encode(typval_T *argvars, typval_T *rettv)
1184{
1185 rettv->v_type = VAR_STRING;
1186 rettv->vval.v_string = json_encode(&argvars[0], 0);
1187}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001188#endif