blob: e5e85eb6dbb8126cf893a43019337a4e2711b584 [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);
22static int json_decode_item(js_read_T *reader, typval_T *res, int options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023
24/*
25 * Encode "val" into a JSON format string.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020026 * The result is added to "gap"
27 * Returns FAIL on failure and makes gap->ga_data empty.
28 */
29 static int
30json_encode_gap(garray_T *gap, typval_T *val, int options)
31{
32 if (json_encode_item(gap, val, get_copyID(), options) == FAIL)
33 {
34 ga_clear(gap);
35 gap->ga_data = vim_strsave((char_u *)"");
36 return FAIL;
37 }
38 return OK;
39}
40
41/*
42 * Encode "val" into a JSON format string.
Bram Moolenaar55fab432016-02-07 16:53:13 +010043 * The result is in allocated memory.
44 * The result is empty when encoding fails.
Bram Moolenaarf1f07922016-08-26 17:58:53 +020045 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
Bram Moolenaar520e1e42016-01-23 19:46:28 +010046 */
47 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010048json_encode(typval_T *val, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010049{
50 garray_T ga;
51
52 /* Store bytes in the growarray. */
53 ga_init2(&ga, 1, 4000);
Bram Moolenaarf1f07922016-08-26 17:58:53 +020054 json_encode_gap(&ga, val, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010055 return ga.ga_data;
56}
57
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}
87
Bram Moolenaar520e1e42016-01-23 19:46:28 +010088 static void
89write_string(garray_T *gap, char_u *str)
90{
91 char_u *res = str;
92 char_u numbuf[NUMBUFLEN];
93
94 if (res == NULL)
95 ga_concat(gap, (char_u *)"null");
96 else
97 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +010098#if defined(FEAT_MBYTE) && defined(USE_ICONV)
99 vimconv_T conv;
100 char_u *converted = NULL;
101
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100102 if (!enc_utf8)
103 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100104 /* Convert the text from 'encoding' to utf-8, the JSON string is
105 * always utf-8. */
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100106 conv.vc_type = CONV_NONE;
107 convert_setup(&conv, p_enc, (char_u*)"utf-8");
108 if (conv.vc_type != CONV_NONE)
109 converted = res = string_convert(&conv, res, NULL);
110 convert_setup(&conv, NULL, NULL);
111 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100112#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100113 ga_append(gap, '"');
114 while (*res != NUL)
115 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100116 int c;
117#ifdef FEAT_MBYTE
118 /* always use utf-8 encoding, ignore 'encoding' */
119 c = utf_ptr2char(res);
120#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100121 c = *res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100122#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100123
124 switch (c)
125 {
126 case 0x08:
127 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
128 case 0x09:
129 ga_append(gap, '\\'); ga_append(gap, 't'); break;
130 case 0x0a:
131 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
132 case 0x0c:
133 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
134 case 0x0d:
135 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
136 case 0x22: /* " */
137 case 0x5c: /* \ */
138 ga_append(gap, '\\');
139 ga_append(gap, c);
140 break;
141 default:
142 if (c >= 0x20)
143 {
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100144#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100145 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100146#else
147 numbuf[0] = c;
148 numbuf[1] = NUL;
149#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100150 ga_concat(gap, numbuf);
151 }
152 else
153 {
154 vim_snprintf((char *)numbuf, NUMBUFLEN,
155 "\\u%04lx", (long)c);
156 ga_concat(gap, numbuf);
157 }
158 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100159#ifdef FEAT_MBYTE
160 res += utf_ptr2len(res);
161#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100162 ++res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100163#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100164 }
165 ga_append(gap, '"');
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100166#if defined(FEAT_MBYTE) && defined(USE_ICONV)
167 vim_free(converted);
168#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100169 }
170}
171
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100172/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100173 * Return TRUE if "key" can be used without quotes.
174 * That is when it starts with a letter and only contains letters, digits and
175 * underscore.
176 */
177 static int
178is_simple_key(char_u *key)
179{
180 char_u *p;
181
182 if (!ASCII_ISALPHA(*key))
183 return FALSE;
184 for (p = key + 1; *p != NUL; ++p)
185 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
186 return FALSE;
187 return TRUE;
188}
189
190/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100191 * Encode "val" into "gap".
192 * Return FAIL or OK.
193 */
194 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100195json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100196{
197 char_u numbuf[NUMBUFLEN];
198 char_u *res;
199 list_T *l;
200 dict_T *d;
201
202 switch (val->v_type)
203 {
204 case VAR_SPECIAL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100205 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100206 {
207 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
208 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100209 case VVAL_NONE: if ((options & JSON_JS) != 0
210 && (options & JSON_NO_NONE) == 0)
211 /* empty item */
212 break;
213 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100214 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
215 }
216 break;
217
218 case VAR_NUMBER:
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200219 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
220 val->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100221 ga_concat(gap, numbuf);
222 break;
223
224 case VAR_STRING:
225 res = val->vval.v_string;
226 write_string(gap, res);
227 break;
228
229 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100230 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100231 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100232 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100233 /* no JSON equivalent TODO: better error */
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100234 EMSG(_(e_invarg));
235 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100236
237 case VAR_LIST:
238 l = val->vval.v_list;
239 if (l == NULL)
240 ga_concat(gap, (char_u *)"null");
241 else
242 {
243 if (l->lv_copyID == copyID)
244 ga_concat(gap, (char_u *)"[]");
245 else
246 {
247 listitem_T *li;
248
249 l->lv_copyID = copyID;
250 ga_append(gap, '[');
251 for (li = l->lv_first; li != NULL && !got_int; )
252 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100253 if (json_encode_item(gap, &li->li_tv, copyID,
254 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100255 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100256 if ((options & JSON_JS)
257 && li->li_next == NULL
258 && li->li_tv.v_type == VAR_SPECIAL
259 && li->li_tv.vval.v_number == VVAL_NONE)
260 /* add an extra comma if the last item is v:none */
261 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100262 li = li->li_next;
263 if (li != NULL)
264 ga_append(gap, ',');
265 }
266 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100267 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100268 }
269 }
270 break;
271
272 case VAR_DICT:
273 d = val->vval.v_dict;
274 if (d == NULL)
275 ga_concat(gap, (char_u *)"null");
276 else
277 {
278 if (d->dv_copyID == copyID)
279 ga_concat(gap, (char_u *)"{}");
280 else
281 {
282 int first = TRUE;
283 int todo = (int)d->dv_hashtab.ht_used;
284 hashitem_T *hi;
285
286 d->dv_copyID = copyID;
287 ga_append(gap, '{');
288
289 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
290 ++hi)
291 if (!HASHITEM_EMPTY(hi))
292 {
293 --todo;
294 if (first)
295 first = FALSE;
296 else
297 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100298 if ((options & JSON_JS)
299 && is_simple_key(hi->hi_key))
300 ga_concat(gap, hi->hi_key);
301 else
302 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100303 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100304 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100305 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100306 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100307 }
308 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100309 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100310 }
311 }
312 break;
313
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100314 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100315#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100316# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100317 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100318 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100319 else if (isinf(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100320 ga_concat(gap, (char_u *)"Infinity");
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100321 else
322# endif
323 {
324 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
325 val->vval.v_float);
326 ga_concat(gap, numbuf);
327 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100328 break;
329#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100330 case VAR_UNKNOWN:
Bram Moolenaar95f09602016-11-10 20:01:45 +0100331 internal_error("json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100332 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100333 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100334 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100335}
336
337/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100338 * When "reader" has less than NUMBUFLEN bytes available, call the fill
339 * callback to get more.
340 */
341 static void
342fill_numbuflen(js_read_T *reader)
343{
344 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
345 - reader->js_used < NUMBUFLEN)
346 {
347 if (reader->js_fill(reader))
348 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
349 }
350}
351
352/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100353 * Skip white space in "reader". All characters <= space are considered white
354 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100355 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100356 */
357 static void
358json_skip_white(js_read_T *reader)
359{
360 int c;
361
Bram Moolenaar56ead342016-02-02 18:20:08 +0100362 for (;;)
363 {
364 c = reader->js_buf[reader->js_used];
365 if (reader->js_fill != NULL && c == NUL)
366 {
367 if (reader->js_fill(reader))
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200368 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100369 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar46c00a62016-03-28 14:11:42 +0200370 continue;
371 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100372 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100373 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100374 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100375 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100376 }
377 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100378}
379
Bram Moolenaar56ead342016-02-02 18:20:08 +0100380 static int
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100381json_decode_string(js_read_T *reader, typval_T *res, int quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100382{
383 garray_T ga;
384 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100385 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100386 int c;
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200387 varnumber_T nr;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100388
Bram Moolenaar56ead342016-02-02 18:20:08 +0100389 if (res != NULL)
390 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100391
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100392 p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */
393 while (*p != quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100394 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100395 /* The JSON is always expected to be utf-8, thus use utf functions
396 * here. The string is converted below if needed. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100397 if (*p == NUL || p[1] == NUL
398#ifdef FEAT_MBYTE
399 || utf_ptr2len(p) < utf_byte2len(*p)
400#endif
401 )
402 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100403 /* Not enough bytes to make a character or end of the string. Get
404 * more if possible. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100405 if (reader->js_fill == NULL)
406 break;
407 len = (int)(reader->js_end - p);
408 reader->js_used = (int)(p - reader->js_buf);
409 if (!reader->js_fill(reader))
410 break; /* didn't get more */
411 p = reader->js_buf + reader->js_used;
412 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
413 continue;
414 }
415
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100416 if (*p == '\\')
417 {
418 c = -1;
419 switch (p[1])
420 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100421 case '\\': c = '\\'; break;
422 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100423 case 'b': c = BS; break;
424 case 't': c = TAB; break;
425 case 'n': c = NL; break;
426 case 'f': c = FF; break;
427 case 'r': c = CAR; break;
428 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100429 if (reader->js_fill != NULL
430 && (int)(reader->js_end - p) < NUMBUFLEN)
431 {
432 reader->js_used = (int)(p - reader->js_buf);
433 if (reader->js_fill(reader))
434 {
435 p = reader->js_buf + reader->js_used;
436 reader->js_end = reader->js_buf
437 + STRLEN(reader->js_buf);
438 }
439 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100440 nr = 0;
441 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100442 vim_str2nr(p + 2, NULL, &len,
443 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
444 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100445 if (0xd800 <= nr && nr <= 0xdfff
446 && (int)(reader->js_end - p) >= 6
447 && *p == '\\' && *(p+1) == 'u')
448 {
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200449 varnumber_T nr2 = 0;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100450
451 /* decode surrogate pair: \ud812\u3456 */
452 len = 0;
453 vim_str2nr(p + 2, NULL, &len,
454 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4);
455 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
456 {
457 p += len + 2;
458 nr = (((nr - 0xd800) << 10) |
459 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
460 }
461 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100462 if (res != NULL)
463 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100464#ifdef FEAT_MBYTE
Bram Moolenaardc633cf2016-04-23 14:33:19 +0200465 char_u buf[NUMBUFLEN];
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100466 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100467 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100468#else
Bram Moolenaar22fcfad2016-07-01 18:17:26 +0200469 ga_append(&ga, (int)nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100470#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100471 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100472 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100473 default:
474 /* not a special char, skip over \ */
475 ++p;
476 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100477 }
478 if (c > 0)
479 {
480 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100481 if (res != NULL)
482 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100483 }
484 }
485 else
486 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100487#ifdef FEAT_MBYTE
488 len = utf_ptr2len(p);
489#else
490 len = 1;
491#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100492 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100493 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100494 if (ga_grow(&ga, len) == FAIL)
495 {
496 ga_clear(&ga);
497 return FAIL;
498 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100499 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
500 ga.ga_len += len;
501 }
502 p += len;
503 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100504 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100505
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100506 reader->js_used = (int)(p - reader->js_buf);
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100507 if (*p == quote)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100508 {
509 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100510 if (res != NULL)
511 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100512 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100513 res->v_type = VAR_STRING;
Bram Moolenaarb3628722016-02-28 14:56:39 +0100514#if defined(FEAT_MBYTE) && defined(USE_ICONV)
515 if (!enc_utf8)
516 {
517 vimconv_T conv;
518
519 /* Convert the utf-8 string to 'encoding'. */
520 conv.vc_type = CONV_NONE;
521 convert_setup(&conv, (char_u*)"utf-8", p_enc);
522 if (conv.vc_type != CONV_NONE)
523 {
524 res->vval.v_string =
525 string_convert(&conv, ga.ga_data, NULL);
526 vim_free(ga.ga_data);
527 }
528 convert_setup(&conv, NULL, NULL);
529 }
530 else
531#endif
532 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100533 }
534 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100535 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100536 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100537 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100538 res->v_type = VAR_SPECIAL;
539 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100540 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100541 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100542 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100543}
544
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100545typedef enum {
546 JSON_ARRAY, /* parsing items in an array */
547 JSON_OBJECT_KEY, /* parsing key of an object */
548 JSON_OBJECT /* parsing item in an object, after the key */
549} json_decode_T;
550
551typedef struct {
552 json_decode_T jd_type;
553 typval_T jd_tv; /* the list or dict */
554 typval_T jd_key_tv;
555 char_u *jd_key;
556} json_dec_item_T;
557
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100558/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100559 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100560 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100561 *
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100562 * Return FAIL for a decoding error (and give an error).
Bram Moolenaar56ead342016-02-02 18:20:08 +0100563 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100564 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100565 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100566json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100567{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100568 char_u *p;
569 int len;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100570 int retval;
571 garray_T stack;
572 typval_T item;
573 typval_T *cur_item;
574 json_dec_item_T *top_item;
575 char_u key_buf[NUMBUFLEN];
576
577 ga_init2(&stack, sizeof(json_dec_item_T), 100);
578 cur_item = res;
579 init_tv(&item);
Bram Moolenaare32abbe2017-01-10 22:57:34 +0100580 if (res != NULL)
581 init_tv(res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100582
Bram Moolenaar56ead342016-02-02 18:20:08 +0100583 fill_numbuflen(reader);
584 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100585 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100586 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100587 top_item = NULL;
588 if (stack.ga_len > 0)
589 {
590 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
591 json_skip_white(reader);
592 p = reader->js_buf + reader->js_used;
593 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100594 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100595 retval = MAYBE;
596 if (top_item->jd_type == JSON_OBJECT)
597 /* did get the key, clear it */
598 clear_tv(&top_item->jd_key_tv);
599 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100600 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100601 if (top_item->jd_type == JSON_OBJECT_KEY
602 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100603 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100604 /* Check for end of object or array. */
605 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100606 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100607 ++reader->js_used; /* consume the ']' or '}' */
608 --stack.ga_len;
609 if (stack.ga_len == 0)
610 {
611 retval = OK;
612 goto theend;
613 }
614 if (cur_item != NULL)
615 cur_item = &top_item->jd_tv;
616 goto item_end;
617 }
618 }
619 }
620
621 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
622 && (options & JSON_JS)
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100623 && reader->js_buf[reader->js_used] != '"'
624 && reader->js_buf[reader->js_used] != '\'')
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100625 {
626 char_u *key;
627
628 /* accept an object key that is not in quotes */
629 key = p = reader->js_buf + reader->js_used;
630 while (*p != NUL && *p != ':' && *p > ' ')
631 ++p;
Bram Moolenaare2c60372017-01-22 15:56:26 +0100632 if (cur_item != NULL)
633 {
634 cur_item->v_type = VAR_STRING;
635 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
636 top_item->jd_key = cur_item->vval.v_string;
637 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100638 reader->js_used += (int)(p - key);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100639 }
640 else
641 {
642 switch (*p)
643 {
644 case '[': /* start of array */
645 if (ga_grow(&stack, 1) == FAIL)
646 {
647 retval = FAIL;
648 break;
649 }
650 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
651 {
652 cur_item->v_type = VAR_SPECIAL;
653 cur_item->vval.v_number = VVAL_NONE;
654 retval = FAIL;
655 break;
656 }
657
658 ++reader->js_used; /* consume the '[' */
659 top_item = ((json_dec_item_T *)stack.ga_data)
660 + stack.ga_len;
661 top_item->jd_type = JSON_ARRAY;
662 ++stack.ga_len;
663 if (cur_item != NULL)
664 {
665 top_item->jd_tv = *cur_item;
666 cur_item = &item;
667 }
668 continue;
669
670 case '{': /* start of object */
671 if (ga_grow(&stack, 1) == FAIL)
672 {
673 retval = FAIL;
674 break;
675 }
676 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
677 {
678 cur_item->v_type = VAR_SPECIAL;
679 cur_item->vval.v_number = VVAL_NONE;
680 retval = FAIL;
681 break;
682 }
683
684 ++reader->js_used; /* consume the '{' */
685 top_item = ((json_dec_item_T *)stack.ga_data)
686 + stack.ga_len;
687 top_item->jd_type = JSON_OBJECT_KEY;
688 ++stack.ga_len;
689 if (cur_item != NULL)
690 {
691 top_item->jd_tv = *cur_item;
692 cur_item = &top_item->jd_key_tv;
693 }
694 continue;
695
696 case '"': /* string */
Bram Moolenaaree142ad2017-01-11 21:50:08 +0100697 retval = json_decode_string(reader, cur_item, *p);
698 break;
699
700 case '\'':
701 if (options & JSON_JS)
702 retval = json_decode_string(reader, cur_item, *p);
703 else
704 {
705 EMSG(_(e_invarg));
706 retval = FAIL;
707 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100708 break;
709
710 case ',': /* comma: empty item */
711 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100712 {
713 EMSG(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100714 retval = FAIL;
715 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100716 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100717 /* FALLTHROUGH */
718 case NUL: /* empty */
719 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100720 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100721 cur_item->v_type = VAR_SPECIAL;
722 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100723 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100724 retval = OK;
725 break;
726
727 default:
728 if (VIM_ISDIGIT(*p) || *p == '-')
729 {
730#ifdef FEAT_FLOAT
731 char_u *sp = p;
732
733 if (*sp == '-')
734 {
735 ++sp;
736 if (*sp == NUL)
737 {
738 retval = MAYBE;
739 break;
740 }
741 if (!VIM_ISDIGIT(*sp))
742 {
743 EMSG(_(e_invarg));
744 retval = FAIL;
745 break;
746 }
747 }
748 sp = skipdigits(sp);
749 if (*sp == '.' || *sp == 'e' || *sp == 'E')
750 {
751 if (cur_item == NULL)
752 {
753 float_T f;
754
755 len = string2float(p, &f);
756 }
757 else
758 {
759 cur_item->v_type = VAR_FLOAT;
760 len = string2float(p, &cur_item->vval.v_float);
761 }
762 }
763 else
764#endif
765 {
766 varnumber_T nr;
767
768 vim_str2nr(reader->js_buf + reader->js_used,
769 NULL, &len, 0, /* what */
770 &nr, NULL, 0);
771 if (cur_item != NULL)
772 {
773 cur_item->v_type = VAR_NUMBER;
774 cur_item->vval.v_number = nr;
775 }
776 }
777 reader->js_used += len;
778 retval = OK;
779 break;
780 }
781 if (STRNICMP((char *)p, "false", 5) == 0)
782 {
783 reader->js_used += 5;
784 if (cur_item != NULL)
785 {
786 cur_item->v_type = VAR_SPECIAL;
787 cur_item->vval.v_number = VVAL_FALSE;
788 }
789 retval = OK;
790 break;
791 }
792 if (STRNICMP((char *)p, "true", 4) == 0)
793 {
794 reader->js_used += 4;
795 if (cur_item != NULL)
796 {
797 cur_item->v_type = VAR_SPECIAL;
798 cur_item->vval.v_number = VVAL_TRUE;
799 }
800 retval = OK;
801 break;
802 }
803 if (STRNICMP((char *)p, "null", 4) == 0)
804 {
805 reader->js_used += 4;
806 if (cur_item != NULL)
807 {
808 cur_item->v_type = VAR_SPECIAL;
809 cur_item->vval.v_number = VVAL_NULL;
810 }
811 retval = OK;
812 break;
813 }
814#ifdef FEAT_FLOAT
815 if (STRNICMP((char *)p, "NaN", 3) == 0)
816 {
817 reader->js_used += 3;
818 if (cur_item != NULL)
819 {
820 cur_item->v_type = VAR_FLOAT;
821 cur_item->vval.v_float = NAN;
822 }
823 retval = OK;
824 break;
825 }
826 if (STRNICMP((char *)p, "Infinity", 8) == 0)
827 {
828 reader->js_used += 8;
829 if (cur_item != NULL)
830 {
831 cur_item->v_type = VAR_FLOAT;
832 cur_item->vval.v_float = INFINITY;
833 }
834 retval = OK;
835 break;
836 }
837#endif
838 /* check for truncated name */
839 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
840 if (
841 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
842#ifdef FEAT_FLOAT
843 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
844 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
845#endif
846 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
847 || STRNICMP((char *)p, "null", len) == 0)))
848
849 retval = MAYBE;
850 else
851 retval = FAIL;
852 break;
853 }
854
855 /* We are finished when retval is FAIL or MAYBE and when at the
856 * toplevel. */
857 if (retval == FAIL)
858 break;
859 if (retval == MAYBE || stack.ga_len == 0)
860 goto theend;
861
862 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
863 && cur_item != NULL)
864 {
865 top_item->jd_key = get_tv_string_buf_chk(cur_item, key_buf);
Bram Moolenaar059b7482017-02-05 16:34:43 +0100866 if (top_item->jd_key == NULL)
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100867 {
868 clear_tv(cur_item);
869 EMSG(_(e_invarg));
870 retval = FAIL;
871 goto theend;
872 }
873 }
874 }
875
876item_end:
877 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
878 switch (top_item->jd_type)
879 {
880 case JSON_ARRAY:
881 if (res != NULL)
882 {
883 listitem_T *li = listitem_alloc();
884
885 if (li == NULL)
886 {
887 clear_tv(cur_item);
888 retval = FAIL;
889 goto theend;
890 }
891 li->li_tv = *cur_item;
892 list_append(top_item->jd_tv.vval.v_list, li);
893 }
894 if (cur_item != NULL)
895 cur_item = &item;
896
897 json_skip_white(reader);
898 p = reader->js_buf + reader->js_used;
899 if (*p == ',')
900 ++reader->js_used;
901 else if (*p != ']')
902 {
903 if (*p == NUL)
904 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100905 else
906 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100907 EMSG(_(e_invarg));
908 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100909 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100910 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100911 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100912 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100913
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100914 case JSON_OBJECT_KEY:
915 json_skip_white(reader);
916 p = reader->js_buf + reader->js_used;
917 if (*p != ':')
918 {
919 if (cur_item != NULL)
920 clear_tv(cur_item);
921 if (*p == NUL)
922 retval = MAYBE;
923 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100924 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100925 EMSG(_(e_invarg));
926 retval = FAIL;
927 }
928 goto theend;
929 }
930 ++reader->js_used;
931 json_skip_white(reader);
932 top_item->jd_type = JSON_OBJECT;
933 if (cur_item != NULL)
934 cur_item = &item;
935 break;
936
937 case JSON_OBJECT:
938 if (cur_item != NULL
939 && dict_find(top_item->jd_tv.vval.v_dict,
940 top_item->jd_key, -1) != NULL)
941 {
Bram Moolenaar83381f72017-01-14 14:36:08 +0100942 EMSG2(_("E938: Duplicate key in JSON: \"%s\""),
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100943 top_item->jd_key);
944 clear_tv(&top_item->jd_key_tv);
945 clear_tv(cur_item);
946 retval = FAIL;
947 goto theend;
948 }
949
950 if (cur_item != NULL)
951 {
952 dictitem_T *di = dictitem_alloc(top_item->jd_key);
953
954 clear_tv(&top_item->jd_key_tv);
955 if (di == NULL)
956 {
957 clear_tv(cur_item);
958 retval = FAIL;
959 goto theend;
960 }
961 di->di_tv = *cur_item;
962 di->di_tv.v_lock = 0;
963 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
964 {
965 dictitem_free(di);
966 retval = FAIL;
967 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100968 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100969 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100970
971 json_skip_white(reader);
972 p = reader->js_buf + reader->js_used;
973 if (*p == ',')
974 ++reader->js_used;
975 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100976 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100977 if (*p == NUL)
978 retval = MAYBE;
979 else
980 {
981 EMSG(_(e_invarg));
982 retval = FAIL;
983 }
984 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100985 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100986 top_item->jd_type = JSON_OBJECT_KEY;
987 if (cur_item != NULL)
988 cur_item = &top_item->jd_key_tv;
989 break;
990 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100991 }
992
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100993 /* Get here when parsing failed. */
Bram Moolenaar7756e742016-10-21 20:35:37 +0200994 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100995 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100996 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100997 res->v_type = VAR_SPECIAL;
998 res->vval.v_number = VVAL_NONE;
999 }
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001000 EMSG(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001001
1002theend:
1003 ga_clear(&stack);
Bram Moolenaar8b2f1952017-01-10 19:44:18 +01001004 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001005}
1006
1007/*
1008 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001009 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001010 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001011 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001012 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001013json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001014{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001015 int ret;
1016
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001017 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001018 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001019 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001020 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001021 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001022 {
1023 if (ret == MAYBE)
1024 EMSG(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001025 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001026 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001027 json_skip_white(reader);
1028 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001029 {
1030 EMSG(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001031 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001032 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001033 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001034}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001035
1036/*
1037 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001038 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001039 * Return FAIL for a decoding error.
1040 * Return MAYBE for an incomplete message.
1041 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001042 */
1043 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001044json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001045{
1046 int ret;
1047
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001048 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001049 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1050 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001051 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001052 json_skip_white(reader);
1053
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001054 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001055}
1056
1057/*
1058 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaare2c60372017-01-22 15:56:26 +01001059 * "options" can be JSON_JS or zero.
1060 * This is only used for testing.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001061 * Return FAIL if the message has a decoding error.
1062 * Return MAYBE if the message is truncated, need to read more.
1063 * This only works reliable if the message contains an object, array or
1064 * string. A number might be trucated without knowing.
1065 * Does not advance the reader.
1066 */
1067 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001068json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001069{
1070 int used_save = reader->js_used;
1071 int ret;
1072
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001073 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001074 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1075 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001076 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001077 reader->js_used = used_save;
1078 return ret;
1079}
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001080#endif