annotate src/json.c @ 35086:35dedcbfd4d7 default tip

Added tag v9.1.0384 for changeset 9868a8818e018b699d07de45cb5b225a3fde4516
author Christian Brabandt <cb@256bit.org>
date Tue, 30 Apr 2024 21:00:04 +0200
parents 7d9d2404a3d4
children
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
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
51 // Store bytes in the growarray.
7712
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 }
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
89
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
90 /*
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
91 * Encode "val" into a JSON format string prefixed by the LSP HTTP header.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
92 * Returns NULL when out of memory.
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
93 */
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
94 char_u *
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
95 json_encode_lsp_msg(typval_T *val)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
96 {
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
97 garray_T ga;
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
98 garray_T lspga;
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
99
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
100 ga_init2(&ga, 1, 4000);
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
101 if (json_encode_gap(&ga, val, 0) == FAIL)
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
102 return NULL;
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
103 ga_append(&ga, NUL);
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
104
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
105 ga_init2(&lspga, 1, 4000);
32311
10a03ae8ba60 patch 9.0.1487: Content-type header for LSP channel not according to spec
Bram Moolenaar <Bram@vim.org>
parents: 32098
diff changeset
106 // Header according to LSP specification.
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
107 vim_snprintf((char *)IObuff, IOSIZE,
32996
dd8da8f1c2bc patch 9.0.1790: Redundant LSP Content-Type header
Christian Brabandt <cb@256bit.org>
parents: 32311
diff changeset
108 "Content-Length: %u\r\n\r\n",
28244
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
109 ga.ga_len - 1);
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
110 ga_concat(&lspga, IObuff);
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
111 ga_concat_len(&lspga, ga.ga_data, ga.ga_len);
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
112 ga_clear(&ga);
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
113 return lspga.ga_data;
85b07a942518 patch 8.2.4648: handling LSP messages is a bit slow
Bram Moolenaar <Bram@vim.org>
parents: 27426
diff changeset
114 }
15555
d89c5b339c2a patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents: 15517
diff changeset
115 #endif
7864
6b0891de44a9 commit https://github.com/vim/vim/commit/fb1f62691eae7c79a28b3b17a60e72ce198c71a2
Christian Brabandt <cb@256bit.org>
parents: 7791
diff changeset
116
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
117 /*
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
118 * Lookup table to quickly know if the given ASCII character must be escaped.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
119 */
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
120 static const char ascii_needs_escape[128] = {
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
121 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x0.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
122 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 0x1.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
123 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x2.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
124 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x3.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
125 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x4.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
126 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, // 0x5.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
127 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x6.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
128 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 0x7.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
129 };
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
130
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
131 /*
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
132 * Encode the utf-8 encoded string "str" into "gap".
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
133 */
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
134 static void
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
135 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
136 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
137 char_u *res = str;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
138 char_u numbuf[NUMBUFLEN];
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
139 char_u *from;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
140 #if defined(USE_ICONV)
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
141 vimconv_T conv;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
142 char_u *converted = NULL;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
143 #endif
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
144 int c;
7712
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 if (res == NULL)
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
147 {
13031
c35d266308c2 patch 8.0.1391: encoding empty string to JSON sometimes gives "null"
Christian Brabandt <cb@256bit.org>
parents: 10839
diff changeset
148 ga_concat(gap, (char_u *)"\"\"");
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
149 return;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
150 }
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
151
15597
536dd2bc5ac9 patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 15555
diff changeset
152 #if defined(USE_ICONV)
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
153 if (!enc_utf8)
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
154 {
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
155 // Convert the text from 'encoding' to utf-8, because a JSON string is
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
156 // always utf-8.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
157 conv.vc_type = CONV_NONE;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
158 convert_setup(&conv, p_enc, (char_u*)"utf-8");
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
159 if (conv.vc_type != CONV_NONE)
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
160 converted = res = string_convert(&conv, res, NULL);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
161 convert_setup(&conv, NULL, NULL);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
162 }
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
163 #endif
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
164 ga_append(gap, '"');
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
165 // `from` is the beginning of a sequence of bytes we can directly copy from
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
166 // the input string, avoiding the overhead associated to decoding/encoding
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
167 // them.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
168 from = res;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
169 while ((c = *res) != NUL)
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
170 {
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
171 // always use utf-8 encoding, ignore 'encoding'
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
172 if (c < 0x80)
8293
367a7fed630b commit https://github.com/vim/vim/commit/f97ddbeb255c64a2b3d9db4b049278cd286070a6
Christian Brabandt <cb@256bit.org>
parents: 8289
diff changeset
173 {
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
174 if (!ascii_needs_escape[c])
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
175 {
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
176 res += 1;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
177 continue;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
178 }
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
179
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
180 if (res != from)
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
181 ga_concat_len(gap, from, res - from);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
182 from = res + 1;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
183
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
184 switch (c)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
185 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
186 case 0x08:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
187 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
188 case 0x09:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
189 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
190 case 0x0a:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
191 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
192 case 0x0c:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
193 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
194 case 0x0d:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
195 ga_append(gap, '\\'); ga_append(gap, 'r'); break;
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
196 case 0x22: // "
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
197 case 0x5c: // backslash
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
198 ga_append(gap, '\\');
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
199 ga_append(gap, c);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
200 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
201 default:
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
202 vim_snprintf((char *)numbuf, NUMBUFLEN, "\\u%04lx",
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
203 (long)c);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
204 ga_concat(gap, numbuf);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
205 }
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
206
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
207 res += 1;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
208 }
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
209 else
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
210 {
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
211 int l = utf_ptr2len(res);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
212
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
213 if (l > 1)
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
214 {
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
215 res += l;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
216 continue;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
217 }
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
218
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
219 // Invalid utf-8 sequence, replace it with the Unicode replacement
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
220 // character U+FFFD.
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
221 if (res != from)
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
222 ga_concat_len(gap, from, res - from);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
223 from = res + 1;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
224
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
225 numbuf[utf_char2bytes(0xFFFD, numbuf)] = NUL;
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
226 ga_concat(gap, numbuf);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
227
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
228 res += l;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
229 }
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
230 }
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
231
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
232 if (res != from)
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
233 ga_concat_len(gap, from, res - from);
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
234
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
235 ga_append(gap, '"');
15597
536dd2bc5ac9 patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 15555
diff changeset
236 #if defined(USE_ICONV)
28339
0860b12c5788 patch 8.2.4695: JSON encoding could be faster
Bram Moolenaar <Bram@vim.org>
parents: 28315
diff changeset
237 vim_free(converted);
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
238 #endif
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
239 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
240
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
241 /*
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
242 * 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
243 * 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
244 * underscore.
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
245 */
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
246 static int
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
247 is_simple_key(char_u *key)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
248 {
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
249 char_u *p;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
250
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
251 if (!ASCII_ISALPHA(*key))
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
252 return FALSE;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
253 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
254 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
255 return FALSE;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
256 return TRUE;
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
257 }
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
258
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
259 /*
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
260 * Encode "val" into "gap".
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
261 * Return FAIL or OK.
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
262 */
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
263 static int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
264 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
265 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
266 char_u numbuf[NUMBUFLEN];
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
267 char_u *res;
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
268 blob_T *b;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
269 list_T *l;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
270 dict_T *d;
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
271 int i;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
272
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
273 switch (val->v_type)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
274 {
19102
ba9f50bfda83 patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
275 case VAR_BOOL:
19515
020056c2fd39 patch 8.2.0315: build failure on HP-UX system
Bram Moolenaar <Bram@vim.org>
parents: 19477
diff changeset
276 switch ((long)val->vval.v_number)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
277 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
278 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
279 case VVAL_TRUE: ga_concat(gap, (char_u *)"true"); break;
19102
ba9f50bfda83 patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
280 }
ba9f50bfda83 patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
281 break;
ba9f50bfda83 patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
282
ba9f50bfda83 patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
283 case VAR_SPECIAL:
19515
020056c2fd39 patch 8.2.0315: build failure on HP-UX system
Bram Moolenaar <Bram@vim.org>
parents: 19477
diff changeset
284 switch ((long)val->vval.v_number)
19102
ba9f50bfda83 patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
285 {
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
286 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
287 && (options & JSON_NO_NONE) == 0)
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
288 // empty item
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
289 break;
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
290 // FALLTHROUGH
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
291 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
292 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
293 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
294
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
295 case VAR_NUMBER:
9389
32e34e574716 commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents: 8989
diff changeset
296 vim_snprintf((char *)numbuf, NUMBUFLEN, "%lld",
19477
2bb0e80fcd32 patch 8.2.0296: mixing up "long long" and __int64 may cause problems
Bram Moolenaar <Bram@vim.org>
parents: 19201
diff changeset
297 (varnumber_T)val->vval.v_number);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
298 ga_concat(gap, numbuf);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
299 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
300
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
301 case VAR_STRING:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
302 res = val->vval.v_string;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
303 write_string(gap, res);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
304 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
305
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
306 case VAR_FUNC:
8538
c337c813c64d commit https://github.com/vim/vim/commit/1735bc988c546cc962c5f94792815b4d7cb79710
Christian Brabandt <cb@256bit.org>
parents: 8414
diff changeset
307 case VAR_PARTIAL:
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
308 case VAR_JOB:
8041
c6443e78cf2d commit https://github.com/vim/vim/commit/7707344ddec9069b495b2a5ed41f2104466fc88b
Christian Brabandt <cb@256bit.org>
parents: 7967
diff changeset
309 case VAR_CHANNEL:
24606
a4fda40e0bb9 patch 8.2.2842: Vim9: skip argument to searchpair() is not compiled
Bram Moolenaar <Bram@vim.org>
parents: 23891
diff changeset
310 case VAR_INSTR:
31396
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30310
diff changeset
311 case VAR_CLASS:
307f68a41b03 patch 9.0.1031: Vim9 class is not implemented yet
Bram Moolenaar <Bram@vim.org>
parents: 30310
diff changeset
312 case VAR_OBJECT:
33678
7d9d2404a3d4 patch 9.0.2076: Vim9: No support for type aliases
Christian Brabandt <cb@256bit.org>
parents: 32996
diff changeset
313 case VAR_TYPEALIAS:
23891
650070143c56 patch 8.2.2488: json_encode() gives generic argument error
Bram Moolenaar <Bram@vim.org>
parents: 21461
diff changeset
314 semsg(_(e_cannot_json_encode_str), vartype_name(val->v_type));
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
315 return FAIL;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
316
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
317 case VAR_BLOB:
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
318 b = val->vval.v_blob;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
319 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
320 ga_concat(gap, (char_u *)"[]");
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
321 else
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
322 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
323 ga_append(gap, '[');
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
324 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
325 {
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
326 if (i > 0)
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
327 ga_concat(gap, (char_u *)",");
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
328 vim_snprintf((char *)numbuf, NUMBUFLEN, "%d",
27426
41e0dcf38521 patch 8.2.4241: some type casts are redundant
Bram Moolenaar <Bram@vim.org>
parents: 26966
diff changeset
329 blob_get(b, i));
15454
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
330 ga_concat(gap, numbuf);
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
331 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
332 ga_append(gap, ']');
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
333 }
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
334 break;
1d2b5c016f17 patch 8.1.0735: cannot handle binary data
Bram Moolenaar <Bram@vim.org>
parents: 15446
diff changeset
335
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
336 case VAR_LIST:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
337 l = val->vval.v_list;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
338 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
339 ga_concat(gap, (char_u *)"[]");
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
340 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
341 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
342 if (l->lv_copyID == copyID)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
343 ga_concat(gap, (char_u *)"[]");
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
344 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
345 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
346 listitem_T *li;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
347
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
348 l->lv_copyID = copyID;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
349 ga_append(gap, '[');
20392
4c317d8c1051 patch 8.2.0751: Vim9: performance can be improved
Bram Moolenaar <Bram@vim.org>
parents: 19922
diff changeset
350 CHECK_LIST_MATERIALIZE(l);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
351 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
352 {
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
353 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
354 options & JSON_JS) == FAIL)
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
355 return FAIL;
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
356 if ((options & JSON_JS)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
357 && li->li_next == NULL
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
358 && 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
359 && li->li_tv.vval.v_number == VVAL_NONE)
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
360 // add an extra comma if the last item is v:none
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
361 ga_append(gap, ',');
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
362 li = li->li_next;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
363 if (li != NULL)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
364 ga_append(gap, ',');
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
365 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
366 ga_append(gap, ']');
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
367 l->lv_copyID = 0;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
368 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
369 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
370 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
371
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
372 case VAR_DICT:
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
373 d = val->vval.v_dict;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
374 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
375 ga_concat(gap, (char_u *)"{}");
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
376 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
377 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
378 if (d->dv_copyID == copyID)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
379 ga_concat(gap, (char_u *)"{}");
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
380 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
381 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
382 int first = TRUE;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
383 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
384 hashitem_T *hi;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
385
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
386 d->dv_copyID = copyID;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
387 ga_append(gap, '{');
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
388
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
389 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
390 ++hi)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
391 if (!HASHITEM_EMPTY(hi))
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
392 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
393 --todo;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
394 if (first)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
395 first = FALSE;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
396 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
397 ga_append(gap, ',');
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
398 if ((options & JSON_JS)
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
399 && is_simple_key(hi->hi_key))
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
400 ga_concat(gap, hi->hi_key);
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
401 else
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
402 write_string(gap, hi->hi_key);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
403 ga_append(gap, ':');
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
404 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
405 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
406 return FAIL;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
407 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
408 ga_append(gap, '}');
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
409 d->dv_copyID = 0;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
410 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
411 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
412 break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
413
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
414 case VAR_FLOAT:
30310
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
415 #if defined(HAVE_MATH_H)
8275
ff900e499f79 commit https://github.com/vim/vim/commit/7ce686c990ea8c490d16be7f1c6bd95eb48816f9
Christian Brabandt <cb@256bit.org>
parents: 8230
diff changeset
416 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
417 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
418 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
419 {
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
420 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
421 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
422 else
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
423 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
424 }
8228
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
425 else
30310
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
426 #endif
8228
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
427 {
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
428 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g",
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
429 val->vval.v_float);
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
430 ga_concat(gap, numbuf);
a0e552c51c34 commit https://github.com/vim/vim/commit/f1b6ac72293e658bb6e68c5cfd926c405b1b6f34
Christian Brabandt <cb@256bit.org>
parents: 8146
diff changeset
431 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
432 break;
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
433 case VAR_UNKNOWN:
19922
1f42c49c3d29 patch 8.2.0517: Vim9: cannot separate "func" and "func(): void"
Bram Moolenaar <Bram@vim.org>
parents: 19554
diff changeset
434 case VAR_ANY:
19181
94eda51ba9ba patch 8.2.0149: maintaining a Vim9 branch separately is more work
Bram Moolenaar <Bram@vim.org>
parents: 19102
diff changeset
435 case VAR_VOID:
19554
b38d73f36467 patch 8.2.0334: abort called when using test_void()
Bram Moolenaar <Bram@vim.org>
parents: 19515
diff changeset
436 internal_error_no_abort("json_encode_item()");
7965
646d5148fee2 commit https://github.com/vim/vim/commit/55fab439a6f3bba6dbe780ac034b84d5822a1a96
Christian Brabandt <cb@256bit.org>
parents: 7947
diff changeset
437 return FAIL;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
438 }
7736
f2ddad8cbce7 commit https://github.com/vim/vim/commit/fcaaae6b3fdbf3421a1ff95a25ae16d82381c39a
Christian Brabandt <cb@256bit.org>
parents: 7732
diff changeset
439 return OK;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
440 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
441
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
442 /*
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
443 * 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
444 * callback to get more.
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
445 */
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
446 static void
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
447 fill_numbuflen(js_read_T *reader)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
448 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
449 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
450 - reader->js_used < NUMBUFLEN)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
451 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
452 if (reader->js_fill(reader))
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
453 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
454 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
455 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
456
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
457 /*
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
458 * 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
459 * space.
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
460 * Also tops up readahead when needed.
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
461 */
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
462 static void
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
463 json_skip_white(js_read_T *reader)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
464 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
465 int c;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
466
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
467 for (;;)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
468 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
469 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
470 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
471 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
472 if (reader->js_fill(reader))
8755
7038ec89d1fd commit https://github.com/vim/vim/commit/46c00a6565b8f1f4b7b1041d03eaceaf6ffc4aee
Christian Brabandt <cb@256bit.org>
parents: 8655
diff changeset
473 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
474 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
475 continue;
7038ec89d1fd commit https://github.com/vim/vim/commit/46c00a6565b8f1f4b7b1041d03eaceaf6ffc4aee
Christian Brabandt <cb@256bit.org>
parents: 8655
diff changeset
476 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
477 }
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
478 if (c == NUL || c > ' ')
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
479 break;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
480 ++reader->js_used;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
481 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
482 fill_numbuflen(reader);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
483 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
484
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
485 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
486 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
487 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
488 garray_T ga;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
489 int len;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
490 char_u *p;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
491 int c;
9389
32e34e574716 commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents: 8989
diff changeset
492 varnumber_T nr;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
493
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
494 if (res != NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
495 ga_init2(&ga, 1, 200);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
496
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
497 p = reader->js_buf + reader->js_used + 1; // skip over " or '
10563
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
498 while (*p != quote)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
499 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
500 // The JSON is always expected to be utf-8, thus use utf functions
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
501 // 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
502 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
503 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
504 // Not enough bytes to make a character or end of the string. Get
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
505 // more if possible.
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
506 if (reader->js_fill == NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
507 break;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
508 len = (int)(reader->js_end - p);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
509 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
510 if (!reader->js_fill(reader))
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
511 break; // didn't get more
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
512 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
513 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
514 continue;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
515 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
516
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
517 if (*p == '\\')
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
518 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
519 c = -1;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
520 switch (p[1])
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
521 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
522 case '\\': c = '\\'; break;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
523 case '"': c = '"'; break;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
524 case 'b': c = BS; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
525 case 't': c = TAB; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
526 case 'n': c = NL; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
527 case 'f': c = FF; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
528 case 'r': c = CAR; break;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
529 case 'u':
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
530 if (reader->js_fill != NULL
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
531 && (int)(reader->js_end - p) < NUMBUFLEN)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
532 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
533 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
534 if (reader->js_fill(reader))
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
535 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
536 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
537 reader->js_end = reader->js_buf
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
538 + STRLEN(reader->js_buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
539 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
540 }
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
541 nr = 0;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
542 len = 0;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
543 vim_str2nr(p + 2, NULL, &len,
32098
39f4126d2a0d patch 9.0.1380: CTRL-X on 2**64 subtracts two
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
544 STR2NR_HEX + STR2NR_FORCE, &nr, NULL, 4, TRUE, NULL);
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
545 if (len == 0)
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
546 {
16815
6fdb0ae0cac3 patch 8.1.1409: Coverity warns for using uninitialized memory
Bram Moolenaar <Bram@vim.org>
parents: 16706
diff changeset
547 if (res != NULL)
6fdb0ae0cac3 patch 8.1.1409: Coverity warns for using uninitialized memory
Bram Moolenaar <Bram@vim.org>
parents: 16706
diff changeset
548 ga_clear(&ga);
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
549 return FAIL;
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
550 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
551 p += len + 2;
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
552 if (0xd800 <= nr && nr <= 0xdfff
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
553 && (int)(reader->js_end - p) >= 6
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
554 && *p == '\\' && *(p+1) == 'u')
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
555 {
9389
32e34e574716 commit https://github.com/vim/vim/commit/22fcfad29276bd5f317faf516637dcd491b96a12
Christian Brabandt <cb@256bit.org>
parents: 8989
diff changeset
556 varnumber_T nr2 = 0;
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
557
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
558 // decode surrogate pair: \ud812\u3456
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
559 len = 0;
32098
39f4126d2a0d patch 9.0.1380: CTRL-X on 2**64 subtracts two
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
560 vim_str2nr(p + 2, NULL, &len, STR2NR_HEX + STR2NR_FORCE,
39f4126d2a0d patch 9.0.1380: CTRL-X on 2**64 subtracts two
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
561 &nr2, NULL, 4, TRUE, NULL);
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
562 if (len == 0)
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
563 {
16815
6fdb0ae0cac3 patch 8.1.1409: Coverity warns for using uninitialized memory
Bram Moolenaar <Bram@vim.org>
parents: 16706
diff changeset
564 if (res != NULL)
6fdb0ae0cac3 patch 8.1.1409: Coverity warns for using uninitialized memory
Bram Moolenaar <Bram@vim.org>
parents: 16706
diff changeset
565 ga_clear(&ga);
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
566 return FAIL;
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
567 }
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
568 if (0xdc00 <= nr2 && nr2 <= 0xdfff)
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
569 {
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
570 p += len + 2;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
571 nr = (((nr - 0xd800) << 10) |
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
572 ((nr2 - 0xdc00) & 0x3ff)) + 0x10000;
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
573 }
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
574 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
575 if (res != NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
576 {
8989
e600e696c0a1 commit https://github.com/vim/vim/commit/dc633cf82758f67f656cda7fa8ccc30414ee53f8
Christian Brabandt <cb@256bit.org>
parents: 8755
diff changeset
577 char_u buf[NUMBUFLEN];
16815
6fdb0ae0cac3 patch 8.1.1409: Coverity warns for using uninitialized memory
Bram Moolenaar <Bram@vim.org>
parents: 16706
diff changeset
578
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
579 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
580 ga_concat(&ga, buf);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
581 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
582 break;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
583 default:
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
584 // not a special char, skip over backslash
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
585 ++p;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
586 continue;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
587 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
588 if (c > 0)
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
589 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
590 p += 2;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
591 if (res != NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
592 ga_append(&ga, c);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
593 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
594 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
595 else
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
596 {
8283
b8a56d4d83e0 commit https://github.com/vim/vim/commit/b6ff81188d27fae774d9ad2dfb498f596d697d4b
Christian Brabandt <cb@256bit.org>
parents: 8275
diff changeset
597 len = utf_ptr2len(p);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
598 if (res != NULL)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
599 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
600 if (ga_grow(&ga, len) == FAIL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
601 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
602 ga_clear(&ga);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
603 return FAIL;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
604 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
605 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
606 ga.ga_len += len;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
607 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
608 p += len;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
609 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
610 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
611
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
612 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
613 if (*p == quote)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
614 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
615 ++reader->js_used;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
616 if (res != NULL)
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
617 {
8308
c1f29c1a968a commit https://github.com/vim/vim/commit/80e78847395b5c8ada7861674774d81bd0a42789
Christian Brabandt <cb@256bit.org>
parents: 8306
diff changeset
618 ga_append(&ga, NUL);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
619 res->v_type = VAR_STRING;
15597
536dd2bc5ac9 patch 8.1.0806: too many #ifdefs
Bram Moolenaar <Bram@vim.org>
parents: 15555
diff changeset
620 #if defined(USE_ICONV)
8306
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
621 if (!enc_utf8)
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
622 {
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
623 vimconv_T conv;
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
624
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
625 // Convert the utf-8 string to 'encoding'.
8306
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
626 conv.vc_type = CONV_NONE;
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
627 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
628 if (conv.vc_type != CONV_NONE)
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
629 {
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
630 res->vval.v_string =
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
631 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
632 vim_free(ga.ga_data);
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
633 }
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
634 convert_setup(&conv, NULL, NULL);
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
635 }
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
636 else
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
637 #endif
7fcf3f6020c1 commit https://github.com/vim/vim/commit/b36287283781e09d8874157b9d9bdc5243a2f319
Christian Brabandt <cb@256bit.org>
parents: 8304
diff changeset
638 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
639 }
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
640 return OK;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
641 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
642 if (res != NULL)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
643 {
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
644 res->v_type = VAR_SPECIAL;
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
645 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
646 ga_clear(&ga);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
647 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
648 return MAYBE;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
649 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
650
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
651 typedef enum {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
652 JSON_ARRAY, // parsing items in an array
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
653 JSON_OBJECT_KEY, // parsing key of an object
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
654 JSON_OBJECT // parsing item in an object, after the key
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
655 } 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
656
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
657 typedef struct {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
658 json_decode_T jd_type;
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
659 typval_T jd_tv; // the list or dict
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
660 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
661 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
662 } 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
663
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
664 /*
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
665 * 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
666 * 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
667 *
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
668 * 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
669 * Return MAYBE for an incomplete message.
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
670 */
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
671 static int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
672 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
673 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
674 char_u *p;
21275
c14e628cd4bb patch 8.2.1188: memory leak with invalid json input
Bram Moolenaar <Bram@vim.org>
parents: 20830
diff changeset
675 int i;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
676 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
677 int retval;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
678 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
679 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
680 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
681 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
682 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
683
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
684 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
685 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
686 init_tv(&item);
10561
e025378406d1 patch 8.0.0170: crash in channel test
Christian Brabandt <cb@256bit.org>
parents: 10559
diff changeset
687 if (res != NULL)
24822
5f8dd7b3ae41 patch 8.2.2949: tests failing because no error for float to string conversion
Bram Moolenaar <Bram@vim.org>
parents: 24665
diff changeset
688 init_tv(res);
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
689
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
690 fill_numbuflen(reader);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
691 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
692 for (;;)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
693 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
694 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
695 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
696 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
697 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
698 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
699 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
700 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
701 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
702 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
703 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
704 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
705 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
706 || 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
707 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
708 // Check for end of object or array.
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
709 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
710 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
711 ++reader->js_used; // consume the ']' or '}'
10559
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 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
714 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
715 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
716 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
717 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
718 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
719 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
720 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
721 }
10553
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
722 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
723 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
724
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
725 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
726 && (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
727 && 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
728 && reader->js_buf[reader->js_used] != '\''
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
729 && reader->js_buf[reader->js_used] != '['
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
730 && 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
731 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
732 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
733
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
734 // accept an object key that is not in quotes
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
735 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
736 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
737 ++p;
10652
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
738 if (cur_item != NULL)
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
739 {
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
740 cur_item->v_type = VAR_STRING;
20830
9064044fd4f6 patch 8.2.0967: unnecessary type casts for vim_strnsave()
Bram Moolenaar <Bram@vim.org>
parents: 20492
diff changeset
741 cur_item->vval.v_string = vim_strnsave(key, p - key);
10652
bb4360d64eb2 patch 8.0.0216: decoding js style json may fail
Christian Brabandt <cb@256bit.org>
parents: 10581
diff changeset
742 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
743 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
744 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
745 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
746 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
747 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
748 switch (*p)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
749 {
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
750 case '[': // start of array
13456
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
751 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
752 {
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
753 retval = FAIL;
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
754 break;
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
755 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
756 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
757 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
758 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
759 break;
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 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
762 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
763 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
764 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
765 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
766 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
767 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
768
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
769 ++reader->js_used; // consume the '['
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
770 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
771 + 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
772 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
773 ++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
774 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
775 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
776 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
777 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
778 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
779 continue;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
780
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
781 case '{': // start of object
13456
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
782 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
783 {
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
784 retval = FAIL;
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
785 break;
7495e3ee1a69 patch 8.0.1602: crash in parsing JSON
Christian Brabandt <cb@256bit.org>
parents: 13031
diff changeset
786 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
787 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
788 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
789 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
790 break;
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 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
793 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
794 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
795 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
796 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
797 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
798 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
799
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
800 ++reader->js_used; // consume the '{'
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
801 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
802 + 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
803 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
804 ++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
805 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
806 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
807 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
808 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
809 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
810 continue;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
811
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
812 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
813 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
814 break;
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
815
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
816 case '\'':
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
817 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
818 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
819 else
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
820 {
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
821 semsg(_(e_json_decode_error_at_str), p);
10563
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
822 retval = FAIL;
bac9cec298ed patch 8.0.0171: JS style JSON does not support single quotes
Christian Brabandt <cb@256bit.org>
parents: 10561
diff changeset
823 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
824 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
825
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
826 case ',': // comma: empty item
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
827 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
828 {
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
829 semsg(_(e_json_decode_error_at_str), p);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
830 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
831 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
832 }
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
833 // FALLTHROUGH
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
834 case NUL: // empty
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
835 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
836 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
837 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
838 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
839 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
840 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
841 break;
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 default:
19193
9f98957582d6 patch 8.2.0155: warnings from MinGW compiler; tests fail without +float
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
844 if (VIM_ISDIGIT(*p) || (*p == '-'
9f98957582d6 patch 8.2.0155: warnings from MinGW compiler; tests fail without +float
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
845 && (VIM_ISDIGIT(p[1]) || p[1] == NUL)))
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
846 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
847 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
848
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
849 if (*sp == '-')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
850 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
851 ++sp;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
852 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
853 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
854 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
855 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
856 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
857 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
858 {
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
859 semsg(_(e_json_decode_error_at_str), p);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
860 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
861 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
862 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
863 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
864 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
865 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
866 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
867 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
868 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
869 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
870
25557
763ea8f075db patch 8.2.3315: cannot use single quote in a float number for readability
Bram Moolenaar <Bram@vim.org>
parents: 25384
diff changeset
871 len = string2float(p, &f, FALSE);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
872 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
873 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
874 {
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->v_type = VAR_FLOAT;
25557
763ea8f075db patch 8.2.3315: cannot use single quote in a float number for readability
Bram Moolenaar <Bram@vim.org>
parents: 25384
diff changeset
876 len = string2float(p, &cur_item->vval.v_float,
763ea8f075db patch 8.2.3315: cannot use single quote in a float number for readability
Bram Moolenaar <Bram@vim.org>
parents: 25384
diff changeset
877 FALSE);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
878 }
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 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
881 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
882 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
883
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
884 vim_str2nr(reader->js_buf + reader->js_used,
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
885 NULL, &len, 0, // what
32098
39f4126d2a0d patch 9.0.1380: CTRL-X on 2**64 subtracts two
Bram Moolenaar <Bram@vim.org>
parents: 31396
diff changeset
886 &nr, NULL, 0, TRUE, NULL);
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
887 if (len == 0)
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
888 {
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
889 semsg(_(e_json_decode_error_at_str), p);
16706
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
890 retval = FAIL;
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
891 goto theend;
77bcb5055fec patch 8.1.1355: obvious mistakes are accepted as valid expressions
Bram Moolenaar <Bram@vim.org>
parents: 16306
diff changeset
892 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
893 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
894 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
895 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
896 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
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 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
900 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
901 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
902 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
903 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
904 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
905 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
906 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
907 {
19102
ba9f50bfda83 patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
908 cur_item->v_type = VAR_BOOL;
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
909 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
910 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
911 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
912 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
913 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
914 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
915 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
916 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
917 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
918 {
19102
ba9f50bfda83 patch 8.2.0111: VAR_SPECIAL is also used for booleans
Bram Moolenaar <Bram@vim.org>
parents: 18800
diff changeset
919 cur_item->v_type = VAR_BOOL;
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
920 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
921 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
922 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
923 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
924 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
925 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
926 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
927 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
928 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
929 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
930 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
931 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
932 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
933 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
934 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
935 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
936 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
937 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
938 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
939 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
940 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
941 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
942 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
943 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
944 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
945 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
946 }
15446
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
947 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
948 {
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
949 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
950 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
951 {
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
952 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
953 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
954 }
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
955 retval = OK;
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
956 break;
8ac454818352 patch 8.1.0731: JS encoding does not handle negative infinity
Bram Moolenaar <Bram@vim.org>
parents: 15211
diff changeset
957 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
958 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
959 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
960 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
961 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
962 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
963 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
964 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
965 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
966 retval = OK;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
967 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
968 }
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
969 // check for truncated name
19193
9f98957582d6 patch 8.2.0155: warnings from MinGW compiler; tests fail without +float
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
970 len = (int)(reader->js_end
9f98957582d6 patch 8.2.0155: warnings from MinGW compiler; tests fail without +float
Bram Moolenaar <Bram@vim.org>
parents: 19181
diff changeset
971 - (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
972 if (
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
973 (len < 5 && STRNICMP((char *)p, "false", len) == 0)
30310
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
974 || (len < 9
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
975 && STRNICMP((char *)p, "-Infinity", len) == 0)
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
976 || (len < 8
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
977 && 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
978 || (len < 3 && STRNICMP((char *)p, "NaN", len) == 0)
30310
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
979 || (len < 4
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
980 && (STRNICMP((char *)p, "true", len) == 0
029c59bf78f1 patch 9.0.0491: no good reason to build without the float feature
Bram Moolenaar <Bram@vim.org>
parents: 28339
diff changeset
981 || STRNICMP((char *)p, "null", 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
982
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
983 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
984 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
985 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
986 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
987 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
988
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
989 // We are finished when retval is FAIL or MAYBE and when at the
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
990 // toplevel.
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
991 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
992 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
993 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
994 goto theend;
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 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
997 && 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
998 {
24822
5f8dd7b3ae41 patch 8.2.2949: tests failing because no error for float to string conversion
Bram Moolenaar <Bram@vim.org>
parents: 24665
diff changeset
999 if (cur_item->v_type == VAR_FLOAT)
5f8dd7b3ae41 patch 8.2.2949: tests failing because no error for float to string conversion
Bram Moolenaar <Bram@vim.org>
parents: 24665
diff changeset
1000 {
5f8dd7b3ae41 patch 8.2.2949: tests failing because no error for float to string conversion
Bram Moolenaar <Bram@vim.org>
parents: 24665
diff changeset
1001 // cannot use a float as a key
26883
7f150a4936f2 patch 8.2.3970: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26865
diff changeset
1002 emsg(_(e_using_float_as_string));
24822
5f8dd7b3ae41 patch 8.2.2949: tests failing because no error for float to string conversion
Bram Moolenaar <Bram@vim.org>
parents: 24665
diff changeset
1003 retval = FAIL;
5f8dd7b3ae41 patch 8.2.2949: tests failing because no error for float to string conversion
Bram Moolenaar <Bram@vim.org>
parents: 24665
diff changeset
1004 goto theend;
5f8dd7b3ae41 patch 8.2.2949: tests failing because no error for float to string conversion
Bram Moolenaar <Bram@vim.org>
parents: 24665
diff changeset
1005 }
15211
de63593896b3 patch 8.1.0615: get_tv function names are not consistent
Bram Moolenaar <Bram@vim.org>
parents: 14862
diff changeset
1006 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
1007 if (top_item->jd_key == NULL)
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1008 {
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 25557
diff changeset
1009 emsg(_(e_invalid_argument));
10559
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;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1012 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1013 }
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
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1016 item_end:
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1017 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
1018 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
1019 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1020 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
1021 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
1022 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1023 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
1024
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1025 if (li == NULL)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1026 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1027 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
1028 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1029 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1030 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1031 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
1032 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
1033 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1034 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
1035 cur_item = &item;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
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 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
1038 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
1039 if (*p == ',')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1040 ++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
1041 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
1042 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1043 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
1044 retval = MAYBE;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1045 else
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1046 {
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
1047 semsg(_(e_json_decode_error_at_str), p);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1048 retval = FAIL;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1049 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1050 goto theend;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1051 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1052 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1053
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1054 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
1055 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
1056 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
1057 if (*p != ':')
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1058 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1059 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
1060 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
1061 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
1062 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1063 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1064 {
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
1065 semsg(_(e_json_decode_error_at_str), p);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1066 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1067 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1068 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1069 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1070 ++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
1071 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
1072 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
1073 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
1074 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
1075 break;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1076
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1077 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
1078 if (cur_item != NULL
28315
62cc3b60493b patch 8.2.4683: verbose check with dict_find() to see if a key is present
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
1079 && dict_has_key(top_item->jd_tv.vval.v_dict,
62cc3b60493b patch 8.2.4683: verbose check with dict_find() to see if a key is present
Bram Moolenaar <Bram@vim.org>
parents: 28244
diff changeset
1080 (char *)top_item->jd_key))
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1081 {
26966
ac75c145f0a9 patch 8.2.4012: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26915
diff changeset
1082 semsg(_(e_duplicate_key_in_json_str), top_item->jd_key);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1083 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
1084 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1085 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1086 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1087
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1088 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
1089 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1090 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
1091
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1092 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
1093 if (di == NULL)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1094 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1095 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
1096 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1097 goto theend;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1098 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1099 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
1100 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
1101 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
1102 {
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1103 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
1104 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1105 goto theend;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1106 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1107 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1108
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1109 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
1110 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
1111 if (*p == ',')
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1112 ++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
1113 else if (*p != '}')
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1114 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1115 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
1116 retval = MAYBE;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1117 else
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1118 {
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
1119 semsg(_(e_json_decode_error_at_str), p);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1120 retval = FAIL;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1121 }
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1122 goto theend;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1123 }
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1124 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
1125 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
1126 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
1127 break;
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1128 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1129 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1130
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1131 // Get here when parsing failed.
10299
7b2f95633e28 commit https://github.com/vim/vim/commit/7756e7465d627ff9cd01e59625484a8c302ef853
Christian Brabandt <cb@256bit.org>
parents: 10042
diff changeset
1132 if (res != NULL)
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1133 {
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1134 clear_tv(res);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1135 res->v_type = VAR_SPECIAL;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1136 res->vval.v_number = VVAL_NONE;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1137 }
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
1138 semsg(_(e_json_decode_error_at_str), p);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1139
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1140 theend:
21275
c14e628cd4bb patch 8.2.1188: memory leak with invalid json input
Bram Moolenaar <Bram@vim.org>
parents: 20830
diff changeset
1141 for (i = 0; i < stack.ga_len; i++)
c14e628cd4bb patch 8.2.1188: memory leak with invalid json input
Bram Moolenaar <Bram@vim.org>
parents: 20830
diff changeset
1142 clear_tv(&(((json_dec_item_T *)stack.ga_data) + i)->jd_key_tv);
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1143 ga_clear(&stack);
21275
c14e628cd4bb patch 8.2.1188: memory leak with invalid json input
Bram Moolenaar <Bram@vim.org>
parents: 20830
diff changeset
1144
10559
4d8be28b5913 patch 8.0.0169: json_decode() may run out of stack space
Christian Brabandt <cb@256bit.org>
parents: 10553
diff changeset
1145 return retval;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1146 }
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1147
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1148 /*
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1149 * 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
1150 * "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
1151 * 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
1152 */
17789
0f7ae8010787 patch 8.1.1891: functions used in one file are global
Bram Moolenaar <Bram@vim.org>
parents: 17474
diff changeset
1153 static int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1154 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
1155 {
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1156 int ret;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1157
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1158 // 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
1159 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
1160 json_skip_white(reader);
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1161 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
1162 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
1163 {
f83b6a0b6148 patch 8.0.0166: JSON with a duplicate key gives an internal error
Christian Brabandt <cb@256bit.org>
parents: 10359
diff changeset
1164 if (ret == MAYBE)
26915
3631d2deb36c patch 8.2.3986: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26883
diff changeset
1165 semsg(_(e_json_decode_error_at_str), reader->js_buf);
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1166 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
1167 }
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1168 json_skip_white(reader);
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1169 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
1170 {
26883
7f150a4936f2 patch 8.2.3970: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 26865
diff changeset
1171 semsg(_(e_trailing_characters_str), reader->js_buf + reader->js_used);
7868
17e6ff1a74f1 commit https://github.com/vim/vim/commit/19d2f1589850d7db1ce77efec052929246f156e2
Christian Brabandt <cb@256bit.org>
parents: 7864
diff changeset
1172 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
1173 }
7868
17e6ff1a74f1 commit https://github.com/vim/vim/commit/19d2f1589850d7db1ce77efec052929246f156e2
Christian Brabandt <cb@256bit.org>
parents: 7864
diff changeset
1174 return OK;
7712
bce3b5ddb393 commit https://github.com/vim/vim/commit/520e1e41f35b063ede63b41738c82d6636e78c34
Christian Brabandt <cb@256bit.org>
parents:
diff changeset
1175 }
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1176
15555
d89c5b339c2a patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents: 15517
diff changeset
1177 #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
1178 /*
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1179 * 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
1180 * "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
1181 * Return FAIL for a decoding error.
1eb302bf2475 commit https://github.com/vim/vim/commit/ba61ac0d61f46de7d29c64bb0de6d25c2e378be0
Christian Brabandt <cb@256bit.org>
parents: 8538
diff changeset
1182 * Return MAYBE for an incomplete message.
1eb302bf2475 commit https://github.com/vim/vim/commit/ba61ac0d61f46de7d29c64bb0de6d25c2e378be0
Christian Brabandt <cb@256bit.org>
parents: 8538
diff changeset
1183 * Consumes the message anyway.
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1184 */
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1185 int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1186 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
1187 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1188 int ret;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1189
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1190 // 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
1191 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
1192 json_skip_white(reader);
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1193 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
1194 json_skip_white(reader);
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1195
8655
1eb302bf2475 commit https://github.com/vim/vim/commit/ba61ac0d61f46de7d29c64bb0de6d25c2e378be0
Christian Brabandt <cb@256bit.org>
parents: 8538
diff changeset
1196 return ret;
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1197 }
15555
d89c5b339c2a patch 8.1.0785: depending on the configuration some functions are unused
Bram Moolenaar <Bram@vim.org>
parents: 15517
diff changeset
1198 #endif
7883
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1199
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1200 /*
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1201 * 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
1202 * "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
1203 * 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
1204 * 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
1205 * 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
1206 * 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
1207 * 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
1208 * Does not advance the reader.
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1209 */
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1210 int
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1211 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
1212 {
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1213 int used_save = reader->js_used;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1214 int ret;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1215
18800
f41b55f9357c patch 8.1.2388: using old C style comments
Bram Moolenaar <Bram@vim.org>
parents: 17789
diff changeset
1216 // 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
1217 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
1218 json_skip_white(reader);
7967
45ea5ebf3a98 commit https://github.com/vim/vim/commit/595e64e259faefb330866852e1b9f6168544572a
Christian Brabandt <cb@256bit.org>
parents: 7965
diff changeset
1219 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
1220 reader->js_used = used_save;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1221 return ret;
98a96e0ca73b commit https://github.com/vim/vim/commit/56ead341a75e1a0395eee94a3280c67e2278a57e
Christian Brabandt <cb@256bit.org>
parents: 7868
diff changeset
1222 }
17472
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1223
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1224 /*
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1225 * "js_decode()" function
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1226 */
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1227 void
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1228 f_js_decode(typval_T *argvars, typval_T *rettv)
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1229 {
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1230 js_read_T reader;
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1231
25384
e8e2c4d33b9b patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents: 24822
diff changeset
1232 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
e8e2c4d33b9b patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents: 24822
diff changeset
1233 return;
e8e2c4d33b9b patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents: 24822
diff changeset
1234
17472
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1235 reader.js_buf = tv_get_string(&argvars[0]);
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1236 reader.js_fill = NULL;
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1237 reader.js_used = 0;
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1238 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
26865
bce848ec8b1b patch 8.2.3961: error messages are spread out
Bram Moolenaar <Bram@vim.org>
parents: 25557
diff changeset
1239 emsg(_(e_invalid_argument));
17472
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1240 }
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1241
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1242 /*
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1243 * "js_encode()" function
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1244 */
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1245 void
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1246 f_js_encode(typval_T *argvars, typval_T *rettv)
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1247 {
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1248 rettv->v_type = VAR_STRING;
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1249 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1250 }
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1251
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1252 /*
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1253 * "json_decode()" function
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1254 */
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1255 void
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1256 f_json_decode(typval_T *argvars, typval_T *rettv)
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1257 {
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1258 js_read_T reader;
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1259
25384
e8e2c4d33b9b patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents: 24822
diff changeset
1260 if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL)
e8e2c4d33b9b patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents: 24822
diff changeset
1261 return;
e8e2c4d33b9b patch 8.2.3229: Vim9: runtime and compile time type checks are not the same
Bram Moolenaar <Bram@vim.org>
parents: 24822
diff changeset
1262
17472
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1263 reader.js_buf = tv_get_string(&argvars[0]);
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1264 reader.js_fill = NULL;
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1265 reader.js_used = 0;
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1266 json_decode_all(&reader, rettv, 0);
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1267 }
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1268
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1269 /*
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1270 * "json_encode()" function
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1271 */
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1272 void
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1273 f_json_encode(typval_T *argvars, typval_T *rettv)
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1274 {
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1275 rettv->v_type = VAR_STRING;
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1276 rettv->vval.v_string = json_encode(&argvars[0], 0);
dfd87ef822aa patch 8.1.1734: the evalfunc.c file is too big
Bram Moolenaar <Bram@vim.org>
parents: 16815
diff changeset
1277 }
17474
f8c197962c1e patch 8.1.1735: can't build with tiny features
Bram Moolenaar <Bram@vim.org>
parents: 17472
diff changeset
1278 #endif