blob: bbc27b4401eadc71deb019bdc4a8645cedf72a35 [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 {
89 conv.vc_type = CONV_NONE;
90 convert_setup(&conv, p_enc, (char_u*)"utf-8");
91 if (conv.vc_type != CONV_NONE)
92 converted = res = string_convert(&conv, res, NULL);
93 convert_setup(&conv, NULL, NULL);
94 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +010095#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +010096 ga_append(gap, '"');
97 while (*res != NUL)
98 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +010099 int c;
100#ifdef FEAT_MBYTE
101 /* always use utf-8 encoding, ignore 'encoding' */
102 c = utf_ptr2char(res);
103#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100104 c = *res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100105#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100106
107 switch (c)
108 {
109 case 0x08:
110 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
111 case 0x09:
112 ga_append(gap, '\\'); ga_append(gap, 't'); break;
113 case 0x0a:
114 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
115 case 0x0c:
116 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
117 case 0x0d:
118 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
119 case 0x22: /* " */
120 case 0x5c: /* \ */
121 ga_append(gap, '\\');
122 ga_append(gap, c);
123 break;
124 default:
125 if (c >= 0x20)
126 {
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100127#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100128 numbuf[utf_char2bytes(c, numbuf)] = NUL;
Bram Moolenaarfa06a512016-01-28 22:46:58 +0100129#else
130 numbuf[0] = c;
131 numbuf[1] = NUL;
132#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100133 ga_concat(gap, numbuf);
134 }
135 else
136 {
137 vim_snprintf((char *)numbuf, NUMBUFLEN,
138 "\\u%04lx", (long)c);
139 ga_concat(gap, numbuf);
140 }
141 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100142#ifdef FEAT_MBYTE
143 res += utf_ptr2len(res);
144#else
Bram Moolenaar0f526f52016-02-27 22:59:41 +0100145 ++res;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100146#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100147 }
148 ga_append(gap, '"');
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100149#if defined(FEAT_MBYTE) && defined(USE_ICONV)
150 vim_free(converted);
151#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100152 }
153}
154
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100155/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100156 * Return TRUE if "key" can be used without quotes.
157 * That is when it starts with a letter and only contains letters, digits and
158 * underscore.
159 */
160 static int
161is_simple_key(char_u *key)
162{
163 char_u *p;
164
165 if (!ASCII_ISALPHA(*key))
166 return FALSE;
167 for (p = key + 1; *p != NUL; ++p)
168 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
169 return FALSE;
170 return TRUE;
171}
172
173/*
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100174 * Encode "val" into "gap".
175 * Return FAIL or OK.
176 */
177 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100178json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100179{
180 char_u numbuf[NUMBUFLEN];
181 char_u *res;
182 list_T *l;
183 dict_T *d;
184
185 switch (val->v_type)
186 {
187 case VAR_SPECIAL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100188 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100189 {
190 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
191 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100192 case VVAL_NONE: if ((options & JSON_JS) != 0
193 && (options & JSON_NO_NONE) == 0)
194 /* empty item */
195 break;
196 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100197 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
198 }
199 break;
200
201 case VAR_NUMBER:
202 vim_snprintf((char *)numbuf, NUMBUFLEN, "%ld",
203 (long)val->vval.v_number);
204 ga_concat(gap, numbuf);
205 break;
206
207 case VAR_STRING:
208 res = val->vval.v_string;
209 write_string(gap, res);
210 break;
211
212 case VAR_FUNC:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100213 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +0100214 case VAR_CHANNEL:
Bram Moolenaar4f8b8fa2016-02-06 18:42:07 +0100215 /* no JSON equivalent TODO: better error */
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100216 EMSG(_(e_invarg));
217 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100218
219 case VAR_LIST:
220 l = val->vval.v_list;
221 if (l == NULL)
222 ga_concat(gap, (char_u *)"null");
223 else
224 {
225 if (l->lv_copyID == copyID)
226 ga_concat(gap, (char_u *)"[]");
227 else
228 {
229 listitem_T *li;
230
231 l->lv_copyID = copyID;
232 ga_append(gap, '[');
233 for (li = l->lv_first; li != NULL && !got_int; )
234 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100235 if (json_encode_item(gap, &li->li_tv, copyID,
236 options & JSON_JS) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100237 return FAIL;
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100238 if ((options & JSON_JS)
239 && li->li_next == NULL
240 && li->li_tv.v_type == VAR_SPECIAL
241 && li->li_tv.vval.v_number == VVAL_NONE)
242 /* add an extra comma if the last item is v:none */
243 ga_append(gap, ',');
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100244 li = li->li_next;
245 if (li != NULL)
246 ga_append(gap, ',');
247 }
248 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100249 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100250 }
251 }
252 break;
253
254 case VAR_DICT:
255 d = val->vval.v_dict;
256 if (d == NULL)
257 ga_concat(gap, (char_u *)"null");
258 else
259 {
260 if (d->dv_copyID == copyID)
261 ga_concat(gap, (char_u *)"{}");
262 else
263 {
264 int first = TRUE;
265 int todo = (int)d->dv_hashtab.ht_used;
266 hashitem_T *hi;
267
268 d->dv_copyID = copyID;
269 ga_append(gap, '{');
270
271 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
272 ++hi)
273 if (!HASHITEM_EMPTY(hi))
274 {
275 --todo;
276 if (first)
277 first = FALSE;
278 else
279 ga_append(gap, ',');
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100280 if ((options & JSON_JS)
281 && is_simple_key(hi->hi_key))
282 ga_concat(gap, hi->hi_key);
283 else
284 write_string(gap, hi->hi_key);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100285 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100286 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100287 copyID, options | JSON_NO_NONE) == FAIL)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100288 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100289 }
290 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100291 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100292 }
293 }
294 break;
295
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100296 case VAR_FLOAT:
Bram Moolenaar55fab432016-02-07 16:53:13 +0100297#ifdef FEAT_FLOAT
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100298# if defined(HAVE_MATH_H)
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100299 if (isnan(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100300 ga_concat(gap, (char_u *)"NaN");
Bram Moolenaar7ce686c2016-02-27 16:33:22 +0100301 else if (isinf(val->vval.v_float))
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100302 ga_concat(gap, (char_u *)"Infinity");
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100303 else
304# endif
305 {
306 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
307 val->vval.v_float);
308 ga_concat(gap, numbuf);
309 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100310 break;
311#endif
Bram Moolenaar55fab432016-02-07 16:53:13 +0100312 case VAR_UNKNOWN:
Bram Moolenaarc6b14f02016-02-20 15:26:42 +0100313 EMSG2(_(e_intern2), "json_encode_item()");
Bram Moolenaar55fab432016-02-07 16:53:13 +0100314 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100315 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100316 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100317}
318
319/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100320 * When "reader" has less than NUMBUFLEN bytes available, call the fill
321 * callback to get more.
322 */
323 static void
324fill_numbuflen(js_read_T *reader)
325{
326 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
327 - reader->js_used < NUMBUFLEN)
328 {
329 if (reader->js_fill(reader))
330 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
331 }
332}
333
334/*
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100335 * Skip white space in "reader". All characters <= space are considered white
336 * space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100337 * Also tops up readahead when needed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100338 */
339 static void
340json_skip_white(js_read_T *reader)
341{
342 int c;
343
Bram Moolenaar56ead342016-02-02 18:20:08 +0100344 for (;;)
345 {
346 c = reader->js_buf[reader->js_used];
347 if (reader->js_fill != NULL && c == NUL)
348 {
349 if (reader->js_fill(reader))
350 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
351 continue;
352 }
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100353 if (c == NUL || c > ' ')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100354 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100355 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100356 }
357 fill_numbuflen(reader);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100358}
359
Bram Moolenaar56ead342016-02-02 18:20:08 +0100360 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100361json_decode_array(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100362{
363 char_u *p;
364 typval_T item;
365 listitem_T *li;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100366 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100367
Bram Moolenaar56ead342016-02-02 18:20:08 +0100368 if (res != NULL && rettv_list_alloc(res) == FAIL)
369 {
370 res->v_type = VAR_SPECIAL;
371 res->vval.v_number = VVAL_NONE;
372 return FAIL;
373 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100374 ++reader->js_used; /* consume the '[' */
375
376 while (TRUE)
377 {
378 json_skip_white(reader);
379 p = reader->js_buf + reader->js_used;
380 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100381 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100382 if (*p == ']')
383 {
384 ++reader->js_used; /* consume the ']' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100385 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100386 }
387
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100388 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100389 if (ret != OK)
390 return ret;
391 if (res != NULL)
392 {
393 li = listitem_alloc();
394 if (li == NULL)
395 {
396 clear_tv(&item);
397 return FAIL;
398 }
399 li->li_tv = item;
400 list_append(res->vval.v_list, li);
401 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100402
403 json_skip_white(reader);
404 p = reader->js_buf + reader->js_used;
405 if (*p == ',')
406 ++reader->js_used;
407 else if (*p != ']')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100408 {
409 if (*p == NUL)
410 return MAYBE;
411 return FAIL;
412 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100413 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100414 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100415}
416
Bram Moolenaar56ead342016-02-02 18:20:08 +0100417 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100418json_decode_object(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100419{
420 char_u *p;
421 typval_T tvkey;
422 typval_T item;
423 dictitem_T *di;
424 char_u buf[NUMBUFLEN];
Bram Moolenaarfbf9c6b2016-02-02 19:43:57 +0100425 char_u *key = NULL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100426 int ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100427
Bram Moolenaar56ead342016-02-02 18:20:08 +0100428 if (res != NULL && rettv_dict_alloc(res) == FAIL)
429 {
430 res->v_type = VAR_SPECIAL;
431 res->vval.v_number = VVAL_NONE;
432 return FAIL;
433 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100434 ++reader->js_used; /* consume the '{' */
435
436 while (TRUE)
437 {
438 json_skip_white(reader);
439 p = reader->js_buf + reader->js_used;
440 if (*p == NUL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100441 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100442 if (*p == '}')
443 {
444 ++reader->js_used; /* consume the '}' */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100445 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100446 }
447
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100448 if ((options & JSON_JS) && reader->js_buf[reader->js_used] != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100449 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100450 /* accept a key that is not in quotes */
451 key = p = reader->js_buf + reader->js_used;
452 while (*p != NUL && *p != ':' && *p > ' ')
453 ++p;
454 tvkey.v_type = VAR_STRING;
455 tvkey.vval.v_string = vim_strnsave(key, (int)(p - key));
456 reader->js_used += (int)(p - key);
457 key = tvkey.vval.v_string;
458 }
459 else
460 {
461 ret = json_decode_item(reader, res == NULL ? NULL : &tvkey,
462 options);
463 if (ret != OK)
464 return ret;
465 if (res != NULL)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100466 {
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100467 key = get_tv_string_buf_chk(&tvkey, buf);
468 if (key == NULL || *key == NUL)
469 {
470 clear_tv(&tvkey);
471 return FAIL;
472 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100473 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100474 }
475
476 json_skip_white(reader);
477 p = reader->js_buf + reader->js_used;
478 if (*p != ':')
479 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100480 if (res != NULL)
481 clear_tv(&tvkey);
482 if (*p == NUL)
483 return MAYBE;
484 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100485 }
486 ++reader->js_used;
487 json_skip_white(reader);
488
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100489 ret = json_decode_item(reader, res == NULL ? NULL : &item, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100490 if (ret != OK)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100491 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100492 if (res != NULL)
493 clear_tv(&tvkey);
494 return ret;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100495 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100496
497 if (res != NULL)
498 {
499 di = dictitem_alloc(key);
500 clear_tv(&tvkey);
501 if (di == NULL)
502 {
503 clear_tv(&item);
504 return FAIL;
505 }
506 di->di_tv = item;
507 if (dict_add(res->vval.v_dict, di) == FAIL)
508 {
509 dictitem_free(di);
510 return FAIL;
511 }
512 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100513
514 json_skip_white(reader);
515 p = reader->js_buf + reader->js_used;
516 if (*p == ',')
517 ++reader->js_used;
518 else if (*p != '}')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100519 {
520 if (*p == NUL)
521 return MAYBE;
522 return FAIL;
523 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100524 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100525 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100526}
527
Bram Moolenaar56ead342016-02-02 18:20:08 +0100528 static int
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100529json_decode_string(js_read_T *reader, typval_T *res)
530{
531 garray_T ga;
532 int len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100533 char_u *p;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100534 int c;
535 long nr;
536 char_u buf[NUMBUFLEN];
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100537#if defined(FEAT_MBYTE) && defined(USE_ICONV)
538 vimconv_T conv;
539 char_u *converted = NULL;
540#endif
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100541
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 " */
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100546#if defined(FEAT_MBYTE) && defined(USE_ICONV)
Bram Moolenaarf97ddbe2016-02-27 21:13:38 +0100547 if (!enc_utf8)
548 {
549 conv.vc_type = CONV_NONE;
550 convert_setup(&conv, (char_u*)"utf-8", p_enc);
551 if (conv.vc_type != CONV_NONE)
552 converted = p = string_convert(&conv, p, NULL);
553 convert_setup(&conv, NULL, NULL);
554 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100555#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100556 while (*p != '"')
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100557 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100558 if (*p == NUL || p[1] == NUL
559#ifdef FEAT_MBYTE
560 || utf_ptr2len(p) < utf_byte2len(*p)
561#endif
562 )
563 {
564 if (reader->js_fill == NULL)
565 break;
566 len = (int)(reader->js_end - p);
567 reader->js_used = (int)(p - reader->js_buf);
568 if (!reader->js_fill(reader))
569 break; /* didn't get more */
570 p = reader->js_buf + reader->js_used;
571 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
572 continue;
573 }
574
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100575 if (*p == '\\')
576 {
577 c = -1;
578 switch (p[1])
579 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100580 case '\\': c = '\\'; break;
581 case '"': c = '"'; break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100582 case 'b': c = BS; break;
583 case 't': c = TAB; break;
584 case 'n': c = NL; break;
585 case 'f': c = FF; break;
586 case 'r': c = CAR; break;
587 case 'u':
Bram Moolenaar56ead342016-02-02 18:20:08 +0100588 if (reader->js_fill != NULL
589 && (int)(reader->js_end - p) < NUMBUFLEN)
590 {
591 reader->js_used = (int)(p - reader->js_buf);
592 if (reader->js_fill(reader))
593 {
594 p = reader->js_buf + reader->js_used;
595 reader->js_end = reader->js_buf
596 + STRLEN(reader->js_buf);
597 }
598 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100599 nr = 0;
600 len = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100601 vim_str2nr(p + 2, NULL, &len,
602 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
603 p += len + 2;
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100604 if (0xd800 <= nr && nr <= 0xdfff
605 && (int)(reader->js_end - p) >= 6
606 && *p == '\\' && *(p+1) == 'u')
607 {
608 long nr2 = 0;
609
610 /* decode surrogate pair: \ud812\u3456 */
611 len = 0;
612 vim_str2nr(p + 2, NULL, &len,
613 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4);
614 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
615 {
616 p += len + 2;
617 nr = (((nr - 0xd800) << 10) |
618 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
619 }
620 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100621 if (res != NULL)
622 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100623#ifdef FEAT_MBYTE
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100624 buf[utf_char2bytes((int)nr, buf)] = NUL;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100625 ga_concat(&ga, buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100626#else
Bram Moolenaar56ead342016-02-02 18:20:08 +0100627 ga_append(&ga, nr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100628#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100629 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100630 break;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100631 default:
632 /* not a special char, skip over \ */
633 ++p;
634 continue;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100635 }
636 if (c > 0)
637 {
638 p += 2;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100639 if (res != NULL)
640 ga_append(&ga, c);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100641 }
642 }
643 else
644 {
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100645#ifdef FEAT_MBYTE
646 len = utf_ptr2len(p);
647#else
648 len = 1;
649#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100650 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100651 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100652 if (ga_grow(&ga, len) == FAIL)
653 {
654 ga_clear(&ga);
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100655#if defined(FEAT_MBYTE) && defined(USE_ICONV)
656 vim_free(converted);
657#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100658 return FAIL;
659 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100660 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
661 ga.ga_len += len;
662 }
663 p += len;
664 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100665 }
Bram Moolenaarb6ff8112016-02-27 18:41:27 +0100666#if defined(FEAT_MBYTE) && defined(USE_ICONV)
667 vim_free(converted);
668#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100669
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100670 reader->js_used = (int)(p - reader->js_buf);
671 if (*p == '"')
672 {
673 ++reader->js_used;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100674 if (res != NULL)
675 {
676 res->v_type = VAR_STRING;
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +0100677 res->vval.v_string = ga.ga_data;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100678 }
679 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100680 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100681 if (res != NULL)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100682 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100683 res->v_type = VAR_SPECIAL;
684 res->vval.v_number = VVAL_NONE;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100685 ga_clear(&ga);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100686 }
Bram Moolenaar56ead342016-02-02 18:20:08 +0100687 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100688}
689
690/*
Bram Moolenaar56ead342016-02-02 18:20:08 +0100691 * Decode one item and put it in "res". If "res" is NULL only advance.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100692 * Must already have skipped white space.
Bram Moolenaar56ead342016-02-02 18:20:08 +0100693 *
694 * Return FAIL for a decoding error.
695 * Return MAYBE for an incomplete message.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100696 */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100697 static int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100698json_decode_item(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100699{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100700 char_u *p;
701 int len;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100702
Bram Moolenaar56ead342016-02-02 18:20:08 +0100703 fill_numbuflen(reader);
704 p = reader->js_buf + reader->js_used;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100705 switch (*p)
706 {
707 case '[': /* array */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100708 return json_decode_array(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100709
710 case '{': /* object */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100711 return json_decode_object(reader, res, options);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100712
713 case '"': /* string */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100714 return json_decode_string(reader, res);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100715
716 case ',': /* comma: empty item */
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100717 if ((options & JSON_JS) == 0)
718 return FAIL;
719 /* FALLTHROUGH */
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100720 case NUL: /* empty */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100721 if (res != NULL)
722 {
723 res->v_type = VAR_SPECIAL;
724 res->vval.v_number = VVAL_NONE;
725 }
726 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100727
728 default:
729 if (VIM_ISDIGIT(*p) || *p == '-')
730 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100731 char_u *sp = p;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100732
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100733#ifdef FEAT_FLOAT
734 if (*sp == '-')
Bram Moolenaar56ead342016-02-02 18:20:08 +0100735 {
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100736 ++sp;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100737 if (*sp == NUL)
738 return MAYBE;
739 if (!VIM_ISDIGIT(*sp))
740 return FAIL;
741 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100742 sp = skipdigits(sp);
743 if (*sp == '.' || *sp == 'e' || *sp == 'E')
744 {
Bram Moolenaar56ead342016-02-02 18:20:08 +0100745 if (res == NULL)
746 {
747 float_T f;
748
749 len = string2float(p, &f);
750 }
751 else
752 {
753 res->v_type = VAR_FLOAT;
754 len = string2float(p, &res->vval.v_float);
755 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100756 }
757 else
758#endif
759 {
760 long nr;
761
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100762 vim_str2nr(reader->js_buf + reader->js_used,
763 NULL, &len, 0, /* what */
764 &nr, NULL, 0);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100765 if (res != NULL)
766 {
767 res->v_type = VAR_NUMBER;
768 res->vval.v_number = nr;
769 }
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100770 }
771 reader->js_used += len;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100772 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100773 }
774 if (STRNICMP((char *)p, "false", 5) == 0)
775 {
776 reader->js_used += 5;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100777 if (res != NULL)
778 {
779 res->v_type = VAR_SPECIAL;
780 res->vval.v_number = VVAL_FALSE;
781 }
782 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100783 }
784 if (STRNICMP((char *)p, "true", 4) == 0)
785 {
786 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100787 if (res != NULL)
788 {
789 res->v_type = VAR_SPECIAL;
790 res->vval.v_number = VVAL_TRUE;
791 }
792 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100793 }
794 if (STRNICMP((char *)p, "null", 4) == 0)
795 {
796 reader->js_used += 4;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100797 if (res != NULL)
798 {
799 res->v_type = VAR_SPECIAL;
800 res->vval.v_number = VVAL_NULL;
801 }
802 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100803 }
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100804#ifdef FEAT_FLOAT
805 if (STRNICMP((char *)p, "NaN", 3) == 0)
806 {
807 reader->js_used += 3;
808 if (res != NULL)
809 {
810 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100811 res->vval.v_float = NAN;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100812 }
813 return OK;
814 }
815 if (STRNICMP((char *)p, "Infinity", 8) == 0)
816 {
817 reader->js_used += 8;
818 if (res != NULL)
819 {
820 res->v_type = VAR_FLOAT;
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +0100821 res->vval.v_float = INFINITY;
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100822 }
823 return OK;
824 }
825#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100826 /* check for truncated name */
827 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100828 if (
829 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
830#ifdef FEAT_FLOAT
831 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
832 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
833#endif
Bram Moolenaar56ead342016-02-02 18:20:08 +0100834 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
835 || STRNICMP((char *)p, "null", len) == 0)))
836 return MAYBE;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100837 break;
838 }
839
Bram Moolenaar56ead342016-02-02 18:20:08 +0100840 if (res != NUL)
841 {
842 res->v_type = VAR_SPECIAL;
843 res->vval.v_number = VVAL_NONE;
844 }
845 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100846}
847
848/*
849 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100850 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100851 * Return FAIL if not the whole message was consumed.
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100852 */
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100853 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100854json_decode_all(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100855{
Bram Moolenaar56ead342016-02-02 18:20:08 +0100856 int ret;
857
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100858 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100859 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100860 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100861 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100862 if (ret != OK)
863 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100864 json_skip_white(reader);
865 if (reader->js_buf[reader->js_used] != NUL)
Bram Moolenaar19d2f152016-02-01 21:38:19 +0100866 return FAIL;
867 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100868}
Bram Moolenaar56ead342016-02-02 18:20:08 +0100869
870/*
871 * Decode the JSON from "reader" and store the result in "res".
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100872 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100873 * Return FAIL if the message has a decoding error or the message is
874 * truncated. Consumes the message anyway.
875 */
876 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100877json_decode(js_read_T *reader, typval_T *res, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100878{
879 int ret;
880
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100881 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100882 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
883 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100884 ret = json_decode_item(reader, res, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100885 json_skip_white(reader);
886
887 return ret == OK ? OK : FAIL;
888}
889
890/*
891 * Decode the JSON from "reader" to find the end of the message.
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100892 * "options" can be JSON_JS or zero;
Bram Moolenaar56ead342016-02-02 18:20:08 +0100893 * Return FAIL if the message has a decoding error.
894 * Return MAYBE if the message is truncated, need to read more.
895 * This only works reliable if the message contains an object, array or
896 * string. A number might be trucated without knowing.
897 * Does not advance the reader.
898 */
899 int
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100900json_find_end(js_read_T *reader, int options)
Bram Moolenaar56ead342016-02-02 18:20:08 +0100901{
902 int used_save = reader->js_used;
903 int ret;
904
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100905 /* We find the end once, to avoid calling strlen() many times. */
Bram Moolenaar56ead342016-02-02 18:20:08 +0100906 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
907 json_skip_white(reader);
Bram Moolenaar595e64e2016-02-07 19:19:53 +0100908 ret = json_decode_item(reader, NULL, options);
Bram Moolenaar56ead342016-02-02 18:20:08 +0100909 reader->js_used = used_save;
910 return ret;
911}
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100912#endif