blob: 091d98f5926f9c1c8c75dac6a948a187dac8f2ec [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 Moolenaar520e1e42016-01-23 19:46:28 +0100381json_decode_string(js_read_T *reader, typval_T *res)
382{
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 Moolenaar56ead342016-02-02 18:20:08 +0100392 p = reader->js_buf + reader->js_used + 1; /* skip over " */
393 while (*p != '"')
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);
507 if (*p == '"')
508 {
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 Moolenaar520e1e42016-01-23 19:46:28 +0100580
Bram Moolenaar56ead342016-02-02 18:20:08 +0100581 fill_numbuflen(reader);
582 p = reader->js_buf + reader->js_used;
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100583 for (;;)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100584 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100585 top_item = NULL;
586 if (stack.ga_len > 0)
587 {
588 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
589 json_skip_white(reader);
590 p = reader->js_buf + reader->js_used;
591 if (*p == NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100592 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100593 retval = MAYBE;
594 if (top_item->jd_type == JSON_OBJECT)
595 /* did get the key, clear it */
596 clear_tv(&top_item->jd_key_tv);
597 goto theend;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100598 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100599 if (top_item->jd_type == JSON_OBJECT_KEY
600 || top_item->jd_type == JSON_ARRAY)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100601 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100602 /* Check for end of object or array. */
603 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
Bram Moolenaar56ead342016-02-02 18:20:08 +0100604 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100605 ++reader->js_used; /* consume the ']' or '}' */
606 --stack.ga_len;
607 if (stack.ga_len == 0)
608 {
609 retval = OK;
610 goto theend;
611 }
612 if (cur_item != NULL)
613 cur_item = &top_item->jd_tv;
614 goto item_end;
615 }
616 }
617 }
618
619 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
620 && (options & JSON_JS)
621 && reader->js_buf[reader->js_used] != '"')
622 {
623 char_u *key;
624
625 /* accept an object key that is not in quotes */
626 key = p = reader->js_buf + reader->js_used;
627 while (*p != NUL && *p != ':' && *p > ' ')
628 ++p;
629 cur_item->v_type = VAR_STRING;
630 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
631 reader->js_used += (int)(p - key);
632 top_item->jd_key = cur_item->vval.v_string;
633 }
634 else
635 {
636 switch (*p)
637 {
638 case '[': /* start of array */
639 if (ga_grow(&stack, 1) == FAIL)
640 {
641 retval = FAIL;
642 break;
643 }
644 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
645 {
646 cur_item->v_type = VAR_SPECIAL;
647 cur_item->vval.v_number = VVAL_NONE;
648 retval = FAIL;
649 break;
650 }
651
652 ++reader->js_used; /* consume the '[' */
653 top_item = ((json_dec_item_T *)stack.ga_data)
654 + stack.ga_len;
655 top_item->jd_type = JSON_ARRAY;
656 ++stack.ga_len;
657 if (cur_item != NULL)
658 {
659 top_item->jd_tv = *cur_item;
660 cur_item = &item;
661 }
662 continue;
663
664 case '{': /* start of object */
665 if (ga_grow(&stack, 1) == FAIL)
666 {
667 retval = FAIL;
668 break;
669 }
670 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
671 {
672 cur_item->v_type = VAR_SPECIAL;
673 cur_item->vval.v_number = VVAL_NONE;
674 retval = FAIL;
675 break;
676 }
677
678 ++reader->js_used; /* consume the '{' */
679 top_item = ((json_dec_item_T *)stack.ga_data)
680 + stack.ga_len;
681 top_item->jd_type = JSON_OBJECT_KEY;
682 ++stack.ga_len;
683 if (cur_item != NULL)
684 {
685 top_item->jd_tv = *cur_item;
686 cur_item = &top_item->jd_key_tv;
687 }
688 continue;
689
690 case '"': /* string */
691 retval = json_decode_string(reader, cur_item);
692 break;
693
694 case ',': /* comma: empty item */
695 if ((options & JSON_JS) == 0)
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100696 {
697 EMSG(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100698 retval = FAIL;
699 break;
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100700 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100701 /* FALLTHROUGH */
702 case NUL: /* empty */
703 if (cur_item != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100704 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100705 cur_item->v_type = VAR_SPECIAL;
706 cur_item->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100707 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100708 retval = OK;
709 break;
710
711 default:
712 if (VIM_ISDIGIT(*p) || *p == '-')
713 {
714#ifdef FEAT_FLOAT
715 char_u *sp = p;
716
717 if (*sp == '-')
718 {
719 ++sp;
720 if (*sp == NUL)
721 {
722 retval = MAYBE;
723 break;
724 }
725 if (!VIM_ISDIGIT(*sp))
726 {
727 EMSG(_(e_invarg));
728 retval = FAIL;
729 break;
730 }
731 }
732 sp = skipdigits(sp);
733 if (*sp == '.' || *sp == 'e' || *sp == 'E')
734 {
735 if (cur_item == NULL)
736 {
737 float_T f;
738
739 len = string2float(p, &f);
740 }
741 else
742 {
743 cur_item->v_type = VAR_FLOAT;
744 len = string2float(p, &cur_item->vval.v_float);
745 }
746 }
747 else
748#endif
749 {
750 varnumber_T nr;
751
752 vim_str2nr(reader->js_buf + reader->js_used,
753 NULL, &len, 0, /* what */
754 &nr, NULL, 0);
755 if (cur_item != NULL)
756 {
757 cur_item->v_type = VAR_NUMBER;
758 cur_item->vval.v_number = nr;
759 }
760 }
761 reader->js_used += len;
762 retval = OK;
763 break;
764 }
765 if (STRNICMP((char *)p, "false", 5) == 0)
766 {
767 reader->js_used += 5;
768 if (cur_item != NULL)
769 {
770 cur_item->v_type = VAR_SPECIAL;
771 cur_item->vval.v_number = VVAL_FALSE;
772 }
773 retval = OK;
774 break;
775 }
776 if (STRNICMP((char *)p, "true", 4) == 0)
777 {
778 reader->js_used += 4;
779 if (cur_item != NULL)
780 {
781 cur_item->v_type = VAR_SPECIAL;
782 cur_item->vval.v_number = VVAL_TRUE;
783 }
784 retval = OK;
785 break;
786 }
787 if (STRNICMP((char *)p, "null", 4) == 0)
788 {
789 reader->js_used += 4;
790 if (cur_item != NULL)
791 {
792 cur_item->v_type = VAR_SPECIAL;
793 cur_item->vval.v_number = VVAL_NULL;
794 }
795 retval = OK;
796 break;
797 }
798#ifdef FEAT_FLOAT
799 if (STRNICMP((char *)p, "NaN", 3) == 0)
800 {
801 reader->js_used += 3;
802 if (cur_item != NULL)
803 {
804 cur_item->v_type = VAR_FLOAT;
805 cur_item->vval.v_float = NAN;
806 }
807 retval = OK;
808 break;
809 }
810 if (STRNICMP((char *)p, "Infinity", 8) == 0)
811 {
812 reader->js_used += 8;
813 if (cur_item != NULL)
814 {
815 cur_item->v_type = VAR_FLOAT;
816 cur_item->vval.v_float = INFINITY;
817 }
818 retval = OK;
819 break;
820 }
821#endif
822 /* check for truncated name */
823 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
824 if (
825 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
826#ifdef FEAT_FLOAT
827 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
828 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
829#endif
830 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
831 || STRNICMP((char *)p, "null", len) == 0)))
832
833 retval = MAYBE;
834 else
835 retval = FAIL;
836 break;
837 }
838
839 /* We are finished when retval is FAIL or MAYBE and when at the
840 * toplevel. */
841 if (retval == FAIL)
842 break;
843 if (retval == MAYBE || stack.ga_len == 0)
844 goto theend;
845
846 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
847 && cur_item != NULL)
848 {
849 top_item->jd_key = get_tv_string_buf_chk(cur_item, key_buf);
850 if (top_item->jd_key == NULL || *top_item->jd_key == NUL)
851 {
852 clear_tv(cur_item);
853 EMSG(_(e_invarg));
854 retval = FAIL;
855 goto theend;
856 }
857 }
858 }
859
860item_end:
861 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
862 switch (top_item->jd_type)
863 {
864 case JSON_ARRAY:
865 if (res != NULL)
866 {
867 listitem_T *li = listitem_alloc();
868
869 if (li == NULL)
870 {
871 clear_tv(cur_item);
872 retval = FAIL;
873 goto theend;
874 }
875 li->li_tv = *cur_item;
876 list_append(top_item->jd_tv.vval.v_list, li);
877 }
878 if (cur_item != NULL)
879 cur_item = &item;
880
881 json_skip_white(reader);
882 p = reader->js_buf + reader->js_used;
883 if (*p == ',')
884 ++reader->js_used;
885 else if (*p != ']')
886 {
887 if (*p == NUL)
888 retval = MAYBE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100889 else
890 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100891 EMSG(_(e_invarg));
892 retval = FAIL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100893 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100894 goto theend;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100895 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100896 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100897
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100898 case JSON_OBJECT_KEY:
899 json_skip_white(reader);
900 p = reader->js_buf + reader->js_used;
901 if (*p != ':')
902 {
903 if (cur_item != NULL)
904 clear_tv(cur_item);
905 if (*p == NUL)
906 retval = MAYBE;
907 else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100908 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100909 EMSG(_(e_invarg));
910 retval = FAIL;
911 }
912 goto theend;
913 }
914 ++reader->js_used;
915 json_skip_white(reader);
916 top_item->jd_type = JSON_OBJECT;
917 if (cur_item != NULL)
918 cur_item = &item;
919 break;
920
921 case JSON_OBJECT:
922 if (cur_item != NULL
923 && dict_find(top_item->jd_tv.vval.v_dict,
924 top_item->jd_key, -1) != NULL)
925 {
926 EMSG2(_("E937: Duplicate key in JSON: \"%s\""),
927 top_item->jd_key);
928 clear_tv(&top_item->jd_key_tv);
929 clear_tv(cur_item);
930 retval = FAIL;
931 goto theend;
932 }
933
934 if (cur_item != NULL)
935 {
936 dictitem_T *di = dictitem_alloc(top_item->jd_key);
937
938 clear_tv(&top_item->jd_key_tv);
939 if (di == NULL)
940 {
941 clear_tv(cur_item);
942 retval = FAIL;
943 goto theend;
944 }
945 di->di_tv = *cur_item;
946 di->di_tv.v_lock = 0;
947 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
948 {
949 dictitem_free(di);
950 retval = FAIL;
951 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100952 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100953 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100954
955 json_skip_white(reader);
956 p = reader->js_buf + reader->js_used;
957 if (*p == ',')
958 ++reader->js_used;
959 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100960 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100961 if (*p == NUL)
962 retval = MAYBE;
963 else
964 {
965 EMSG(_(e_invarg));
966 retval = FAIL;
967 }
968 goto theend;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100969 }
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100970 top_item->jd_type = JSON_OBJECT_KEY;
971 if (cur_item != NULL)
972 cur_item = &top_item->jd_key_tv;
973 break;
974 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100975 }
976
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100977 /* Get here when parsing failed. */
Bram Moolenaar7756e742016-10-21 20:35:37 +0200978 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100979 {
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100980 clear_tv(res);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100981 res->v_type = VAR_SPECIAL;
982 res->vval.v_number = VVAL_NONE;
983 }
Bram Moolenaar03c60c12017-01-10 15:15:37 +0100984 EMSG(_(e_invarg));
Bram Moolenaar8b2f1952017-01-10 19:44:18 +0100985
986theend:
987 ga_clear(&stack);
988 clear_tv(&item);
989 return retval;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100990}
991
992/*
993 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100994 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100995 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100996 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100997 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100998json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100999{
Bram Moolenaar56ead342016-02-02 18:20:08 +01001000 int ret;
1001
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001002 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001003 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001004 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001005 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001006 if (ret != OK)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001007 {
1008 if (ret == MAYBE)
1009 EMSG(_(e_invarg));
Bram Moolenaar56ead342016-02-02 18:20:08 +01001010 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001011 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001012 json_skip_white(reader);
1013 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001014 {
1015 EMSG(_(e_trailing));
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001016 return FAIL;
Bram Moolenaar03c60c12017-01-10 15:15:37 +01001017 }
Bram Moolenaar19d2f152016-02-01 21:38:19 +01001018 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001019}
Bram Moolenaar56ead342016-02-02 18:20:08 +01001020
1021/*
1022 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001023 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001024 * Return FAIL for a decoding error.
1025 * Return MAYBE for an incomplete message.
1026 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +01001027 */
1028 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001029json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001030{
1031 int ret;
1032
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001033 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001034 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1035 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001036 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001037 json_skip_white(reader);
1038
Bram Moolenaarba61ac02016-03-20 16:40:37 +01001039 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001040}
1041
1042/*
1043 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001044 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +01001045 * Return FAIL if the message has a decoding error.
1046 * Return MAYBE if the message is truncated, need to read more.
1047 * This only works reliable if the message contains an object, array or
1048 * string. A number might be trucated without knowing.
1049 * Does not advance the reader.
1050 */
1051 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001052json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +01001053{
1054 int used_save = reader->js_used;
1055 int ret;
1056
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001057 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +01001058 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
1059 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +01001060 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +01001061 reader->js_used = used_save;
1062 return ret;
1063}
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001064#endif