blob: cf887e5afd9394813d5ecf47ababfad607eb17f8 [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;
509 if (dict_add(res->vval.v_dict, di) == FAIL)
510 {
511 dictitem_free(di);
512 return FAIL;
513 }
514 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100515
516 json_skip_white(reader);
517 p = reader->js_buf + reader->js_used;
518 if (*p == ',')
519 ++reader->js_used;
520 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100521 {
522 if (*p == NUL)
523 return MAYBE;
524 return FAIL;
525 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100526 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100527 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100528}
529
Bram Moolenaar56ead342016-02-02 18:20:08 +0100530 static int
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100531json_decode_string(js_read_T *reader, typval_T *res)
532{
533 garray_T ga;
534 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100535 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100536 int c;
537 long nr;
538 char_u buf[NUMBUFLEN];
539
Bram Moolenaar56ead342016-02-02 18:20:08 +0100540 if (res != NULL)
541 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100542
Bram Moolenaar56ead342016-02-02 18:20:08 +0100543 p = reader->js_buf + reader->js_used + 1; /* skip over " */
544 while (*p != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100545 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100546 /* The JSON is always expected to be utf-8, thus use utf functions
547 * here. The string is converted below if needed. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100548 if (*p == NUL || p[1] == NUL
549#ifdef FEAT_MBYTE
550 || utf_ptr2len(p) < utf_byte2len(*p)
551#endif
552 )
553 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100554 /* Not enough bytes to make a character or end of the string. Get
555 * more if possible. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100556 if (reader->js_fill == NULL)
557 break;
558 len = (int)(reader->js_end - p);
559 reader->js_used = (int)(p - reader->js_buf);
560 if (!reader->js_fill(reader))
561 break; /* didn't get more */
562 p = reader->js_buf + reader->js_used;
563 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
564 continue;
565 }
566
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100567 if (*p == '\\')
568 {
569 c = -1;
570 switch (p[1])
571 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100572 case '\\': c = '\\'; break;
573 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100574 case 'b': c = BS; break;
575 case 't': c = TAB; break;
576 case 'n': c = NL; break;
577 case 'f': c = FF; break;
578 case 'r': c = CAR; break;
579 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100580 if (reader->js_fill != NULL
581 && (int)(reader->js_end - p) < NUMBUFLEN)
582 {
583 reader->js_used = (int)(p - reader->js_buf);
584 if (reader->js_fill(reader))
585 {
586 p = reader->js_buf + reader->js_used;
587 reader->js_end = reader->js_buf
588 + STRLEN(reader->js_buf);
589 }
590 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100591 nr = 0;
592 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100593 vim_str2nr(p + 2, NULL, &len,
594 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
595 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100596 if (0xd800 <= nr && nr <= 0xdfff
597 && (int)(reader->js_end - p) >= 6
598 && *p == '\\' && *(p+1) == 'u')
599 {
600 long nr2 = 0;
601
602 /* decode surrogate pair: \ud812\u3456 */
603 len = 0;
604 vim_str2nr(p + 2, NULL, &len,
605 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4);
606 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
607 {
608 p += len + 2;
609 nr = (((nr - 0xd800) << 10) |
610 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
611 }
612 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100613 if (res != NULL)
614 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100615#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100616 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100617 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100618#else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100619 ga_append(&ga, nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100620#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100621 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100622 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100623 default:
624 /* not a special char, skip over \ */
625 ++p;
626 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100627 }
628 if (c > 0)
629 {
630 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100631 if (res != NULL)
632 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100633 }
634 }
635 else
636 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100637#ifdef FEAT_MBYTE
638 len = utf_ptr2len(p);
639#else
640 len = 1;
641#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100642 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100643 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100644 if (ga_grow(&ga, len) == FAIL)
645 {
646 ga_clear(&ga);
647 return FAIL;
648 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100649 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
650 ga.ga_len += len;
651 }
652 p += len;
653 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100654 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100655
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100656 reader->js_used = (int)(p - reader->js_buf);
657 if (*p == '"')
658 {
659 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100660 if (res != NULL)
661 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100662 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100663 res->v_type = VAR_STRING;
Bram Moolenaarb3628722016-02-28 14:56:39 +0100664#if defined(FEAT_MBYTE) && defined(USE_ICONV)
665 if (!enc_utf8)
666 {
667 vimconv_T conv;
668
669 /* Convert the utf-8 string to 'encoding'. */
670 conv.vc_type = CONV_NONE;
671 convert_setup(&conv, (char_u*)"utf-8", p_enc);
672 if (conv.vc_type != CONV_NONE)
673 {
674 res->vval.v_string =
675 string_convert(&conv, ga.ga_data, NULL);
676 vim_free(ga.ga_data);
677 }
678 convert_setup(&conv, NULL, NULL);
679 }
680 else
681#endif
682 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100683 }
684 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100685 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100686 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100687 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100688 res->v_type = VAR_SPECIAL;
689 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100690 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100691 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100692 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100693}
694
695/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100696 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100697 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100698 *
699 * Return FAIL for a decoding error.
700 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100701 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100702 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100703json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100704{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100705 char_u *p;
706 int len;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100707
Bram Moolenaar56ead342016-02-02 18:20:08 +0100708 fill_numbuflen(reader);
709 p = reader->js_buf + reader->js_used;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100710 switch (*p)
711 {
712 case '[': /* array */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100713 return json_decode_array(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100714
715 case '{': /* object */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100716 return json_decode_object(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100717
718 case '"': /* string */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100719 return json_decode_string(reader, res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100720
721 case ',': /* comma: empty item */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100722 if ((options & JSON_JS) == 0)
723 return FAIL;
724 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100725 case NUL: /* empty */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100726 if (res != NULL)
727 {
728 res->v_type = VAR_SPECIAL;
729 res->vval.v_number = VVAL_NONE;
730 }
731 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100732
733 default:
734 if (VIM_ISDIGIT(*p) || *p == '-')
735 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100736 char_u *sp = p;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100737
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100738#ifdef FEAT_FLOAT
739 if (*sp == '-')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100740 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100741 ++sp;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100742 if (*sp == NUL)
743 return MAYBE;
744 if (!VIM_ISDIGIT(*sp))
745 return FAIL;
746 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100747 sp = skipdigits(sp);
748 if (*sp == '.' || *sp == 'e' || *sp == 'E')
749 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100750 if (res == NULL)
751 {
752 float_T f;
753
754 len = string2float(p, &f);
755 }
756 else
757 {
758 res->v_type = VAR_FLOAT;
759 len = string2float(p, &res->vval.v_float);
760 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100761 }
762 else
763#endif
764 {
765 long nr;
766
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100767 vim_str2nr(reader->js_buf + reader->js_used,
768 NULL, &len, 0, /* what */
769 &nr, NULL, 0);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100770 if (res != NULL)
771 {
772 res->v_type = VAR_NUMBER;
773 res->vval.v_number = nr;
774 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100775 }
776 reader->js_used += len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100777 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100778 }
779 if (STRNICMP((char *)p, "false", 5) == 0)
780 {
781 reader->js_used += 5;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100782 if (res != NULL)
783 {
784 res->v_type = VAR_SPECIAL;
785 res->vval.v_number = VVAL_FALSE;
786 }
787 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100788 }
789 if (STRNICMP((char *)p, "true", 4) == 0)
790 {
791 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100792 if (res != NULL)
793 {
794 res->v_type = VAR_SPECIAL;
795 res->vval.v_number = VVAL_TRUE;
796 }
797 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100798 }
799 if (STRNICMP((char *)p, "null", 4) == 0)
800 {
801 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100802 if (res != NULL)
803 {
804 res->v_type = VAR_SPECIAL;
805 res->vval.v_number = VVAL_NULL;
806 }
807 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100808 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100809#ifdef FEAT_FLOAT
810 if (STRNICMP((char *)p, "NaN", 3) == 0)
811 {
812 reader->js_used += 3;
813 if (res != NULL)
814 {
815 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100816 res->vval.v_float = NAN;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100817 }
818 return OK;
819 }
820 if (STRNICMP((char *)p, "Infinity", 8) == 0)
821 {
822 reader->js_used += 8;
823 if (res != NULL)
824 {
825 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100826 res->vval.v_float = INFINITY;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100827 }
828 return OK;
829 }
830#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100831 /* check for truncated name */
832 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100833 if (
834 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
835#ifdef FEAT_FLOAT
836 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
837 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
838#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100839 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
840 || STRNICMP((char *)p, "null", len) == 0)))
841 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100842 break;
843 }
844
Bram Moolenaar56ead342016-02-02 18:20:08 +0100845 if (res != NUL)
846 {
847 res->v_type = VAR_SPECIAL;
848 res->vval.v_number = VVAL_NONE;
849 }
850 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100851}
852
853/*
854 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100855 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100856 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100857 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100858 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100859json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100860{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100861 int ret;
862
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100863 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100864 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100865 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100866 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100867 if (ret != OK)
868 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100869 json_skip_white(reader);
870 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100871 return FAIL;
872 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100873}
Bram Moolenaar56ead342016-02-02 18:20:08 +0100874
875/*
876 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100877 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100878 * Return FAIL if the message has a decoding error or the message is
879 * truncated. Consumes the message anyway.
880 */
881 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100882json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100883{
884 int ret;
885
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100886 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100887 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
888 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100889 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100890 json_skip_white(reader);
891
892 return ret == OK ? OK : FAIL;
893}
894
895/*
896 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100897 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100898 * Return FAIL if the message has a decoding error.
899 * Return MAYBE if the message is truncated, need to read more.
900 * This only works reliable if the message contains an object, array or
901 * string. A number might be trucated without knowing.
902 * Does not advance the reader.
903 */
904 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100905json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100906{
907 int used_save = reader->js_used;
908 int ret;
909
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100910 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100911 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
912 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100913 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100914 reader->js_used = used_save;
915 return ret;
916}
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100917#endif