blob: da0ba4bb2a4c1431f19b27d7b32d936ac18623ef [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 Moolenaara5d59532020-01-26 21:42:03 +0100764 if (VIM_ISDIGIT(*p) || (*p == '-'
765 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100766 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100767 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);
Bram Moolenaara5d59532020-01-26 21:42:03 +0100785#ifdef FEAT_FLOAT
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100786 if (*sp == '.' || *sp == 'e' || *sp == 'E')
787 {
788 if (cur_item == NULL)
789 {
790 float_T f;
791
792 len = string2float(p, &f);
793 }
794 else
795 {
796 cur_item->v_type = VAR_FLOAT;
797 len = string2float(p, &cur_item->vval.v_float);
798 }
799 }
800 else
801#endif
802 {
803 varnumber_T nr;
804
805 vim_str2nr(reader->js_buf + reader->js_used,
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100806 NULL, &len, 0, // what
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200807 &nr, NULL, 0, TRUE);
808 if (len == 0)
809 {
810 emsg(_(e_invarg));
811 retval = FAIL;
812 goto theend;
813 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100814 if (cur_item != NULL)
815 {
816 cur_item->v_type = VAR_NUMBER;
817 cur_item->vval.v_number = nr;
818 }
819 }
820 reader->js_used += len;
821 retval = OK;
822 break;
823 }
824 if (STRNICMP((char *)p, "false", 5) == 0)
825 {
826 reader->js_used += 5;
827 if (cur_item != NULL)
828 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100829 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100830 cur_item->vval.v_number = VVAL_FALSE;
831 }
832 retval = OK;
833 break;
834 }
835 if (STRNICMP((char *)p, "true", 4) == 0)
836 {
837 reader->js_used += 4;
838 if (cur_item != NULL)
839 {
Bram Moolenaar9b4a15d2020-01-11 16:05:23 +0100840 cur_item->v_type = VAR_BOOL;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100841 cur_item->vval.v_number = VVAL_TRUE;
842 }
843 retval = OK;
844 break;
845 }
846 if (STRNICMP((char *)p, "null", 4) == 0)
847 {
848 reader->js_used += 4;
849 if (cur_item != NULL)
850 {
851 cur_item->v_type = VAR_SPECIAL;
852 cur_item->vval.v_number = VVAL_NULL;
853 }
854 retval = OK;
855 break;
856 }
857#ifdef FEAT_FLOAT
858 if (STRNICMP((char *)p, "NaN", 3) == 0)
859 {
860 reader->js_used += 3;
861 if (cur_item != NULL)
862 {
863 cur_item->v_type = VAR_FLOAT;
864 cur_item->vval.v_float = NAN;
865 }
866 retval = OK;
867 break;
868 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100869 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
870 {
871 reader->js_used += 9;
872 if (cur_item != NULL)
873 {
874 cur_item->v_type = VAR_FLOAT;
875 cur_item->vval.v_float = -INFINITY;
876 }
877 retval = OK;
878 break;
879 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100880 if (STRNICMP((char *)p, "Infinity", 8) == 0)
881 {
882 reader->js_used += 8;
883 if (cur_item != NULL)
884 {
885 cur_item->v_type = VAR_FLOAT;
886 cur_item->vval.v_float = INFINITY;
887 }
888 retval = OK;
889 break;
890 }
891#endif
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100892 // check for truncated name
Bram Moolenaara5d59532020-01-26 21:42:03 +0100893 len = (int)(reader->js_end
894 - (reader->js_buf + reader->js_used));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100895 if (
896 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
897#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100898 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100899 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
900 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
901#endif
902 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
903 || STRNICMP((char *)p, "null", len) == 0)))
904
905 retval = MAYBE;
906 else
907 retval = FAIL;
908 break;
909 }
910
Bram Moolenaar4ba37b52019-12-04 21:57:43 +0100911 // We are finished when retval is FAIL or MAYBE and when at the
912 // toplevel.
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100913 if (retval == FAIL)
914 break;
915 if (retval == MAYBE || stack.ga_len == 0)
916 goto theend;
917
918 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
919 && cur_item != NULL)
920 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100921 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100922 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100923 {
924 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100925 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100926 retval = FAIL;
927 goto theend;
928 }
929 }
930 }
931
932item_end:
933 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
934 switch (top_item->jd_type)
935 {
936 case JSON_ARRAY:
937 if (res != NULL)
938 {
939 listitem_T *li = listitem_alloc();
940
941 if (li == NULL)
942 {
943 clear_tv(cur_item);
944 retval = FAIL;
945 goto theend;
946 }
947 li->li_tv = *cur_item;
948 list_append(top_item->jd_tv.vval.v_list, li);
949 }
950 if (cur_item != NULL)
951 cur_item = &item;
952
953 json_skip_white(reader);
954 p = reader->js_buf + reader->js_used;
955 if (*p == ',')
956 ++reader->js_used;
957 else if (*p != ']')
958 {
959 if (*p == NUL)
960 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100961 else
962 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100963 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100964 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100965 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100966 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100967 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100968 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100969
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100970 case JSON_OBJECT_KEY:
971 json_skip_white(reader);
972 p = reader->js_buf + reader->js_used;
973 if (*p != ':')
974 {
975 if (cur_item != NULL)
976 clear_tv(cur_item);
977 if (*p == NUL)
978 retval = MAYBE;
979 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100980 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100981 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100982 retval = FAIL;
983 }
984 goto theend;
985 }
986 ++reader->js_used;
987 json_skip_white(reader);
988 top_item->jd_type = JSON_OBJECT;
989 if (cur_item != NULL)
990 cur_item = &item;
991 break;
992
993 case JSON_OBJECT:
994 if (cur_item != NULL
995 && dict_find(top_item->jd_tv.vval.v_dict,
996 top_item->jd_key, -1) != NULL)
997 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100998 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100999 top_item->jd_key);
1000 clear_tv(&top_item->jd_key_tv);
1001 clear_tv(cur_item);
1002 retval = FAIL;
1003 goto theend;
1004 }
1005
1006 if (cur_item != NULL)
1007 {
1008 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1009
1010 clear_tv(&top_item->jd_key_tv);
1011 if (di == NULL)
1012 {
1013 clear_tv(cur_item);
1014 retval = FAIL;
1015 goto theend;
1016 }
1017 di->di_tv = *cur_item;
1018 di->di_tv.v_lock = 0;
1019 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1020 {
1021 dictitem_free(di);
1022 retval = FAIL;
1023 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001024 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001025 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001026
1027 json_skip_white(reader);
1028 p = reader->js_buf + reader->js_used;
1029 if (*p == ',')
1030 ++reader->js_used;
1031 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001032 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001033 if (*p == NUL)
1034 retval = MAYBE;
1035 else
1036 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001037 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001038 retval = FAIL;
1039 }
1040 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001041 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001042 top_item->jd_type = JSON_OBJECT_KEY;
1043 if (cur_item != NULL)
1044 cur_item = &top_item->jd_key_tv;
1045 break;
1046 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001047 }
1048
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001049 // Get here when parsing failed.
Bram Moolenaar7756e742016-10-21 20:35:37 +02001050 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001051 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001052 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001053 res->v_type = VAR_SPECIAL;
1054 res->vval.v_number = VVAL_NONE;
1055 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001056 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001057
1058theend:
1059 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001060 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001061}
1062
1063/*
1064 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001065 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001066 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001067 */
Bram Moolenaar5843f5f2019-08-20 20:13:45 +02001068 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001069json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001070{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001071 int ret;
1072
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001073 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001074 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001075 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001076 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001077 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001078 {
1079 if (ret == MAYBE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001080 emsg(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001081 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001082 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001083 json_skip_white(reader);
1084 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001085 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001086 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001087 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001088 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001089 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001090}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001091
Bram Moolenaar113e1072019-01-20 15:30:40 +01001092#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001093/*
1094 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001095 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001096 * Return FAIL for a decoding error.
1097 * Return MAYBE for an incomplete message.
1098 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001099 */
1100 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001101json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001102{
1103 int ret;
1104
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001105 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001106 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1107 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001108 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001109 json_skip_white(reader);
1110
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001111 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001112}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001113#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001114
1115/*
1116 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001117 * "options" can be JSON_JS or zero.
1118 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001119 * Return FAIL if the message has a decoding error.
1120 * Return MAYBE if the message is truncated, need to read more.
1121 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001122 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001123 * Does not advance the reader.
1124 */
1125 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001126json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001127{
1128 int used_save = reader->js_used;
1129 int ret;
1130
Bram Moolenaar4ba37b52019-12-04 21:57:43 +01001131 // We find the end once, to avoid calling strlen() many times.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001132 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1133 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001134 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001135 reader->js_used = used_save;
1136 return ret;
1137}
Bram Moolenaar29b7d7a2019-07-22 23:03:57 +02001138
1139/*
1140 * "js_decode()" function
1141 */
1142 void
1143f_js_decode(typval_T *argvars, typval_T *rettv)
1144{
1145 js_read_T reader;
1146
1147 reader.js_buf = tv_get_string(&argvars[0]);
1148 reader.js_fill = NULL;
1149 reader.js_used = 0;
1150 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
1151 emsg(_(e_invarg));
1152}
1153
1154/*
1155 * "js_encode()" function
1156 */
1157 void
1158f_js_encode(typval_T *argvars, typval_T *rettv)
1159{
1160 rettv->v_type = VAR_STRING;
1161 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
1162}
1163
1164/*
1165 * "json_decode()" function
1166 */
1167 void
1168f_json_decode(typval_T *argvars, typval_T *rettv)
1169{
1170 js_read_T reader;
1171
1172 reader.js_buf = tv_get_string(&argvars[0]);
1173 reader.js_fill = NULL;
1174 reader.js_used = 0;
1175 json_decode_all(&reader, rettv, 0);
1176}
1177
1178/*
1179 * "json_encode()" function
1180 */
1181 void
1182f_json_encode(typval_T *argvars, typval_T *rettv)
1183{
1184 rettv->v_type = VAR_STRING;
1185 rettv->vval.v_string = json_encode(&argvars[0], 0);
1186}
Bram Moolenaarc61a48d2019-07-22 23:16:33 +02001187#endif