blob: 9738fc5fec59ee37e13dc9c12b7be46732b8da46 [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 Moolenaar1735bc92016-03-14 23:05:14 +0100215 case VAR_PARTIAL:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100216 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100217 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100218 /* no JSON equivalent TODO: better error */
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100219 EMSG(_(e_invarg));
220 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100221
222 case VAR_LIST:
223 l = val->vval.v_list;
224 if (l == NULL)
225 ga_concat(gap, (char_u *)"null");
226 else
227 {
228 if (l->lv_copyID == copyID)
229 ga_concat(gap, (char_u *)"[]");
230 else
231 {
232 listitem_T *li;
233
234 l->lv_copyID = copyID;
235 ga_append(gap, '[');
236 for (li = l->lv_first; li != NULL && !got_int; )
237 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100238 if (json_encode_item(gap, &li->li_tv, copyID,
239 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100240 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100241 if ((options & JSON_JS)
242 && li->li_next == NULL
243 && li->li_tv.v_type == VAR_SPECIAL
244 && li->li_tv.vval.v_number == VVAL_NONE)
245 /* add an extra comma if the last item is v:none */
246 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100247 li = li->li_next;
248 if (li != NULL)
249 ga_append(gap, ',');
250 }
251 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100252 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100253 }
254 }
255 break;
256
257 case VAR_DICT:
258 d = val->vval.v_dict;
259 if (d == NULL)
260 ga_concat(gap, (char_u *)"null");
261 else
262 {
263 if (d->dv_copyID == copyID)
264 ga_concat(gap, (char_u *)"{}");
265 else
266 {
267 int first = TRUE;
268 int todo = (int)d->dv_hashtab.ht_used;
269 hashitem_T *hi;
270
271 d->dv_copyID = copyID;
272 ga_append(gap, '{');
273
274 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
275 ++hi)
276 if (!HASHITEM_EMPTY(hi))
277 {
278 --todo;
279 if (first)
280 first = FALSE;
281 else
282 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100283 if ((options & JSON_JS)
284 && is_simple_key(hi->hi_key))
285 ga_concat(gap, hi->hi_key);
286 else
287 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100288 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100289 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100290 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100291 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100292 }
293 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100294 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100295 }
296 }
297 break;
298
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100299 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100300#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100301# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100302 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100303 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100304 else if (isinf(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100305 ga_concat(gap, (char_u *)"Infinity");
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100306 else
307# endif
308 {
309 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
310 val->vval.v_float);
311 ga_concat(gap, numbuf);
312 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100313 break;
314#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100315 case VAR_UNKNOWN:
Bram Moolenaarc6b14f02016-02-20 15:26:42 +0100316 EMSG2(_(e_intern2), "json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100317 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100318 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100319 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100320}
321
322/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100323 * When "reader" has less than NUMBUFLEN bytes available, call the fill
324 * callback to get more.
325 */
326 static void
327fill_numbuflen(js_read_T *reader)
328{
329 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
330 - reader->js_used < NUMBUFLEN)
331 {
332 if (reader->js_fill(reader))
333 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
334 }
335}
336
337/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100338 * Skip white space in "reader". All characters <= space are considered white
339 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100340 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100341 */
342 static void
343json_skip_white(js_read_T *reader)
344{
345 int c;
346
Bram Moolenaar56ead342016-02-02 18:20:08 +0100347 for (;;)
348 {
349 c = reader->js_buf[reader->js_used];
350 if (reader->js_fill != NULL && c == NUL)
351 {
352 if (reader->js_fill(reader))
353 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
354 continue;
355 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100356 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100357 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100358 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100359 }
360 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100361}
362
Bram Moolenaar56ead342016-02-02 18:20:08 +0100363 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100364json_decode_array(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100365{
366 char_u *p;
367 typval_T item;
368 listitem_T *li;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100369 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100370
Bram Moolenaar56ead342016-02-02 18:20:08 +0100371 if (res != NULL && rettv_list_alloc(res) == FAIL)
372 {
373 res->v_type = VAR_SPECIAL;
374 res->vval.v_number = VVAL_NONE;
375 return FAIL;
376 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100377 ++reader->js_used; /* consume the '[' */
378
379 while (TRUE)
380 {
381 json_skip_white(reader);
382 p = reader->js_buf + reader->js_used;
383 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100384 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100385 if (*p == ']')
386 {
387 ++reader->js_used; /* consume the ']' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100388 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100389 }
390
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100391 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100392 if (ret != OK)
393 return ret;
394 if (res != NULL)
395 {
396 li = listitem_alloc();
397 if (li == NULL)
398 {
399 clear_tv(&item);
400 return FAIL;
401 }
402 li->li_tv = item;
403 list_append(res->vval.v_list, li);
404 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100405
406 json_skip_white(reader);
407 p = reader->js_buf + reader->js_used;
408 if (*p == ',')
409 ++reader->js_used;
410 else if (*p != ']')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100411 {
412 if (*p == NUL)
413 return MAYBE;
414 return FAIL;
415 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100416 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100417 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100418}
419
Bram Moolenaar56ead342016-02-02 18:20:08 +0100420 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100421json_decode_object(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100422{
423 char_u *p;
424 typval_T tvkey;
425 typval_T item;
426 dictitem_T *di;
427 char_u buf[NUMBUFLEN];
Bram Moolenaarfbf9c6b2016-02-02 19:43:57 +0100428 char_u *key = NULL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100429 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100430
Bram Moolenaar56ead342016-02-02 18:20:08 +0100431 if (res != NULL && rettv_dict_alloc(res) == FAIL)
432 {
433 res->v_type = VAR_SPECIAL;
434 res->vval.v_number = VVAL_NONE;
435 return FAIL;
436 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100437 ++reader->js_used; /* consume the '{' */
438
439 while (TRUE)
440 {
441 json_skip_white(reader);
442 p = reader->js_buf + reader->js_used;
443 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100444 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100445 if (*p == '}')
446 {
447 ++reader->js_used; /* consume the '}' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100448 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100449 }
450
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100451 if ((options & JSON_JS) && reader->js_buf[reader->js_used] != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100452 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100453 /* accept a key that is not in quotes */
454 key = p = reader->js_buf + reader->js_used;
455 while (*p != NUL && *p != ':' && *p > ' ')
456 ++p;
457 tvkey.v_type = VAR_STRING;
458 tvkey.vval.v_string = vim_strnsave(key, (int)(p - key));
459 reader->js_used += (int)(p - key);
460 key = tvkey.vval.v_string;
461 }
462 else
463 {
464 ret = json_decode_item(reader, res == NULL ? NULL : &tvkey,
465 options);
466 if (ret != OK)
467 return ret;
468 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100469 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100470 key = get_tv_string_buf_chk(&tvkey, buf);
471 if (key == NULL || *key == NUL)
472 {
473 clear_tv(&tvkey);
474 return FAIL;
475 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100476 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100477 }
478
479 json_skip_white(reader);
480 p = reader->js_buf + reader->js_used;
481 if (*p != ':')
482 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100483 if (res != NULL)
484 clear_tv(&tvkey);
485 if (*p == NUL)
486 return MAYBE;
487 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100488 }
489 ++reader->js_used;
490 json_skip_white(reader);
491
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100492 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100493 if (ret != OK)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100494 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100495 if (res != NULL)
496 clear_tv(&tvkey);
497 return ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100498 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100499
500 if (res != NULL)
501 {
502 di = dictitem_alloc(key);
503 clear_tv(&tvkey);
504 if (di == NULL)
505 {
506 clear_tv(&item);
507 return FAIL;
508 }
509 di->di_tv = item;
Bram Moolenaar2588b5a2016-03-05 23:23:02 +0100510 di->di_tv.v_lock = 0;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100511 if (dict_add(res->vval.v_dict, di) == FAIL)
512 {
513 dictitem_free(di);
514 return FAIL;
515 }
516 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100517
518 json_skip_white(reader);
519 p = reader->js_buf + reader->js_used;
520 if (*p == ',')
521 ++reader->js_used;
522 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100523 {
524 if (*p == NUL)
525 return MAYBE;
526 return FAIL;
527 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100528 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100529 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100530}
531
Bram Moolenaar56ead342016-02-02 18:20:08 +0100532 static int
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100533json_decode_string(js_read_T *reader, typval_T *res)
534{
535 garray_T ga;
536 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100537 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100538 int c;
539 long nr;
540 char_u buf[NUMBUFLEN];
541
Bram Moolenaar56ead342016-02-02 18:20:08 +0100542 if (res != NULL)
543 ga_init2(&ga, 1, 200);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100544
Bram Moolenaar56ead342016-02-02 18:20:08 +0100545 p = reader->js_buf + reader->js_used + 1; /* skip over " */
546 while (*p != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100547 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100548 /* The JSON is always expected to be utf-8, thus use utf functions
549 * here. The string is converted below if needed. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100550 if (*p == NUL || p[1] == NUL
551#ifdef FEAT_MBYTE
552 || utf_ptr2len(p) < utf_byte2len(*p)
553#endif
554 )
555 {
Bram Moolenaarb3628722016-02-28 14:56:39 +0100556 /* Not enough bytes to make a character or end of the string. Get
557 * more if possible. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100558 if (reader->js_fill == NULL)
559 break;
560 len = (int)(reader->js_end - p);
561 reader->js_used = (int)(p - reader->js_buf);
562 if (!reader->js_fill(reader))
563 break; /* didn't get more */
564 p = reader->js_buf + reader->js_used;
565 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
566 continue;
567 }
568
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100569 if (*p == '\\')
570 {
571 c = -1;
572 switch (p[1])
573 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100574 case '\\': c = '\\'; break;
575 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100576 case 'b': c = BS; break;
577 case 't': c = TAB; break;
578 case 'n': c = NL; break;
579 case 'f': c = FF; break;
580 case 'r': c = CAR; break;
581 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100582 if (reader->js_fill != NULL
583 && (int)(reader->js_end - p) < NUMBUFLEN)
584 {
585 reader->js_used = (int)(p - reader->js_buf);
586 if (reader->js_fill(reader))
587 {
588 p = reader->js_buf + reader->js_used;
589 reader->js_end = reader->js_buf
590 + STRLEN(reader->js_buf);
591 }
592 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100593 nr = 0;
594 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100595 vim_str2nr(p + 2, NULL, &len,
596 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
597 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100598 if (0xd800 <= nr && nr <= 0xdfff
599 && (int)(reader->js_end - p) >= 6
600 && *p == '\\' && *(p+1) == 'u')
601 {
602 long nr2 = 0;
603
604 /* decode surrogate pair: \ud812\u3456 */
605 len = 0;
606 vim_str2nr(p + 2, NULL, &len,
607 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4);
608 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
609 {
610 p += len + 2;
611 nr = (((nr - 0xd800) << 10) |
612 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
613 }
614 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100615 if (res != NULL)
616 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100617#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100618 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100619 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100620#else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100621 ga_append(&ga, nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100622#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100623 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100624 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100625 default:
626 /* not a special char, skip over \ */
627 ++p;
628 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100629 }
630 if (c > 0)
631 {
632 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100633 if (res != NULL)
634 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100635 }
636 }
637 else
638 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100639#ifdef FEAT_MBYTE
640 len = utf_ptr2len(p);
641#else
642 len = 1;
643#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100644 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100645 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100646 if (ga_grow(&ga, len) == FAIL)
647 {
648 ga_clear(&ga);
649 return FAIL;
650 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100651 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
652 ga.ga_len += len;
653 }
654 p += len;
655 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100656 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100657
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100658 reader->js_used = (int)(p - reader->js_buf);
659 if (*p == '"')
660 {
661 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100662 if (res != NULL)
663 {
Bram Moolenaar80e78842016-02-28 15:21:13 +0100664 ga_append(&ga, NUL);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100665 res->v_type = VAR_STRING;
Bram Moolenaarb3628722016-02-28 14:56:39 +0100666#if defined(FEAT_MBYTE) && defined(USE_ICONV)
667 if (!enc_utf8)
668 {
669 vimconv_T conv;
670
671 /* Convert the utf-8 string to 'encoding'. */
672 conv.vc_type = CONV_NONE;
673 convert_setup(&conv, (char_u*)"utf-8", p_enc);
674 if (conv.vc_type != CONV_NONE)
675 {
676 res->vval.v_string =
677 string_convert(&conv, ga.ga_data, NULL);
678 vim_free(ga.ga_data);
679 }
680 convert_setup(&conv, NULL, NULL);
681 }
682 else
683#endif
684 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100685 }
686 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100687 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100688 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100689 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100690 res->v_type = VAR_SPECIAL;
691 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100692 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100693 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100694 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100695}
696
697/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100698 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100699 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100700 *
701 * Return FAIL for a decoding error.
702 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100703 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100704 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100705json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100706{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100707 char_u *p;
708 int len;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100709
Bram Moolenaar56ead342016-02-02 18:20:08 +0100710 fill_numbuflen(reader);
711 p = reader->js_buf + reader->js_used;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100712 switch (*p)
713 {
714 case '[': /* array */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100715 return json_decode_array(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100716
717 case '{': /* object */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100718 return json_decode_object(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100719
720 case '"': /* string */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100721 return json_decode_string(reader, res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100722
723 case ',': /* comma: empty item */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100724 if ((options & JSON_JS) == 0)
725 return FAIL;
726 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100727 case NUL: /* empty */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100728 if (res != NULL)
729 {
730 res->v_type = VAR_SPECIAL;
731 res->vval.v_number = VVAL_NONE;
732 }
733 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100734
735 default:
736 if (VIM_ISDIGIT(*p) || *p == '-')
737 {
Bram Moolenaar10b369f2016-02-29 23:12:49 +0100738#ifdef FEAT_FLOAT
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100739 char_u *sp = p;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100740
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100741 if (*sp == '-')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100742 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100743 ++sp;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100744 if (*sp == NUL)
745 return MAYBE;
746 if (!VIM_ISDIGIT(*sp))
747 return FAIL;
748 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100749 sp = skipdigits(sp);
750 if (*sp == '.' || *sp == 'e' || *sp == 'E')
751 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100752 if (res == NULL)
753 {
754 float_T f;
755
756 len = string2float(p, &f);
757 }
758 else
759 {
760 res->v_type = VAR_FLOAT;
761 len = string2float(p, &res->vval.v_float);
762 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100763 }
764 else
765#endif
766 {
767 long nr;
768
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100769 vim_str2nr(reader->js_buf + reader->js_used,
770 NULL, &len, 0, /* what */
771 &nr, NULL, 0);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100772 if (res != NULL)
773 {
774 res->v_type = VAR_NUMBER;
775 res->vval.v_number = nr;
776 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100777 }
778 reader->js_used += len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100779 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100780 }
781 if (STRNICMP((char *)p, "false", 5) == 0)
782 {
783 reader->js_used += 5;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100784 if (res != NULL)
785 {
786 res->v_type = VAR_SPECIAL;
787 res->vval.v_number = VVAL_FALSE;
788 }
789 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100790 }
791 if (STRNICMP((char *)p, "true", 4) == 0)
792 {
793 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100794 if (res != NULL)
795 {
796 res->v_type = VAR_SPECIAL;
797 res->vval.v_number = VVAL_TRUE;
798 }
799 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100800 }
801 if (STRNICMP((char *)p, "null", 4) == 0)
802 {
803 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100804 if (res != NULL)
805 {
806 res->v_type = VAR_SPECIAL;
807 res->vval.v_number = VVAL_NULL;
808 }
809 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100810 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100811#ifdef FEAT_FLOAT
812 if (STRNICMP((char *)p, "NaN", 3) == 0)
813 {
814 reader->js_used += 3;
815 if (res != NULL)
816 {
817 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100818 res->vval.v_float = NAN;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100819 }
820 return OK;
821 }
822 if (STRNICMP((char *)p, "Infinity", 8) == 0)
823 {
824 reader->js_used += 8;
825 if (res != NULL)
826 {
827 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100828 res->vval.v_float = INFINITY;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100829 }
830 return OK;
831 }
832#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100833 /* check for truncated name */
834 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100835 if (
836 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
837#ifdef FEAT_FLOAT
838 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
839 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
840#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100841 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
842 || STRNICMP((char *)p, "null", len) == 0)))
843 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100844 break;
845 }
846
Bram Moolenaar56ead342016-02-02 18:20:08 +0100847 if (res != NUL)
848 {
849 res->v_type = VAR_SPECIAL;
850 res->vval.v_number = VVAL_NONE;
851 }
852 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100853}
854
855/*
856 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100857 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100858 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100859 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100860 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100861json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100862{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100863 int ret;
864
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100865 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100866 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100867 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100868 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100869 if (ret != OK)
870 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100871 json_skip_white(reader);
872 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100873 return FAIL;
874 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100875}
Bram Moolenaar56ead342016-02-02 18:20:08 +0100876
877/*
878 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100879 * "options" can be JSON_JS or zero;
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100880 * Return FAIL for a decoding error.
881 * Return MAYBE for an incomplete message.
882 * Consumes the message anyway.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100883 */
884 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100885json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100886{
887 int ret;
888
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100889 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100890 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
891 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100892 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100893 json_skip_white(reader);
894
Bram Moolenaarba61ac02016-03-20 16:40:37 +0100895 return ret;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100896}
897
898/*
899 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100900 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100901 * Return FAIL if the message has a decoding error.
902 * Return MAYBE if the message is truncated, need to read more.
903 * This only works reliable if the message contains an object, array or
904 * string. A number might be trucated without knowing.
905 * Does not advance the reader.
906 */
907 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100908json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100909{
910 int used_save = reader->js_used;
911 int ret;
912
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100913 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100914 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
915 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100916 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100917 reader->js_used = used_save;
918 return ret;
919}
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100920#endif