annotate src/json.c @ 16706:77bcb5055fec v8.1.1355

patch 8.1.1355: obvious mistakes are accepted as valid expressions commit https://github.com/vim/vim/commit/16e9b85113e0b354ece1cb4f5fcc7866850f3685 Author: Bram Moolenaar <Bram@vim.org> Date: Sun May 19 19:59:35 2019 +0200 patch 8.1.1355: obvious mistakes are accepted as valid expressions Problem: Obvious mistakes are accepted as valid expressions. Solution: Be more strict about parsing numbers. (Yasuhiro Matsumoto, closes #3981)
author Bram Moolenaar <Bram@vim.org>
date Sun, 19 May 2019 20:00:09 +0200
parents a2c598cbe220
children 6fdb0ae0cac3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10042
4aead6a9b7a9 commit https://github.com/vim/vim/commit/edf3f97ae2af024708ebb4ac614227327033ca47
Christian Brabandt <cb@256bit.org>
parents: 9969
diff changeset
1 /* vi:set ts=8 sts=4 sw=4 noet:
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
2 *
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
3 * VIM - Vi IMproved by Bram Moolenaar
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
4 *
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
5 * Do ":help uganda" in Vim to read copying and usage conditions.
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
6 * Do ":help credits" in Vim to see a list of people who contributed.
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
7 * See README.txt for an overview of the Vim source code.
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
8 */
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
9
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
10 /*
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
11 * json.c: Encoding and decoding JSON.
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
12 *
7782
3a99194bd187 commit https://github.com/vim/vim/commit/009d84a34f3678ec93921bee3bc05be2fd606264
Christian Brabandt <cb@256bit.org>
parents: 7736
diff changeset
13 * Follows this standard: https://tools.ietf.org/html/rfc7159.html
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
14 */
8295
18fd94bd4eb8 commit https://github.com/vim/vim/commit/fefecb0fbe14c44d46f91036d76bbb6c28162da8
Christian Brabandt <cb@256bit.org>
parents: 8293
diff changeset
15 #define USING_FLOAT_STUFF
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
16
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
17 #include "vim.h"
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
18
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
19 #if defined(FEAT_EVAL) || defined(PROTO)
8228
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
20
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
21 static int json_encode_item(garray_T *gap, typval_T *val, int copyID, int options);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
22
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
23 /*
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
24 * Encode "val" into a JSON format string.
9969
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
25 * The result is added to "gap"
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
26 * Returns FAIL on failure and makes gap->ga_data empty.
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
27 */
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
28 static int
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
29 json_encode_gap(garray_T *gap, typval_T *val, int options)
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
30 {
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
31 if (json_encode_item(gap, val, get_copyID(), options) == FAIL)
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
32 {
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
33 ga_clear(gap);
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
34 gap->ga_data = vim_strsave((char_u *)"");
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
35 return FAIL;
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
36 }
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
37 return OK;
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
38 }
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
39
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
40 /*
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
41 * Encode "val" into a JSON format string.
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
42 * The result is in allocated memory.
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
43 * The result is empty when encoding fails.
9969
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
44 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
45 */
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
46 char_u *
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
47 json_encode(typval_T *val, int options)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
48 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
49 garray_T ga;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
50
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
51 /* Store bytes in the growarray. */
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
52 ga_init2(&ga, 1, 4000);
9969
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
53 json_encode_gap(&ga, val, options);
16306
a2c598cbe220 patch 8.1.1158: json encoded string is sometimes missing the final NUL
Bram Moolenaar <Bram@vim.org>
parents: 15597
diff changeset
54 ga_append(&ga, NUL);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
55 return ga.ga_data;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
56 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
57
15555
d89c5b339c2a patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents: 15517
diff changeset
58 #if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7864
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
59 /*
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
60 * Encode ["nr", "val"] into a JSON format string in allocated memory.
9969
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
61 * "options" can contain JSON_JS, JSON_NO_NONE and JSON_NL.
7864
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
62 * Returns NULL when out of memory.
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
63 */
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
64 char_u *
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
65 json_encode_nr_expr(int nr, typval_T *val, int options)
7864
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
66 {
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
67 typval_T listtv;
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
68 typval_T nrtv;
9969
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
69 garray_T ga;
7864
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
70
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
71 nrtv.v_type = VAR_NUMBER;
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
72 nrtv.vval.v_number = nr;
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
73 if (rettv_list_alloc(&listtv) == FAIL)
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
74 return NULL;
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
75 if (list_append_tv(listtv.vval.v_list, &nrtv) == FAIL
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
76 || list_append_tv(listtv.vval.v_list, val) == FAIL)
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
77 {
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
78 list_unref(listtv.vval.v_list);
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
79 return NULL;
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
80 }
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
81
9969
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
82 ga_init2(&ga, 1, 4000);
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
83 if (json_encode_gap(&ga, &listtv, options) == OK && (options & JSON_NL))
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
84 ga_append(&ga, '\n');
7864
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
85 list_unref(listtv.vval.v_list);
16306
a2c598cbe220 patch 8.1.1158: json encoded string is sometimes missing the final NUL
Bram Moolenaar <Bram@vim.org>
parents: 15597
diff changeset
86 ga_append(&ga, NUL);
9969
176e34b0d678 commit https://github.com/vim/vim/commit/f1f0792e55e72cdc7c833b30f565a9b02f18bb1e
Christian Brabandt <cb@256bit.org>
parents: 9389
diff changeset
87 return ga.ga_data;
7864
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
88 }
15555
d89c5b339c2a patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents: 15517
diff changeset
89 #endif
7864
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
90
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
91 static void
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
92 write_string(garray_T *gap, char_u *str)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
93 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
94 char_u *res = str;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
95 char_u numbuf[NUMBUFLEN];
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
96
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
97 if (res == NULL)
13031
c35d266308c2 patch 8.0.1391: encoding empty string to JSON sometimes gives "null"
Christian Brabandt <cb@256bit.org>
parents: 10839
diff changeset
98 ga_concat(gap, (char_u *)"\"\"");
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
99 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
100 {
15597
536dd2bc5ac9 patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 15555
diff changeset
101 #if defined(USE_ICONV)
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
102 vimconv_T conv;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
103 char_u *converted = NULL;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
104
8293
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
105 if (!enc_utf8)
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
106 {
8306
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
107 /* Convert the text from 'encoding' to utf-8, the JSON string is
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
108 * always utf-8. */
8293
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
109 conv.vc_type = CONV_NONE;
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
110 convert_setup(&conv, p_enc, (char_u*)"utf-8");
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
111 if (conv.vc_type != CONV_NONE)
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
112 converted = res = string_convert(&conv, res, NULL);
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
113 convert_setup(&conv, NULL, NULL);
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
114 }
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
115 #endif
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
116 ga_append(gap, '"');
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
117 while (*res != NUL)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
118 {
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
119 int c;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
120 /* always use utf-8 encoding, ignore 'encoding' */
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
121 c = utf_ptr2char(res);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
122
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
123 switch (c)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
124 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
125 case 0x08:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
126 ga_append(gap, '\\'); ga_append(gap, 'b'); break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
127 case 0x09:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
128 ga_append(gap, '\\'); ga_append(gap, 't'); break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
129 case 0x0a:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
130 ga_append(gap, '\\'); ga_append(gap, 'n'); break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
131 case 0x0c:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
132 ga_append(gap, '\\'); ga_append(gap, 'f'); break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
133 case 0x0d:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
134 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
135 case 0x22: /* " */
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
136 case 0x5c: /* \ */
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
137 ga_append(gap, '\\');
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
138 ga_append(gap, c);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
139 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
140 default:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
141 if (c >= 0x20)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
142 {
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
143 numbuf[utf_char2bytes(c, numbuf)] = NUL;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
144 ga_concat(gap, numbuf);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
145 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
146 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
147 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
148 vim_snprintf((char *)numbuf, NUMBUFLEN,
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
149 "\\u%04lx", (long)c);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
150 ga_concat(gap, numbuf);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
151 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
152 }
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
153 res += utf_ptr2len(res);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
154 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
155 ga_append(gap, '"');
15597
536dd2bc5ac9 patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 15555
diff changeset
156 #if defined(USE_ICONV)
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
157 vim_free(converted);
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
158 #endif
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
159 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
160 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
161
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
162 /*
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
163 * Return TRUE if "key" can be used without quotes.
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
164 * That is when it starts with a letter and only contains letters, digits and
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
165 * underscore.
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
166 */
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
167 static int
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
168 is_simple_key(char_u *key)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
169 {
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
170 char_u *p;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
171
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
172 if (!ASCII_ISALPHA(*key))
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
173 return FALSE;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
174 for (p = key + 1; *p != NUL; ++p)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
175 if (!ASCII_ISALPHA(*p) && *p != '_' && !vim_isdigit(*p))
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
176 return FALSE;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
177 return TRUE;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
178 }
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
179
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
180 /*
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
181 * Encode "val" into "gap".
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
182 * Return FAIL or OK.
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
183 */
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
184 static int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
185 json_encode_item(garray_T *gap, typval_T *val, int copyID, int options)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
186 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
187 char_u numbuf[NUMBUFLEN];
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
188 char_u *res;
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
189 blob_T *b;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
190 list_T *l;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
191 dict_T *d;
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
192 int i;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
193
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
194 switch (val->v_type)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
195 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
196 case VAR_SPECIAL:
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
197 switch (val->vval.v_number)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
198 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
199 case VVAL_FALSE: ga_concat(gap, (char_u *)"false"); break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
200 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
201 case VVAL_NONE: if ((options & JSON_JS) != 0
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
202 && (options & JSON_NO_NONE) == 0)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
203 /* empty item */
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
204 break;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
205 /* FALLTHROUGH */
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
206 case VVAL_NULL: ga_concat(gap, (char_u *)"null"); break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
207 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
208 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
209
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
210 case VAR_NUMBER:
9389
32e34e574716 commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents: 8989
diff changeset
211 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
15517
2ad5f0ffaa2e patch 8.1.0766: various problems when using Vim on VMS
Bram Moolenaar <Bram@vim.org>
parents: 15470
diff changeset
212 (long_long_T)val->vval.v_number);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
213 ga_concat(gap, numbuf);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
214 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
215
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
216 case VAR_STRING:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
217 res = val->vval.v_string;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
218 write_string(gap, res);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
219 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
220
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
221 case VAR_FUNC:
8538
c337c813c64d commit https://github.com/vim/vim/commit/1735bc988c546cc962c5f94792815b4d7cb79710
Christian Brabandt <cb@256bit.org>
parents: 8414
diff changeset
222 case VAR_PARTIAL:
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
223 case VAR_JOB:
8041
c6443e78cf2d commit https://github.com/vim/vim/commit/7707344ddec9069b495b2a5ed41f2104466fc88b
Christian Brabandt <cb@256bit.org>
parents: 7967
diff changeset
224 case VAR_CHANNEL:
7947
b2922673917a commit https://github.com/vim/vim/commit/4f8b8faec31a934920a723053e8dcf47b6fac08c
Christian Brabandt <cb@256bit.org>
parents: 7931
diff changeset
225 /* no JSON equivalent TODO: better error */
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
226 emsg(_(e_invarg));
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
227 return FAIL;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
228
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
229 case VAR_BLOB:
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
230 b = val->vval.v_blob;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
231 if (b == NULL || b->bv_ga.ga_len == 0)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
232 ga_concat(gap, (char_u *)"[]");
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
233 else
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
234 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
235 ga_append(gap, '[');
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
236 for (i = 0; i < b->bv_ga.ga_len; i++)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
237 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
238 if (i > 0)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
239 ga_concat(gap, (char_u *)",");
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
240 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
241 (int)blob_get(b, i));
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
242 ga_concat(gap, numbuf);
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
243 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
244 ga_append(gap, ']');
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
245 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
246 break;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
247
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
248 case VAR_LIST:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
249 l = val->vval.v_list;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
250 if (l == NULL)
13031
c35d266308c2 patch 8.0.1391: encoding empty string to JSON sometimes gives "null"
Christian Brabandt <cb@256bit.org>
parents: 10839
diff changeset
251 ga_concat(gap, (char_u *)"[]");
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
252 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
253 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
254 if (l->lv_copyID == copyID)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
255 ga_concat(gap, (char_u *)"[]");
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
256 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
257 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
258 listitem_T *li;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
259
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
260 l->lv_copyID = copyID;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
261 ga_append(gap, '[');
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
262 for (li = l->lv_first; li != NULL && !got_int; )
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
263 {
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
264 if (json_encode_item(gap, &li->li_tv, copyID,
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
265 options & JSON_JS) == FAIL)
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
266 return FAIL;
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
267 if ((options & JSON_JS)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
268 && li->li_next == NULL
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
269 && li->li_tv.v_type == VAR_SPECIAL
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
270 && li->li_tv.vval.v_number == VVAL_NONE)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
271 /* add an extra comma if the last item is v:none */
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
272 ga_append(gap, ',');
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
273 li = li->li_next;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
274 if (li != NULL)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
275 ga_append(gap, ',');
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
276 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
277 ga_append(gap, ']');
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
278 l->lv_copyID = 0;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
279 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
280 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
281 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
282
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
283 case VAR_DICT:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
284 d = val->vval.v_dict;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
285 if (d == NULL)
13031
c35d266308c2 patch 8.0.1391: encoding empty string to JSON sometimes gives "null"
Christian Brabandt <cb@256bit.org>
parents: 10839
diff changeset
286 ga_concat(gap, (char_u *)"{}");
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
287 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
288 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
289 if (d->dv_copyID == copyID)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
290 ga_concat(gap, (char_u *)"{}");
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
291 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
292 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
293 int first = TRUE;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
294 int todo = (int)d->dv_hashtab.ht_used;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
295 hashitem_T *hi;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
296
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
297 d->dv_copyID = copyID;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
298 ga_append(gap, '{');
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
299
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
300 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
301 ++hi)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
302 if (!HASHITEM_EMPTY(hi))
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
303 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
304 --todo;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
305 if (first)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
306 first = FALSE;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
307 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
308 ga_append(gap, ',');
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
309 if ((options & JSON_JS)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
310 && is_simple_key(hi->hi_key))
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
311 ga_concat(gap, hi->hi_key);
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
312 else
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
313 write_string(gap, hi->hi_key);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
314 ga_append(gap, ':');
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
315 if (json_encode_item(gap, &dict_lookup(hi)->di_tv,
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
316 copyID, options | JSON_NO_NONE) == FAIL)
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
317 return FAIL;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
318 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
319 ga_append(gap, '}');
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
320 d->dv_copyID = 0;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
321 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
322 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
323 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
324
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
325 case VAR_FLOAT:
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
326 #ifdef FEAT_FLOAT
8228
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
327 # if defined(HAVE_MATH_H)
8275
ff900e499f79 commit https://github.com/vim/vim/commit/7ce686c990ea8c490d16be7f1c6bd95eb48816f9
Christian Brabandt <cb@256bit.org>
parents: 8230
diff changeset
328 if (isnan(val->vval.v_float))
8228
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
329 ga_concat(gap, (char_u *)"NaN");
8275
ff900e499f79 commit https://github.com/vim/vim/commit/7ce686c990ea8c490d16be7f1c6bd95eb48816f9
Christian Brabandt <cb@256bit.org>
parents: 8230
diff changeset
330 else if (isinf(val->vval.v_float))
15446
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
331 {
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
332 if (val->vval.v_float < 0.0)
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
333 ga_concat(gap, (char_u *)"-Infinity");
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
334 else
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
335 ga_concat(gap, (char_u *)"Infinity");
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
336 }
8228
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
337 else
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
338 # endif
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
339 {
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
340 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
341 val->vval.v_float);
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
342 ga_concat(gap, numbuf);
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
343 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
344 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
345 #endif
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
346 case VAR_UNKNOWN:
10359
66f1b5bf3fa6 commit https://github.com/vim/vim/commit/95f096030ed1a8afea028f2ea295d6f6a70f466f
Christian Brabandt <cb@256bit.org>
parents: 10344
diff changeset
347 internal_error("json_encode_item()");
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
348 return FAIL;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
349 }
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
350 return OK;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
351 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
352
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
353 /*
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
354 * When "reader" has less than NUMBUFLEN bytes available, call the fill
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
355 * callback to get more.
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
356 */
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
357 static void
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
358 fill_numbuflen(js_read_T *reader)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
359 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
360 if (reader->js_fill != NULL && (int)(reader->js_end - reader->js_buf)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
361 - reader->js_used < NUMBUFLEN)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
362 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
363 if (reader->js_fill(reader))
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
364 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
365 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
366 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
367
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
368 /*
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
369 * Skip white space in "reader". All characters <= space are considered white
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
370 * space.
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
371 * Also tops up readahead when needed.
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
372 */
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
373 static void
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
374 json_skip_white(js_read_T *reader)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
375 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
376 int c;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
377
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
378 for (;;)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
379 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
380 c = reader->js_buf[reader->js_used];
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
381 if (reader->js_fill != NULL && c == NUL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
382 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
383 if (reader->js_fill(reader))
8755
7038ec89d1fd commit https://github.com/vim/vim/commit/46c00a6565b8f1f4b7b1041d03eaceaf6ffc4aee
Christian Brabandt <cb@256bit.org>
parents: 8655
diff changeset
384 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
385 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
8755
7038ec89d1fd commit https://github.com/vim/vim/commit/46c00a6565b8f1f4b7b1041d03eaceaf6ffc4aee
Christian Brabandt <cb@256bit.org>
parents: 8655
diff changeset
386 continue;
7038ec89d1fd commit https://github.com/vim/vim/commit/46c00a6565b8f1f4b7b1041d03eaceaf6ffc4aee
Christian Brabandt <cb@256bit.org>
parents: 8655
diff changeset
387 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
388 }
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
389 if (c == NUL || c > ' ')
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
390 break;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
391 ++reader->js_used;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
392 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
393 fill_numbuflen(reader);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
394 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
395
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
396 static int
10563
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
397 json_decode_string(js_read_T *reader, typval_T *res, int quote)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
398 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
399 garray_T ga;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
400 int len;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
401 char_u *p;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
402 int c;
9389
32e34e574716 commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents: 8989
diff changeset
403 varnumber_T nr;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
404
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
405 if (res != NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
406 ga_init2(&ga, 1, 200);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
407
10563
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
408 p = reader->js_buf + reader->js_used + 1; /* skip over " or ' */
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
409 while (*p != quote)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
410 {
8306
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
411 /* The JSON is always expected to be utf-8, thus use utf functions
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
412 * here. The string is converted below if needed. */
15597
536dd2bc5ac9 patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 15555
diff changeset
413 if (*p == NUL || p[1] == NUL || utf_ptr2len(p) < utf_byte2len(*p))
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
414 {
8306
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
415 /* Not enough bytes to make a character or end of the string. Get
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
416 * more if possible. */
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
417 if (reader->js_fill == NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
418 break;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
419 len = (int)(reader->js_end - p);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
420 reader->js_used = (int)(p - reader->js_buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
421 if (!reader->js_fill(reader))
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
422 break; /* didn't get more */
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
423 p = reader->js_buf + reader->js_used;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
424 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
425 continue;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
426 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
427
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
428 if (*p == '\\')
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
429 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
430 c = -1;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
431 switch (p[1])
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
432 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
433 case '\\': c = '\\'; break;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
434 case '"': c = '"'; break;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
435 case 'b': c = BS; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
436 case 't': c = TAB; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
437 case 'n': c = NL; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
438 case 'f': c = FF; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
439 case 'r': c = CAR; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
440 case 'u':
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
441 if (reader->js_fill != NULL
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
442 && (int)(reader->js_end - p) < NUMBUFLEN)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
443 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
444 reader->js_used = (int)(p - reader->js_buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
445 if (reader->js_fill(reader))
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
446 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
447 p = reader->js_buf + reader->js_used;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
448 reader->js_end = reader->js_buf
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
449 + STRLEN(reader->js_buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
450 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
451 }
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
452 nr = 0;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
453 len = 0;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
454 vim_str2nr(p + 2, NULL, &len,
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
455 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE);
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
456 if (len == 0)
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
457 {
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
458 ga_clear(&ga);
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
459 return FAIL;
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
460 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
461 p += len + 2;
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
462 if (0xd800 <= nr && nr <= 0xdfff
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
463 && (int)(reader->js_end - p) >= 6
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
464 && *p == '\\' && *(p+1) == 'u')
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
465 {
9389
32e34e574716 commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents: 8989
diff changeset
466 varnumber_T nr2 = 0;
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
467
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
468 /* decode surrogate pair: \ud812\u3456 */
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
469 len = 0;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
470 vim_str2nr(p + 2, NULL, &len,
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
471 STR2NR_HEX + STR2NR_FORCE, &nr2, NULL, 4, TRUE);
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
472 if (len == 0)
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
473 {
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
474 ga_clear(&ga);
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
475 return FAIL;
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
476 }
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
477 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
478 {
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
479 p += len + 2;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
480 nr = (((nr - 0xd800) << 10) |
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
481 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
482 }
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
483 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
484 if (res != NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
485 {
8989
e600e696c0a1 commit https://github.com/vim/vim/commit/dc633cf82758f67f656cda7fa8ccc30414ee53f8
Christian Brabandt <cb@256bit.org>
parents: 8755
diff changeset
486 char_u buf[NUMBUFLEN];
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
487 buf[utf_char2bytes((int)nr, buf)] = NUL;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
488 ga_concat(&ga, buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
489 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
490 break;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
491 default:
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
492 /* not a special char, skip over \ */
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
493 ++p;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
494 continue;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
495 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
496 if (c > 0)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
497 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
498 p += 2;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
499 if (res != NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
500 ga_append(&ga, c);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
501 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
502 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
503 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
504 {
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
505 len = utf_ptr2len(p);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
506 if (res != NULL)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
507 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
508 if (ga_grow(&ga, len) == FAIL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
509 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
510 ga_clear(&ga);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
511 return FAIL;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
512 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
513 mch_memmove((char *)ga.ga_data + ga.ga_len, p, (size_t)len);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
514 ga.ga_len += len;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
515 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
516 p += len;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
517 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
518 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
519
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
520 reader->js_used = (int)(p - reader->js_buf);
10563
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
521 if (*p == quote)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
522 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
523 ++reader->js_used;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
524 if (res != NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
525 {
8308
c1f29c1a968a commit https://github.com/vim/vim/commit/80e78847395b5c8ada7861674774d81bd0a42789
Christian Brabandt <cb@256bit.org>
parents: 8306
diff changeset
526 ga_append(&ga, NUL);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
527 res->v_type = VAR_STRING;
15597
536dd2bc5ac9 patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 15555
diff changeset
528 #if defined(USE_ICONV)
8306
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
529 if (!enc_utf8)
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
530 {
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
531 vimconv_T conv;
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
532
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
533 /* Convert the utf-8 string to 'encoding'. */
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
534 conv.vc_type = CONV_NONE;
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
535 convert_setup(&conv, (char_u*)"utf-8", p_enc);
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
536 if (conv.vc_type != CONV_NONE)
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
537 {
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
538 res->vval.v_string =
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
539 string_convert(&conv, ga.ga_data, NULL);
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
540 vim_free(ga.ga_data);
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
541 }
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
542 convert_setup(&conv, NULL, NULL);
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
543 }
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
544 else
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
545 #endif
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
546 res->vval.v_string = ga.ga_data;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
547 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
548 return OK;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
549 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
550 if (res != NULL)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
551 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
552 res->v_type = VAR_SPECIAL;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
553 res->vval.v_number = VVAL_NONE;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
554 ga_clear(&ga);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
555 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
556 return MAYBE;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
557 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
558
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
559 typedef enum {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
560 JSON_ARRAY, /* parsing items in an array */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
561 JSON_OBJECT_KEY, /* parsing key of an object */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
562 JSON_OBJECT /* parsing item in an object, after the key */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
563 } json_decode_T;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
564
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
565 typedef struct {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
566 json_decode_T jd_type;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
567 typval_T jd_tv; /* the list or dict */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
568 typval_T jd_key_tv;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
569 char_u *jd_key;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
570 } json_dec_item_T;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
571
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
572 /*
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
573 * Decode one item and put it in "res". If "res" is NULL only advance.
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
574 * Must already have skipped white space.
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
575 *
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
576 * Return FAIL for a decoding error (and give an error).
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
577 * Return MAYBE for an incomplete message.
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
578 */
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
579 static int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
580 json_decode_item(js_read_T *reader, typval_T *res, int options)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
581 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
582 char_u *p;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
583 int len;
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
584 int retval;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
585 garray_T stack;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
586 typval_T item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
587 typval_T *cur_item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
588 json_dec_item_T *top_item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
589 char_u key_buf[NUMBUFLEN];
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
590
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
591 ga_init2(&stack, sizeof(json_dec_item_T), 100);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
592 cur_item = res;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
593 init_tv(&item);
10561
e025378406d1 patch 8.0.0170: crash in channel test
Christian Brabandt <cb@256bit.org>
parents: 10559
diff changeset
594 if (res != NULL)
e025378406d1 patch 8.0.0170: crash in channel test
Christian Brabandt <cb@256bit.org>
parents: 10559
diff changeset
595 init_tv(res);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
596
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
597 fill_numbuflen(reader);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
598 p = reader->js_buf + reader->js_used;
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
599 for (;;)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
600 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
601 top_item = NULL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
602 if (stack.ga_len > 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
603 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
604 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
605 json_skip_white(reader);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
606 p = reader->js_buf + reader->js_used;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
607 if (*p == NUL)
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
608 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
609 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
610 if (top_item->jd_type == JSON_OBJECT)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
611 /* did get the key, clear it */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
612 clear_tv(&top_item->jd_key_tv);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
613 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
614 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
615 if (top_item->jd_type == JSON_OBJECT_KEY
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
616 || top_item->jd_type == JSON_ARRAY)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
617 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
618 /* Check for end of object or array. */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
619 if (*p == (top_item->jd_type == JSON_ARRAY ? ']' : '}'))
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
620 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
621 ++reader->js_used; /* consume the ']' or '}' */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
622 --stack.ga_len;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
623 if (stack.ga_len == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
624 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
625 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
626 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
627 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
628 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
629 cur_item = &top_item->jd_tv;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
630 goto item_end;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
631 }
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
632 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
633 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
634
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
635 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
636 && (options & JSON_JS)
10563
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
637 && reader->js_buf[reader->js_used] != '"'
13456
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
638 && reader->js_buf[reader->js_used] != '\''
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
639 && reader->js_buf[reader->js_used] != '['
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
640 && reader->js_buf[reader->js_used] != '{')
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
641 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
642 char_u *key;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
643
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
644 /* accept an object key that is not in quotes */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
645 key = p = reader->js_buf + reader->js_used;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
646 while (*p != NUL && *p != ':' && *p > ' ')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
647 ++p;
10652
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
648 if (cur_item != NULL)
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
649 {
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
650 cur_item->v_type = VAR_STRING;
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
651 cur_item->vval.v_string = vim_strnsave(key, (int)(p - key));
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
652 top_item->jd_key = cur_item->vval.v_string;
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
653 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
654 reader->js_used += (int)(p - key);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
655 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
656 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
657 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
658 switch (*p)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
659 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
660 case '[': /* start of array */
13456
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
661 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
662 {
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
663 retval = FAIL;
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
664 break;
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
665 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
666 if (ga_grow(&stack, 1) == FAIL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
667 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
668 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
669 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
670 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
671 if (cur_item != NULL && rettv_list_alloc(cur_item) == FAIL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
672 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
673 cur_item->v_type = VAR_SPECIAL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
674 cur_item->vval.v_number = VVAL_NONE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
675 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
676 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
677 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
678
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
679 ++reader->js_used; /* consume the '[' */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
680 top_item = ((json_dec_item_T *)stack.ga_data)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
681 + stack.ga_len;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
682 top_item->jd_type = JSON_ARRAY;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
683 ++stack.ga_len;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
684 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
685 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
686 top_item->jd_tv = *cur_item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
687 cur_item = &item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
688 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
689 continue;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
690
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
691 case '{': /* start of object */
13456
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
692 if (top_item && top_item->jd_type == JSON_OBJECT_KEY)
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
693 {
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
694 retval = FAIL;
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
695 break;
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
696 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
697 if (ga_grow(&stack, 1) == FAIL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
698 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
699 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
700 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
701 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
702 if (cur_item != NULL && rettv_dict_alloc(cur_item) == FAIL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
703 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
704 cur_item->v_type = VAR_SPECIAL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
705 cur_item->vval.v_number = VVAL_NONE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
706 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
707 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
708 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
709
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
710 ++reader->js_used; /* consume the '{' */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
711 top_item = ((json_dec_item_T *)stack.ga_data)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
712 + stack.ga_len;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
713 top_item->jd_type = JSON_OBJECT_KEY;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
714 ++stack.ga_len;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
715 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
716 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
717 top_item->jd_tv = *cur_item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
718 cur_item = &top_item->jd_key_tv;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
719 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
720 continue;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
721
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
722 case '"': /* string */
10563
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
723 retval = json_decode_string(reader, cur_item, *p);
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
724 break;
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
725
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
726 case '\'':
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
727 if (options & JSON_JS)
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
728 retval = json_decode_string(reader, cur_item, *p);
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
729 else
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
730 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
731 emsg(_(e_invarg));
10563
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
732 retval = FAIL;
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
733 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
734 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
735
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
736 case ',': /* comma: empty item */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
737 if ((options & JSON_JS) == 0)
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
738 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
739 emsg(_(e_invarg));
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
740 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
741 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
742 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
743 /* FALLTHROUGH */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
744 case NUL: /* empty */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
745 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
746 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
747 cur_item->v_type = VAR_SPECIAL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
748 cur_item->vval.v_number = VVAL_NONE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
749 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
750 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
751 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
752
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
753 default:
15446
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
754 if (VIM_ISDIGIT(*p) || (*p == '-' && VIM_ISDIGIT(p[1])))
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
755 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
756 #ifdef FEAT_FLOAT
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
757 char_u *sp = p;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
758
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
759 if (*sp == '-')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
760 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
761 ++sp;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
762 if (*sp == NUL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
763 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
764 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
765 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
766 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
767 if (!VIM_ISDIGIT(*sp))
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
768 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
769 emsg(_(e_invarg));
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
770 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
771 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
772 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
773 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
774 sp = skipdigits(sp);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
775 if (*sp == '.' || *sp == 'e' || *sp == 'E')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
776 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
777 if (cur_item == NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
778 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
779 float_T f;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
780
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
781 len = string2float(p, &f);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
782 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
783 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
784 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
785 cur_item->v_type = VAR_FLOAT;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
786 len = string2float(p, &cur_item->vval.v_float);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
787 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
788 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
789 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
790 #endif
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
791 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
792 varnumber_T nr;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
793
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
794 vim_str2nr(reader->js_buf + reader->js_used,
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
795 NULL, &len, 0, /* what */
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
796 &nr, NULL, 0, TRUE);
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
797 if (len == 0)
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
798 {
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
799 emsg(_(e_invarg));
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
800 retval = FAIL;
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
801 goto theend;
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
802 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
803 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
804 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
805 cur_item->v_type = VAR_NUMBER;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
806 cur_item->vval.v_number = nr;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
807 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
808 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
809 reader->js_used += len;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
810 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
811 break;
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
812 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
813 if (STRNICMP((char *)p, "false", 5) == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
814 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
815 reader->js_used += 5;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
816 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
817 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
818 cur_item->v_type = VAR_SPECIAL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
819 cur_item->vval.v_number = VVAL_FALSE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
820 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
821 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
822 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
823 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
824 if (STRNICMP((char *)p, "true", 4) == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
825 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
826 reader->js_used += 4;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
827 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
828 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
829 cur_item->v_type = VAR_SPECIAL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
830 cur_item->vval.v_number = VVAL_TRUE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
831 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
832 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
833 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
834 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
835 if (STRNICMP((char *)p, "null", 4) == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
836 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
837 reader->js_used += 4;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
838 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
839 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
840 cur_item->v_type = VAR_SPECIAL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
841 cur_item->vval.v_number = VVAL_NULL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
842 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
843 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
844 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
845 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
846 #ifdef FEAT_FLOAT
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
847 if (STRNICMP((char *)p, "NaN", 3) == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
848 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
849 reader->js_used += 3;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
850 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
851 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
852 cur_item->v_type = VAR_FLOAT;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
853 cur_item->vval.v_float = NAN;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
854 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
855 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
856 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
857 }
15446
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
858 if (STRNICMP((char *)p, "-Infinity", 9) == 0)
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
859 {
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
860 reader->js_used += 9;
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
861 if (cur_item != NULL)
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
862 {
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
863 cur_item->v_type = VAR_FLOAT;
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
864 cur_item->vval.v_float = -INFINITY;
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
865 }
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
866 retval = OK;
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
867 break;
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
868 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
869 if (STRNICMP((char *)p, "Infinity", 8) == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
870 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
871 reader->js_used += 8;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
872 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
873 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
874 cur_item->v_type = VAR_FLOAT;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
875 cur_item->vval.v_float = INFINITY;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
876 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
877 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
878 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
879 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
880 #endif
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
881 /* check for truncated name */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
882 len = (int)(reader->js_end - (reader->js_buf + reader->js_used));
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
883 if (
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
884 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
885 #ifdef FEAT_FLOAT
15446
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
886 || (len < 9 && STRNICMP((char *)p, "-Infinity", len) == 0)
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
887 || (len < 8 && STRNICMP((char *)p, "Infinity", len) == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
888 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
889 #endif
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
890 || (len < 4 && (STRNICMP((char *)p, "true", len) == 0
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
891 || STRNICMP((char *)p, "null", len) == 0)))
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
892
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
893 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
894 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
895 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
896 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
897 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
898
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
899 /* We are finished when retval is FAIL or MAYBE and when at the
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
900 * toplevel. */
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
901 if (retval == FAIL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
902 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
903 if (retval == MAYBE || stack.ga_len == 0)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
904 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
905
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
906 if (top_item != NULL && top_item->jd_type == JSON_OBJECT_KEY
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
907 && cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
908 {
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 14862
diff changeset
909 top_item->jd_key = tv_get_string_buf_chk(cur_item, key_buf);
10839
287049007bbc patch 8.0.0309: cannot use an empty key in json
Christian Brabandt <cb@256bit.org>
parents: 10652
diff changeset
910 if (top_item->jd_key == NULL)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
911 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
912 clear_tv(cur_item);
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
913 emsg(_(e_invarg));
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
914 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
915 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
916 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
917 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
918 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
919
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
920 item_end:
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
921 top_item = ((json_dec_item_T *)stack.ga_data) + stack.ga_len - 1;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
922 switch (top_item->jd_type)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
923 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
924 case JSON_ARRAY:
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
925 if (res != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
926 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
927 listitem_T *li = listitem_alloc();
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
928
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
929 if (li == NULL)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
930 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
931 clear_tv(cur_item);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
932 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
933 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
934 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
935 li->li_tv = *cur_item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
936 list_append(top_item->jd_tv.vval.v_list, li);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
937 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
938 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
939 cur_item = &item;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
940
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
941 json_skip_white(reader);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
942 p = reader->js_buf + reader->js_used;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
943 if (*p == ',')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
944 ++reader->js_used;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
945 else if (*p != ']')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
946 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
947 if (*p == NUL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
948 retval = MAYBE;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
949 else
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
950 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
951 emsg(_(e_invarg));
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
952 retval = FAIL;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
953 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
954 goto theend;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
955 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
956 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
957
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
958 case JSON_OBJECT_KEY:
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
959 json_skip_white(reader);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
960 p = reader->js_buf + reader->js_used;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
961 if (*p != ':')
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
962 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
963 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
964 clear_tv(cur_item);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
965 if (*p == NUL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
966 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
967 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
968 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
969 emsg(_(e_invarg));
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
970 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
971 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
972 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
973 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
974 ++reader->js_used;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
975 json_skip_white(reader);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
976 top_item->jd_type = JSON_OBJECT;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
977 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
978 cur_item = &item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
979 break;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
980
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
981 case JSON_OBJECT:
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
982 if (cur_item != NULL
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
983 && dict_find(top_item->jd_tv.vval.v_dict,
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
984 top_item->jd_key, -1) != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
985 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
986 semsg(_("E938: Duplicate key in JSON: \"%s\""),
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
987 top_item->jd_key);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
988 clear_tv(&top_item->jd_key_tv);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
989 clear_tv(cur_item);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
990 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
991 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
992 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
993
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
994 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
995 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
996 dictitem_T *di = dictitem_alloc(top_item->jd_key);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
997
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
998 clear_tv(&top_item->jd_key_tv);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
999 if (di == NULL)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1000 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1001 clear_tv(cur_item);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1002 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1003 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1004 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1005 di->di_tv = *cur_item;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1006 di->di_tv.v_lock = 0;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1007 if (dict_add(top_item->jd_tv.vval.v_dict, di) == FAIL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1008 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1009 dictitem_free(di);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1010 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1011 goto theend;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1012 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1013 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1014
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1015 json_skip_white(reader);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1016 p = reader->js_buf + reader->js_used;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1017 if (*p == ',')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1018 ++reader->js_used;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1019 else if (*p != '}')
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1020 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1021 if (*p == NUL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1022 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1023 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1024 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
1025 emsg(_(e_invarg));
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1026 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1027 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1028 goto theend;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1029 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1030 top_item->jd_type = JSON_OBJECT_KEY;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1031 if (cur_item != NULL)
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1032 cur_item = &top_item->jd_key_tv;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1033 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1034 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1035 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1036
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1037 /* Get here when parsing failed. */
10299
7b2f95633e28 commit https://github.com/vim/vim/commit/7756e7465d627ff9cd01e59625484a8c302ef853
Christian Brabandt <cb@256bit.org>
parents: 10042
diff changeset
1038 if (res != NULL)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1039 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1040 clear_tv(res);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1041 res->v_type = VAR_SPECIAL;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1042 res->vval.v_number = VVAL_NONE;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1043 }
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
1044 emsg(_(e_invarg));
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1045
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1046 theend:
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1047 ga_clear(&stack);
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1048 return retval;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1049 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1050
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1051 /*
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1052 * Decode the JSON from "reader" and store the result in "res".
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1053 * "options" can be JSON_JS or zero;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1054 * Return FAIL if not the whole message was consumed.
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1055 */
7868
17e6ff1a74f1 commit https://github.com/vim/vim/commit/19d2f1589850d7db1ce77efec052929246f156e2
Christian Brabandt <cb@256bit.org>
parents: 7864
diff changeset
1056 int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1057 json_decode_all(js_read_T *reader, typval_T *res, int options)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1058 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1059 int ret;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1060
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1061 /* We find the end once, to avoid calling strlen() many times. */
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1062 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1063 json_skip_white(reader);
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1064 ret = json_decode_item(reader, res, options);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1065 if (ret != OK)
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
1066 {
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
1067 if (ret == MAYBE)
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
1068 emsg(_(e_invarg));
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1069 return FAIL;
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
1070 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1071 json_skip_white(reader);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1072 if (reader->js_buf[reader->js_used] != NUL)
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
1073 {
15470
55ccc2d353bd patch 8.1.0743: giving error messages is not flexible
Bram Moolenaar <Bram@vim.org>
parents: 15454
diff changeset
1074 emsg(_(e_trailing));
7868
17e6ff1a74f1 commit https://github.com/vim/vim/commit/19d2f1589850d7db1ce77efec052929246f156e2
Christian Brabandt <cb@256bit.org>
parents: 7864
diff changeset
1075 return FAIL;
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
1076 }
7868
17e6ff1a74f1 commit https://github.com/vim/vim/commit/19d2f1589850d7db1ce77efec052929246f156e2
Christian Brabandt <cb@256bit.org>
parents: 7864
diff changeset
1077 return OK;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1078 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1079
15555
d89c5b339c2a patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents: 15517
diff changeset
1080 #if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1081 /*
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1082 * Decode the JSON from "reader" and store the result in "res".
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1083 * "options" can be JSON_JS or zero;
8655
1eb302bf2475 commit https://github.com/vim/vim/commit/ba61ac0d61f46de7d29c64bb0de6d25c2e378be0
Christian Brabandt <cb@256bit.org>
parents: 8538
diff changeset
1084 * Return FAIL for a decoding error.
1eb302bf2475 commit https://github.com/vim/vim/commit/ba61ac0d61f46de7d29c64bb0de6d25c2e378be0
Christian Brabandt <cb@256bit.org>
parents: 8538
diff changeset
1085 * Return MAYBE for an incomplete message.
1eb302bf2475 commit https://github.com/vim/vim/commit/ba61ac0d61f46de7d29c64bb0de6d25c2e378be0
Christian Brabandt <cb@256bit.org>
parents: 8538
diff changeset
1086 * Consumes the message anyway.
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1087 */
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1088 int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1089 json_decode(js_read_T *reader, typval_T *res, int options)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1090 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1091 int ret;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1092
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1093 /* We find the end once, to avoid calling strlen() many times. */
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1094 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1095 json_skip_white(reader);
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1096 ret = json_decode_item(reader, res, options);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1097 json_skip_white(reader);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1098
8655
1eb302bf2475 commit https://github.com/vim/vim/commit/ba61ac0d61f46de7d29c64bb0de6d25c2e378be0
Christian Brabandt <cb@256bit.org>
parents: 8538
diff changeset
1099 return ret;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1100 }
15555
d89c5b339c2a patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents: 15517
diff changeset
1101 #endif
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1102
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1103 /*
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1104 * Decode the JSON from "reader" to find the end of the message.
10652
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
1105 * "options" can be JSON_JS or zero.
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
1106 * This is only used for testing.
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1107 * Return FAIL if the message has a decoding error.
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1108 * Return MAYBE if the message is truncated, need to read more.
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1109 * This only works reliable if the message contains an object, array or
15446
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
1110 * string. A number might be truncated without knowing.
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1111 * Does not advance the reader.
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1112 */
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1113 int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1114 json_find_end(js_read_T *reader, int options)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1115 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1116 int used_save = reader->js_used;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1117 int ret;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1118
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1119 /* We find the end once, to avoid calling strlen() many times. */
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1120 reader->js_end = reader->js_buf + STRLEN(reader->js_buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1121 json_skip_white(reader);
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1122 ret = json_decode_item(reader, NULL, options);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1123 reader->js_used = used_save;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1124 return ret;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1125 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1126 #endif