blob: 8674bf26595bc18beb03b7105994d124574fcd98 [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 {
458 ga_clear(&ga);
459 return FAIL;
460 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100461 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100462 if (0xd800 <= nr && nr <= 0xdfff
463 && (int)(reader->js_end - p) >= 6
464 && *p == '\\' && *(p+1) == 'u')
465 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200466 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100467
468 /* decode surrogate pair: \ud812\u3456 */
469 len = 0;
470 vim_str2nr(p + 2, NULL, &len,
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200471 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
472 if (len == 0)
473 {
474 ga_clear(&ga);
475 return FAIL;
476 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100477 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
478 {
479 p += len + 2;
480 nr = (((nr - 0xd800) << 10) |
481 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
482 }
483 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100484 if (res != NULL)
485 {
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200486 char_u buf[NUMBUFLEN];
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100487 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100488 ga_concat(&ga, buf);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100489 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100490 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100491 default:
492 /* not a special char, skip over \ */
493 ++p;
494 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100495 }
496 if (c > 0)
497 {
498 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100499 if (res != NULL)
500 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100501 }
502 }
503 else
504 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100505 len = utf_ptr2len(p);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100506 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100507 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100508 if (ga_grow(&ga, len) == FAIL)
509 {
510 ga_clear(&ga);
511 return FAIL;
512 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100513 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
514 ga.ga_len += len;
515 }
516 p += len;
517 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100518 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100519
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100520 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100521 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100522 {
523 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100524 if (res != NULL)
525 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100526 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100527 res->v_type = VAR_STRING;
Bram Moolenaarfc3abf42019-01-24 15:54:21 +0100528#if defined(USE_ICONV)
Bram Moolenaarb3628722016-02-28 14:56:39 +0100529 if (!enc_utf8)
530 {
531 vimconv_T conv;
532
533 /* Convert the utf-8 string to 'encoding'. */
534 conv.vc_type = CONV_NONE;
535 convert_setup(&conv, (char_u*)"utf-8", p_enc);
536 if (conv.vc_type != CONV_NONE)
537 {
538 res->vval.v_string =
539 string_convert(&conv, ga.ga_data, NULL);
540 vim_free(ga.ga_data);
541 }
542 convert_setup(&conv, NULL, NULL);
543 }
544 else
545#endif
546 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100547 }
548 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100549 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100550 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100551 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100552 res->v_type = VAR_SPECIAL;
553 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100554 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100555 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100556 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100557}
558
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100559typedef enum {
560 JSON_ARRAY, /* parsing items in an array */
561 JSON_OBJECT_KEY, /* parsing key of an object */
562 JSON_OBJECT /* parsing item in an object, after the key */
563} json_decode_T;
564
565typedef struct {
566 json_decode_T jd_type;
567 typval_T jd_tv; /* the list or dict */
568 typval_T jd_key_tv;
569 char_u *jd_key;
570} json_dec_item_T;
571
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100572/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100573 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100574 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100575 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100576 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100577 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100578 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100579 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100580json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100581{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100582 char_u *p;
583 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100584 int retval;
585 garray_T stack;
586 typval_T item;
587 typval_T *cur_item;
588 json_dec_item_T *top_item;
589 char_u key_buf[NUMBUFLEN];
590
591 ga_init2(&stack, sizeof(json_dec_item_T), 100);
592 cur_item = res;
593 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100594 if (res != NULL)
595 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100596
Bram Moolenaar56ead342016-02-02 18:20:08 +0100597 fill_numbuflen(reader);
598 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100599 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100600 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100601 top_item = NULL;
602 if (stack.ga_len > 0)
603 {
604 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
605 json_skip_white(reader);
606 p = reader->js_buf + reader->js_used;
607 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100608 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100609 retval = MAYBE;
610 if (top_item->jd_type == JSON_OBJECT)
611 /* did get the key, clear it */
612 clear_tv(&top_item->jd_key_tv);
613 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100614 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100615 if (top_item->jd_type == JSON_OBJECT_KEY
616 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100617 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100618 /* Check for end of object or array. */
619 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100620 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100621 ++reader->js_used; /* consume the ']' or '}' */
622 --stack.ga_len;
623 if (stack.ga_len == 0)
624 {
625 retval = OK;
626 goto theend;
627 }
628 if (cur_item != NULL)
629 cur_item = &top_item->jd_tv;
630 goto item_end;
631 }
632 }
633 }
634
635 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
636 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100637 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100638 && reader->js_buf[reader->js_used] != '\''
639 && reader->js_buf[reader->js_used] != '['
640 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100641 {
642 char_u *key;
643
644 /* accept an object key that is not in quotes */
645 key = p = reader->js_buf + reader->js_used;
646 while (*p != NUL && *p != ':' && *p > ' ')
647 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100648 if (cur_item != NULL)
649 {
650 cur_item->v_type = VAR_STRING;
651 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
652 top_item->jd_key = cur_item->vval.v_string;
653 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100654 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100655 }
656 else
657 {
658 switch (*p)
659 {
660 case '[': /* start of array */
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100661 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
662 {
663 retval = FAIL;
664 break;
665 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100666 if (ga_grow(&stack, 1) == FAIL)
667 {
668 retval = FAIL;
669 break;
670 }
671 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
672 {
673 cur_item->v_type = VAR_SPECIAL;
674 cur_item->vval.v_number = VVAL_NONE;
675 retval = FAIL;
676 break;
677 }
678
679 ++reader->js_used; /* consume the '[' */
680 top_item = ((json_dec_item_T *)stack.ga_data)
681 + stack.ga_len;
682 top_item->jd_type = JSON_ARRAY;
683 ++stack.ga_len;
684 if (cur_item != NULL)
685 {
686 top_item->jd_tv = *cur_item;
687 cur_item = &item;
688 }
689 continue;
690
691 case '{': /* start of object */
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100692 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
693 {
694 retval = FAIL;
695 break;
696 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100697 if (ga_grow(&stack, 1) == FAIL)
698 {
699 retval = FAIL;
700 break;
701 }
702 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
703 {
704 cur_item->v_type = VAR_SPECIAL;
705 cur_item->vval.v_number = VVAL_NONE;
706 retval = FAIL;
707 break;
708 }
709
710 ++reader->js_used; /* consume the '{' */
711 top_item = ((json_dec_item_T *)stack.ga_data)
712 + stack.ga_len;
713 top_item->jd_type = JSON_OBJECT_KEY;
714 ++stack.ga_len;
715 if (cur_item != NULL)
716 {
717 top_item->jd_tv = *cur_item;
718 cur_item = &top_item->jd_key_tv;
719 }
720 continue;
721
722 case '"': /* string */
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100723 retval = json_decode_string(reader, cur_item, *p);
724 break;
725
726 case '\'':
727 if (options & JSON_JS)
728 retval = json_decode_string(reader, cur_item, *p);
729 else
730 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100731 emsg(_(e_invarg));
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100732 retval = FAIL;
733 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100734 break;
735
736 case ',': /* comma: empty item */
737 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100738 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100739 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100740 retval = FAIL;
741 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100742 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100743 /* FALLTHROUGH */
744 case NUL: /* empty */
745 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100746 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100747 cur_item->v_type = VAR_SPECIAL;
748 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100749 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100750 retval = OK;
751 break;
752
753 default:
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100754 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100755 {
756#ifdef FEAT_FLOAT
757 char_u *sp = p;
758
759 if (*sp == '-')
760 {
761 ++sp;
762 if (*sp == NUL)
763 {
764 retval = MAYBE;
765 break;
766 }
767 if (!VIM_ISDIGIT(*sp))
768 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100769 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100770 retval = FAIL;
771 break;
772 }
773 }
774 sp = skipdigits(sp);
775 if (*sp == '.' || *sp == 'e' || *sp == 'E')
776 {
777 if (cur_item == NULL)
778 {
779 float_T f;
780
781 len = string2float(p, &f);
782 }
783 else
784 {
785 cur_item->v_type = VAR_FLOAT;
786 len = string2float(p, &cur_item->vval.v_float);
787 }
788 }
789 else
790#endif
791 {
792 varnumber_T nr;
793
794 vim_str2nr(reader->js_buf + reader->js_used,
795 NULL, &len, 0, /* what */
Bram Moolenaar16e9b852019-05-19 19:59:35 +0200796 &nr, NULL, 0, TRUE);
797 if (len == 0)
798 {
799 emsg(_(e_invarg));
800 retval = FAIL;
801 goto theend;
802 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100803 if (cur_item != NULL)
804 {
805 cur_item->v_type = VAR_NUMBER;
806 cur_item->vval.v_number = nr;
807 }
808 }
809 reader->js_used += len;
810 retval = OK;
811 break;
812 }
813 if (STRNICMP((char *)p, "false", 5) == 0)
814 {
815 reader->js_used += 5;
816 if (cur_item != NULL)
817 {
818 cur_item->v_type = VAR_SPECIAL;
819 cur_item->vval.v_number = VVAL_FALSE;
820 }
821 retval = OK;
822 break;
823 }
824 if (STRNICMP((char *)p, "true", 4) == 0)
825 {
826 reader->js_used += 4;
827 if (cur_item != NULL)
828 {
829 cur_item->v_type = VAR_SPECIAL;
830 cur_item->vval.v_number = VVAL_TRUE;
831 }
832 retval = OK;
833 break;
834 }
835 if (STRNICMP((char *)p, "null", 4) == 0)
836 {
837 reader->js_used += 4;
838 if (cur_item != NULL)
839 {
840 cur_item->v_type = VAR_SPECIAL;
841 cur_item->vval.v_number = VVAL_NULL;
842 }
843 retval = OK;
844 break;
845 }
846#ifdef FEAT_FLOAT
847 if (STRNICMP((char *)p, "NaN", 3) == 0)
848 {
849 reader->js_used += 3;
850 if (cur_item != NULL)
851 {
852 cur_item->v_type = VAR_FLOAT;
853 cur_item->vval.v_float = NAN;
854 }
855 retval = OK;
856 break;
857 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100858 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
859 {
860 reader->js_used += 9;
861 if (cur_item != NULL)
862 {
863 cur_item->v_type = VAR_FLOAT;
864 cur_item->vval.v_float = -INFINITY;
865 }
866 retval = OK;
867 break;
868 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100869 if (STRNICMP((char *)p, "Infinity", 8) == 0)
870 {
871 reader->js_used += 8;
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 }
880#endif
881 /* check for truncated name */
882 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
883 if (
884 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
885#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100886 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100887 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
888 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
889#endif
890 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
891 || STRNICMP((char *)p, "null", len) == 0)))
892
893 retval = MAYBE;
894 else
895 retval = FAIL;
896 break;
897 }
898
899 /* We are finished when retval is FAIL or MAYBE and when at the
900 * toplevel. */
901 if (retval == FAIL)
902 break;
903 if (retval == MAYBE || stack.ga_len == 0)
904 goto theend;
905
906 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
907 && cur_item != NULL)
908 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100909 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100910 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100911 {
912 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100913 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100914 retval = FAIL;
915 goto theend;
916 }
917 }
918 }
919
920item_end:
921 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
922 switch (top_item->jd_type)
923 {
924 case JSON_ARRAY:
925 if (res != NULL)
926 {
927 listitem_T *li = listitem_alloc();
928
929 if (li == NULL)
930 {
931 clear_tv(cur_item);
932 retval = FAIL;
933 goto theend;
934 }
935 li->li_tv = *cur_item;
936 list_append(top_item->jd_tv.vval.v_list, li);
937 }
938 if (cur_item != NULL)
939 cur_item = &item;
940
941 json_skip_white(reader);
942 p = reader->js_buf + reader->js_used;
943 if (*p == ',')
944 ++reader->js_used;
945 else if (*p != ']')
946 {
947 if (*p == NUL)
948 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100949 else
950 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100951 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100952 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100953 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100954 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100955 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100956 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100957
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100958 case JSON_OBJECT_KEY:
959 json_skip_white(reader);
960 p = reader->js_buf + reader->js_used;
961 if (*p != ':')
962 {
963 if (cur_item != NULL)
964 clear_tv(cur_item);
965 if (*p == NUL)
966 retval = MAYBE;
967 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100968 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100969 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100970 retval = FAIL;
971 }
972 goto theend;
973 }
974 ++reader->js_used;
975 json_skip_white(reader);
976 top_item->jd_type = JSON_OBJECT;
977 if (cur_item != NULL)
978 cur_item = &item;
979 break;
980
981 case JSON_OBJECT:
982 if (cur_item != NULL
983 && dict_find(top_item->jd_tv.vval.v_dict,
984 top_item->jd_key, -1) != NULL)
985 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100986 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100987 top_item->jd_key);
988 clear_tv(&top_item->jd_key_tv);
989 clear_tv(cur_item);
990 retval = FAIL;
991 goto theend;
992 }
993
994 if (cur_item != NULL)
995 {
996 dictitem_T *di = dictitem_alloc(top_item->jd_key);
997
998 clear_tv(&top_item->jd_key_tv);
999 if (di == NULL)
1000 {
1001 clear_tv(cur_item);
1002 retval = FAIL;
1003 goto theend;
1004 }
1005 di->di_tv = *cur_item;
1006 di->di_tv.v_lock = 0;
1007 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1008 {
1009 dictitem_free(di);
1010 retval = FAIL;
1011 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001012 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001013 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001014
1015 json_skip_white(reader);
1016 p = reader->js_buf + reader->js_used;
1017 if (*p == ',')
1018 ++reader->js_used;
1019 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001020 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001021 if (*p == NUL)
1022 retval = MAYBE;
1023 else
1024 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001025 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001026 retval = FAIL;
1027 }
1028 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001029 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001030 top_item->jd_type = JSON_OBJECT_KEY;
1031 if (cur_item != NULL)
1032 cur_item = &top_item->jd_key_tv;
1033 break;
1034 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001035 }
1036
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001037 /* Get here when parsing failed. */
Bram Moolenaar7756e742016-10-21 20:35:37 +02001038 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001039 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001040 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001041 res->v_type = VAR_SPECIAL;
1042 res->vval.v_number = VVAL_NONE;
1043 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001044 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001045
1046theend:
1047 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001048 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001049}
1050
1051/*
1052 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001053 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001054 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001055 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001056 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001057json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001058{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001059 int ret;
1060
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001061 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001062 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001063 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001064 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001065 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001066 {
1067 if (ret == MAYBE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001068 emsg(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001069 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001070 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001071 json_skip_white(reader);
1072 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001073 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001074 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001075 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001076 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001077 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001078}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001079
Bram Moolenaar113e1072019-01-20 15:30:40 +01001080#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001081/*
1082 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001083 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001084 * Return FAIL for a decoding error.
1085 * Return MAYBE for an incomplete message.
1086 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001087 */
1088 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001089json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001090{
1091 int ret;
1092
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001093 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001094 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1095 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001096 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001097 json_skip_white(reader);
1098
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001099 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001100}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001101#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001102
1103/*
1104 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001105 * "options" can be JSON_JS or zero.
1106 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001107 * Return FAIL if the message has a decoding error.
1108 * Return MAYBE if the message is truncated, need to read more.
1109 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001110 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001111 * Does not advance the reader.
1112 */
1113 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001114json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001115{
1116 int used_save = reader->js_used;
1117 int ret;
1118
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001119 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001120 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1121 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001122 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001123 reader->js_used = used_save;
1124 return ret;
1125}
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001126#endif