blob: d9da10d3590d8ce1a16427f8ec381bc86235ccd4 [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 Moolenaarfb1f6262016-01-31 20:24:32 +010057/*
Bram Moolenaar55fab432016-02-07 16:53:13 +010058 * Encode ["nr", "val"] into a JSON format string in allocated memory.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020059 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010060 * Returns NULL when out of memory.
61 */
62 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010063json_encode_nr_expr(int nr, typval_T *val, int options)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010064{
65 typval_T listtv;
66 typval_T nrtv;
Bram Moolenaarf1f07922016-08-26 17:58:53 +020067 garray_T ga;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010068
69 nrtv.v_type = VAR_NUMBER;
70 nrtv.vval.v_number = nr;
71 if (rettv_list_alloc(&listtv) == FAIL)
72 return NULL;
73 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
74 || list_append_tv(listtv.vval.v_list, val) == FAIL)
75 {
76 list_unref(listtv.vval.v_list);
77 return NULL;
78 }
79
Bram Moolenaarf1f07922016-08-26 17:58:53 +020080 ga_init2(&ga, 1, 4000);
81 if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL))
82 ga_append(&ga, '\n');
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010083 list_unref(listtv.vval.v_list);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020084 return ga.ga_data;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010085}
86
Bram Moolenaar520e1e42016-01-23 19:46:28 +010087 static void
88write_string(garray_T *gap, char_u *str)
89{
90 char_u *res = str;
91 char_u numbuf[NUMBUFLEN];
92
93 if (res == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +010094 ga_concat(gap, (char_u *)"\"\"");
Bram Moolenaar520e1e42016-01-23 19:46:28 +010095 else
96 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +010097#if defined(FEAT_MBYTE) && defined(USE_ICONV)
98 vimconv_T conv;
99 char_u *converted = NULL;
100
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100101 if (!enc_utf8)
102 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100103 /* Convert the text from 'encoding' to utf-8, the JSON string is
104 * always utf-8. */
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100105 conv.vc_type = CONV_NONE;
106 convert_setup(&conv, p_enc, (char_u*)"utf-8");
107 if (conv.vc_type != CONV_NONE)
108 converted = res = string_convert(&conv, res, NULL);
109 convert_setup(&conv, NULL, NULL);
110 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100111#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100112 ga_append(gap, '"');
113 while (*res != NUL)
114 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100115 int c;
116#ifdef FEAT_MBYTE
117 /* always use utf-8 encoding, ignore 'encoding' */
118 c = utf_ptr2char(res);
119#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100120 c = *res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100121#endif
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 Moolenaarfa06a512016-01-28 22:46:58 +0100143#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100144 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100145#else
146 numbuf[0] = c;
147 numbuf[1] = NUL;
148#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100149 ga_concat(gap, numbuf);
150 }
151 else
152 {
153 vim_snprintf((char *)numbuf, NUMBUFLEN,
154 "\\u%04lx", (long)c);
155 ga_concat(gap, numbuf);
156 }
157 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100158#ifdef FEAT_MBYTE
159 res += utf_ptr2len(res);
160#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100161 ++res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100162#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100163 }
164 ga_append(gap, '"');
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100165#if defined(FEAT_MBYTE) && defined(USE_ICONV)
166 vim_free(converted);
167#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100168 }
169}
170
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100171/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100172 * Return TRUE if "key" can be used without quotes.
173 * That is when it starts with a letter and only contains letters, digits and
174 * underscore.
175 */
176 static int
177is_simple_key(char_u *key)
178{
179 char_u *p;
180
181 if (!ASCII_ISALPHA(*key))
182 return FALSE;
183 for (p = key + 1; *p != NUL; ++p)
184 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
185 return FALSE;
186 return TRUE;
187}
188
189/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100190 * Encode "val" into "gap".
191 * Return FAIL or OK.
192 */
193 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100194json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100195{
196 char_u numbuf[NUMBUFLEN];
197 char_u *res;
198 list_T *l;
199 dict_T *d;
200
201 switch (val->v_type)
202 {
203 case VAR_SPECIAL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100204 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100205 {
206 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
207 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100208 case VVAL_NONE: if ((options & JSON_JS) != 0
209 && (options & JSON_NO_NONE) == 0)
210 /* empty item */
211 break;
212 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100213 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
214 }
215 break;
216
217 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200218 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
Bram Moolenaarea391762018-04-08 13:07:22 +0200219 (long long)val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100220 ga_concat(gap, numbuf);
221 break;
222
223 case VAR_STRING:
224 res = val->vval.v_string;
225 write_string(gap, res);
226 break;
227
228 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100229 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100230 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100231 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100232 /* no JSON equivalent TODO: better error */
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100233 EMSG(_(e_invarg));
234 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100235
236 case VAR_LIST:
237 l = val->vval.v_list;
238 if (l == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100239 ga_concat(gap, (char_u *)"[]");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100240 else
241 {
242 if (l->lv_copyID == copyID)
243 ga_concat(gap, (char_u *)"[]");
244 else
245 {
246 listitem_T *li;
247
248 l->lv_copyID = copyID;
249 ga_append(gap, '[');
250 for (li = l->lv_first; li != NULL && !got_int; )
251 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100252 if (json_encode_item(gap, &li->li_tv, copyID,
253 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100254 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100255 if ((options & JSON_JS)
256 && li->li_next == NULL
257 && li->li_tv.v_type == VAR_SPECIAL
258 && li->li_tv.vval.v_number == VVAL_NONE)
259 /* add an extra comma if the last item is v:none */
260 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100261 li = li->li_next;
262 if (li != NULL)
263 ga_append(gap, ',');
264 }
265 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100266 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100267 }
268 }
269 break;
270
271 case VAR_DICT:
272 d = val->vval.v_dict;
273 if (d == NULL)
Bram Moolenaarb29d3282017-12-15 21:25:01 +0100274 ga_concat(gap, (char_u *)"{}");
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100275 else
276 {
277 if (d->dv_copyID == copyID)
278 ga_concat(gap, (char_u *)"{}");
279 else
280 {
281 int first = TRUE;
282 int todo = (int)d->dv_hashtab.ht_used;
283 hashitem_T *hi;
284
285 d->dv_copyID = copyID;
286 ga_append(gap, '{');
287
288 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
289 ++hi)
290 if (!HASHITEM_EMPTY(hi))
291 {
292 --todo;
293 if (first)
294 first = FALSE;
295 else
296 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100297 if ((options & JSON_JS)
298 && is_simple_key(hi->hi_key))
299 ga_concat(gap, hi->hi_key);
300 else
301 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100302 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100303 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100304 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100305 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100306 }
307 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100308 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100309 }
310 }
311 break;
312
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100313 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100314#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100315# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100316 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100317 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100318 else if (isinf(val->vval.v_float))
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100319 {
320 if (val->vval.v_float < 0.0)
321 ga_concat(gap, (char_u *)"-Infinity");
322 else
323 ga_concat(gap, (char_u *)"Infinity");
324 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100325 else
326# endif
327 {
328 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
329 val->vval.v_float);
330 ga_concat(gap, numbuf);
331 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100332 break;
333#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100334 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +0100335 internal_error("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100336 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100337 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100338 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100339}
340
341/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100342 * When "reader" has less than NUMBUFLEN bytes available, call the fill
343 * callback to get more.
344 */
345 static void
346fill_numbuflen(js_read_T *reader)
347{
348 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
349 - reader->js_used < NUMBUFLEN)
350 {
351 if (reader->js_fill(reader))
352 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
353 }
354}
355
356/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100357 * Skip white space in "reader". All characters <= space are considered white
358 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100359 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100360 */
361 static void
362json_skip_white(js_read_T *reader)
363{
364 int c;
365
Bram Moolenaar56ead342016-02-02 18:20:08 +0100366 for (;;)
367 {
368 c = reader->js_buf[reader->js_used];
369 if (reader->js_fill != NULL && c == NUL)
370 {
371 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200372 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100373 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200374 continue;
375 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100376 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100377 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100378 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100379 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100380 }
381 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100382}
383
Bram Moolenaar56ead342016-02-02 18:20:08 +0100384 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100385json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100386{
387 garray_T ga;
388 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100389 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100390 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200391 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100392
Bram Moolenaar56ead342016-02-02 18:20:08 +0100393 if (res != NULL)
394 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100395
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100396 p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */
397 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100398 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100399 /* The JSON is always expected to be utf-8, thus use utf functions
400 * here. The string is converted below if needed. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100401 if (*p == NUL || p[1] == NUL
402#ifdef FEAT_MBYTE
403 || utf_ptr2len(p) < utf_byte2len(*p)
404#endif
405 )
406 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100407 /* Not enough bytes to make a character or end of the string. Get
408 * more if possible. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100409 if (reader->js_fill == NULL)
410 break;
411 len = (int)(reader->js_end - p);
412 reader->js_used = (int)(p - reader->js_buf);
413 if (!reader->js_fill(reader))
414 break; /* didn't get more */
415 p = reader->js_buf + reader->js_used;
416 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
417 continue;
418 }
419
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100420 if (*p == '\\')
421 {
422 c = -1;
423 switch (p[1])
424 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100425 case '\\': c = '\\'; break;
426 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100427 case 'b': c = BS; break;
428 case 't': c = TAB; break;
429 case 'n': c = NL; break;
430 case 'f': c = FF; break;
431 case 'r': c = CAR; break;
432 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100433 if (reader->js_fill != NULL
434 && (int)(reader->js_end - p) < NUMBUFLEN)
435 {
436 reader->js_used = (int)(p - reader->js_buf);
437 if (reader->js_fill(reader))
438 {
439 p = reader->js_buf + reader->js_used;
440 reader->js_end = reader->js_buf
441 + STRLEN(reader->js_buf);
442 }
443 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100444 nr = 0;
445 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100446 vim_str2nr(p + 2, NULL, &len,
447 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
448 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100449 if (0xd800 <= nr && nr <= 0xdfff
450 && (int)(reader->js_end - p) >= 6
451 && *p == '\\' && *(p+1) == 'u')
452 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200453 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100454
455 /* decode surrogate pair: \ud812\u3456 */
456 len = 0;
457 vim_str2nr(p + 2, NULL, &len,
458 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4);
459 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
460 {
461 p += len + 2;
462 nr = (((nr - 0xd800) << 10) |
463 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
464 }
465 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100466 if (res != NULL)
467 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100468#ifdef FEAT_MBYTE
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200469 char_u buf[NUMBUFLEN];
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100470 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100471 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100472#else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200473 ga_append(&ga, (int)nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100474#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100475 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100476 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100477 default:
478 /* not a special char, skip over \ */
479 ++p;
480 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100481 }
482 if (c > 0)
483 {
484 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100485 if (res != NULL)
486 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100487 }
488 }
489 else
490 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100491#ifdef FEAT_MBYTE
492 len = utf_ptr2len(p);
493#else
494 len = 1;
495#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100496 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100497 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100498 if (ga_grow(&ga, len) == FAIL)
499 {
500 ga_clear(&ga);
501 return FAIL;
502 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100503 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
504 ga.ga_len += len;
505 }
506 p += len;
507 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100508 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100509
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100510 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100511 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100512 {
513 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100514 if (res != NULL)
515 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100516 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100517 res->v_type = VAR_STRING;
Bram Moolenaarb3628722016-02-28 14:56:39 +0100518#if defined(FEAT_MBYTE) && defined(USE_ICONV)
519 if (!enc_utf8)
520 {
521 vimconv_T conv;
522
523 /* Convert the utf-8 string to 'encoding'. */
524 conv.vc_type = CONV_NONE;
525 convert_setup(&conv, (char_u*)"utf-8", p_enc);
526 if (conv.vc_type != CONV_NONE)
527 {
528 res->vval.v_string =
529 string_convert(&conv, ga.ga_data, NULL);
530 vim_free(ga.ga_data);
531 }
532 convert_setup(&conv, NULL, NULL);
533 }
534 else
535#endif
536 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100537 }
538 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100539 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100540 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100541 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100542 res->v_type = VAR_SPECIAL;
543 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100544 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100545 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100546 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100547}
548
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100549typedef enum {
550 JSON_ARRAY, /* parsing items in an array */
551 JSON_OBJECT_KEY, /* parsing key of an object */
552 JSON_OBJECT /* parsing item in an object, after the key */
553} json_decode_T;
554
555typedef struct {
556 json_decode_T jd_type;
557 typval_T jd_tv; /* the list or dict */
558 typval_T jd_key_tv;
559 char_u *jd_key;
560} json_dec_item_T;
561
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100562/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100563 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100564 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100565 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100566 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100567 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100568 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100569 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100570json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100571{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100572 char_u *p;
573 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100574 int retval;
575 garray_T stack;
576 typval_T item;
577 typval_T *cur_item;
578 json_dec_item_T *top_item;
579 char_u key_buf[NUMBUFLEN];
580
581 ga_init2(&stack, sizeof(json_dec_item_T), 100);
582 cur_item = res;
583 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100584 if (res != NULL)
585 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100586
Bram Moolenaar56ead342016-02-02 18:20:08 +0100587 fill_numbuflen(reader);
588 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100589 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100590 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100591 top_item = NULL;
592 if (stack.ga_len > 0)
593 {
594 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
595 json_skip_white(reader);
596 p = reader->js_buf + reader->js_used;
597 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100598 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100599 retval = MAYBE;
600 if (top_item->jd_type == JSON_OBJECT)
601 /* did get the key, clear it */
602 clear_tv(&top_item->jd_key_tv);
603 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100604 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100605 if (top_item->jd_type == JSON_OBJECT_KEY
606 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100607 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100608 /* Check for end of object or array. */
609 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100610 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100611 ++reader->js_used; /* consume the ']' or '}' */
612 --stack.ga_len;
613 if (stack.ga_len == 0)
614 {
615 retval = OK;
616 goto theend;
617 }
618 if (cur_item != NULL)
619 cur_item = &top_item->jd_tv;
620 goto item_end;
621 }
622 }
623 }
624
625 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
626 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100627 && reader->js_buf[reader->js_used] != '"'
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100628 && reader->js_buf[reader->js_used] != '\''
629 && reader->js_buf[reader->js_used] != '['
630 && reader->js_buf[reader->js_used] != '{')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100631 {
632 char_u *key;
633
634 /* accept an object key that is not in quotes */
635 key = p = reader->js_buf + reader->js_used;
636 while (*p != NUL && *p != ':' && *p > ' ')
637 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100638 if (cur_item != NULL)
639 {
640 cur_item->v_type = VAR_STRING;
641 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
642 top_item->jd_key = cur_item->vval.v_string;
643 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100644 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100645 }
646 else
647 {
648 switch (*p)
649 {
650 case '[': /* start of array */
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100651 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
652 {
653 retval = FAIL;
654 break;
655 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100656 if (ga_grow(&stack, 1) == FAIL)
657 {
658 retval = FAIL;
659 break;
660 }
661 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
662 {
663 cur_item->v_type = VAR_SPECIAL;
664 cur_item->vval.v_number = VVAL_NONE;
665 retval = FAIL;
666 break;
667 }
668
669 ++reader->js_used; /* consume the '[' */
670 top_item = ((json_dec_item_T *)stack.ga_data)
671 + stack.ga_len;
672 top_item->jd_type = JSON_ARRAY;
673 ++stack.ga_len;
674 if (cur_item != NULL)
675 {
676 top_item->jd_tv = *cur_item;
677 cur_item = &item;
678 }
679 continue;
680
681 case '{': /* start of object */
Bram Moolenaar625f0c12018-03-13 13:10:41 +0100682 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
683 {
684 retval = FAIL;
685 break;
686 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100687 if (ga_grow(&stack, 1) == FAIL)
688 {
689 retval = FAIL;
690 break;
691 }
692 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
693 {
694 cur_item->v_type = VAR_SPECIAL;
695 cur_item->vval.v_number = VVAL_NONE;
696 retval = FAIL;
697 break;
698 }
699
700 ++reader->js_used; /* consume the '{' */
701 top_item = ((json_dec_item_T *)stack.ga_data)
702 + stack.ga_len;
703 top_item->jd_type = JSON_OBJECT_KEY;
704 ++stack.ga_len;
705 if (cur_item != NULL)
706 {
707 top_item->jd_tv = *cur_item;
708 cur_item = &top_item->jd_key_tv;
709 }
710 continue;
711
712 case '"': /* string */
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100713 retval = json_decode_string(reader, cur_item, *p);
714 break;
715
716 case '\'':
717 if (options & JSON_JS)
718 retval = json_decode_string(reader, cur_item, *p);
719 else
720 {
721 EMSG(_(e_invarg));
722 retval = FAIL;
723 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100724 break;
725
726 case ',': /* comma: empty item */
727 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100728 {
729 EMSG(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100730 retval = FAIL;
731 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100732 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100733 /* FALLTHROUGH */
734 case NUL: /* empty */
735 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100736 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100737 cur_item->v_type = VAR_SPECIAL;
738 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100739 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100740 retval = OK;
741 break;
742
743 default:
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100744 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100745 {
746#ifdef FEAT_FLOAT
747 char_u *sp = p;
748
749 if (*sp == '-')
750 {
751 ++sp;
752 if (*sp == NUL)
753 {
754 retval = MAYBE;
755 break;
756 }
757 if (!VIM_ISDIGIT(*sp))
758 {
759 EMSG(_(e_invarg));
760 retval = FAIL;
761 break;
762 }
763 }
764 sp = skipdigits(sp);
765 if (*sp == '.' || *sp == 'e' || *sp == 'E')
766 {
767 if (cur_item == NULL)
768 {
769 float_T f;
770
771 len = string2float(p, &f);
772 }
773 else
774 {
775 cur_item->v_type = VAR_FLOAT;
776 len = string2float(p, &cur_item->vval.v_float);
777 }
778 }
779 else
780#endif
781 {
782 varnumber_T nr;
783
784 vim_str2nr(reader->js_buf + reader->js_used,
785 NULL, &len, 0, /* what */
786 &nr, NULL, 0);
787 if (cur_item != NULL)
788 {
789 cur_item->v_type = VAR_NUMBER;
790 cur_item->vval.v_number = nr;
791 }
792 }
793 reader->js_used += len;
794 retval = OK;
795 break;
796 }
797 if (STRNICMP((char *)p, "false", 5) == 0)
798 {
799 reader->js_used += 5;
800 if (cur_item != NULL)
801 {
802 cur_item->v_type = VAR_SPECIAL;
803 cur_item->vval.v_number = VVAL_FALSE;
804 }
805 retval = OK;
806 break;
807 }
808 if (STRNICMP((char *)p, "true", 4) == 0)
809 {
810 reader->js_used += 4;
811 if (cur_item != NULL)
812 {
813 cur_item->v_type = VAR_SPECIAL;
814 cur_item->vval.v_number = VVAL_TRUE;
815 }
816 retval = OK;
817 break;
818 }
819 if (STRNICMP((char *)p, "null", 4) == 0)
820 {
821 reader->js_used += 4;
822 if (cur_item != NULL)
823 {
824 cur_item->v_type = VAR_SPECIAL;
825 cur_item->vval.v_number = VVAL_NULL;
826 }
827 retval = OK;
828 break;
829 }
830#ifdef FEAT_FLOAT
831 if (STRNICMP((char *)p, "NaN", 3) == 0)
832 {
833 reader->js_used += 3;
834 if (cur_item != NULL)
835 {
836 cur_item->v_type = VAR_FLOAT;
837 cur_item->vval.v_float = NAN;
838 }
839 retval = OK;
840 break;
841 }
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100842 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
843 {
844 reader->js_used += 9;
845 if (cur_item != NULL)
846 {
847 cur_item->v_type = VAR_FLOAT;
848 cur_item->vval.v_float = -INFINITY;
849 }
850 retval = OK;
851 break;
852 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100853 if (STRNICMP((char *)p, "Infinity", 8) == 0)
854 {
855 reader->js_used += 8;
856 if (cur_item != NULL)
857 {
858 cur_item->v_type = VAR_FLOAT;
859 cur_item->vval.v_float = INFINITY;
860 }
861 retval = OK;
862 break;
863 }
864#endif
865 /* check for truncated name */
866 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
867 if (
868 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
869#ifdef FEAT_FLOAT
Bram Moolenaar5f6b3792019-01-12 14:24:27 +0100870 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100871 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
872 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
873#endif
874 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
875 || STRNICMP((char *)p, "null", len) == 0)))
876
877 retval = MAYBE;
878 else
879 retval = FAIL;
880 break;
881 }
882
883 /* We are finished when retval is FAIL or MAYBE and when at the
884 * toplevel. */
885 if (retval == FAIL)
886 break;
887 if (retval == MAYBE || stack.ga_len == 0)
888 goto theend;
889
890 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
891 && cur_item != NULL)
892 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100893 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100894 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100895 {
896 clear_tv(cur_item);
897 EMSG(_(e_invarg));
898 retval = FAIL;
899 goto theend;
900 }
901 }
902 }
903
904item_end:
905 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
906 switch (top_item->jd_type)
907 {
908 case JSON_ARRAY:
909 if (res != NULL)
910 {
911 listitem_T *li = listitem_alloc();
912
913 if (li == NULL)
914 {
915 clear_tv(cur_item);
916 retval = FAIL;
917 goto theend;
918 }
919 li->li_tv = *cur_item;
920 list_append(top_item->jd_tv.vval.v_list, li);
921 }
922 if (cur_item != NULL)
923 cur_item = &item;
924
925 json_skip_white(reader);
926 p = reader->js_buf + reader->js_used;
927 if (*p == ',')
928 ++reader->js_used;
929 else if (*p != ']')
930 {
931 if (*p == NUL)
932 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100933 else
934 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100935 EMSG(_(e_invarg));
936 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100937 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100938 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100939 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100940 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100941
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100942 case JSON_OBJECT_KEY:
943 json_skip_white(reader);
944 p = reader->js_buf + reader->js_used;
945 if (*p != ':')
946 {
947 if (cur_item != NULL)
948 clear_tv(cur_item);
949 if (*p == NUL)
950 retval = MAYBE;
951 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100952 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100953 EMSG(_(e_invarg));
954 retval = FAIL;
955 }
956 goto theend;
957 }
958 ++reader->js_used;
959 json_skip_white(reader);
960 top_item->jd_type = JSON_OBJECT;
961 if (cur_item != NULL)
962 cur_item = &item;
963 break;
964
965 case JSON_OBJECT:
966 if (cur_item != NULL
967 && dict_find(top_item->jd_tv.vval.v_dict,
968 top_item->jd_key, -1) != NULL)
969 {
Bram Moolenaar83381f72017-01-14 14:36:08 +0100970 EMSG2(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100971 top_item->jd_key);
972 clear_tv(&top_item->jd_key_tv);
973 clear_tv(cur_item);
974 retval = FAIL;
975 goto theend;
976 }
977
978 if (cur_item != NULL)
979 {
980 dictitem_T *di = dictitem_alloc(top_item->jd_key);
981
982 clear_tv(&top_item->jd_key_tv);
983 if (di == NULL)
984 {
985 clear_tv(cur_item);
986 retval = FAIL;
987 goto theend;
988 }
989 di->di_tv = *cur_item;
990 di->di_tv.v_lock = 0;
991 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
992 {
993 dictitem_free(di);
994 retval = FAIL;
995 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100996 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100997 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100998
999 json_skip_white(reader);
1000 p = reader->js_buf + reader->js_used;
1001 if (*p == ',')
1002 ++reader->js_used;
1003 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +01001004 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001005 if (*p == NUL)
1006 retval = MAYBE;
1007 else
1008 {
1009 EMSG(_(e_invarg));
1010 retval = FAIL;
1011 }
1012 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001013 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001014 top_item->jd_type = JSON_OBJECT_KEY;
1015 if (cur_item != NULL)
1016 cur_item = &top_item->jd_key_tv;
1017 break;
1018 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001019 }
1020
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001021 /* Get here when parsing failed. */
Bram Moolenaar7756e742016-10-21 20:35:37 +02001022 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001023 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001024 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001025 res->v_type = VAR_SPECIAL;
1026 res->vval.v_number = VVAL_NONE;
1027 }
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001028 EMSG(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001029
1030theend:
1031 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001032 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001033}
1034
1035/*
1036 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001037 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001038 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001039 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001040 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001041json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001042{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001043 int ret;
1044
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001045 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001046 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001047 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001048 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001049 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001050 {
1051 if (ret == MAYBE)
1052 EMSG(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001053 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001054 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001055 json_skip_white(reader);
1056 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001057 {
1058 EMSG(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001059 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001060 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001061 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001062}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001063
1064/*
1065 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001066 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001067 * Return FAIL for a decoding error.
1068 * Return MAYBE for an incomplete message.
1069 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001070 */
1071 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001072json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001073{
1074 int ret;
1075
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001076 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001077 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1078 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001079 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001080 json_skip_white(reader);
1081
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001082 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001083}
1084
1085/*
1086 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001087 * "options" can be JSON_JS or zero.
1088 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001089 * Return FAIL if the message has a decoding error.
1090 * Return MAYBE if the message is truncated, need to read more.
1091 * This only works reliable if the message contains an object, array or
Bram Moolenaar5f6b3792019-01-12 14:24:27 +01001092 * string. A number might be truncated without knowing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001093 * Does not advance the reader.
1094 */
1095 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001096json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001097{
1098 int used_save = reader->js_used;
1099 int ret;
1100
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001101 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001102 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1103 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001104 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001105 reader->js_used = used_save;
1106 return ret;
1107}
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001108#endif