blob: da8beea874700ba58c7920d8bae6ebcaba0c5d2b [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 *
13 * Follows this standard: http://www.ietf.org/rfc/rfc4627.txt
14 */
15
16#include "vim.h"
17
18#if defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaarfcaaae62016-01-24 16:49:11 +010019static int json_encode_item(garray_T *gap, typval_T *val, int copyID);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010020static void json_decode_item(js_read_T *reader, typval_T *res);
21
22/*
23 * Encode "val" into a JSON format string.
24 */
25 char_u *
26json_encode(typval_T *val)
27{
28 garray_T ga;
29
30 /* Store bytes in the growarray. */
31 ga_init2(&ga, 1, 4000);
32 json_encode_item(&ga, val, get_copyID());
33 return ga.ga_data;
34}
35
36 static void
37write_string(garray_T *gap, char_u *str)
38{
39 char_u *res = str;
40 char_u numbuf[NUMBUFLEN];
41
42 if (res == NULL)
43 ga_concat(gap, (char_u *)"null");
44 else
45 {
46 ga_append(gap, '"');
47 while (*res != NUL)
48 {
49 int c = PTR2CHAR(res);
50
51 switch (c)
52 {
53 case 0x08:
54 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
55 case 0x09:
56 ga_append(gap, '\\'); ga_append(gap, 't'); break;
57 case 0x0a:
58 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
59 case 0x0c:
60 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
61 case 0x0d:
62 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
63 case 0x22: /* " */
64 case 0x5c: /* \ */
65 ga_append(gap, '\\');
66 ga_append(gap, c);
67 break;
68 default:
69 if (c >= 0x20)
70 {
71 numbuf[mb_char2bytes(c, numbuf)] = NUL;
72 ga_concat(gap, numbuf);
73 }
74 else
75 {
76 vim_snprintf((char *)numbuf, NUMBUFLEN,
77 "\\u%04lx", (long)c);
78 ga_concat(gap, numbuf);
79 }
80 }
81 mb_cptr_adv(res);
82 }
83 ga_append(gap, '"');
84 }
85}
86
Bram Moolenaarfcaaae62016-01-24 16:49:11 +010087/*
88 * Encode "val" into "gap".
89 * Return FAIL or OK.
90 */
91 static int
Bram Moolenaar520e1e42016-01-23 19:46:28 +010092json_encode_item(garray_T *gap, typval_T *val, int copyID)
93{
94 char_u numbuf[NUMBUFLEN];
95 char_u *res;
96 list_T *l;
97 dict_T *d;
98
99 switch (val->v_type)
100 {
101 case VAR_SPECIAL:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100102 switch (val->vval.v_number)
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100103 {
104 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
105 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
106 case VVAL_NONE: break;
107 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
108 }
109 break;
110
111 case VAR_NUMBER:
112 vim_snprintf((char *)numbuf, NUMBUFLEN, "%ld",
113 (long)val->vval.v_number);
114 ga_concat(gap, numbuf);
115 break;
116
117 case VAR_STRING:
118 res = val->vval.v_string;
119 write_string(gap, res);
120 break;
121
122 case VAR_FUNC:
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100123 /* no JSON equivalent */
124 EMSG(_(e_invarg));
125 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100126
127 case VAR_LIST:
128 l = val->vval.v_list;
129 if (l == NULL)
130 ga_concat(gap, (char_u *)"null");
131 else
132 {
133 if (l->lv_copyID == copyID)
134 ga_concat(gap, (char_u *)"[]");
135 else
136 {
137 listitem_T *li;
138
139 l->lv_copyID = copyID;
140 ga_append(gap, '[');
141 for (li = l->lv_first; li != NULL && !got_int; )
142 {
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100143 if (json_encode_item(gap, &li->li_tv, copyID) == FAIL)
144 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100145 li = li->li_next;
146 if (li != NULL)
147 ga_append(gap, ',');
148 }
149 ga_append(gap, ']');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100150 l->lv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100151 }
152 }
153 break;
154
155 case VAR_DICT:
156 d = val->vval.v_dict;
157 if (d == NULL)
158 ga_concat(gap, (char_u *)"null");
159 else
160 {
161 if (d->dv_copyID == copyID)
162 ga_concat(gap, (char_u *)"{}");
163 else
164 {
165 int first = TRUE;
166 int todo = (int)d->dv_hashtab.ht_used;
167 hashitem_T *hi;
168
169 d->dv_copyID = copyID;
170 ga_append(gap, '{');
171
172 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
173 ++hi)
174 if (!HASHITEM_EMPTY(hi))
175 {
176 --todo;
177 if (first)
178 first = FALSE;
179 else
180 ga_append(gap, ',');
181 write_string(gap, hi->hi_key);
182 ga_append(gap, ':');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100183 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
184 copyID) == FAIL)
185 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100186 }
187 ga_append(gap, '}');
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100188 d->dv_copyID = 0;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100189 }
190 }
191 break;
192
193#ifdef FEAT_FLOAT
194 case VAR_FLOAT:
195 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", val->vval.v_float);
196 ga_concat(gap, numbuf);
197 break;
198#endif
199 default: EMSG2(_(e_intern2), "json_encode_item()"); break;
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100200 return FAIL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100201 }
Bram Moolenaarfcaaae62016-01-24 16:49:11 +0100202 return OK;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100203}
204
205/*
206 * Skip white space in "reader".
207 */
208 static void
209json_skip_white(js_read_T *reader)
210{
211 int c;
212
213 while ((c = reader->js_buf[reader->js_used]) == ' '
214 || c == TAB || c == NL || c == CAR)
215 ++reader->js_used;
216}
217
218/*
219 * Make sure there are at least enough characters buffered to read a number.
220 */
221 static void
222json_fill_buffer(js_read_T *reader UNUSED)
223{
224 /* TODO */
225}
226
227 static void
228json_decode_array(js_read_T *reader, typval_T *res)
229{
230 char_u *p;
231 typval_T item;
232 listitem_T *li;
233
234 if (rettv_list_alloc(res) == FAIL)
Bram Moolenaar6039c7f2016-01-24 15:05:32 +0100235 goto failsilent;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100236 ++reader->js_used; /* consume the '[' */
237
238 while (TRUE)
239 {
240 json_skip_white(reader);
241 p = reader->js_buf + reader->js_used;
242 if (*p == NUL)
243 goto fail;
244 if (*p == ']')
245 {
246 ++reader->js_used; /* consume the ']' */
247 return;
248 }
249
250 if (!reader->js_eof && (int)(reader->js_end - p) < NUMBUFLEN)
251 json_fill_buffer(reader);
252
253 json_decode_item(reader, &item);
254 li = listitem_alloc();
255 if (li == NULL)
256 return;
257 li->li_tv = item;
258 list_append(res->vval.v_list, li);
259
260 json_skip_white(reader);
261 p = reader->js_buf + reader->js_used;
262 if (*p == ',')
263 ++reader->js_used;
264 else if (*p != ']')
265 goto fail;
266 }
267fail:
Bram Moolenaar6039c7f2016-01-24 15:05:32 +0100268 EMSG(_(e_invarg));
269failsilent:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100270 res->v_type = VAR_SPECIAL;
271 res->vval.v_number = VVAL_NONE;
272}
273
274 static void
275json_decode_object(js_read_T *reader, typval_T *res)
276{
277 char_u *p;
278 typval_T tvkey;
279 typval_T item;
280 dictitem_T *di;
281 char_u buf[NUMBUFLEN];
282 char_u *key;
283
284 if (rettv_dict_alloc(res) == FAIL)
Bram Moolenaar6039c7f2016-01-24 15:05:32 +0100285 goto failsilent;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100286 ++reader->js_used; /* consume the '{' */
287
288 while (TRUE)
289 {
290 json_skip_white(reader);
291 p = reader->js_buf + reader->js_used;
292 if (*p == NUL)
293 goto fail;
294 if (*p == '}')
295 {
296 ++reader->js_used; /* consume the '}' */
297 return;
298 }
299
300 if (!reader->js_eof && (int)(reader->js_end - p) < NUMBUFLEN)
301 json_fill_buffer(reader);
302 json_decode_item(reader, &tvkey);
303 key = get_tv_string_buf_chk(&tvkey, buf);
304 if (key == NULL || *key == NUL)
305 {
306 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
307 if (key != NULL)
308 EMSG(_(e_emptykey));
309 clear_tv(&tvkey);
Bram Moolenaar6039c7f2016-01-24 15:05:32 +0100310 goto failsilent;
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100311 }
312
313 json_skip_white(reader);
314 p = reader->js_buf + reader->js_used;
315 if (*p != ':')
316 {
317 clear_tv(&tvkey);
318 goto fail;
319 }
320 ++reader->js_used;
321 json_skip_white(reader);
322
323 if (!reader->js_eof && (int)(reader->js_end - p) < NUMBUFLEN)
324 json_fill_buffer(reader);
325 json_decode_item(reader, &item);
326
327 di = dictitem_alloc(key);
328 clear_tv(&tvkey);
329 if (di == NULL)
330 {
331 clear_tv(&item);
332 goto fail;
333 }
334 di->di_tv = item;
Bram Moolenaar2dedb452016-01-23 21:38:51 +0100335 if (dict_add(res->vval.v_dict, di) == FAIL)
336 dictitem_free(di);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100337
338 json_skip_white(reader);
339 p = reader->js_buf + reader->js_used;
340 if (*p == ',')
341 ++reader->js_used;
342 else if (*p != '}')
343 goto fail;
344 }
345fail:
Bram Moolenaar6039c7f2016-01-24 15:05:32 +0100346 EMSG(_(e_invarg));
347failsilent:
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100348 res->v_type = VAR_SPECIAL;
349 res->vval.v_number = VVAL_NONE;
350}
351
352 static void
353json_decode_string(js_read_T *reader, typval_T *res)
354{
355 garray_T ga;
356 int len;
357 char_u *p = reader->js_buf + reader->js_used + 1;
358 int c;
359 long nr;
360 char_u buf[NUMBUFLEN];
361
362 ga_init2(&ga, 1, 200);
363
364 /* TODO: fill buffer when needed. */
365 while (*p != NUL && *p != '"')
366 {
367 if (*p == '\\')
368 {
369 c = -1;
370 switch (p[1])
371 {
372 case 'b': c = BS; break;
373 case 't': c = TAB; break;
374 case 'n': c = NL; break;
375 case 'f': c = FF; break;
376 case 'r': c = CAR; break;
377 case 'u':
378 vim_str2nr(p + 2, NULL, &len,
379 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4);
380 p += len + 2;
381#ifdef FEAT_MBYTE
382 buf[(*mb_char2bytes)((int)nr, buf)] = NUL;
383 ga_concat(&ga, buf);
384#else
385 ga_append(&ga, nr);
386#endif
387 break;
388 default: c = p[1]; break;
389 }
390 if (c > 0)
391 {
392 p += 2;
393 ga_append(&ga, c);
394 }
395 }
396 else
397 {
398 len = MB_PTR2LEN(p);
399 if (ga_grow(&ga, len) == OK)
400 {
401 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
402 ga.ga_len += len;
403 }
404 p += len;
405 }
406 if (!reader->js_eof && (int)(reader->js_end - p) < NUMBUFLEN)
407 {
408 reader->js_used = (int)(p - reader->js_buf);
409 json_fill_buffer(reader);
410 p = reader->js_buf + reader->js_used;
411 }
412 }
413 reader->js_used = (int)(p - reader->js_buf);
414 if (*p == '"')
415 {
416 ++reader->js_used;
417 res->v_type = VAR_STRING;
Bram Moolenaar2dedb452016-01-23 21:38:51 +0100418 if (ga.ga_data == NULL)
419 res->vval.v_string = NULL;
420 else
421 res->vval.v_string = vim_strsave(ga.ga_data);
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100422 }
423 else
424 {
Bram Moolenaarbd4593f2016-01-23 22:51:07 +0100425 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100426 res->v_type = VAR_SPECIAL;
427 res->vval.v_number = VVAL_NONE;
428 }
429 ga_clear(&ga);
430}
431
432/*
433 * Decode one item and put it in "result".
434 * Must already have skipped white space.
435 */
436 static void
437json_decode_item(js_read_T *reader, typval_T *res)
438{
439 char_u *p = reader->js_buf + reader->js_used;
440
441 switch (*p)
442 {
443 case '[': /* array */
444 json_decode_array(reader, res);
445 return;
446
447 case '{': /* object */
448 json_decode_object(reader, res);
449 return;
450
451 case '"': /* string */
452 json_decode_string(reader, res);
453 return;
454
455 case ',': /* comma: empty item */
456 case NUL: /* empty */
457 res->v_type = VAR_SPECIAL;
458 res->vval.v_number = VVAL_NONE;
459 return;
460
461 default:
462 if (VIM_ISDIGIT(*p) || *p == '-')
463 {
464 int len;
465 char_u *sp = p;
466#ifdef FEAT_FLOAT
467 if (*sp == '-')
468 ++sp;
469 sp = skipdigits(sp);
470 if (*sp == '.' || *sp == 'e' || *sp == 'E')
471 {
472 res->v_type = VAR_FLOAT;
473 len = string2float(p, &res->vval.v_float);
474 }
475 else
476#endif
477 {
478 long nr;
479
480 res->v_type = VAR_NUMBER;
481 vim_str2nr(reader->js_buf + reader->js_used,
482 NULL, &len, 0, /* what */
483 &nr, NULL, 0);
484 res->vval.v_number = nr;
485 }
486 reader->js_used += len;
487 return;
488 }
489 if (STRNICMP((char *)p, "false", 5) == 0)
490 {
491 reader->js_used += 5;
492 res->v_type = VAR_SPECIAL;
493 res->vval.v_number = VVAL_FALSE;
494 return;
495 }
496 if (STRNICMP((char *)p, "true", 4) == 0)
497 {
498 reader->js_used += 4;
499 res->v_type = VAR_SPECIAL;
500 res->vval.v_number = VVAL_TRUE;
501 return;
502 }
503 if (STRNICMP((char *)p, "null", 4) == 0)
504 {
505 reader->js_used += 4;
506 res->v_type = VAR_SPECIAL;
507 res->vval.v_number = VVAL_NULL;
508 return;
509 }
510 break;
511 }
512
513 EMSG(_(e_invarg));
514 res->v_type = VAR_SPECIAL;
515 res->vval.v_number = VVAL_NONE;
516}
517
518/*
519 * Decode the JSON from "reader" and store the result in "res".
520 */
521 void
522json_decode(js_read_T *reader, typval_T *res)
523{
524 json_skip_white(reader);
525 json_decode_item(reader, res);
526 json_skip_white(reader);
527 if (reader->js_buf[reader->js_used] != NUL)
528 EMSG(_(e_invarg));
529}
530#endif