… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002 |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
17 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
18 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
19 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_JSON_RPC 0x00000040 |
|
|
16 | #define F_ALLOW_NONREF 0x00000080 |
20 | #define F_ALLOW_NONREF 0x00000080UL |
|
|
21 | #define F_SHRINK 0x00000100UL |
|
|
22 | #define F_MAXDEPTH 0xf8000000UL |
|
|
23 | #define S_MAXDEPTH 27 |
|
|
24 | |
|
|
25 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
26 | |
|
|
27 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
28 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
29 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
32 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
34 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
35 | |
|
|
36 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
37 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
22 | |
38 | |
23 | #define SB do { |
39 | #define SB do { |
24 | #define SE } while (0) |
40 | #define SE } while (0) |
25 | |
41 | |
26 | static HV *json_stash; |
42 | static HV *json_stash; // JSON::XS:: |
|
|
43 | |
|
|
44 | ///////////////////////////////////////////////////////////////////////////// |
|
|
45 | // utility functions |
|
|
46 | |
|
|
47 | static UV * |
|
|
48 | SvJSON (SV *sv) |
|
|
49 | { |
|
|
50 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
51 | croak ("object is not of type JSON::XS"); |
|
|
52 | |
|
|
53 | return &SvUVX (SvRV (sv)); |
|
|
54 | } |
|
|
55 | |
|
|
56 | static void |
|
|
57 | shrink (SV *sv) |
|
|
58 | { |
|
|
59 | sv_utf8_downgrade (sv, 1); |
|
|
60 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
61 | { |
|
|
62 | #ifdef SvPV_shrink_to_cur |
|
|
63 | SvPV_shrink_to_cur (sv); |
|
|
64 | #elif defined (SvPV_renew) |
|
|
65 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
66 | #endif |
|
|
67 | } |
|
|
68 | } |
|
|
69 | |
|
|
70 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
71 | // case of an error. |
|
|
72 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
73 | // but use the very good perl function to parse anything else. |
|
|
74 | // note that we never call this function for a ascii codepoints |
|
|
75 | static UV |
|
|
76 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
77 | { |
|
|
78 | if (s[0] > 0xdf || s[0] < 0xc2) |
|
|
79 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
80 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
81 | { |
|
|
82 | *clen = 2; |
|
|
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
84 | } |
|
|
85 | else |
|
|
86 | { |
|
|
87 | *clen = (STRLEN)-1; |
|
|
88 | return (UV)-1; |
|
|
89 | } |
|
|
90 | } |
|
|
91 | |
|
|
92 | ///////////////////////////////////////////////////////////////////////////// |
|
|
93 | // encoder |
27 | |
94 | |
28 | // structure used for encoding JSON |
95 | // structure used for encoding JSON |
29 | typedef struct |
96 | typedef struct |
30 | { |
97 | { |
31 | char *cur; |
98 | char *cur; // SvPVX (sv) + current output position |
32 | STRLEN len; // SvLEN (sv) |
|
|
33 | char *end; // SvEND (sv) |
99 | char *end; // SvEND (sv) |
34 | SV *sv; |
100 | SV *sv; // result scalar |
35 | UV flags; |
101 | U32 flags; // F_* |
36 | int max_recurse; |
102 | U32 indent; // indentation level |
37 | int indent; |
103 | U32 maxdepth; // max. indentation/recursion level |
38 | } enc_t; |
104 | } enc_t; |
39 | |
|
|
40 | // structure used for decoding JSON |
|
|
41 | typedef struct |
|
|
42 | { |
|
|
43 | char *cur; |
|
|
44 | char *end; |
|
|
45 | const char *err; |
|
|
46 | UV flags; |
|
|
47 | } dec_t; |
|
|
48 | |
|
|
49 | static UV * |
|
|
50 | SvJSON (SV *sv) |
|
|
51 | { |
|
|
52 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
53 | croak ("object is not of type JSON::XS"); |
|
|
54 | |
|
|
55 | return &SvUVX (SvRV (sv)); |
|
|
56 | } |
|
|
57 | |
|
|
58 | ///////////////////////////////////////////////////////////////////////////// |
|
|
59 | |
105 | |
60 | static void |
106 | static void |
61 | need (enc_t *enc, STRLEN len) |
107 | need (enc_t *enc, STRLEN len) |
62 | { |
108 | { |
63 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
64 | { |
110 | { |
65 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
66 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
67 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
68 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
69 | } |
115 | } |
70 | } |
116 | } |
71 | |
117 | |
72 | static void |
118 | static void |
73 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
85 | |
131 | |
86 | while (str < end) |
132 | while (str < end) |
87 | { |
133 | { |
88 | unsigned char ch = *(unsigned char *)str; |
134 | unsigned char ch = *(unsigned char *)str; |
89 | |
135 | |
90 | if (ch == '"') |
136 | if (ch >= 0x20 && ch < 0x80) // most common case |
91 | { |
137 | { |
|
|
138 | if (ch == '"') // but with slow exceptions |
|
|
139 | { |
92 | need (enc, len += 1); |
140 | need (enc, len += 1); |
93 | *enc->cur++ = '\\'; |
141 | *enc->cur++ = '\\'; |
94 | *enc->cur++ = '"'; |
142 | *enc->cur++ = '"'; |
95 | ++str; |
|
|
96 | } |
143 | } |
97 | else if (ch == '\\') |
144 | else if (ch == '\\') |
98 | { |
145 | { |
99 | need (enc, len += 1); |
146 | need (enc, len += 1); |
100 | *enc->cur++ = '\\'; |
147 | *enc->cur++ = '\\'; |
101 | *enc->cur++ = '\\'; |
148 | *enc->cur++ = '\\'; |
102 | ++str; |
|
|
103 | } |
149 | } |
104 | else if (ch >= 0x20 && ch < 0x80) // most common case |
150 | else |
105 | { |
|
|
106 | *enc->cur++ = ch; |
151 | *enc->cur++ = ch; |
107 | ++str; |
152 | |
108 | } |
|
|
109 | else if (ch == '\015') |
|
|
110 | { |
|
|
111 | need (enc, len += 1); |
|
|
112 | *enc->cur++ = '\\'; |
|
|
113 | *enc->cur++ = 'r'; |
|
|
114 | ++str; |
|
|
115 | } |
|
|
116 | else if (ch == '\012') |
|
|
117 | { |
|
|
118 | need (enc, len += 1); |
|
|
119 | *enc->cur++ = '\\'; |
|
|
120 | *enc->cur++ = 'n'; |
|
|
121 | ++str; |
153 | ++str; |
122 | } |
154 | } |
123 | else |
155 | else |
124 | { |
156 | { |
125 | STRLEN clen; |
157 | switch (ch) |
126 | UV uch; |
|
|
127 | |
|
|
128 | if (is_utf8) |
|
|
129 | { |
158 | { |
130 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
159 | case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
131 | if (clen < 0) |
160 | case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
132 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
161 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
133 | } |
162 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
134 | else |
163 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
135 | { |
|
|
136 | uch = ch; |
|
|
137 | clen = 1; |
|
|
138 | } |
|
|
139 | |
164 | |
140 | if (uch < 0x80 || enc->flags & F_ASCII) |
165 | default: |
141 | { |
|
|
142 | if (uch > 0xFFFFUL) |
|
|
143 | { |
166 | { |
|
|
167 | STRLEN clen; |
|
|
168 | UV uch; |
|
|
169 | |
|
|
170 | if (is_utf8) |
|
|
171 | { |
|
|
172 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
173 | uch = decode_utf8 (str, end - str, &clen); |
|
|
174 | if (clen == (STRLEN)-1) |
|
|
175 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
|
|
176 | } |
|
|
177 | else |
|
|
178 | { |
|
|
179 | uch = ch; |
|
|
180 | clen = 1; |
|
|
181 | } |
|
|
182 | |
|
|
183 | if (uch > 0x10FFFFUL) |
|
|
184 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
185 | |
|
|
186 | if (uch < 0x80 || enc->flags & F_ASCII) |
|
|
187 | { |
|
|
188 | if (uch > 0xFFFFUL) |
|
|
189 | { |
144 | need (enc, len += 11); |
190 | need (enc, len += 11); |
145 | sprintf (enc->cur, "\\u%04x\\u%04x", |
191 | sprintf (enc->cur, "\\u%04x\\u%04x", |
146 | (uch - 0x10000) / 0x400 + 0xD800, |
192 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
147 | (uch - 0x10000) % 0x400 + 0xDC00); |
193 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
148 | enc->cur += 12; |
194 | enc->cur += 12; |
|
|
195 | } |
|
|
196 | else |
|
|
197 | { |
|
|
198 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
199 | need (enc, len += 5); |
|
|
200 | *enc->cur++ = '\\'; |
|
|
201 | *enc->cur++ = 'u'; |
|
|
202 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
203 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
204 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
205 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
206 | } |
|
|
207 | |
|
|
208 | str += clen; |
|
|
209 | } |
|
|
210 | else if (is_utf8) |
|
|
211 | { |
|
|
212 | need (enc, len += clen); |
|
|
213 | do |
|
|
214 | { |
|
|
215 | *enc->cur++ = *str++; |
|
|
216 | } |
|
|
217 | while (--clen); |
|
|
218 | } |
|
|
219 | else |
|
|
220 | { |
|
|
221 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
|
|
222 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
223 | ++str; |
|
|
224 | } |
149 | } |
225 | } |
150 | else |
|
|
151 | { |
|
|
152 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
153 | need (enc, len += 5); |
|
|
154 | *enc->cur++ = '\\'; |
|
|
155 | *enc->cur++ = 'u'; |
|
|
156 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
157 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
158 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
159 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
160 | } |
|
|
161 | |
|
|
162 | str += clen; |
|
|
163 | } |
|
|
164 | else if (is_utf8) |
|
|
165 | { |
|
|
166 | need (enc, len += clen); |
|
|
167 | while (clen--) |
|
|
168 | *enc->cur++ = *str++; |
|
|
169 | } |
|
|
170 | else |
|
|
171 | { |
|
|
172 | need (enc, 10); // never more than 11 bytes needed |
|
|
173 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
174 | ++str; |
|
|
175 | } |
226 | } |
176 | } |
227 | } |
177 | |
228 | |
178 | --len; |
229 | --len; |
179 | } |
230 | } |
180 | } |
231 | } |
181 | |
232 | |
182 | #define INDENT SB \ |
233 | static void |
|
|
234 | encode_indent (enc_t *enc) |
|
|
235 | { |
183 | if (enc->flags & F_INDENT) \ |
236 | if (enc->flags & F_INDENT) |
184 | { \ |
237 | { |
185 | int i_; \ |
238 | int spaces = enc->indent * INDENT_STEP; |
186 | need (enc, enc->indent); \ |
|
|
187 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
188 | encode_ch (enc, ' '); \ |
|
|
189 | } \ |
|
|
190 | SE |
|
|
191 | |
239 | |
192 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
240 | need (enc, spaces); |
193 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
241 | memset (enc->cur, ' ', spaces); |
194 | #define COMMA SB \ |
242 | enc->cur += spaces; |
|
|
243 | } |
|
|
244 | } |
|
|
245 | |
|
|
246 | static void |
|
|
247 | encode_space (enc_t *enc) |
|
|
248 | { |
|
|
249 | need (enc, 1); |
195 | encode_ch (enc, ','); \ |
250 | encode_ch (enc, ' '); |
|
|
251 | } |
|
|
252 | |
|
|
253 | static void |
|
|
254 | encode_nl (enc_t *enc) |
|
|
255 | { |
196 | if (enc->flags & F_INDENT) \ |
256 | if (enc->flags & F_INDENT) |
197 | NL; \ |
257 | { |
|
|
258 | need (enc, 1); |
|
|
259 | encode_ch (enc, '\n'); |
|
|
260 | } |
|
|
261 | } |
|
|
262 | |
|
|
263 | static void |
|
|
264 | encode_comma (enc_t *enc) |
|
|
265 | { |
|
|
266 | encode_ch (enc, ','); |
|
|
267 | |
|
|
268 | if (enc->flags & F_INDENT) |
|
|
269 | encode_nl (enc); |
198 | else if (enc->flags & F_SPACE_AFTER) \ |
270 | else if (enc->flags & F_SPACE_AFTER) |
199 | SPACE; \ |
271 | encode_space (enc); |
200 | SE |
272 | } |
201 | |
273 | |
202 | static void encode_sv (enc_t *enc, SV *sv); |
274 | static void encode_sv (enc_t *enc, SV *sv); |
203 | |
275 | |
204 | static void |
276 | static void |
205 | encode_av (enc_t *enc, AV *av) |
277 | encode_av (enc_t *enc, AV *av) |
206 | { |
278 | { |
207 | int i, len = av_len (av); |
279 | int i, len = av_len (av); |
208 | |
280 | |
209 | encode_ch (enc, '['); NL; |
281 | if (enc->indent >= enc->maxdepth) |
|
|
282 | croak ("data structure too deep (hit recursion limit)"); |
|
|
283 | |
|
|
284 | encode_ch (enc, '['); encode_nl (enc); |
210 | ++enc->indent; |
285 | ++enc->indent; |
211 | |
286 | |
212 | for (i = 0; i <= len; ++i) |
287 | for (i = 0; i <= len; ++i) |
213 | { |
288 | { |
214 | INDENT; |
289 | encode_indent (enc); |
215 | encode_sv (enc, *av_fetch (av, i, 0)); |
290 | encode_sv (enc, *av_fetch (av, i, 0)); |
216 | |
291 | |
217 | if (i < len) |
292 | if (i < len) |
218 | COMMA; |
293 | encode_comma (enc); |
219 | } |
294 | } |
220 | |
295 | |
221 | NL; |
296 | encode_nl (enc); |
222 | |
297 | |
223 | --enc->indent; |
298 | --enc->indent; |
224 | INDENT; encode_ch (enc, ']'); |
299 | encode_indent (enc); encode_ch (enc, ']'); |
225 | } |
300 | } |
226 | |
301 | |
227 | static void |
302 | static void |
228 | encode_he (enc_t *enc, HE *he) |
303 | encode_he (enc_t *enc, HE *he) |
229 | { |
304 | { |
… | |
… | |
243 | else |
318 | else |
244 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
319 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
245 | |
320 | |
246 | encode_ch (enc, '"'); |
321 | encode_ch (enc, '"'); |
247 | |
322 | |
248 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
323 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
249 | encode_ch (enc, ':'); |
324 | encode_ch (enc, ':'); |
250 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
325 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
251 | encode_sv (enc, HeVAL (he)); |
326 | encode_sv (enc, HeVAL (he)); |
252 | } |
327 | } |
253 | |
328 | |
254 | // compare hash entries, used when all keys are bytestrings |
329 | // compare hash entries, used when all keys are bytestrings |
255 | static int |
330 | static int |
… | |
… | |
261 | HE *b = *(HE **)b_; |
336 | HE *b = *(HE **)b_; |
262 | |
337 | |
263 | STRLEN la = HeKLEN (a); |
338 | STRLEN la = HeKLEN (a); |
264 | STRLEN lb = HeKLEN (b); |
339 | STRLEN lb = HeKLEN (b); |
265 | |
340 | |
266 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
341 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
267 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
342 | cmp = la - lb; |
268 | |
343 | |
269 | return cmp; |
344 | return cmp; |
270 | } |
345 | } |
271 | |
346 | |
272 | // compare hash entries, used when some keys are sv's or utf-x |
347 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
279 | static void |
354 | static void |
280 | encode_hv (enc_t *enc, HV *hv) |
355 | encode_hv (enc_t *enc, HV *hv) |
281 | { |
356 | { |
282 | int count, i; |
357 | int count, i; |
283 | |
358 | |
|
|
359 | if (enc->indent >= enc->maxdepth) |
|
|
360 | croak ("data structure too deep (hit recursion limit)"); |
|
|
361 | |
284 | encode_ch (enc, '{'); NL; ++enc->indent; |
362 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
285 | |
363 | |
286 | if ((count = hv_iterinit (hv))) |
364 | if ((count = hv_iterinit (hv))) |
287 | { |
365 | { |
288 | // for canonical output we have to sort by keys first |
366 | // for canonical output we have to sort by keys first |
289 | // actually, this is mostly due to the stupid so-called |
367 | // actually, this is mostly due to the stupid so-called |
290 | // security workaround added somewhere in 5.8.x. |
368 | // security workaround added somewhere in 5.8.x. |
291 | // that randomises hash orderings |
369 | // that randomises hash orderings |
292 | if (enc->flags & F_CANONICAL) |
370 | if (enc->flags & F_CANONICAL) |
293 | { |
371 | { |
294 | HE *he, *hes [count]; |
372 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
295 | int fast = 1; |
373 | int fast = 1; |
296 | |
374 | |
297 | i = 0; |
375 | i = 0; |
298 | while ((he = hv_iternext (hv))) |
376 | while ((he = hv_iternext (hv))) |
299 | { |
377 | { |
… | |
… | |
306 | |
384 | |
307 | if (fast) |
385 | if (fast) |
308 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
386 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
309 | else |
387 | else |
310 | { |
388 | { |
311 | // hack to disable "use bytes" |
389 | // hack to forcefully disable "use bytes" |
312 | COP *oldcop = PL_curcop, cop; |
390 | COP cop = *PL_curcop; |
313 | cop.op_private = 0; |
391 | cop.op_private = 0; |
|
|
392 | |
|
|
393 | ENTER; |
|
|
394 | SAVETMPS; |
|
|
395 | |
|
|
396 | SAVEVPTR (PL_curcop); |
314 | PL_curcop = &cop; |
397 | PL_curcop = &cop; |
315 | |
398 | |
316 | SAVETMPS; |
|
|
317 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
399 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
400 | |
318 | FREETMPS; |
401 | FREETMPS; |
319 | |
402 | LEAVE; |
320 | PL_curcop = oldcop; |
|
|
321 | } |
403 | } |
322 | |
404 | |
323 | for (i = 0; i < count; ++i) |
405 | for (i = 0; i < count; ++i) |
324 | { |
406 | { |
325 | INDENT; |
407 | encode_indent (enc); |
326 | encode_he (enc, hes [i]); |
408 | encode_he (enc, hes [i]); |
327 | |
409 | |
328 | if (i < count - 1) |
410 | if (i < count - 1) |
329 | COMMA; |
411 | encode_comma (enc); |
330 | } |
412 | } |
331 | |
413 | |
332 | NL; |
414 | encode_nl (enc); |
333 | } |
415 | } |
334 | else |
416 | else |
335 | { |
417 | { |
336 | SV *sv; |
|
|
337 | HE *he = hv_iternext (hv); |
418 | HE *he = hv_iternext (hv); |
338 | |
419 | |
339 | for (;;) |
420 | for (;;) |
340 | { |
421 | { |
341 | INDENT; |
422 | encode_indent (enc); |
342 | encode_he (enc, he); |
423 | encode_he (enc, he); |
343 | |
424 | |
344 | if (!(he = hv_iternext (hv))) |
425 | if (!(he = hv_iternext (hv))) |
345 | break; |
426 | break; |
346 | |
427 | |
347 | COMMA; |
428 | encode_comma (enc); |
348 | } |
429 | } |
349 | |
430 | |
350 | NL; |
431 | encode_nl (enc); |
351 | } |
432 | } |
352 | } |
433 | } |
353 | |
434 | |
354 | --enc->indent; INDENT; encode_ch (enc, '}'); |
435 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
436 | } |
|
|
437 | |
|
|
438 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
439 | static void |
|
|
440 | encode_rv (enc_t *enc, SV *sv) |
|
|
441 | { |
|
|
442 | svtype svt; |
|
|
443 | |
|
|
444 | SvGETMAGIC (sv); |
|
|
445 | svt = SvTYPE (sv); |
|
|
446 | |
|
|
447 | if (svt == SVt_PVHV) |
|
|
448 | encode_hv (enc, (HV *)sv); |
|
|
449 | else if (svt == SVt_PVAV) |
|
|
450 | encode_av (enc, (AV *)sv); |
|
|
451 | else if (svt < SVt_PVAV) |
|
|
452 | { |
|
|
453 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
454 | encode_str (enc, "false", 5, 0); |
|
|
455 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
456 | encode_str (enc, "true", 4, 0); |
|
|
457 | else |
|
|
458 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
459 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
460 | } |
|
|
461 | else |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
355 | } |
464 | } |
356 | |
465 | |
357 | static void |
466 | static void |
358 | encode_sv (enc_t *enc, SV *sv) |
467 | encode_sv (enc_t *enc, SV *sv) |
359 | { |
468 | { |
… | |
… | |
380 | SvIsUV(sv) |
489 | SvIsUV(sv) |
381 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
490 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
382 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
491 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
383 | } |
492 | } |
384 | else if (SvROK (sv)) |
493 | else if (SvROK (sv)) |
385 | { |
494 | encode_rv (enc, SvRV (sv)); |
386 | if (!--enc->max_recurse) |
|
|
387 | croak ("data structure too deep (hit recursion limit)"); |
|
|
388 | |
|
|
389 | sv = SvRV (sv); |
|
|
390 | |
|
|
391 | switch (SvTYPE (sv)) |
|
|
392 | { |
|
|
393 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
|
|
394 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
|
|
395 | |
|
|
396 | default: |
|
|
397 | croak ("JSON can only represent references to arrays or hashes"); |
|
|
398 | } |
|
|
399 | } |
|
|
400 | else if (!SvOK (sv)) |
495 | else if (!SvOK (sv)) |
401 | encode_str (enc, "null", 4, 0); |
496 | encode_str (enc, "null", 4, 0); |
402 | else |
497 | else |
403 | croak ("encountered perl type that JSON cannot handle"); |
498 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
499 | SvPV_nolen (sv), SvFLAGS (sv)); |
404 | } |
500 | } |
405 | |
501 | |
406 | static SV * |
502 | static SV * |
407 | encode_json (SV *scalar, UV flags) |
503 | encode_json (SV *scalar, U32 flags) |
408 | { |
504 | { |
|
|
505 | enc_t enc; |
|
|
506 | |
409 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
507 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
410 | croak ("hash- or arraref required (not a simple scalar, use allow_nonref to allow this)"); |
508 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
411 | |
509 | |
412 | enc_t enc; |
|
|
413 | enc.flags = flags; |
510 | enc.flags = flags; |
414 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
511 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
415 | enc.cur = SvPVX (enc.sv); |
512 | enc.cur = SvPVX (enc.sv); |
416 | enc.end = SvEND (enc.sv); |
513 | enc.end = SvEND (enc.sv); |
417 | enc.max_recurse = 0; |
|
|
418 | enc.indent = 0; |
514 | enc.indent = 0; |
|
|
515 | enc.maxdepth = DEC_DEPTH (flags); |
419 | |
516 | |
420 | SvPOK_only (enc.sv); |
517 | SvPOK_only (enc.sv); |
421 | encode_sv (&enc, scalar); |
518 | encode_sv (&enc, scalar); |
422 | |
519 | |
423 | if (!(flags & (F_ASCII | F_UTF8))) |
520 | if (!(flags & (F_ASCII | F_UTF8))) |
424 | SvUTF8_on (enc.sv); |
521 | SvUTF8_on (enc.sv); |
425 | |
522 | |
426 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
523 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
524 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
525 | |
|
|
526 | if (enc.flags & F_SHRINK) |
|
|
527 | shrink (enc.sv); |
|
|
528 | |
427 | return enc.sv; |
529 | return enc.sv; |
428 | } |
530 | } |
429 | |
531 | |
430 | ///////////////////////////////////////////////////////////////////////////// |
532 | ///////////////////////////////////////////////////////////////////////////// |
|
|
533 | // decoder |
431 | |
534 | |
432 | #define WS \ |
535 | // structure used for decoding JSON |
|
|
536 | typedef struct |
|
|
537 | { |
|
|
538 | char *cur; // current parser pointer |
|
|
539 | char *end; // end of input string |
|
|
540 | const char *err; // parse error, if != 0 |
|
|
541 | U32 flags; // F_* |
|
|
542 | U32 depth; // recursion depth |
|
|
543 | U32 maxdepth; // recursion depth limit |
|
|
544 | } dec_t; |
|
|
545 | |
|
|
546 | static void |
|
|
547 | decode_ws (dec_t *dec) |
|
|
548 | { |
433 | for (;;) \ |
549 | for (;;) |
434 | { \ |
550 | { |
435 | char ch = *dec->cur; \ |
551 | char ch = *dec->cur; |
|
|
552 | |
436 | if (ch > 0x20 \ |
553 | if (ch > 0x20 |
437 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
554 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
438 | break; \ |
555 | break; |
|
|
556 | |
439 | ++dec->cur; \ |
557 | ++dec->cur; |
440 | } |
558 | } |
|
|
559 | } |
441 | |
560 | |
442 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
561 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
562 | |
443 | #define EXPECT_CH(ch) SB \ |
563 | #define EXPECT_CH(ch) SB \ |
444 | if (*dec->cur != ch) \ |
564 | if (*dec->cur != ch) \ |
445 | ERR (# ch " expected"); \ |
565 | ERR (# ch " expected"); \ |
446 | ++dec->cur; \ |
566 | ++dec->cur; \ |
447 | SE |
567 | SE |
448 | |
568 | |
|
|
569 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
570 | #define DEC_DEC_DEPTH --dec->depth |
|
|
571 | |
449 | static SV *decode_sv (dec_t *dec); |
572 | static SV *decode_sv (dec_t *dec); |
450 | |
573 | |
451 | static signed char decode_hexdigit[256]; |
574 | static signed char decode_hexdigit[256]; |
452 | |
575 | |
453 | static UV |
576 | static UV |
454 | decode_4hex (dec_t *dec) |
577 | decode_4hex (dec_t *dec) |
455 | { |
578 | { |
456 | signed char d1, d2, d3, d4; |
579 | signed char d1, d2, d3, d4; |
|
|
580 | unsigned char *cur = (unsigned char *)dec->cur; |
457 | |
581 | |
458 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
|
|
459 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
582 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
460 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
|
|
461 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
583 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
462 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
463 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
584 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
464 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
465 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
585 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
466 | |
586 | |
467 | dec->cur += 4; |
587 | dec->cur += 4; |
468 | |
588 | |
469 | return ((UV)d1) << 12 |
589 | return ((UV)d1) << 12 |
470 | | ((UV)d2) << 8 |
590 | | ((UV)d2) << 8 |
… | |
… | |
473 | |
593 | |
474 | fail: |
594 | fail: |
475 | return (UV)-1; |
595 | return (UV)-1; |
476 | } |
596 | } |
477 | |
597 | |
478 | #define APPEND_GROW(n) SB \ |
|
|
479 | if (cur + (n) >= end) \ |
|
|
480 | { \ |
|
|
481 | STRLEN ofs = cur - SvPVX (sv); \ |
|
|
482 | SvGROW (sv, ofs + (n) + 1); \ |
|
|
483 | cur = SvPVX (sv) + ofs; \ |
|
|
484 | end = SvEND (sv); \ |
|
|
485 | } \ |
|
|
486 | SE |
|
|
487 | |
|
|
488 | #define APPEND_CH(ch) SB \ |
|
|
489 | APPEND_GROW (1); \ |
|
|
490 | *cur++ = (ch); \ |
|
|
491 | SE |
|
|
492 | |
|
|
493 | static SV * |
598 | static SV * |
494 | decode_str (dec_t *dec) |
599 | decode_str (dec_t *dec) |
495 | { |
600 | { |
496 | SV *sv = NEWSV (0,2); |
601 | SV *sv = 0; |
497 | int utf8 = 0; |
602 | int utf8 = 0; |
498 | char *cur = SvPVX (sv); |
|
|
499 | char *end = SvEND (sv); |
|
|
500 | |
603 | |
501 | for (;;) |
604 | do |
502 | { |
605 | { |
503 | unsigned char ch = *(unsigned char *)dec->cur; |
606 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
|
|
607 | char *cur = buf; |
504 | |
608 | |
505 | if (ch == '"') |
609 | do |
506 | break; |
|
|
507 | else if (ch == '\\') |
|
|
508 | { |
610 | { |
509 | switch (*++dec->cur) |
611 | unsigned char ch = *(unsigned char *)dec->cur++; |
|
|
612 | |
|
|
613 | if (ch == '"') |
510 | { |
614 | { |
511 | case '\\': |
615 | --dec->cur; |
512 | case '/': |
616 | break; |
513 | case '"': APPEND_CH (*dec->cur++); break; |
617 | } |
514 | |
618 | else if (ch == '\\') |
515 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
619 | { |
516 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
620 | switch (*dec->cur) |
517 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
518 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
519 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
520 | |
|
|
521 | case 'u': |
|
|
522 | { |
621 | { |
523 | UV lo, hi; |
622 | case '\\': |
524 | ++dec->cur; |
623 | case '/': |
|
|
624 | case '"': *cur++ = *dec->cur++; break; |
525 | |
625 | |
526 | hi = decode_4hex (dec); |
626 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
527 | if (hi == (UV)-1) |
627 | case 't': ++dec->cur; *cur++ = '\011'; break; |
528 | goto fail; |
628 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
|
|
629 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
|
|
630 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
529 | |
631 | |
530 | // possibly a surrogate pair |
632 | case 'u': |
531 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
532 | { |
633 | { |
533 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
634 | UV lo, hi; |
534 | ERR ("illegal surrogate character"); |
|
|
535 | |
|
|
536 | dec->cur += 2; |
635 | ++dec->cur; |
537 | |
636 | |
538 | lo = decode_4hex (dec); |
637 | hi = decode_4hex (dec); |
539 | if (lo == (UV)-1) |
638 | if (hi == (UV)-1) |
540 | goto fail; |
639 | goto fail; |
541 | |
640 | |
|
|
641 | // possibly a surrogate pair |
|
|
642 | if (hi >= 0xd800) |
|
|
643 | if (hi < 0xdc00) |
|
|
644 | { |
|
|
645 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
|
|
646 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
647 | |
|
|
648 | dec->cur += 2; |
|
|
649 | |
|
|
650 | lo = decode_4hex (dec); |
|
|
651 | if (lo == (UV)-1) |
|
|
652 | goto fail; |
|
|
653 | |
542 | if (lo < 0xdc00 || lo >= 0xe000) |
654 | if (lo < 0xdc00 || lo >= 0xe000) |
543 | ERR ("surrogate pair expected"); |
655 | ERR ("surrogate pair expected"); |
544 | |
656 | |
545 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
657 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
658 | } |
|
|
659 | else if (hi < 0xe000) |
|
|
660 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
661 | |
|
|
662 | if (hi >= 0x80) |
|
|
663 | { |
|
|
664 | utf8 = 1; |
|
|
665 | |
|
|
666 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
667 | } |
|
|
668 | else |
|
|
669 | *cur++ = hi; |
546 | } |
670 | } |
547 | else if (lo >= 0xdc00 && lo < 0xe000) |
|
|
548 | ERR ("illegal surrogate character"); |
|
|
549 | |
|
|
550 | if (hi >= 0x80) |
|
|
551 | { |
671 | break; |
552 | utf8 = 1; |
|
|
553 | |
672 | |
554 | APPEND_GROW (4); // at most 4 bytes for 21 bits |
|
|
555 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
556 | } |
|
|
557 | else |
673 | default: |
558 | APPEND_CH (hi); |
674 | --dec->cur; |
|
|
675 | ERR ("illegal backslash escape sequence in string"); |
559 | } |
676 | } |
|
|
677 | } |
|
|
678 | else if (ch >= 0x20 && ch <= 0x7f) |
|
|
679 | *cur++ = ch; |
|
|
680 | else if (ch >= 0x80) |
|
|
681 | { |
|
|
682 | STRLEN clen; |
|
|
683 | UV uch; |
|
|
684 | |
|
|
685 | --dec->cur; |
|
|
686 | |
|
|
687 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
|
|
688 | if (clen == (STRLEN)-1) |
|
|
689 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
690 | |
|
|
691 | do |
|
|
692 | *cur++ = *dec->cur++; |
|
|
693 | while (--clen); |
|
|
694 | |
|
|
695 | utf8 = 1; |
|
|
696 | } |
|
|
697 | else |
|
|
698 | { |
|
|
699 | --dec->cur; |
|
|
700 | |
|
|
701 | if (!ch) |
|
|
702 | ERR ("unexpected end of string while parsing JSON string"); |
560 | break; |
703 | else |
|
|
704 | ERR ("invalid character encountered while parsing JSON string"); |
561 | } |
705 | } |
562 | } |
706 | } |
563 | else if (ch >= 0x20 && ch <= 0x7f) |
707 | while (cur < buf + SHORT_STRING_LEN); |
564 | APPEND_CH (*dec->cur++); |
708 | |
565 | else if (ch >= 0x80) |
709 | { |
|
|
710 | STRLEN len = cur - buf; |
|
|
711 | |
|
|
712 | if (sv) |
566 | { |
713 | { |
567 | STRLEN clen; |
714 | SvGROW (sv, SvCUR (sv) + len + 1); |
568 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
715 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
569 | if (clen < 0) |
716 | SvCUR_set (sv, SvCUR (sv) + len); |
570 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
571 | |
|
|
572 | APPEND_GROW (clen); |
|
|
573 | memcpy (cur, dec->cur, clen); |
|
|
574 | cur += clen; |
|
|
575 | dec->cur += clen; |
|
|
576 | } |
717 | } |
577 | else |
718 | else |
578 | ERR ("invalid character encountered"); |
719 | sv = newSVpvn (buf, len); |
579 | } |
720 | } |
|
|
721 | } |
|
|
722 | while (*dec->cur != '"'); |
580 | |
723 | |
581 | ++dec->cur; |
724 | ++dec->cur; |
582 | |
725 | |
583 | SvCUR_set (sv, cur - SvPVX (sv)); |
726 | if (sv) |
584 | |
727 | { |
585 | SvPOK_only (sv); |
728 | SvPOK_only (sv); |
586 | *SvEND (sv) = 0; |
729 | *SvEND (sv) = 0; |
587 | |
730 | |
588 | if (utf8) |
731 | if (utf8) |
589 | SvUTF8_on (sv); |
732 | SvUTF8_on (sv); |
|
|
733 | } |
|
|
734 | else |
|
|
735 | sv = newSVpvn ("", 0); |
590 | |
736 | |
591 | return sv; |
737 | return sv; |
592 | |
738 | |
593 | fail: |
739 | fail: |
594 | SvREFCNT_dec (sv); |
|
|
595 | return 0; |
740 | return 0; |
596 | } |
741 | } |
597 | |
742 | |
598 | static SV * |
743 | static SV * |
599 | decode_num (dec_t *dec) |
744 | decode_num (dec_t *dec) |
… | |
… | |
609 | { |
754 | { |
610 | ++dec->cur; |
755 | ++dec->cur; |
611 | if (*dec->cur >= '0' && *dec->cur <= '9') |
756 | if (*dec->cur >= '0' && *dec->cur <= '9') |
612 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
757 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
613 | } |
758 | } |
614 | |
759 | else if (*dec->cur < '0' || *dec->cur > '9') |
615 | // int |
760 | ERR ("malformed number (no digits after initial minus)"); |
|
|
761 | else |
|
|
762 | do |
|
|
763 | { |
|
|
764 | ++dec->cur; |
|
|
765 | } |
616 | while (*dec->cur >= '0' && *dec->cur <= '9') |
766 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
617 | ++dec->cur; |
|
|
618 | |
767 | |
619 | // [frac] |
768 | // [frac] |
620 | if (*dec->cur == '.') |
769 | if (*dec->cur == '.') |
621 | { |
770 | { |
622 | is_nv = 1; |
771 | ++dec->cur; |
|
|
772 | |
|
|
773 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
774 | ERR ("malformed number (no digits after decimal point)"); |
623 | |
775 | |
624 | do |
776 | do |
625 | { |
777 | { |
626 | ++dec->cur; |
778 | ++dec->cur; |
627 | } |
779 | } |
628 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
780 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
781 | |
|
|
782 | is_nv = 1; |
629 | } |
783 | } |
630 | |
784 | |
631 | // [exp] |
785 | // [exp] |
632 | if (*dec->cur == 'e' || *dec->cur == 'E') |
786 | if (*dec->cur == 'e' || *dec->cur == 'E') |
633 | { |
787 | { |
634 | is_nv = 1; |
|
|
635 | |
|
|
636 | ++dec->cur; |
788 | ++dec->cur; |
|
|
789 | |
637 | if (*dec->cur == '-' || *dec->cur == '+') |
790 | if (*dec->cur == '-' || *dec->cur == '+') |
638 | ++dec->cur; |
791 | ++dec->cur; |
639 | |
792 | |
|
|
793 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
794 | ERR ("malformed number (no digits after exp sign)"); |
|
|
795 | |
|
|
796 | do |
|
|
797 | { |
|
|
798 | ++dec->cur; |
|
|
799 | } |
640 | while (*dec->cur >= '0' && *dec->cur <= '9') |
800 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
641 | ++dec->cur; |
801 | |
|
|
802 | is_nv = 1; |
642 | } |
803 | } |
643 | |
804 | |
644 | if (!is_nv) |
805 | if (!is_nv) |
645 | { |
806 | { |
646 | UV uv; |
807 | UV uv; |
… | |
… | |
664 | static SV * |
825 | static SV * |
665 | decode_av (dec_t *dec) |
826 | decode_av (dec_t *dec) |
666 | { |
827 | { |
667 | AV *av = newAV (); |
828 | AV *av = newAV (); |
668 | |
829 | |
|
|
830 | DEC_INC_DEPTH; |
|
|
831 | decode_ws (dec); |
|
|
832 | |
|
|
833 | if (*dec->cur == ']') |
|
|
834 | ++dec->cur; |
|
|
835 | else |
669 | for (;;) |
836 | for (;;) |
670 | { |
837 | { |
671 | SV *value; |
838 | SV *value; |
672 | |
839 | |
673 | value = decode_sv (dec); |
840 | value = decode_sv (dec); |
674 | if (!value) |
841 | if (!value) |
675 | goto fail; |
842 | goto fail; |
676 | |
843 | |
677 | av_push (av, value); |
844 | av_push (av, value); |
678 | |
845 | |
679 | WS; |
846 | decode_ws (dec); |
680 | |
847 | |
681 | if (*dec->cur == ']') |
848 | if (*dec->cur == ']') |
682 | { |
849 | { |
683 | ++dec->cur; |
850 | ++dec->cur; |
684 | break; |
851 | break; |
|
|
852 | } |
685 | } |
853 | |
686 | |
|
|
687 | if (*dec->cur != ',') |
854 | if (*dec->cur != ',') |
688 | ERR (", or ] expected while parsing array"); |
855 | ERR (", or ] expected while parsing array"); |
689 | |
856 | |
690 | ++dec->cur; |
857 | ++dec->cur; |
691 | } |
858 | } |
692 | |
859 | |
|
|
860 | DEC_DEC_DEPTH; |
693 | return newRV_noinc ((SV *)av); |
861 | return newRV_noinc ((SV *)av); |
694 | |
862 | |
695 | fail: |
863 | fail: |
696 | SvREFCNT_dec (av); |
864 | SvREFCNT_dec (av); |
|
|
865 | DEC_DEC_DEPTH; |
697 | return 0; |
866 | return 0; |
698 | } |
867 | } |
699 | |
868 | |
700 | static SV * |
869 | static SV * |
701 | decode_hv (dec_t *dec) |
870 | decode_hv (dec_t *dec) |
702 | { |
871 | { |
703 | HV *hv = newHV (); |
872 | HV *hv = newHV (); |
704 | |
873 | |
|
|
874 | DEC_INC_DEPTH; |
|
|
875 | decode_ws (dec); |
|
|
876 | |
|
|
877 | if (*dec->cur == '}') |
|
|
878 | ++dec->cur; |
|
|
879 | else |
705 | for (;;) |
880 | for (;;) |
706 | { |
881 | { |
707 | SV *key, *value; |
882 | SV *key, *value; |
708 | |
883 | |
709 | WS; EXPECT_CH ('"'); |
884 | decode_ws (dec); EXPECT_CH ('"'); |
710 | |
885 | |
711 | key = decode_str (dec); |
886 | key = decode_str (dec); |
712 | if (!key) |
887 | if (!key) |
713 | goto fail; |
|
|
714 | |
|
|
715 | WS; EXPECT_CH (':'); |
|
|
716 | |
|
|
717 | value = decode_sv (dec); |
|
|
718 | if (!value) |
|
|
719 | { |
|
|
720 | SvREFCNT_dec (key); |
|
|
721 | goto fail; |
888 | goto fail; |
|
|
889 | |
|
|
890 | decode_ws (dec); EXPECT_CH (':'); |
|
|
891 | |
|
|
892 | value = decode_sv (dec); |
|
|
893 | if (!value) |
|
|
894 | { |
|
|
895 | SvREFCNT_dec (key); |
|
|
896 | goto fail; |
722 | } |
897 | } |
723 | |
898 | |
724 | //TODO: optimise |
|
|
725 | hv_store_ent (hv, key, value, 0); |
899 | hv_store_ent (hv, key, value, 0); |
|
|
900 | SvREFCNT_dec (key); |
726 | |
901 | |
727 | WS; |
902 | decode_ws (dec); |
728 | |
903 | |
729 | if (*dec->cur == '}') |
904 | if (*dec->cur == '}') |
730 | { |
905 | { |
731 | ++dec->cur; |
906 | ++dec->cur; |
732 | break; |
907 | break; |
733 | } |
908 | } |
734 | |
909 | |
735 | if (*dec->cur != ',') |
910 | if (*dec->cur != ',') |
736 | ERR (", or } expected while parsing object/hash"); |
911 | ERR (", or } expected while parsing object/hash"); |
737 | |
912 | |
738 | ++dec->cur; |
913 | ++dec->cur; |
739 | } |
914 | } |
740 | |
915 | |
|
|
916 | DEC_DEC_DEPTH; |
741 | return newRV_noinc ((SV *)hv); |
917 | return newRV_noinc ((SV *)hv); |
742 | |
918 | |
743 | fail: |
919 | fail: |
744 | SvREFCNT_dec (hv); |
920 | SvREFCNT_dec (hv); |
|
|
921 | DEC_DEC_DEPTH; |
745 | return 0; |
922 | return 0; |
746 | } |
923 | } |
747 | |
924 | |
748 | static SV * |
925 | static SV * |
749 | decode_sv (dec_t *dec) |
926 | decode_sv (dec_t *dec) |
750 | { |
927 | { |
751 | WS; |
928 | decode_ws (dec); |
752 | switch (*dec->cur) |
929 | switch (*dec->cur) |
753 | { |
930 | { |
754 | case '"': ++dec->cur; return decode_str (dec); |
931 | case '"': ++dec->cur; return decode_str (dec); |
755 | case '[': ++dec->cur; return decode_av (dec); |
932 | case '[': ++dec->cur; return decode_av (dec); |
756 | case '{': ++dec->cur; return decode_hv (dec); |
933 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
784 | |
961 | |
785 | case 'n': |
962 | case 'n': |
786 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
963 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
787 | { |
964 | { |
788 | dec->cur += 4; |
965 | dec->cur += 4; |
789 | return newSViv (1); |
966 | return newSVsv (&PL_sv_undef); |
790 | } |
967 | } |
791 | else |
968 | else |
792 | ERR ("'null' expected"); |
969 | ERR ("'null' expected"); |
793 | |
970 | |
794 | break; |
971 | break; |
795 | |
972 | |
796 | default: |
973 | default: |
797 | ERR ("malformed json string"); |
974 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
798 | break; |
975 | break; |
799 | } |
976 | } |
800 | |
977 | |
801 | fail: |
978 | fail: |
802 | return 0; |
979 | return 0; |
803 | } |
980 | } |
804 | |
981 | |
805 | static SV * |
982 | static SV * |
806 | decode_json (SV *string, UV flags) |
983 | decode_json (SV *string, U32 flags) |
807 | { |
984 | { |
|
|
985 | dec_t dec; |
808 | SV *sv; |
986 | SV *sv; |
809 | |
987 | |
|
|
988 | SvUPGRADE (string, SVt_PV); |
|
|
989 | |
810 | if (!(flags & F_UTF8)) |
990 | if (flags & F_UTF8) |
|
|
991 | sv_utf8_downgrade (string, 0); |
|
|
992 | else |
811 | sv_utf8_upgrade (string); |
993 | sv_utf8_upgrade (string); |
812 | |
994 | |
813 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
995 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
814 | |
996 | |
815 | dec_t dec; |
|
|
816 | dec.flags = flags; |
997 | dec.flags = flags; |
817 | dec.cur = SvPVX (string); |
998 | dec.cur = SvPVX (string); |
818 | dec.end = SvEND (string); |
999 | dec.end = SvEND (string); |
819 | dec.err = 0; |
1000 | dec.err = 0; |
|
|
1001 | dec.depth = 0; |
|
|
1002 | dec.maxdepth = DEC_DEPTH (dec.flags); |
820 | |
1003 | |
821 | *dec.end = 1; // invalid anywhere |
1004 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
822 | sv = decode_sv (&dec); |
1005 | sv = decode_sv (&dec); |
823 | *dec.end = 0; |
|
|
824 | |
1006 | |
825 | if (!sv) |
1007 | if (!sv) |
826 | { |
1008 | { |
|
|
1009 | IV offset = dec.flags & F_UTF8 |
|
|
1010 | ? dec.cur - SvPVX (string) |
827 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
1011 | : utf8_distance (dec.cur, SvPVX (string)); |
828 | SV *uni = sv_newmortal (); |
1012 | SV *uni = sv_newmortal (); |
829 | |
1013 | |
|
|
1014 | // horrible hack to silence warning inside pv_uni_display |
|
|
1015 | COP cop = *PL_curcop; |
|
|
1016 | cop.cop_warnings = pWARN_NONE; |
|
|
1017 | ENTER; |
|
|
1018 | SAVEVPTR (PL_curcop); |
|
|
1019 | PL_curcop = &cop; |
830 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1020 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
1021 | LEAVE; |
|
|
1022 | |
831 | croak ("%s, at character %d (%s)", |
1023 | croak ("%s, at character offset %d [\"%s\"]", |
832 | dec.err, |
1024 | dec.err, |
833 | (int)offset, |
1025 | (int)offset, |
834 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1026 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
835 | } |
1027 | } |
836 | |
1028 | |
837 | sv = sv_2mortal (sv); |
1029 | sv = sv_2mortal (sv); |
838 | |
1030 | |
839 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1031 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
840 | croak ("JSON object or array expected (but number, string, true, false or null found, use allow_nonref to allow this)"); |
1032 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
841 | |
1033 | |
842 | return sv; |
1034 | return sv; |
843 | } |
1035 | } |
844 | |
1036 | |
|
|
1037 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1038 | // XS interface functions |
|
|
1039 | |
845 | MODULE = JSON::XS PACKAGE = JSON::XS |
1040 | MODULE = JSON::XS PACKAGE = JSON::XS |
846 | |
1041 | |
847 | BOOT: |
1042 | BOOT: |
848 | { |
1043 | { |
849 | int i; |
1044 | int i; |
850 | |
1045 | |
851 | memset (decode_hexdigit, 0xff, 256); |
1046 | memset (decode_hexdigit, 0xff, 256); |
|
|
1047 | |
852 | for (i = 10; i--; ) |
1048 | for (i = 0; i < 256; ++i) |
853 | decode_hexdigit ['0' + i] = i; |
1049 | decode_hexdigit [i] = |
854 | |
1050 | i >= '0' && i <= '9' ? i - '0' |
855 | for (i = 7; i--; ) |
1051 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
856 | { |
1052 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
857 | decode_hexdigit ['a' + i] = 10 + i; |
1053 | : -1; |
858 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
859 | } |
|
|
860 | |
1054 | |
861 | json_stash = gv_stashpv ("JSON::XS", 1); |
1055 | json_stash = gv_stashpv ("JSON::XS", 1); |
862 | } |
1056 | } |
863 | |
1057 | |
864 | PROTOTYPES: DISABLE |
1058 | PROTOTYPES: DISABLE |
… | |
… | |
867 | CODE: |
1061 | CODE: |
868 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1062 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
869 | OUTPUT: |
1063 | OUTPUT: |
870 | RETVAL |
1064 | RETVAL |
871 | |
1065 | |
872 | SV *ascii (SV *self, int enable) |
1066 | SV *ascii (SV *self, int enable = 1) |
873 | ALIAS: |
1067 | ALIAS: |
874 | ascii = F_ASCII |
1068 | ascii = F_ASCII |
875 | utf8 = F_UTF8 |
1069 | utf8 = F_UTF8 |
876 | indent = F_INDENT |
1070 | indent = F_INDENT |
877 | canonical = F_CANONICAL |
1071 | canonical = F_CANONICAL |
878 | space_before = F_SPACE_BEFORE |
1072 | space_before = F_SPACE_BEFORE |
879 | space_after = F_SPACE_AFTER |
1073 | space_after = F_SPACE_AFTER |
880 | json_rpc = F_JSON_RPC |
|
|
881 | pretty = F_PRETTY |
1074 | pretty = F_PRETTY |
882 | allow_nonref = F_ALLOW_NONREF |
1075 | allow_nonref = F_ALLOW_NONREF |
|
|
1076 | shrink = F_SHRINK |
883 | CODE: |
1077 | CODE: |
884 | { |
1078 | { |
885 | UV *uv = SvJSON (self); |
1079 | UV *uv = SvJSON (self); |
886 | if (enable) |
1080 | if (enable) |
887 | *uv |= ix; |
1081 | *uv |= ix; |
… | |
… | |
891 | RETVAL = newSVsv (self); |
1085 | RETVAL = newSVsv (self); |
892 | } |
1086 | } |
893 | OUTPUT: |
1087 | OUTPUT: |
894 | RETVAL |
1088 | RETVAL |
895 | |
1089 | |
|
|
1090 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1091 | CODE: |
|
|
1092 | { |
|
|
1093 | UV *uv = SvJSON (self); |
|
|
1094 | UV log2 = 0; |
|
|
1095 | |
|
|
1096 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1097 | |
|
|
1098 | while ((1UL << log2) < max_depth) |
|
|
1099 | ++log2; |
|
|
1100 | |
|
|
1101 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1102 | |
|
|
1103 | RETVAL = newSVsv (self); |
|
|
1104 | } |
|
|
1105 | OUTPUT: |
|
|
1106 | RETVAL |
|
|
1107 | |
896 | void encode (SV *self, SV *scalar) |
1108 | void encode (SV *self, SV *scalar) |
897 | PPCODE: |
1109 | PPCODE: |
898 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1110 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
899 | |
1111 | |
900 | void decode (SV *self, SV *jsonstr) |
1112 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
902 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1114 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
903 | |
1115 | |
904 | PROTOTYPES: ENABLE |
1116 | PROTOTYPES: ENABLE |
905 | |
1117 | |
906 | void to_json (SV *scalar) |
1118 | void to_json (SV *scalar) |
|
|
1119 | ALIAS: |
|
|
1120 | objToJson = 0 |
907 | PPCODE: |
1121 | PPCODE: |
908 | XPUSHs (encode_json (scalar, F_UTF8)); |
1122 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
909 | |
1123 | |
910 | void from_json (SV *jsonstr) |
1124 | void from_json (SV *jsonstr) |
|
|
1125 | ALIAS: |
|
|
1126 | jsonToObj = 0 |
911 | PPCODE: |
1127 | PPCODE: |
912 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1128 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
913 | |
1129 | |