blob: 6ed11ad42e9b413ac7615265d799cf85b0b0929b [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 Moolenaar520e1e42016-01-23 19:46:28 +010054 return ga.ga_data;
55}
56
Bram Moolenaar113e1072019-01-20 15:30:40 +010057#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010058/*
Bram Moolenaar55fab432016-02-07 16:53:13 +010059 * Encode ["nr", "val"] into a JSON format string in allocated memory.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020060 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010061 * Returns NULL when out of memory.
62 */
63 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010064json_encode_nr_expr(int nr, typval_T *val, int options)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010065{
66 typval_T listtv;
67 typval_T nrtv;
Bram Moolenaarf1f07922016-08-26 17:58:53 +020068 garray_T ga;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010069
70 nrtv.v_type = VAR_NUMBER;
71 nrtv.vval.v_number = nr;
72 if (rettv_list_alloc(&listtv) == FAIL)
73 return NULL;
74 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
75 || list_append_tv(listtv.vval.v_list, val) == FAIL)
76 {
77 list_unref(listtv.vval.v_list);
78 return NULL;
79 }
80
Bram Moolenaarf1f07922016-08-26 17:58:53 +020081 ga_init2(&ga, 1, 4000);
82 if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL))
83 ga_append(&ga, '\n');
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010084 list_unref(listtv.vval.v_list);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020085 return ga.ga_data;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010086}
Bram Moolenaar113e1072019-01-20 15:30:40 +010087#endif
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010088
Bram Moolenaar520e1e42016-01-23 19:46:28 +010089 static void
90write_string(garray_T *gap, char_u *str)
91{
92 char_u *res = str;
93 char_u numbuf[NUMBUFLEN];
94
95 if (res == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +010096 ga_concat(gap, (char_u *)"\"\"");
Bram Moolenaar520e1e42016-01-23 19:46:28 +010097 else
98 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +010099#if defined(FEAT_MBYTE) && defined(USE_ICONV)
100 vimconv_T conv;
101 char_u *converted = NULL;
102
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100103 if (!enc_utf8)
104 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100105 /* Convert the text from 'encoding' to utf-8, the JSON string is
106 * always utf-8. */
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100107 conv.vc_type = CONV_NONE;
108 convert_setup(&conv, p_enc, (char_u*)"utf-8");
109 if (conv.vc_type != CONV_NONE)
110 converted = res = string_convert(&conv, res, NULL);
111 convert_setup(&conv, NULL, NULL);
112 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100113#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100114 ga_append(gap, '"');
115 while (*res != NUL)
116 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100117 int c;
118#ifdef FEAT_MBYTE
119 /* always use utf-8 encoding, ignore 'encoding' */
120 c = utf_ptr2char(res);
121#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100122 c = *res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100123#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100124
125 switch (c)
126 {
127 case 0x08:
128 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
129 case 0x09:
130 ga_append(gap, '\\'); ga_append(gap, 't'); break;
131 case 0x0a:
132 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
133 case 0x0c:
134 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
135 case 0x0d:
136 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
137 case 0x22: /* " */
138 case 0x5c: /* \ */
139 ga_append(gap, '\\');
140 ga_append(gap, c);
141 break;
142 default:
143 if (c >= 0x20)
144 {
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100145#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100146 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100147#else
148 numbuf[0] = c;
149 numbuf[1] = NUL;
150#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100151 ga_concat(gap, numbuf);
152 }
153 else
154 {
155 vim_snprintf((char *)numbuf, NUMBUFLEN,
156 "\\u%04lx", (long)c);
157 ga_concat(gap, numbuf);
158 }
159 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100160#ifdef FEAT_MBYTE
161 res += utf_ptr2len(res);
162#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100163 ++res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100164#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100165 }
166 ga_append(gap, '"');
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100167#if defined(FEAT_MBYTE) && defined(USE_ICONV)
168 vim_free(converted);
169#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100170 }
171}
172
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100173/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100174 * Return TRUE if "key" can be used without quotes.
175 * That is when it starts with a letter and only contains letters, digits and
176 * underscore.
177 */
178 static int
179is_simple_key(char_u *key)
180{
181 char_u *p;
182
183 if (!ASCII_ISALPHA(*key))
184 return FALSE;
185 for (p = key + 1; *p != NUL; ++p)
186 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
187 return FALSE;
188 return TRUE;
189}
190
191/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100192 * Encode "val" into "gap".
193 * Return FAIL or OK.
194 */
195 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100196json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100197{
198 char_u numbuf[NUMBUFLEN];
199 char_u *res;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100200 blob_T *b;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100201 list_T *l;
202 dict_T *d;
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100203 int i;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100204
205 switch (val->v_type)
206 {
207 case VAR_SPECIAL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100208 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100209 {
210 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
211 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100212 case VVAL_NONE: if ((options & JSON_JS) != 0
213 && (options & JSON_NO_NONE) == 0)
214 /* empty item */
215 break;
216 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100217 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
218 }
219 break;
220
221 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200222 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaar88c86eb2019-01-17 17:13:30 +0100223 (long_long_T)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100224 ga_concat(gap, numbuf);
225 break;
226
227 case VAR_STRING:
228 res = val->vval.v_string;
229 write_string(gap, res);
230 break;
231
232 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100233 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100234 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100235 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100236 /* no JSON equivalent TODO: better error */
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100237 emsg(_(e_invarg));
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100238 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100239
Bram Moolenaar6e5ea8d2019-01-12 22:47:31 +0100240 case VAR_BLOB:
241 b = val->vval.v_blob;
242 if (b == NULL || b->bv_ga.ga_len == 0)
243 ga_concat(gap, (char_u *)"[]");
244 else
245 {
246 ga_append(gap, '[');
247 for (i = 0; i < b->bv_ga.ga_len; i++)
248 {
249 if (i > 0)
250 ga_concat(gap, (char_u *)",");
251 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
252 (int)blob_get(b, i));
253 ga_concat(gap, numbuf);
254 }
255 ga_append(gap, ']');
256 }
257 break;
258
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100259 case VAR_LIST:
260 l = val->vval.v_list;
261 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100262 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100263 else
264 {
265 if (l->lv_copyID == copyID)
266 ga_concat(gap, (char_u *)"[]");
267 else
268 {
269 listitem_T *li;
270
271 l->lv_copyID = copyID;
272 ga_append(gap, '[');
273 for (li = l->lv_first; li != NULL && !got_int; )
274 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100275 if (json_encode_item(gap, &li->li_tv, copyID,
276 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100277 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100278 if ((options & JSON_JS)
279 && li->li_next == NULL
280 && li->li_tv.v_type == VAR_SPECIAL
281 && li->li_tv.vval.v_number == VVAL_NONE)
282 /* add an extra comma if the last item is v:none */
283 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100284 li = li->li_next;
285 if (li != NULL)
286 ga_append(gap, ',');
287 }
288 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100289 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100290 }
291 }
292 break;
293
294 case VAR_DICT:
295 d = val->vval.v_dict;
296 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100297 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100298 else
299 {
300 if (d->dv_copyID == copyID)
301 ga_concat(gap, (char_u *)"{}");
302 else
303 {
304 int first = TRUE;
305 int todo = (int)d->dv_hashtab.ht_used;
306 hashitem_T *hi;
307
308 d->dv_copyID = copyID;
309 ga_append(gap, '{');
310
311 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
312 ++hi)
313 if (!HASHITEM_EMPTY(hi))
314 {
315 --todo;
316 if (first)
317 first = FALSE;
318 else
319 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100320 if ((options & JSON_JS)
321 && is_simple_key(hi->hi_key))
322 ga_concat(gap, hi->hi_key);
323 else
324 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100325 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100326 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100327 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100328 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100329 }
330 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100331 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100332 }
333 }
334 break;
335
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100336 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100337#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100338# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100339 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100340 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100341 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100342 {
343 if (val->vval.v_float < 0.0)
344 ga_concat(gap, (char_u *)"-Infinity");
345 else
346 ga_concat(gap, (char_u *)"Infinity");
347 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100348 else
349# endif
350 {
351 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
352 val->vval.v_float);
353 ga_concat(gap, numbuf);
354 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100355 break;
356#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100357 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +0100358 internal_error("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100359 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100360 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100361 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100362}
363
364/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100365 * When "reader" has less than NUMBUFLEN bytes available, call the fill
366 * callback to get more.
367 */
368 static void
369fill_numbuflen(js_read_T *reader)
370{
371 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
372 - reader->js_used < NUMBUFLEN)
373 {
374 if (reader->js_fill(reader))
375 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
376 }
377}
378
379/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100380 * Skip white space in "reader". All characters <= space are considered white
381 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100382 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100383 */
384 static void
385json_skip_white(js_read_T *reader)
386{
387 int c;
388
Bram Moolenaar56ead342016-02-02 18:20:08 +0100389 for (;;)
390 {
391 c = reader->js_buf[reader->js_used];
392 if (reader->js_fill != NULL && c == NUL)
393 {
394 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200395 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100396 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200397 continue;
398 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100399 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100400 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100401 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100402 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100403 }
404 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100405}
406
Bram Moolenaar56ead342016-02-02 18:20:08 +0100407 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100408json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100409{
410 garray_T ga;
411 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100412 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100413 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200414 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100415
Bram Moolenaar56ead342016-02-02 18:20:08 +0100416 if (res != NULL)
417 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100418
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100419 p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */
420 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100421 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100422 /* The JSON is always expected to be utf-8, thus use utf functions
423 * here. The string is converted below if needed. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100424 if (*p == NUL || p[1] == NUL
425#ifdef FEAT_MBYTE
426 || utf_ptr2len(p) < utf_byte2len(*p)
427#endif
428 )
429 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100430 /* Not enough bytes to make a character or end of the string. Get
431 * more if possible. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100432 if (reader->js_fill == NULL)
433 break;
434 len = (int)(reader->js_end - p);
435 reader->js_used = (int)(p - reader->js_buf);
436 if (!reader->js_fill(reader))
437 break; /* didn't get more */
438 p = reader->js_buf + reader->js_used;
439 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
440 continue;
441 }
442
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100443 if (*p == '\\')
444 {
445 c = -1;
446 switch (p[1])
447 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100448 case '\\': c = '\\'; break;
449 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100450 case 'b': c = BS; break;
451 case 't': c = TAB; break;
452 case 'n': c = NL; break;
453 case 'f': c = FF; break;
454 case 'r': c = CAR; break;
455 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100456 if (reader->js_fill != NULL
457 && (int)(reader->js_end - p) < NUMBUFLEN)
458 {
459 reader->js_used = (int)(p - reader->js_buf);
460 if (reader->js_fill(reader))
461 {
462 p = reader->js_buf + reader->js_used;
463 reader->js_end = reader->js_buf
464 + STRLEN(reader->js_buf);
465 }
466 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100467 nr = 0;
468 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100469 vim_str2nr(p + 2, NULL, &len,
470 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
471 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100472 if (0xd800 <= nr && nr <= 0xdfff
473 && (int)(reader->js_end - p) >= 6
474 && *p == '\\' && *(p+1) == 'u')
475 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200476 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100477
478 /* decode surrogate pair: \ud812\u3456 */
479 len = 0;
480 vim_str2nr(p + 2, NULL, &len,
481 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4);
482 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
483 {
484 p += len + 2;
485 nr = (((nr - 0xd800) << 10) |
486 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
487 }
488 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100489 if (res != NULL)
490 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100491#ifdef FEAT_MBYTE
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200492 char_u buf[NUMBUFLEN];
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100493 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100494 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100495#else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200496 ga_append(&ga, (int)nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100497#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100498 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100499 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100500 default:
501 /* not a special char, skip over \ */
502 ++p;
503 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100504 }
505 if (c > 0)
506 {
507 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100508 if (res != NULL)
509 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100510 }
511 }
512 else
513 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100514#ifdef FEAT_MBYTE
515 len = utf_ptr2len(p);
516#else
517 len = 1;
518#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100519 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100520 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100521 if (ga_grow(&ga, len) == FAIL)
522 {
523 ga_clear(&ga);
524 return FAIL;
525 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100526 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
527 ga.ga_len += len;
528 }
529 p += len;
530 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100531 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100532
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100533 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100534 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100535 {
536 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100537 if (res != NULL)
538 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100539 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100540 res->v_type = VAR_STRING;
Bram Moolenaarb3628722016-02-28 14:56:39 +0100541#if defined(FEAT_MBYTE) && defined(USE_ICONV)
542 if (!enc_utf8)
543 {
544 vimconv_T conv;
545
546 /* Convert the utf-8 string to 'encoding'. */
547 conv.vc_type = CONV_NONE;
548 convert_setup(&conv, (char_u*)"utf-8", p_enc);
549 if (conv.vc_type != CONV_NONE)
550 {
551 res->vval.v_string =
552 string_convert(&conv, ga.ga_data, NULL);
553 vim_free(ga.ga_data);
554 }
555 convert_setup(&conv, NULL, NULL);
556 }
557 else
558#endif
559 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100560 }
561 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100562 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100563 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100564 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100565 res->v_type = VAR_SPECIAL;
566 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100567 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100568 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100569 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100570}
571
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100572typedef enum {
573 JSON_ARRAY, /* parsing items in an array */
574 JSON_OBJECT_KEY, /* parsing key of an object */
575 JSON_OBJECT /* parsing item in an object, after the key */
576} json_decode_T;
577
578typedef struct {
579 json_decode_T jd_type;
580 typval_T jd_tv; /* the list or dict */
581 typval_T jd_key_tv;
582 char_u *jd_key;
583} json_dec_item_T;
584
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100585/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100586 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100587 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100588 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100589 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100590 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100591 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100592 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100593json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100594{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100595 char_u *p;
596 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100597 int retval;
598 garray_T stack;
599 typval_T item;
600 typval_T *cur_item;
601 json_dec_item_T *top_item;
602 char_u key_buf[NUMBUFLEN];
603
604 ga_init2(&stack, sizeof(json_dec_item_T), 100);
605 cur_item = res;
606 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100607 if (res != NULL)
608 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100609
Bram Moolenaar56ead342016-02-02 18:20:08 +0100610 fill_numbuflen(reader);
611 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100612 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100613 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100614 top_item = NULL;
615 if (stack.ga_len > 0)
616 {
617 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
618 json_skip_white(reader);
619 p = reader->js_buf + reader->js_used;
620 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100621 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100622 retval = MAYBE;
623 if (top_item->jd_type == JSON_OBJECT)
624 /* did get the key, clear it */
625 clear_tv(&top_item->jd_key_tv);
626 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100627 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100628 if (top_item->jd_type == JSON_OBJECT_KEY
629 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100630 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100631 /* Check for end of object or array. */
632 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100633 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100634 ++reader->js_used; /* consume the ']' or '}' */
635 --stack.ga_len;
636 if (stack.ga_len == 0)
637 {
638 retval = OK;
639 goto theend;
640 }
641 if (cur_item != NULL)
642 cur_item = &top_item->jd_tv;
643 goto item_end;
644 }
645 }
646 }
647
648 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
649 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100650 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100651 && reader->js_buf[reader->js_used] != '\''
652 && reader->js_buf[reader->js_used] != '['
653 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100654 {
655 char_u *key;
656
657 /* accept an object key that is not in quotes */
658 key = p = reader->js_buf + reader->js_used;
659 while (*p != NUL && *p != ':' && *p > ' ')
660 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100661 if (cur_item != NULL)
662 {
663 cur_item->v_type = VAR_STRING;
664 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
665 top_item->jd_key = cur_item->vval.v_string;
666 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100667 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100668 }
669 else
670 {
671 switch (*p)
672 {
673 case '[': /* start of array */
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100674 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
675 {
676 retval = FAIL;
677 break;
678 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100679 if (ga_grow(&stack, 1) == FAIL)
680 {
681 retval = FAIL;
682 break;
683 }
684 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
685 {
686 cur_item->v_type = VAR_SPECIAL;
687 cur_item->vval.v_number = VVAL_NONE;
688 retval = FAIL;
689 break;
690 }
691
692 ++reader->js_used; /* consume the '[' */
693 top_item = ((json_dec_item_T *)stack.ga_data)
694 + stack.ga_len;
695 top_item->jd_type = JSON_ARRAY;
696 ++stack.ga_len;
697 if (cur_item != NULL)
698 {
699 top_item->jd_tv = *cur_item;
700 cur_item = &item;
701 }
702 continue;
703
704 case '{': /* start of object */
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100705 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
706 {
707 retval = FAIL;
708 break;
709 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100710 if (ga_grow(&stack, 1) == FAIL)
711 {
712 retval = FAIL;
713 break;
714 }
715 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
716 {
717 cur_item->v_type = VAR_SPECIAL;
718 cur_item->vval.v_number = VVAL_NONE;
719 retval = FAIL;
720 break;
721 }
722
723 ++reader->js_used; /* consume the '{' */
724 top_item = ((json_dec_item_T *)stack.ga_data)
725 + stack.ga_len;
726 top_item->jd_type = JSON_OBJECT_KEY;
727 ++stack.ga_len;
728 if (cur_item != NULL)
729 {
730 top_item->jd_tv = *cur_item;
731 cur_item = &top_item->jd_key_tv;
732 }
733 continue;
734
735 case '"': /* string */
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100736 retval = json_decode_string(reader, cur_item, *p);
737 break;
738
739 case '\'':
740 if (options & JSON_JS)
741 retval = json_decode_string(reader, cur_item, *p);
742 else
743 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100744 emsg(_(e_invarg));
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100745 retval = FAIL;
746 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100747 break;
748
749 case ',': /* comma: empty item */
750 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100751 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100752 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100753 retval = FAIL;
754 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100755 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100756 /* FALLTHROUGH */
757 case NUL: /* empty */
758 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100759 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100760 cur_item->v_type = VAR_SPECIAL;
761 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100762 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100763 retval = OK;
764 break;
765
766 default:
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100767 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100768 {
769#ifdef FEAT_FLOAT
770 char_u *sp = p;
771
772 if (*sp == '-')
773 {
774 ++sp;
775 if (*sp == NUL)
776 {
777 retval = MAYBE;
778 break;
779 }
780 if (!VIM_ISDIGIT(*sp))
781 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100782 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100783 retval = FAIL;
784 break;
785 }
786 }
787 sp = skipdigits(sp);
788 if (*sp == '.' || *sp == 'e' || *sp == 'E')
789 {
790 if (cur_item == NULL)
791 {
792 float_T f;
793
794 len = string2float(p, &f);
795 }
796 else
797 {
798 cur_item->v_type = VAR_FLOAT;
799 len = string2float(p, &cur_item->vval.v_float);
800 }
801 }
802 else
803#endif
804 {
805 varnumber_T nr;
806
807 vim_str2nr(reader->js_buf + reader->js_used,
808 NULL, &len, 0, /* what */
809 &nr, NULL, 0);
810 if (cur_item != NULL)
811 {
812 cur_item->v_type = VAR_NUMBER;
813 cur_item->vval.v_number = nr;
814 }
815 }
816 reader->js_used += len;
817 retval = OK;
818 break;
819 }
820 if (STRNICMP((char *)p, "false", 5) == 0)
821 {
822 reader->js_used += 5;
823 if (cur_item != NULL)
824 {
825 cur_item->v_type = VAR_SPECIAL;
826 cur_item->vval.v_number = VVAL_FALSE;
827 }
828 retval = OK;
829 break;
830 }
831 if (STRNICMP((char *)p, "true", 4) == 0)
832 {
833 reader->js_used += 4;
834 if (cur_item != NULL)
835 {
836 cur_item->v_type = VAR_SPECIAL;
837 cur_item->vval.v_number = VVAL_TRUE;
838 }
839 retval = OK;
840 break;
841 }
842 if (STRNICMP((char *)p, "null", 4) == 0)
843 {
844 reader->js_used += 4;
845 if (cur_item != NULL)
846 {
847 cur_item->v_type = VAR_SPECIAL;
848 cur_item->vval.v_number = VVAL_NULL;
849 }
850 retval = OK;
851 break;
852 }
853#ifdef FEAT_FLOAT
854 if (STRNICMP((char *)p, "NaN", 3) == 0)
855 {
856 reader->js_used += 3;
857 if (cur_item != NULL)
858 {
859 cur_item->v_type = VAR_FLOAT;
860 cur_item->vval.v_float = NAN;
861 }
862 retval = OK;
863 break;
864 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100865 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
866 {
867 reader->js_used += 9;
868 if (cur_item != NULL)
869 {
870 cur_item->v_type = VAR_FLOAT;
871 cur_item->vval.v_float = -INFINITY;
872 }
873 retval = OK;
874 break;
875 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100876 if (STRNICMP((char *)p, "Infinity", 8) == 0)
877 {
878 reader->js_used += 8;
879 if (cur_item != NULL)
880 {
881 cur_item->v_type = VAR_FLOAT;
882 cur_item->vval.v_float = INFINITY;
883 }
884 retval = OK;
885 break;
886 }
887#endif
888 /* check for truncated name */
889 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
890 if (
891 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
892#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100893 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100894 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
895 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
896#endif
897 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
898 || STRNICMP((char *)p, "null", len) == 0)))
899
900 retval = MAYBE;
901 else
902 retval = FAIL;
903 break;
904 }
905
906 /* We are finished when retval is FAIL or MAYBE and when at the
907 * toplevel. */
908 if (retval == FAIL)
909 break;
910 if (retval == MAYBE || stack.ga_len == 0)
911 goto theend;
912
913 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
914 && cur_item != NULL)
915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100916 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100917 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100918 {
919 clear_tv(cur_item);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100920 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100921 retval = FAIL;
922 goto theend;
923 }
924 }
925 }
926
927item_end:
928 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
929 switch (top_item->jd_type)
930 {
931 case JSON_ARRAY:
932 if (res != NULL)
933 {
934 listitem_T *li = listitem_alloc();
935
936 if (li == NULL)
937 {
938 clear_tv(cur_item);
939 retval = FAIL;
940 goto theend;
941 }
942 li->li_tv = *cur_item;
943 list_append(top_item->jd_tv.vval.v_list, li);
944 }
945 if (cur_item != NULL)
946 cur_item = &item;
947
948 json_skip_white(reader);
949 p = reader->js_buf + reader->js_used;
950 if (*p == ',')
951 ++reader->js_used;
952 else if (*p != ']')
953 {
954 if (*p == NUL)
955 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100956 else
957 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100958 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100959 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100960 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100961 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100962 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100963 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100964
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100965 case JSON_OBJECT_KEY:
966 json_skip_white(reader);
967 p = reader->js_buf + reader->js_used;
968 if (*p != ':')
969 {
970 if (cur_item != NULL)
971 clear_tv(cur_item);
972 if (*p == NUL)
973 retval = MAYBE;
974 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100975 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100976 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100977 retval = FAIL;
978 }
979 goto theend;
980 }
981 ++reader->js_used;
982 json_skip_white(reader);
983 top_item->jd_type = JSON_OBJECT;
984 if (cur_item != NULL)
985 cur_item = &item;
986 break;
987
988 case JSON_OBJECT:
989 if (cur_item != NULL
990 && dict_find(top_item->jd_tv.vval.v_dict,
991 top_item->jd_key, -1) != NULL)
992 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100993 semsg(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100994 top_item->jd_key);
995 clear_tv(&top_item->jd_key_tv);
996 clear_tv(cur_item);
997 retval = FAIL;
998 goto theend;
999 }
1000
1001 if (cur_item != NULL)
1002 {
1003 dictitem_T *di = dictitem_alloc(top_item->jd_key);
1004
1005 clear_tv(&top_item->jd_key_tv);
1006 if (di == NULL)
1007 {
1008 clear_tv(cur_item);
1009 retval = FAIL;
1010 goto theend;
1011 }
1012 di->di_tv = *cur_item;
1013 di->di_tv.v_lock = 0;
1014 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
1015 {
1016 dictitem_free(di);
1017 retval = FAIL;
1018 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001019 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001020 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001021
1022 json_skip_white(reader);
1023 p = reader->js_buf + reader->js_used;
1024 if (*p == ',')
1025 ++reader->js_used;
1026 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001027 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001028 if (*p == NUL)
1029 retval = MAYBE;
1030 else
1031 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001032 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001033 retval = FAIL;
1034 }
1035 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001036 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001037 top_item->jd_type = JSON_OBJECT_KEY;
1038 if (cur_item != NULL)
1039 cur_item = &top_item->jd_key_tv;
1040 break;
1041 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001042 }
1043
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001044 /* Get here when parsing failed. */
Bram Moolenaar7756e742016-10-21 20:35:37 +02001045 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001046 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001047 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001048 res->v_type = VAR_SPECIAL;
1049 res->vval.v_number = VVAL_NONE;
1050 }
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001051 emsg(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001052
1053theend:
1054 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001055 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001056}
1057
1058/*
1059 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001060 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001061 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001062 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001063 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001064json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001065{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001066 int ret;
1067
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001068 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001069 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001070 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001071 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001072 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001073 {
1074 if (ret == MAYBE)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001075 emsg(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001076 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001077 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001078 json_skip_white(reader);
1079 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001080 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001081 emsg(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001082 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001083 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001084 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001085}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001086
Bram Moolenaar113e1072019-01-20 15:30:40 +01001087#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001088/*
1089 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001090 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001091 * Return FAIL for a decoding error.
1092 * Return MAYBE for an incomplete message.
1093 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001094 */
1095 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001096json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001097{
1098 int ret;
1099
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001100 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001101 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1102 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001103 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001104 json_skip_white(reader);
1105
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001106 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001107}
Bram Moolenaar113e1072019-01-20 15:30:40 +01001108#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +01001109
1110/*
1111 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001112 * "options" can be JSON_JS or zero.
1113 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001114 * Return FAIL if the message has a decoding error.
1115 * Return MAYBE if the message is truncated, need to read more.
1116 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001117 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001118 * Does not advance the reader.
1119 */
1120 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001121json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001122{
1123 int used_save = reader->js_used;
1124 int ret;
1125
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001126 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001127 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1128 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001129 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001130 reader->js_used = used_save;
1131 return ret;
1132}
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001133#endif