blob: 3a5de7092c957d354cd3e9a4ed6fb46f1fbc5114 [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
51 /* Store bytes in the growarray. */
52 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 Moolenaarb3628722016-02-28 14:56:39 +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 Moolenaarb6ff8112016-02-27 18:41:27 +0100120 /* always use utf-8 encoding, ignore 'encoding' */
121 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;
135 case 0x22: /* " */
136 case 0x5c: /* \ */
137 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 {
196 case VAR_SPECIAL:
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 Moolenaar595e64e2016-02-07 19:19:53 +0100201 case VVAL_NONE: if ((options & JSON_JS) != 0
202 && (options & JSON_NO_NONE) == 0)
203 /* empty item */
204 break;
205 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100206 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
207 }
208 break;
209
210 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200211 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +0100212 (long_long_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100213 ga_concat(gap, numbuf);
214 break;
215
216 case VAR_STRING:
217 res = val->vval.v_string;
218 write_string(gap, res);
219 break;
220
221 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100222 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100223 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100224 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100225 /* no JSON equivalent TODO: better error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100226 emsg(_(e_invarg));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100227 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100228
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100229 case VAR_BLOB:
230 b = val->vval.v_blob;
231 if (b == NULL || b->bv_ga.ga_len == 0)
232 ga_concat(gap, (char_u *)"[]");
233 else
234 {
235 ga_append(gap, '[');
236 for (i = 0; i < b->bv_ga.ga_len; i++)
237 {
238 if (i > 0)
239 ga_concat(gap, (char_u *)",");
240 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
241 (int)blob_get(b, i));
242 ga_concat(gap, numbuf);
243 }
244 ga_append(gap, ']');
245 }
246 break;
247
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100248 case VAR_LIST:
249 l = val->vval.v_list;
250 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100251 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100252 else
253 {
254 if (l->lv_copyID == copyID)
255 ga_concat(gap, (char_u *)"[]");
256 else
257 {
258 listitem_T *li;
259
260 l->lv_copyID = copyID;
261 ga_append(gap, '[');
262 for (li = l->lv_first; li != NULL && !got_int; )
263 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100264 if (json_encode_item(gap, &li->li_tv, copyID,
265 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100266 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100267 if ((options & JSON_JS)
268 && li->li_next == NULL
269 && li->li_tv.v_type == VAR_SPECIAL
270 && li->li_tv.vval.v_number == VVAL_NONE)
271 /* add an extra comma if the last item is v:none */
272 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100273 li = li->li_next;
274 if (li != NULL)
275 ga_append(gap, ',');
276 }
277 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100278 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100279 }
280 }
281 break;
282
283 case VAR_DICT:
284 d = val->vval.v_dict;
285 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100286 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100287 else
288 {
289 if (d->dv_copyID == copyID)
290 ga_concat(gap, (char_u *)"{}");
291 else
292 {
293 int first = TRUE;
294 int todo = (int)d->dv_hashtab.ht_used;
295 hashitem_T *hi;
296
297 d->dv_copyID = copyID;
298 ga_append(gap, '{');
299
300 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
301 ++hi)
302 if (!HASHITEM_EMPTY(hi))
303 {
304 --todo;
305 if (first)
306 first = FALSE;
307 else
308 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100309 if ((options & JSON_JS)
310 && is_simple_key(hi->hi_key))
311 ga_concat(gap, hi->hi_key);
312 else
313 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100314 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100315 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100316 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100317 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100318 }
319 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100320 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100321 }
322 }
323 break;
324
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100325 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100326#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100327# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100328 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100329 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100330 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100331 {
332 if (val->vval.v_float < 0.0)
333 ga_concat(gap, (char_u *)"-Infinity");
334 else
335 ga_concat(gap, (char_u *)"Infinity");
336 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100337 else
338# endif
339 {
340 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
341 val->vval.v_float);
342 ga_concat(gap, numbuf);
343 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100344 break;
345#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100346 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +0100347 internal_error("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100348 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100349 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100350 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100351}
352
353/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100354 * When "reader" has less than NUMBUFLEN bytes available, call the fill
355 * callback to get more.
356 */
357 static void
358fill_numbuflen(js_read_T *reader)
359{
360 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
361 - reader->js_used < NUMBUFLEN)
362 {
363 if (reader->js_fill(reader))
364 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
365 }
366}
367
368/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100369 * Skip white space in "reader". All characters <= space are considered white
370 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100371 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100372 */
373 static void
374json_skip_white(js_read_T *reader)
375{
376 int c;
377
Bram Moolenaar56ead342016-02-02 18:20:08 +0100378 for (;;)
379 {
380 c = reader->js_buf[reader->js_used];
381 if (reader->js_fill != NULL && c == NUL)
382 {
383 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200384 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100385 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200386 continue;
387 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100388 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100389 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100390 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100391 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100392 }
393 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100394}
395
Bram Moolenaar56ead342016-02-02 18:20:08 +0100396 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100397json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100398{
399 garray_T ga;
400 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100401 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100402 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200403 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100404
Bram Moolenaar56ead342016-02-02 18:20:08 +0100405 if (res != NULL)
406 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100407
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100408 p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */
409 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100410 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100411 /* The JSON is always expected to be utf-8, thus use utf functions
412 * here. The string is converted below if needed. */
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100413 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100414 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100415 /* Not enough bytes to make a character or end of the string. Get
416 * more if possible. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100417 if (reader->js_fill == NULL)
418 break;
419 len = (int)(reader->js_end - p);
420 reader->js_used = (int)(p - reader->js_buf);
421 if (!reader->js_fill(reader))
422 break; /* didn't get more */
423 p = reader->js_buf + reader->js_used;
424 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
425 continue;
426 }
427
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100428 if (*p == '\\')
429 {
430 c = -1;
431 switch (p[1])
432 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100433 case '\\': c = '\\'; break;
434 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100435 case 'b': c = BS; break;
436 case 't': c = TAB; break;
437 case 'n': c = NL; break;
438 case 'f': c = FF; break;
439 case 'r': c = CAR; break;
440 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100441 if (reader->js_fill != NULL
442 && (int)(reader->js_end - p) < NUMBUFLEN)
443 {
444 reader->js_used = (int)(p - reader->js_buf);
445 if (reader->js_fill(reader))
446 {
447 p = reader->js_buf + reader->js_used;
448 reader->js_end = reader->js_buf
449 + STRLEN(reader->js_buf);
450 }
451 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100452 nr = 0;
453 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100454 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200455 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
456 if (len == 0)
457 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200458 if (res != NULL)
459 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200460 return FAIL;
461 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100462 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100463 if (0xd800 <= nr && nr <= 0xdfff
464 && (int)(reader->js_end - p) >= 6
465 && *p == '\\' && *(p+1) == 'u')
466 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200467 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100468
469 /* decode surrogate pair: \ud812\u3456 */
470 len = 0;
471 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200472 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
473 if (len == 0)
474 {
Bram Moolenaarb4368372019-05-27 20:01:41 +0200475 if (res != NULL)
476 ga_clear(&ga);
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200477 return FAIL;
478 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100479 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
480 {
481 p += len + 2;
482 nr = (((nr - 0xd800) << 10) |
483 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
484 }
485 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100486 if (res != NULL)
487 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200488 char_u buf[NUMBUFLEN];
Bram Moolenaarb4368372019-05-27 20:01:41 +0200489
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100490 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100491 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100492 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100493 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100494 default:
495 /* not a special char, skip over \ */
496 ++p;
497 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100498 }
499 if (c > 0)
500 {
501 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100502 if (res != NULL)
503 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100504 }
505 }
506 else
507 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100508 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100509 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100510 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100511 if (ga_grow(&ga, len) == FAIL)
512 {
513 ga_clear(&ga);
514 return FAIL;
515 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100516 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
517 ga.ga_len += len;
518 }
519 p += len;
520 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100521 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100522
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100523 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100524 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100525 {
526 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100527 if (res != NULL)
528 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100529 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100530 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100531#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100532 if (!enc_utf8)
533 {
534 vimconv_T conv;
535
536 /* Convert the utf-8 string to 'encoding'. */
537 conv.vc_type = CONV_NONE;
538 convert_setup(&conv, (char_u*)"utf-8", p_enc);
539 if (conv.vc_type != CONV_NONE)
540 {
541 res->vval.v_string =
542 string_convert(&conv, ga.ga_data, NULL);
543 vim_free(ga.ga_data);
544 }
545 convert_setup(&conv, NULL, NULL);
546 }
547 else
548#endif
549 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100550 }
551 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100552 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100553 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100554 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100555 res->v_type = VAR_SPECIAL;
556 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100557 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100558 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100559 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100560}
561
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100562typedef enum {
563 JSON_ARRAY, /* parsing items in an array */
564 JSON_OBJECT_KEY, /* parsing key of an object */
565 JSON_OBJECT /* parsing item in an object, after the key */
566} json_decode_T;
567
568typedef struct {
569 json_decode_T jd_type;
570 typval_T jd_tv; /* the list or dict */
571 typval_T jd_key_tv;
572 char_u *jd_key;
573} json_dec_item_T;
574
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100575/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100576 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100577 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100578 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100579 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100580 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100581 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100582 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100583json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100584{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100585 char_u *p;
586 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100587 int retval;
588 garray_T stack;
589 typval_T item;
590 typval_T *cur_item;
591 json_dec_item_T *top_item;
592 char_u key_buf[NUMBUFLEN];
593
594 ga_init2(&stack, sizeof(json_dec_item_T), 100);
595 cur_item = res;
596 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100597 if (res != NULL)
598 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100599
Bram Moolenaar56ead342016-02-02 18:20:08 +0100600 fill_numbuflen(reader);
601 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100602 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100603 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100604 top_item = NULL;
605 if (stack.ga_len > 0)
606 {
607 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
608 json_skip_white(reader);
609 p = reader->js_buf + reader->js_used;
610 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100611 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100612 retval = MAYBE;
613 if (top_item->jd_type == JSON_OBJECT)
614 /* did get the key, clear it */
615 clear_tv(&top_item->jd_key_tv);
616 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100617 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100618 if (top_item->jd_type == JSON_OBJECT_KEY
619 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100620 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100621 /* Check for end of object or array. */
622 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100623 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100624 ++reader->js_used; /* consume the ']' or '}' */
625 --stack.ga_len;
626 if (stack.ga_len == 0)
627 {
628 retval = OK;
629 goto theend;
630 }
631 if (cur_item != NULL)
632 cur_item = &top_item->jd_tv;
633 goto item_end;
634 }
635 }
636 }
637
638 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
639 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100640 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100641 && reader->js_buf[reader->js_used] != '\''
642 && reader->js_buf[reader->js_used] != '['
643 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100644 {
645 char_u *key;
646
647 /* accept an object key that is not in quotes */
648 key = p = reader->js_buf + reader->js_used;
649 while (*p != NUL && *p != ':' && *p > ' ')
650 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100651 if (cur_item != NULL)
652 {
653 cur_item->v_type = VAR_STRING;
654 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
655 top_item->jd_key = cur_item->vval.v_string;
656 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100657 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100658 }
659 else
660 {
661 switch (*p)
662 {
663 case '[': /* start of array */
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100664 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
665 {
666 retval = FAIL;
667 break;
668 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100669 if (ga_grow(&stack, 1) == FAIL)
670 {
671 retval = FAIL;
672 break;
673 }
674 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
675 {
676 cur_item->v_type = VAR_SPECIAL;
677 cur_item->vval.v_number = VVAL_NONE;
678 retval = FAIL;
679 break;
680 }
681
682 ++reader->js_used; /* consume the '[' */
683 top_item = ((json_dec_item_T *)stack.ga_data)
684 + stack.ga_len;
685 top_item->jd_type = JSON_ARRAY;
686 ++stack.ga_len;
687 if (cur_item != NULL)
688 {
689 top_item->jd_tv = *cur_item;
690 cur_item = &item;
691 }
692 continue;
693
694 case '{': /* start of object */
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100695 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
696 {
697 retval = FAIL;
698 break;
699 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100700 if (ga_grow(&stack, 1) == FAIL)
701 {
702 retval = FAIL;
703 break;
704 }
705 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
706 {
707 cur_item->v_type = VAR_SPECIAL;
708 cur_item->vval.v_number = VVAL_NONE;
709 retval = FAIL;
710 break;
711 }
712
713 ++reader->js_used; /* consume the '{' */
714 top_item = ((json_dec_item_T *)stack.ga_data)
715 + stack.ga_len;
716 top_item->jd_type = JSON_OBJECT_KEY;
717 ++stack.ga_len;
718 if (cur_item != NULL)
719 {
720 top_item->jd_tv = *cur_item;
721 cur_item = &top_item->jd_key_tv;
722 }
723 continue;
724
725 case '"': /* string */
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100726 retval = json_decode_string(reader, cur_item, *p);
727 break;
728
729 case '\'':
730 if (options & JSON_JS)
731 retval = json_decode_string(reader, cur_item, *p);
732 else
733 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100734 emsg(_(e_invarg));
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100735 retval = FAIL;
736 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100737 break;
738
739 case ',': /* comma: empty item */
740 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100741 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100742 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100743 retval = FAIL;
744 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100745 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100746 /* FALLTHROUGH */
747 case NUL: /* empty */
748 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100749 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100750 cur_item->v_type = VAR_SPECIAL;
751 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100752 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100753 retval = OK;
754 break;
755
756 default:
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100757 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100758 {
759#ifdef FEAT_FLOAT
760 char_u *sp = p;
761
762 if (*sp == '-')
763 {
764 ++sp;
765 if (*sp == NUL)
766 {
767 retval = MAYBE;
768 break;
769 }
770 if (!VIM_ISDIGIT(*sp))
771 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100772 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100773 retval = FAIL;
774 break;
775 }
776 }
777 sp = skipdigits(sp);
778 if (*sp == '.' || *sp == 'e' || *sp == 'E')
779 {
780 if (cur_item == NULL)
781 {
782 float_T f;
783
784 len = string2float(p, &f);
785 }
786 else
787 {
788 cur_item->v_type = VAR_FLOAT;
789 len = string2float(p, &cur_item->vval.v_float);
790 }
791 }
792 else
793#endif
794 {
795 varnumber_T nr;
796
797 vim_str2nr(reader->js_buf + reader->js_used,
798 NULL, &len, 0, /* what */
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200799 &nr, NULL, 0, TRUE);
800 if (len == 0)
801 {
802 emsg(_(e_invarg));
803 retval = FAIL;
804 goto theend;
805 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100806 if (cur_item != NULL)
807 {
808 cur_item->v_type = VAR_NUMBER;
809 cur_item->vval.v_number = nr;
810 }
811 }
812 reader->js_used += len;
813 retval = OK;
814 break;
815 }
816 if (STRNICMP((char *)p, "false", 5) == 0)
817 {
818 reader->js_used += 5;
819 if (cur_item != NULL)
820 {
821 cur_item->v_type = VAR_SPECIAL;
822 cur_item->vval.v_number = VVAL_FALSE;
823 }
824 retval = OK;
825 break;
826 }
827 if (STRNICMP((char *)p, "true", 4) == 0)
828 {
829 reader->js_used += 4;
830 if (cur_item != NULL)
831 {
832 cur_item->v_type = VAR_SPECIAL;
833 cur_item->vval.v_number = VVAL_TRUE;
834 }
835 retval = OK;
836 break;
837 }
838 if (STRNICMP((char *)p, "null", 4) == 0)
839 {
840 reader->js_used += 4;
841 if (cur_item != NULL)
842 {
843 cur_item->v_type = VAR_SPECIAL;
844 cur_item->vval.v_number = VVAL_NULL;
845 }
846 retval = OK;
847 break;
848 }
849#ifdef FEAT_FLOAT
850 if (STRNICMP((char *)p, "NaN", 3) == 0)
851 {
852 reader->js_used += 3;
853 if (cur_item != NULL)
854 {
855 cur_item->v_type = VAR_FLOAT;
856 cur_item->vval.v_float = NAN;
857 }
858 retval = OK;
859 break;
860 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100861 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
862 {
863 reader->js_used += 9;
864 if (cur_item != NULL)
865 {
866 cur_item->v_type = VAR_FLOAT;
867 cur_item->vval.v_float = -INFINITY;
868 }
869 retval = OK;
870 break;
871 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100872 if (STRNICMP((char *)p, "Infinity", 8) == 0)
873 {
874 reader->js_used += 8;
875 if (cur_item != NULL)
876 {
877 cur_item->v_type = VAR_FLOAT;
878 cur_item->vval.v_float = INFINITY;
879 }
880 retval = OK;
881 break;
882 }
883#endif
884 /* check for truncated name */
885 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
886 if (
887 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
888#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100889 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100890 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
891 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
892#endif
893 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
894 || STRNICMP((char *)p, "null", len) == 0)))
895
896 retval = MAYBE;
897 else
898 retval = FAIL;
899 break;
900 }
901
902 /* We are finished when retval is FAIL or MAYBE and when at the
903 * toplevel. */
904 if (retval == FAIL)
905 break;
906 if (retval == MAYBE || stack.ga_len == 0)
907 goto theend;
908
909 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
910 && cur_item != NULL)
911 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100912 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100913 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100914 {
915 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100916 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100917 retval = FAIL;
918 goto theend;
919 }
920 }
921 }
922
923item_end:
924 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
925 switch (top_item->jd_type)
926 {
927 case JSON_ARRAY:
928 if (res != NULL)
929 {
930 listitem_T *li = listitem_alloc();
931
932 if (li == NULL)
933 {
934 clear_tv(cur_item);
935 retval = FAIL;
936 goto theend;
937 }
938 li->li_tv = *cur_item;
939 list_append(top_item->jd_tv.vval.v_list, li);
940 }
941 if (cur_item != NULL)
942 cur_item = &item;
943
944 json_skip_white(reader);
945 p = reader->js_buf + reader->js_used;
946 if (*p == ',')
947 ++reader->js_used;
948 else if (*p != ']')
949 {
950 if (*p == NUL)
951 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100952 else
953 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100954 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100955 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100956 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100957 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100958 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100959 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100960
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100961 case JSON_OBJECT_KEY:
962 json_skip_white(reader);
963 p = reader->js_buf + reader->js_used;
964 if (*p != ':')
965 {
966 if (cur_item != NULL)
967 clear_tv(cur_item);
968 if (*p == NUL)
969 retval = MAYBE;
970 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100971 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100972 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100973 retval = FAIL;
974 }
975 goto theend;
976 }
977 ++reader->js_used;
978 json_skip_white(reader);
979 top_item->jd_type = JSON_OBJECT;
980 if (cur_item != NULL)
981 cur_item = &item;
982 break;
983
984 case JSON_OBJECT:
985 if (cur_item != NULL
986 && dict_find(top_item->jd_tv.vval.v_dict,
987 top_item->jd_key, -1) != NULL)
988 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100989 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100990 top_item->jd_key);
991 clear_tv(&top_item->jd_key_tv);
992 clear_tv(cur_item);
993 retval = FAIL;
994 goto theend;
995 }
996
997 if (cur_item != NULL)
998 {
999 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1000
1001 clear_tv(&top_item->jd_key_tv);
1002 if (di == NULL)
1003 {
1004 clear_tv(cur_item);
1005 retval = FAIL;
1006 goto theend;
1007 }
1008 di->di_tv = *cur_item;
1009 di->di_tv.v_lock = 0;
1010 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1011 {
1012 dictitem_free(di);
1013 retval = FAIL;
1014 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001015 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001016 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001017
1018 json_skip_white(reader);
1019 p = reader->js_buf + reader->js_used;
1020 if (*p == ',')
1021 ++reader->js_used;
1022 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001023 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001024 if (*p == NUL)
1025 retval = MAYBE;
1026 else
1027 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001028 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001029 retval = FAIL;
1030 }
1031 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001032 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001033 top_item->jd_type = JSON_OBJECT_KEY;
1034 if (cur_item != NULL)
1035 cur_item = &top_item->jd_key_tv;
1036 break;
1037 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001038 }
1039
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001040 /* Get here when parsing failed. */
Bram Moolenaar7756e742016-10-21 20:35:37 +02001041 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001042 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001043 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001044 res->v_type = VAR_SPECIAL;
1045 res->vval.v_number = VVAL_NONE;
1046 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001047 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001048
1049theend:
1050 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001051 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001052}
1053
1054/*
1055 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001056 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001057 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001058 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001059 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001060json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001061{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001062 int ret;
1063
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001064 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001065 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001066 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001067 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001068 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001069 {
1070 if (ret == MAYBE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001071 emsg(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001072 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001073 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001074 json_skip_white(reader);
1075 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001076 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001077 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001078 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001079 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001080 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001081}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001082
Bram Moolenaar113e1072019-01-20 15:30:40 +01001083#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001084/*
1085 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001086 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001087 * Return FAIL for a decoding error.
1088 * Return MAYBE for an incomplete message.
1089 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001090 */
1091 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001092json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001093{
1094 int ret;
1095
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001096 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001097 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1098 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001099 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001100 json_skip_white(reader);
1101
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001102 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001103}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001104#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001105
1106/*
1107 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001108 * "options" can be JSON_JS or zero.
1109 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001110 * Return FAIL if the message has a decoding error.
1111 * Return MAYBE if the message is truncated, need to read more.
1112 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001113 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001114 * Does not advance the reader.
1115 */
1116 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001117json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001118{
1119 int used_save = reader->js_used;
1120 int ret;
1121
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001122 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001123 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1124 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001125 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001126 reader->js_used = used_save;
1127 return ret;
1128}
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001129#endif