blob: 34cf188a1df99056e91aadf175845a66e6976e2c [file] [log] [blame]
Bram Moolenaar520e1e42016-01-23 19:46:28 +01001/* vi:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar55fab432016-02-07 16:53:13 +010026 * The result is in allocated memory.
27 * The result is empty when encoding fails.
Bram Moolenaar595e64e2016-02-07 19:19:53 +010028 * "options" can be JSON_JS or zero;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010029 */
30 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010031json_encode(typval_T *val, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010032{
33 garray_T ga;
34
35 /* Store bytes in the growarray. */
36 ga_init2(&ga, 1, 4000);
Bram Moolenaar595e64e2016-02-07 19:19:53 +010037 if (json_encode_item(&ga, val, get_copyID(), options) == FAIL)
Bram Moolenaar55fab432016-02-07 16:53:13 +010038 {
39 vim_free(ga.ga_data);
40 return vim_strsave((char_u *)"");
41 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +010042 return ga.ga_data;
43}
44
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010045/*
Bram Moolenaar55fab432016-02-07 16:53:13 +010046 * Encode ["nr", "val"] into a JSON format string in allocated memory.
Bram Moolenaar595e64e2016-02-07 19:19:53 +010047 * "options" can be JSON_JS or zero;
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010048 * Returns NULL when out of memory.
49 */
50 char_u *
Bram Moolenaar595e64e2016-02-07 19:19:53 +010051json_encode_nr_expr(int nr, typval_T *val, int options)
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010052{
53 typval_T listtv;
54 typval_T nrtv;
55 char_u *text;
56
57 nrtv.v_type = VAR_NUMBER;
58 nrtv.vval.v_number = nr;
59 if (rettv_list_alloc(&listtv) == FAIL)
60 return NULL;
61 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
62 || list_append_tv(listtv.vval.v_list, val) == FAIL)
63 {
64 list_unref(listtv.vval.v_list);
65 return NULL;
66 }
67
Bram Moolenaar595e64e2016-02-07 19:19:53 +010068 text = json_encode(&listtv, options);
Bram Moolenaarfb1f6262016-01-31 20:24:32 +010069 list_unref(listtv.vval.v_list);
70 return text;
71}
72
Bram Moolenaar520e1e42016-01-23 19:46:28 +010073 static void
74write_string(garray_T *gap, char_u *str)
75{
76 char_u *res = str;
77 char_u numbuf[NUMBUFLEN];
78
79 if (res == NULL)
80 ga_concat(gap, (char_u *)"null");
81 else
82 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +010083#if defined(FEAT_MBYTE) && defined(USE_ICONV)
84 vimconv_T conv;
85 char_u *converted = NULL;
86
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +010087 if (!enc_utf8)
88 {
Bram Moolenaarb3628722016-02-28 14:56:39 +010089 /* Convert the text from 'encoding' to utf-8, the JSON string is
90 * always utf-8. */
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +010091 conv.vc_type = CONV_NONE;
92 convert_setup(&conv, p_enc, (char_u*)"utf-8");
93 if (conv.vc_type != CONV_NONE)
94 converted = res = string_convert(&conv, res, NULL);
95 convert_setup(&conv, NULL, NULL);
96 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +010097#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +010098 ga_append(gap, '"');
99 while (*res != NUL)
100 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100101 int c;
102#ifdef FEAT_MBYTE
103 /* always use utf-8 encoding, ignore 'encoding' */
104 c = utf_ptr2char(res);
105#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100106 c = *res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100107#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100108
109 switch (c)
110 {
111 case 0x08:
112 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
113 case 0x09:
114 ga_append(gap, '\\'); ga_append(gap, 't'); break;
115 case 0x0a:
116 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
117 case 0x0c:
118 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
119 case 0x0d:
120 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
121 case 0x22: /* " */
122 case 0x5c: /* \ */
123 ga_append(gap, '\\');
124 ga_append(gap, c);
125 break;
126 default:
127 if (c >= 0x20)
128 {
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100129#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100130 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100131#else
132 numbuf[0] = c;
133 numbuf[1] = NUL;
134#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100135 ga_concat(gap, numbuf);
136 }
137 else
138 {
139 vim_snprintf((char *)numbuf, NUMBUFLEN,
140 "\\u%04lx", (long)c);
141 ga_concat(gap, numbuf);
142 }
143 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100144#ifdef FEAT_MBYTE
145 res += utf_ptr2len(res);
146#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100147 ++res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100148#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100149 }
150 ga_append(gap, '"');
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100151#if defined(FEAT_MBYTE) && defined(USE_ICONV)
152 vim_free(converted);
153#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100154 }
155}
156
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100157/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100158 * Return TRUE if "key" can be used without quotes.
159 * That is when it starts with a letter and only contains letters, digits and
160 * underscore.
161 */
162 static int
163is_simple_key(char_u *key)
164{
165 char_u *p;
166
167 if (!ASCII_ISALPHA(*key))
168 return FALSE;
169 for (p = key + 1; *p != NUL; ++p)
170 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
171 return FALSE;
172 return TRUE;
173}
174
175/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100176 * Encode "val" into "gap".
177 * Return FAIL or OK.
178 */
179 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100180json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100181{
182 char_u numbuf[NUMBUFLEN];
183 char_u *res;
184 list_T *l;
185 dict_T *d;
186
187 switch (val->v_type)
188 {
189 case VAR_SPECIAL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100190 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100191 {
192 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
193 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100194 case VVAL_NONE: if ((options & JSON_JS) != 0
195 && (options & JSON_NO_NONE) == 0)
196 /* empty item */
197 break;
198 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100199 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
200 }
201 break;
202
203 case VAR_NUMBER:
204 vim_snprintf((char *)numbuf, NUMBUFLEN, "%ld",
205 (long)val->vval.v_number);
206 ga_concat(gap, numbuf);
207 break;
208
209 case VAR_STRING:
210 res = val->vval.v_string;
211 write_string(gap, res);
212 break;
213
214 case VAR_FUNC:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100215 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100216 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100217 /* no JSON equivalent TODO: better error */
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100218 EMSG(_(e_invarg));
219 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100220
221 case VAR_LIST:
222 l = val->vval.v_list;
223 if (l == NULL)
224 ga_concat(gap, (char_u *)"null");
225 else
226 {
227 if (l->lv_copyID == copyID)
228 ga_concat(gap, (char_u *)"[]");
229 else
230 {
231 listitem_T *li;
232
233 l->lv_copyID = copyID;
234 ga_append(gap, '[');
235 for (li = l->lv_first; li != NULL && !got_int; )
236 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100237 if (json_encode_item(gap, &li->li_tv, copyID,
238 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100239 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100240 if ((options & JSON_JS)
241 && li->li_next == NULL
242 && li->li_tv.v_type == VAR_SPECIAL
243 && li->li_tv.vval.v_number == VVAL_NONE)
244 /* add an extra comma if the last item is v:none */
245 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100246 li = li->li_next;
247 if (li != NULL)
248 ga_append(gap, ',');
249 }
250 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100251 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100252 }
253 }
254 break;
255
256 case VAR_DICT:
257 d = val->vval.v_dict;
258 if (d == NULL)
259 ga_concat(gap, (char_u *)"null");
260 else
261 {
262 if (d->dv_copyID == copyID)
263 ga_concat(gap, (char_u *)"{}");
264 else
265 {
266 int first = TRUE;
267 int todo = (int)d->dv_hashtab.ht_used;
268 hashitem_T *hi;
269
270 d->dv_copyID = copyID;
271 ga_append(gap, '{');
272
273 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
274 ++hi)
275 if (!HASHITEM_EMPTY(hi))
276 {
277 --todo;
278 if (first)
279 first = FALSE;
280 else
281 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100282 if ((options & JSON_JS)
283 && is_simple_key(hi->hi_key))
284 ga_concat(gap, hi->hi_key);
285 else
286 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100287 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100288 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100289 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100290 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100291 }
292 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100293 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100294 }
295 }
296 break;
297
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100298 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100299#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100300# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100301 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100302 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100303 else if (isinf(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100304 ga_concat(gap, (char_u *)"Infinity");
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100305 else
306# endif
307 {
308 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
309 val->vval.v_float);
310 ga_concat(gap, numbuf);
311 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100312 break;
313#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100314 case VAR_UNKNOWN:
Bram Moolenaarc6b14f02016-02-20 15:26:42 +0100315 EMSG2(_(e_intern2), "json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100316 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100317 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100318 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100319}
320
321/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100322 * When "reader" has less than NUMBUFLEN bytes available, call the fill
323 * callback to get more.
324 */
325 static void
326fill_numbuflen(js_read_T *reader)
327{
328 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
329 - reader->js_used < NUMBUFLEN)
330 {
331 if (reader->js_fill(reader))
332 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
333 }
334}
335
336/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100337 * Skip white space in "reader". All characters <= space are considered white
338 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100339 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100340 */
341 static void
342json_skip_white(js_read_T *reader)
343{
344 int c;
345
Bram Moolenaar56ead342016-02-02 18:20:08 +0100346 for (;;)
347 {
348 c = reader->js_buf[reader->js_used];
349 if (reader->js_fill != NULL && c == NUL)
350 {
351 if (reader->js_fill(reader))
352 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
353 continue;
354 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100355 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100356 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100357 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100358 }
359 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100360}
361
Bram Moolenaar56ead342016-02-02 18:20:08 +0100362 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100363json_decode_array(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100364{
365 char_u *p;
366 typval_T item;
367 listitem_T *li;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100368 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100369
Bram Moolenaar56ead342016-02-02 18:20:08 +0100370 if (res != NULL && rettv_list_alloc(res) == FAIL)
371 {
372 res->v_type = VAR_SPECIAL;
373 res->vval.v_number = VVAL_NONE;
374 return FAIL;
375 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100376 ++reader->js_used; /* consume the '[' */
377
378 while (TRUE)
379 {
380 json_skip_white(reader);
381 p = reader->js_buf + reader->js_used;
382 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100383 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100384 if (*p == ']')
385 {
386 ++reader->js_used; /* consume the ']' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100387 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100388 }
389
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100390 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100391 if (ret != OK)
392 return ret;
393 if (res != NULL)
394 {
395 li = listitem_alloc();
396 if (li == NULL)
397 {
398 clear_tv(&item);
399 return FAIL;
400 }
401 li->li_tv = item;
402 list_append(res->vval.v_list, li);
403 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100404
405 json_skip_white(reader);
406 p = reader->js_buf + reader->js_used;
407 if (*p == ',')
408 ++reader->js_used;
409 else if (*p != ']')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100410 {
411 if (*p == NUL)
412 return MAYBE;
413 return FAIL;
414 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100415 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100416 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100417}
418
Bram Moolenaar56ead342016-02-02 18:20:08 +0100419 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100420json_decode_object(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100421{
422 char_u *p;
423 typval_T tvkey;
424 typval_T item;
425 dictitem_T *di;
426 char_u buf[NUMBUFLEN];
Bram Moolenaarfbf9c6b2016-02-02 19:43:57 +0100427 char_u *key = NULL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100428 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100429
Bram Moolenaar56ead342016-02-02 18:20:08 +0100430 if (res != NULL && rettv_dict_alloc(res) == FAIL)
431 {
432 res->v_type = VAR_SPECIAL;
433 res->vval.v_number = VVAL_NONE;
434 return FAIL;
435 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100436 ++reader->js_used; /* consume the '{' */
437
438 while (TRUE)
439 {
440 json_skip_white(reader);
441 p = reader->js_buf + reader->js_used;
442 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100443 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100444 if (*p == '}')
445 {
446 ++reader->js_used; /* consume the '}' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100447 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100448 }
449
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100450 if ((options & JSON_JS) && reader->js_buf[reader->js_used] != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100451 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100452 /* accept a key that is not in quotes */
453 key = p = reader->js_buf + reader->js_used;
454 while (*p != NUL && *p != ':' && *p > ' ')
455 ++p;
456 tvkey.v_type = VAR_STRING;
457 tvkey.vval.v_string = vim_strnsave(key, (int)(p - key));
458 reader->js_used += (int)(p - key);
459 key = tvkey.vval.v_string;
460 }
461 else
462 {
463 ret = json_decode_item(reader, res == NULL ? NULL : &tvkey,
464 options);
465 if (ret != OK)
466 return ret;
467 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100468 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100469 key = get_tv_string_buf_chk(&tvkey, buf);
470 if (key == NULL || *key == NUL)
471 {
472 clear_tv(&tvkey);
473 return FAIL;
474 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100475 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100476 }
477
478 json_skip_white(reader);
479 p = reader->js_buf + reader->js_used;
480 if (*p != ':')
481 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100482 if (res != NULL)
483 clear_tv(&tvkey);
484 if (*p == NUL)
485 return MAYBE;
486 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100487 }
488 ++reader->js_used;
489 json_skip_white(reader);
490
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100491 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100492 if (ret != OK)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100493 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100494 if (res != NULL)
495 clear_tv(&tvkey);
496 return ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100497 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100498
499 if (res != NULL)
500 {
501 di = dictitem_alloc(key);
502 clear_tv(&tvkey);
503 if (di == NULL)
504 {
505 clear_tv(&item);
506 return FAIL;
507 }
508 di->di_tv = item;
Bram Moolenaar2588b5a2016-03-05 23:23:02 +0100509 di->di_tv.v_lock = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100510 if (dict_add(res->vval.v_dict, di) == FAIL)
511 {
512 dictitem_free(di);
513 return FAIL;
514 }
515 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100516
517 json_skip_white(reader);
518 p = reader->js_buf + reader->js_used;
519 if (*p == ',')
520 ++reader->js_used;
521 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100522 {
523 if (*p == NUL)
524 return MAYBE;
525 return FAIL;
526 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100527 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100528 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100529}
530
Bram Moolenaar56ead342016-02-02 18:20:08 +0100531 static int
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100532json_decode_string(js_read_T *reader, typval_T *res)
533{
534 garray_T ga;
535 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100536 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100537 int c;
538 long nr;
539 char_u buf[NUMBUFLEN];
540
Bram Moolenaar56ead342016-02-02 18:20:08 +0100541 if (res != NULL)
542 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100543
Bram Moolenaar56ead342016-02-02 18:20:08 +0100544 p = reader->js_buf + reader->js_used + 1; /* skip over " */
545 while (*p != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100546 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100547 /* The JSON is always expected to be utf-8, thus use utf functions
548 * here. The string is converted below if needed. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100549 if (*p == NUL || p[1] == NUL
550#ifdef FEAT_MBYTE
551 || utf_ptr2len(p) < utf_byte2len(*p)
552#endif
553 )
554 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100555 /* Not enough bytes to make a character or end of the string. Get
556 * more if possible. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100557 if (reader->js_fill == NULL)
558 break;
559 len = (int)(reader->js_end - p);
560 reader->js_used = (int)(p - reader->js_buf);
561 if (!reader->js_fill(reader))
562 break; /* didn't get more */
563 p = reader->js_buf + reader->js_used;
564 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
565 continue;
566 }
567
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100568 if (*p == '\\')
569 {
570 c = -1;
571 switch (p[1])
572 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100573 case '\\': c = '\\'; break;
574 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100575 case 'b': c = BS; break;
576 case 't': c = TAB; break;
577 case 'n': c = NL; break;
578 case 'f': c = FF; break;
579 case 'r': c = CAR; break;
580 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100581 if (reader->js_fill != NULL
582 && (int)(reader->js_end - p) < NUMBUFLEN)
583 {
584 reader->js_used = (int)(p - reader->js_buf);
585 if (reader->js_fill(reader))
586 {
587 p = reader->js_buf + reader->js_used;
588 reader->js_end = reader->js_buf
589 + STRLEN(reader->js_buf);
590 }
591 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100592 nr = 0;
593 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100594 vim_str2nr(p + 2, NULL, &len,
595 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
596 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100597 if (0xd800 <= nr && nr <= 0xdfff
598 && (int)(reader->js_end - p) >= 6
599 && *p == '\\' && *(p+1) == 'u')
600 {
601 long nr2 = 0;
602
603 /* decode surrogate pair: \ud812\u3456 */
604 len = 0;
605 vim_str2nr(p + 2, NULL, &len,
606 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4);
607 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
608 {
609 p += len + 2;
610 nr = (((nr - 0xd800) << 10) |
611 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
612 }
613 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100614 if (res != NULL)
615 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100616#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100617 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100618 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100619#else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100620 ga_append(&ga, nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100621#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100622 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100623 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100624 default:
625 /* not a special char, skip over \ */
626 ++p;
627 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100628 }
629 if (c > 0)
630 {
631 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100632 if (res != NULL)
633 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100634 }
635 }
636 else
637 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100638#ifdef FEAT_MBYTE
639 len = utf_ptr2len(p);
640#else
641 len = 1;
642#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100643 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100644 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100645 if (ga_grow(&ga, len) == FAIL)
646 {
647 ga_clear(&ga);
648 return FAIL;
649 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100650 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
651 ga.ga_len += len;
652 }
653 p += len;
654 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100655 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100656
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100657 reader->js_used = (int)(p - reader->js_buf);
658 if (*p == '"')
659 {
660 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100661 if (res != NULL)
662 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100663 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100664 res->v_type = VAR_STRING;
Bram Moolenaarb3628722016-02-28 14:56:39 +0100665#if defined(FEAT_MBYTE) && defined(USE_ICONV)
666 if (!enc_utf8)
667 {
668 vimconv_T conv;
669
670 /* Convert the utf-8 string to 'encoding'. */
671 conv.vc_type = CONV_NONE;
672 convert_setup(&conv, (char_u*)"utf-8", p_enc);
673 if (conv.vc_type != CONV_NONE)
674 {
675 res->vval.v_string =
676 string_convert(&conv, ga.ga_data, NULL);
677 vim_free(ga.ga_data);
678 }
679 convert_setup(&conv, NULL, NULL);
680 }
681 else
682#endif
683 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100684 }
685 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100686 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100687 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100688 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100689 res->v_type = VAR_SPECIAL;
690 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100691 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100692 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100693 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100694}
695
696/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100697 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100698 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100699 *
700 * Return FAIL for a decoding error.
701 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100702 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100703 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100704json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100705{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100706 char_u *p;
707 int len;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100708
Bram Moolenaar56ead342016-02-02 18:20:08 +0100709 fill_numbuflen(reader);
710 p = reader->js_buf + reader->js_used;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100711 switch (*p)
712 {
713 case '[': /* array */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100714 return json_decode_array(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100715
716 case '{': /* object */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100717 return json_decode_object(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100718
719 case '"': /* string */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100720 return json_decode_string(reader, res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100721
722 case ',': /* comma: empty item */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100723 if ((options & JSON_JS) == 0)
724 return FAIL;
725 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100726 case NUL: /* empty */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100727 if (res != NULL)
728 {
729 res->v_type = VAR_SPECIAL;
730 res->vval.v_number = VVAL_NONE;
731 }
732 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100733
734 default:
735 if (VIM_ISDIGIT(*p) || *p == '-')
736 {
Bram Moolenaar10b369f2016-02-29 23:12:49 +0100737#ifdef FEAT_FLOAT
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100738 char_u *sp = p;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100739
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100740 if (*sp == '-')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100741 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100742 ++sp;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100743 if (*sp == NUL)
744 return MAYBE;
745 if (!VIM_ISDIGIT(*sp))
746 return FAIL;
747 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100748 sp = skipdigits(sp);
749 if (*sp == '.' || *sp == 'e' || *sp == 'E')
750 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100751 if (res == NULL)
752 {
753 float_T f;
754
755 len = string2float(p, &f);
756 }
757 else
758 {
759 res->v_type = VAR_FLOAT;
760 len = string2float(p, &res->vval.v_float);
761 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100762 }
763 else
764#endif
765 {
766 long nr;
767
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100768 vim_str2nr(reader->js_buf + reader->js_used,
769 NULL, &len, 0, /* what */
770 &nr, NULL, 0);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100771 if (res != NULL)
772 {
773 res->v_type = VAR_NUMBER;
774 res->vval.v_number = nr;
775 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100776 }
777 reader->js_used += len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100778 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100779 }
780 if (STRNICMP((char *)p, "false", 5) == 0)
781 {
782 reader->js_used += 5;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100783 if (res != NULL)
784 {
785 res->v_type = VAR_SPECIAL;
786 res->vval.v_number = VVAL_FALSE;
787 }
788 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100789 }
790 if (STRNICMP((char *)p, "true", 4) == 0)
791 {
792 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100793 if (res != NULL)
794 {
795 res->v_type = VAR_SPECIAL;
796 res->vval.v_number = VVAL_TRUE;
797 }
798 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100799 }
800 if (STRNICMP((char *)p, "null", 4) == 0)
801 {
802 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100803 if (res != NULL)
804 {
805 res->v_type = VAR_SPECIAL;
806 res->vval.v_number = VVAL_NULL;
807 }
808 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100809 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100810#ifdef FEAT_FLOAT
811 if (STRNICMP((char *)p, "NaN", 3) == 0)
812 {
813 reader->js_used += 3;
814 if (res != NULL)
815 {
816 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100817 res->vval.v_float = NAN;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100818 }
819 return OK;
820 }
821 if (STRNICMP((char *)p, "Infinity", 8) == 0)
822 {
823 reader->js_used += 8;
824 if (res != NULL)
825 {
826 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100827 res->vval.v_float = INFINITY;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100828 }
829 return OK;
830 }
831#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100832 /* check for truncated name */
833 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100834 if (
835 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
836#ifdef FEAT_FLOAT
837 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
838 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
839#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100840 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
841 || STRNICMP((char *)p, "null", len) == 0)))
842 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100843 break;
844 }
845
Bram Moolenaar56ead342016-02-02 18:20:08 +0100846 if (res != NUL)
847 {
848 res->v_type = VAR_SPECIAL;
849 res->vval.v_number = VVAL_NONE;
850 }
851 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100852}
853
854/*
855 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100856 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100857 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100858 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100859 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100860json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100861{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100862 int ret;
863
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100864 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100865 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100866 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100867 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100868 if (ret != OK)
869 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100870 json_skip_white(reader);
871 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100872 return FAIL;
873 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100874}
Bram Moolenaar56ead342016-02-02 18:20:08 +0100875
876/*
877 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100878 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100879 * Return FAIL if the message has a decoding error or the message is
880 * truncated. Consumes the message anyway.
881 */
882 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100883json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100884{
885 int ret;
886
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100887 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100888 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
889 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100890 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100891 json_skip_white(reader);
892
893 return ret == OK ? OK : FAIL;
894}
895
896/*
897 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100898 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100899 * Return FAIL if the message has a decoding error.
900 * Return MAYBE if the message is truncated, need to read more.
901 * This only works reliable if the message contains an object, array or
902 * string. A number might be trucated without knowing.
903 * Does not advance the reader.
904 */
905 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100906json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100907{
908 int used_save = reader->js_used;
909 int ret;
910
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100911 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100912 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
913 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100914 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100915 reader->js_used = used_save;
916 return ret;
917}
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100918#endif