… | |
… | |
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 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
9 | #define F_ASCII 0x00000001 |
20 | #define F_ASCII 0x00000001UL |
|
|
21 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002 |
22 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004 |
23 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008 |
24 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
25 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020 |
26 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_JSON_RPC 0x00000040 |
27 | #define F_ALLOW_NONREF 0x00000100UL |
|
|
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
|
|
31 | #define F_MAXDEPTH 0xf8000000UL |
|
|
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
|
|
35 | |
|
|
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
16 | |
38 | |
17 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
18 | #define F_DEFAULT 0 |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
19 | |
41 | |
20 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
|
|
43 | #define INDENT_STEP 3 // spaces per indentation level |
|
|
44 | |
|
|
45 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
21 | |
46 | |
22 | #define SB do { |
47 | #define SB do { |
23 | #define SE } while (0) |
48 | #define SE } while (0) |
24 | |
49 | |
25 | static HV *json_stash; |
50 | #if __GNUC__ >= 3 |
|
|
51 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
52 | # define inline inline |
|
|
53 | #else |
|
|
54 | # define expect(expr,value) (expr) |
|
|
55 | # define inline static |
|
|
56 | #endif |
|
|
57 | |
|
|
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
60 | |
|
|
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
62 | static SV *json_true, *json_false; |
|
|
63 | |
|
|
64 | ///////////////////////////////////////////////////////////////////////////// |
|
|
65 | // utility functions |
|
|
66 | |
|
|
67 | static UV * |
|
|
68 | SvJSON (SV *sv) |
|
|
69 | { |
|
|
70 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
71 | croak ("object is not of type JSON::XS"); |
|
|
72 | |
|
|
73 | return &SvUVX (SvRV (sv)); |
|
|
74 | } |
|
|
75 | |
|
|
76 | static void |
|
|
77 | shrink (SV *sv) |
|
|
78 | { |
|
|
79 | sv_utf8_downgrade (sv, 1); |
|
|
80 | if (SvLEN (sv) > SvCUR (sv) + 1) |
|
|
81 | { |
|
|
82 | #ifdef SvPV_shrink_to_cur |
|
|
83 | SvPV_shrink_to_cur (sv); |
|
|
84 | #elif defined (SvPV_renew) |
|
|
85 | SvPV_renew (sv, SvCUR (sv) + 1); |
|
|
86 | #endif |
|
|
87 | } |
|
|
88 | } |
|
|
89 | |
|
|
90 | // decode an utf-8 character and return it, or (UV)-1 in |
|
|
91 | // case of an error. |
|
|
92 | // we special-case "safe" characters from U+80 .. U+7FF, |
|
|
93 | // but use the very good perl function to parse anything else. |
|
|
94 | // note that we never call this function for a ascii codepoints |
|
|
95 | inline UV |
|
|
96 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
|
|
97 | { |
|
|
98 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
|
|
99 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
|
|
100 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
|
|
101 | { |
|
|
102 | *clen = 2; |
|
|
103 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
|
|
104 | } |
|
|
105 | else |
|
|
106 | { |
|
|
107 | *clen = (STRLEN)-1; |
|
|
108 | return (UV)-1; |
|
|
109 | } |
|
|
110 | } |
|
|
111 | |
|
|
112 | ///////////////////////////////////////////////////////////////////////////// |
|
|
113 | // encoder |
26 | |
114 | |
27 | // structure used for encoding JSON |
115 | // structure used for encoding JSON |
28 | typedef struct |
116 | typedef struct |
29 | { |
117 | { |
30 | char *cur; |
118 | char *cur; // SvPVX (sv) + current output position |
31 | STRLEN len; // SvLEN (sv) |
|
|
32 | char *end; // SvEND (sv) |
119 | char *end; // SvEND (sv) |
33 | SV *sv; |
120 | SV *sv; // result scalar |
34 | UV flags; |
121 | U32 flags; // F_* |
35 | int max_recurse; |
122 | U32 indent; // indentation level |
36 | int indent; |
123 | U32 maxdepth; // max. indentation/recursion level |
37 | } enc_t; |
124 | } enc_t; |
38 | |
125 | |
39 | // structure used for decoding JSON |
126 | inline void |
40 | typedef struct |
|
|
41 | { |
|
|
42 | char *cur; |
|
|
43 | char *end; |
|
|
44 | char *err; |
|
|
45 | UV flags; |
|
|
46 | } dec_t; |
|
|
47 | |
|
|
48 | static UV * |
|
|
49 | SvJSON (SV *sv) |
|
|
50 | { |
|
|
51 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
52 | croak ("object is not of type JSON::XS"); |
|
|
53 | |
|
|
54 | return &SvUVX (SvRV (sv)); |
|
|
55 | } |
|
|
56 | |
|
|
57 | ///////////////////////////////////////////////////////////////////////////// |
|
|
58 | |
|
|
59 | static void |
|
|
60 | need (enc_t *enc, STRLEN len) |
127 | need (enc_t *enc, STRLEN len) |
61 | { |
128 | { |
62 | if (enc->cur + len >= enc->end) |
129 | if (expect_false (enc->cur + len >= enc->end)) |
63 | { |
130 | { |
64 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
131 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
65 | SvGROW (enc->sv, cur + len + 1); |
132 | SvGROW (enc->sv, cur + len + 1); |
66 | enc->cur = SvPVX (enc->sv) + cur; |
133 | enc->cur = SvPVX (enc->sv) + cur; |
67 | enc->end = SvEND (enc->sv); |
134 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
68 | } |
135 | } |
69 | } |
136 | } |
70 | |
137 | |
71 | static void |
138 | inline void |
72 | encode_ch (enc_t *enc, char ch) |
139 | encode_ch (enc_t *enc, char ch) |
73 | { |
140 | { |
74 | need (enc, 1); |
141 | need (enc, 1); |
75 | *enc->cur++ = ch; |
142 | *enc->cur++ = ch; |
76 | } |
143 | } |
… | |
… | |
78 | static void |
145 | static void |
79 | encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
146 | encode_str (enc_t *enc, char *str, STRLEN len, int is_utf8) |
80 | { |
147 | { |
81 | char *end = str + len; |
148 | char *end = str + len; |
82 | |
149 | |
|
|
150 | need (enc, len); |
|
|
151 | |
83 | while (str < end) |
152 | while (str < end) |
84 | { |
153 | { |
85 | unsigned char ch = *(unsigned char *)str; |
154 | unsigned char ch = *(unsigned char *)str; |
|
|
155 | |
86 | if (ch >= 0x20 && ch < 0x80) // most common case |
156 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
87 | { |
157 | { |
|
|
158 | if (expect_false (ch == '"')) // but with slow exceptions |
|
|
159 | { |
|
|
160 | need (enc, len += 1); |
|
|
161 | *enc->cur++ = '\\'; |
|
|
162 | *enc->cur++ = '"'; |
|
|
163 | } |
|
|
164 | else if (expect_false (ch == '\\')) |
|
|
165 | { |
|
|
166 | need (enc, len += 1); |
|
|
167 | *enc->cur++ = '\\'; |
|
|
168 | *enc->cur++ = '\\'; |
|
|
169 | } |
|
|
170 | else |
88 | *enc->cur++ = ch; |
171 | *enc->cur++ = ch; |
|
|
172 | |
89 | str++; |
173 | ++str; |
90 | } |
174 | } |
91 | else |
175 | else |
92 | { |
176 | { |
93 | STRLEN clen; |
177 | switch (ch) |
94 | UV uch; |
|
|
95 | |
|
|
96 | if (is_utf8) |
|
|
97 | { |
178 | { |
98 | uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
179 | case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
99 | if (clen < 0) |
180 | case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
100 | croak ("malformed UTF-8 character in string, cannot convert to JSON"); |
181 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
101 | } |
182 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
102 | else |
183 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
103 | { |
|
|
104 | uch = ch; |
|
|
105 | clen = 1; |
|
|
106 | } |
|
|
107 | |
184 | |
108 | need (enc, len += 6); |
185 | default: |
109 | |
|
|
110 | if (uch < 0xa0 || enc->flags & F_ASCII) |
|
|
111 | { |
|
|
112 | if (uch > 0xFFFFUL) |
|
|
113 | { |
186 | { |
|
|
187 | STRLEN clen; |
|
|
188 | UV uch; |
|
|
189 | |
|
|
190 | if (is_utf8) |
|
|
191 | { |
|
|
192 | uch = decode_utf8 (str, end - str, &clen); |
|
|
193 | if (clen == (STRLEN)-1) |
|
|
194 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
|
|
195 | } |
|
|
196 | else |
|
|
197 | { |
|
|
198 | uch = ch; |
114 | len += 6; |
199 | clen = 1; |
|
|
200 | } |
|
|
201 | |
|
|
202 | if (uch > 0x10FFFFUL) |
|
|
203 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
204 | |
|
|
205 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
|
|
206 | { |
|
|
207 | if (uch > 0xFFFFUL) |
|
|
208 | { |
115 | need (enc, len += 6); |
209 | need (enc, len += 11); |
116 | sprintf (enc->cur, "\\u%04x\\u%04x", |
210 | sprintf (enc->cur, "\\u%04x\\u%04x", |
117 | (uch - 0x10000) / 0x400 + 0xD800, |
211 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
118 | (uch - 0x10000) % 0x400 + 0xDC00); |
212 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
119 | enc->cur += 12; |
213 | enc->cur += 12; |
|
|
214 | } |
|
|
215 | else |
|
|
216 | { |
|
|
217 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
218 | need (enc, len += 5); |
|
|
219 | *enc->cur++ = '\\'; |
|
|
220 | *enc->cur++ = 'u'; |
|
|
221 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
|
|
222 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
|
|
223 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
|
|
224 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
|
|
225 | } |
|
|
226 | |
|
|
227 | str += clen; |
120 | } |
228 | } |
|
|
229 | else if (enc->flags & F_LATIN1) |
|
|
230 | { |
|
|
231 | *enc->cur++ = uch; |
|
|
232 | str += clen; |
|
|
233 | } |
|
|
234 | else if (is_utf8) |
|
|
235 | { |
|
|
236 | need (enc, len += clen); |
|
|
237 | do |
|
|
238 | { |
|
|
239 | *enc->cur++ = *str++; |
|
|
240 | } |
|
|
241 | while (--clen); |
|
|
242 | } |
121 | else |
243 | else |
122 | { |
244 | { |
123 | sprintf (enc->cur, "\\u%04x", uch); |
245 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
124 | enc->cur += 6; |
246 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
247 | ++str; |
|
|
248 | } |
125 | } |
249 | } |
126 | } |
250 | } |
127 | else if (is_utf8) |
|
|
128 | { |
|
|
129 | memcpy (enc->cur, str, clen); |
|
|
130 | enc->cur += clen; |
|
|
131 | } |
|
|
132 | else |
|
|
133 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
|
|
134 | |
|
|
135 | str += clen; |
|
|
136 | } |
251 | } |
137 | |
252 | |
138 | --len; |
253 | --len; |
139 | } |
254 | } |
140 | } |
255 | } |
141 | |
256 | |
142 | #define INDENT SB \ |
257 | inline void |
|
|
258 | encode_indent (enc_t *enc) |
|
|
259 | { |
143 | if (enc->flags & F_INDENT) \ |
260 | if (enc->flags & F_INDENT) |
144 | { \ |
261 | { |
145 | int i_; \ |
262 | int spaces = enc->indent * INDENT_STEP; |
146 | need (enc, enc->indent); \ |
|
|
147 | for (i_ = enc->indent * 3; i_--; )\ |
|
|
148 | encode_ch (enc, ' '); \ |
|
|
149 | } \ |
|
|
150 | SE |
|
|
151 | |
263 | |
152 | #define SPACE SB need (enc, 1); encode_ch (enc, ' '); SE |
264 | need (enc, spaces); |
153 | #define NL SB if (enc->flags & F_INDENT) { need (enc, 1); encode_ch (enc, '\n'); } SE |
265 | memset (enc->cur, ' ', spaces); |
154 | #define COMMA SB \ |
266 | enc->cur += spaces; |
|
|
267 | } |
|
|
268 | } |
|
|
269 | |
|
|
270 | inline void |
|
|
271 | encode_space (enc_t *enc) |
|
|
272 | { |
|
|
273 | need (enc, 1); |
155 | encode_ch (enc, ','); \ |
274 | encode_ch (enc, ' '); |
|
|
275 | } |
|
|
276 | |
|
|
277 | inline void |
|
|
278 | encode_nl (enc_t *enc) |
|
|
279 | { |
156 | if (enc->flags & F_INDENT) \ |
280 | if (enc->flags & F_INDENT) |
157 | NL; \ |
281 | { |
|
|
282 | need (enc, 1); |
|
|
283 | encode_ch (enc, '\n'); |
|
|
284 | } |
|
|
285 | } |
|
|
286 | |
|
|
287 | inline void |
|
|
288 | encode_comma (enc_t *enc) |
|
|
289 | { |
|
|
290 | encode_ch (enc, ','); |
|
|
291 | |
|
|
292 | if (enc->flags & F_INDENT) |
|
|
293 | encode_nl (enc); |
158 | else if (enc->flags & F_SPACE_AFTER) \ |
294 | else if (enc->flags & F_SPACE_AFTER) |
159 | SPACE; \ |
295 | encode_space (enc); |
160 | SE |
296 | } |
161 | |
297 | |
162 | static void encode_sv (enc_t *enc, SV *sv); |
298 | static void encode_sv (enc_t *enc, SV *sv); |
163 | |
299 | |
164 | static void |
300 | static void |
165 | encode_av (enc_t *enc, AV *av) |
301 | encode_av (enc_t *enc, AV *av) |
166 | { |
302 | { |
167 | int i, len = av_len (av); |
303 | int i, len = av_len (av); |
168 | |
304 | |
169 | encode_ch (enc, '['); NL; |
305 | if (enc->indent >= enc->maxdepth) |
|
|
306 | croak ("data structure too deep (hit recursion limit)"); |
|
|
307 | |
|
|
308 | encode_ch (enc, '['); encode_nl (enc); |
170 | ++enc->indent; |
309 | ++enc->indent; |
171 | |
310 | |
172 | for (i = 0; i <= len; ++i) |
311 | for (i = 0; i <= len; ++i) |
173 | { |
312 | { |
174 | INDENT; |
313 | encode_indent (enc); |
175 | encode_sv (enc, *av_fetch (av, i, 0)); |
314 | encode_sv (enc, *av_fetch (av, i, 0)); |
176 | |
315 | |
177 | if (i < len) |
316 | if (i < len) |
178 | COMMA; |
317 | encode_comma (enc); |
179 | } |
318 | } |
180 | |
319 | |
181 | NL; |
320 | encode_nl (enc); |
182 | |
321 | |
183 | --enc->indent; |
322 | --enc->indent; |
184 | INDENT; encode_ch (enc, ']'); |
323 | encode_indent (enc); encode_ch (enc, ']'); |
185 | } |
324 | } |
186 | |
325 | |
187 | static void |
326 | static void |
188 | encode_he (enc_t *enc, HE *he) |
327 | encode_he (enc_t *enc, HE *he) |
189 | { |
328 | { |
… | |
… | |
191 | |
330 | |
192 | if (HeKLEN (he) == HEf_SVKEY) |
331 | if (HeKLEN (he) == HEf_SVKEY) |
193 | { |
332 | { |
194 | SV *sv = HeSVKEY (he); |
333 | SV *sv = HeSVKEY (he); |
195 | STRLEN len; |
334 | STRLEN len; |
|
|
335 | char *str; |
|
|
336 | |
|
|
337 | SvGETMAGIC (sv); |
196 | char *str = SvPV (sv, len); |
338 | str = SvPV (sv, len); |
197 | |
339 | |
198 | encode_str (enc, str, len, SvUTF8 (sv)); |
340 | encode_str (enc, str, len, SvUTF8 (sv)); |
199 | } |
341 | } |
200 | else |
342 | else |
201 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
343 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
202 | |
344 | |
203 | encode_ch (enc, '"'); |
345 | encode_ch (enc, '"'); |
204 | |
346 | |
205 | if (enc->flags & F_SPACE_BEFORE) SPACE; |
347 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
206 | encode_ch (enc, ':'); |
348 | encode_ch (enc, ':'); |
207 | if (enc->flags & F_SPACE_AFTER ) SPACE; |
349 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
208 | encode_sv (enc, HeVAL (he)); |
350 | encode_sv (enc, HeVAL (he)); |
209 | } |
351 | } |
210 | |
352 | |
211 | // compare hash entries, used when all keys are bytestrings |
353 | // compare hash entries, used when all keys are bytestrings |
212 | static int |
354 | static int |
… | |
… | |
218 | HE *b = *(HE **)b_; |
360 | HE *b = *(HE **)b_; |
219 | |
361 | |
220 | STRLEN la = HeKLEN (a); |
362 | STRLEN la = HeKLEN (a); |
221 | STRLEN lb = HeKLEN (b); |
363 | STRLEN lb = HeKLEN (b); |
222 | |
364 | |
223 | if (!(cmp == memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
365 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
224 | cmp = la < lb ? -1 : la == lb ? 0 : 1; |
366 | cmp = la - lb; |
225 | |
367 | |
226 | return cmp; |
368 | return cmp; |
227 | } |
369 | } |
228 | |
370 | |
229 | // compare hash entries, used when some keys are sv's or utf-x |
371 | // compare hash entries, used when some keys are sv's or utf-x |
… | |
… | |
236 | static void |
378 | static void |
237 | encode_hv (enc_t *enc, HV *hv) |
379 | encode_hv (enc_t *enc, HV *hv) |
238 | { |
380 | { |
239 | int count, i; |
381 | int count, i; |
240 | |
382 | |
|
|
383 | if (enc->indent >= enc->maxdepth) |
|
|
384 | croak ("data structure too deep (hit recursion limit)"); |
|
|
385 | |
241 | encode_ch (enc, '{'); NL; ++enc->indent; |
386 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
242 | |
387 | |
243 | if ((count = hv_iterinit (hv))) |
388 | if ((count = hv_iterinit (hv))) |
244 | { |
389 | { |
245 | // for canonical output we have to sort by keys first |
390 | // for canonical output we have to sort by keys first |
246 | // actually, this is mostly due to the stupid so-called |
391 | // actually, this is mostly due to the stupid so-called |
247 | // security workaround added somewhere in 5.8.x. |
392 | // security workaround added somewhere in 5.8.x. |
248 | // that randomises hash orderings |
393 | // that randomises hash orderings |
249 | if (enc->flags & F_CANONICAL) |
394 | if (enc->flags & F_CANONICAL) |
250 | { |
395 | { |
251 | HE *he, *hes [count]; |
|
|
252 | int fast = 1; |
396 | int fast = 1; |
|
|
397 | HE *he; |
|
|
398 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
399 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
400 | #else |
|
|
401 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
402 | #endif |
253 | |
403 | |
254 | i = 0; |
404 | i = 0; |
255 | while ((he = hv_iternext (hv))) |
405 | while ((he = hv_iternext (hv))) |
256 | { |
406 | { |
257 | hes [i++] = he; |
407 | hes [i++] = he; |
… | |
… | |
263 | |
413 | |
264 | if (fast) |
414 | if (fast) |
265 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
415 | qsort (hes, count, sizeof (HE *), he_cmp_fast); |
266 | else |
416 | else |
267 | { |
417 | { |
268 | // hack to disable "use bytes" |
418 | // hack to forcefully disable "use bytes" |
269 | COP *oldcop = PL_curcop, cop; |
419 | COP cop = *PL_curcop; |
270 | cop.op_private = 0; |
420 | cop.op_private = 0; |
|
|
421 | |
|
|
422 | ENTER; |
|
|
423 | SAVETMPS; |
|
|
424 | |
|
|
425 | SAVEVPTR (PL_curcop); |
271 | PL_curcop = &cop; |
426 | PL_curcop = &cop; |
272 | |
427 | |
273 | SAVETMPS; |
|
|
274 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
428 | qsort (hes, count, sizeof (HE *), he_cmp_slow); |
|
|
429 | |
275 | FREETMPS; |
430 | FREETMPS; |
276 | |
431 | LEAVE; |
277 | PL_curcop = oldcop; |
|
|
278 | } |
432 | } |
279 | |
433 | |
280 | for (i = 0; i < count; ++i) |
434 | for (i = 0; i < count; ++i) |
281 | { |
435 | { |
282 | INDENT; |
436 | encode_indent (enc); |
283 | encode_he (enc, hes [i]); |
437 | encode_he (enc, hes [i]); |
284 | |
438 | |
285 | if (i < count - 1) |
439 | if (i < count - 1) |
286 | COMMA; |
440 | encode_comma (enc); |
287 | } |
441 | } |
288 | |
442 | |
289 | NL; |
443 | encode_nl (enc); |
290 | } |
444 | } |
291 | else |
445 | else |
292 | { |
446 | { |
293 | SV *sv; |
|
|
294 | HE *he = hv_iternext (hv); |
447 | HE *he = hv_iternext (hv); |
295 | |
448 | |
296 | for (;;) |
449 | for (;;) |
297 | { |
450 | { |
298 | INDENT; |
451 | encode_indent (enc); |
299 | encode_he (enc, he); |
452 | encode_he (enc, he); |
300 | |
453 | |
301 | if (!(he = hv_iternext (hv))) |
454 | if (!(he = hv_iternext (hv))) |
302 | break; |
455 | break; |
303 | |
456 | |
304 | COMMA; |
457 | encode_comma (enc); |
305 | } |
458 | } |
306 | |
459 | |
307 | NL; |
460 | encode_nl (enc); |
308 | } |
461 | } |
309 | } |
462 | } |
310 | |
463 | |
311 | --enc->indent; INDENT; encode_ch (enc, '}'); |
464 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
465 | } |
|
|
466 | |
|
|
467 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
468 | static void |
|
|
469 | encode_rv (enc_t *enc, SV *sv) |
|
|
470 | { |
|
|
471 | svtype svt; |
|
|
472 | |
|
|
473 | SvGETMAGIC (sv); |
|
|
474 | svt = SvTYPE (sv); |
|
|
475 | |
|
|
476 | if (expect_false (SvOBJECT (sv))) |
|
|
477 | { |
|
|
478 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
479 | { |
|
|
480 | if (SvIV (sv) == 0) |
|
|
481 | encode_str (enc, "false", 5, 0); |
|
|
482 | else |
|
|
483 | encode_str (enc, "true", 4, 0); |
|
|
484 | } |
|
|
485 | else |
|
|
486 | { |
|
|
487 | #if 0 |
|
|
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
489 | { |
|
|
490 | // not yet |
|
|
491 | } |
|
|
492 | #endif |
|
|
493 | if (enc->flags & F_CONV_BLESSED) |
|
|
494 | { |
|
|
495 | // we re-bless the reference to get overload and other niceties right |
|
|
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
497 | |
|
|
498 | if (to_json) |
|
|
499 | { |
|
|
500 | dSP; |
|
|
501 | ENTER; |
|
|
502 | SAVETMPS; |
|
|
503 | PUSHMARK (SP); |
|
|
504 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
505 | |
|
|
506 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
507 | // check anyways. |
|
|
508 | PUTBACK; |
|
|
509 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
510 | SPAGAIN; |
|
|
511 | |
|
|
512 | encode_sv (enc, POPs); |
|
|
513 | |
|
|
514 | FREETMPS; |
|
|
515 | LEAVE; |
|
|
516 | } |
|
|
517 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
518 | encode_str (enc, "null", 4, 0); |
|
|
519 | else |
|
|
520 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
522 | } |
|
|
523 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
524 | encode_str (enc, "null", 4, 0); |
|
|
525 | else |
|
|
526 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
528 | } |
|
|
529 | } |
|
|
530 | else if (svt == SVt_PVHV) |
|
|
531 | encode_hv (enc, (HV *)sv); |
|
|
532 | else if (svt == SVt_PVAV) |
|
|
533 | encode_av (enc, (AV *)sv); |
|
|
534 | else if (svt < SVt_PVAV) |
|
|
535 | { |
|
|
536 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
537 | encode_str (enc, "false", 5, 0); |
|
|
538 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
539 | encode_str (enc, "true", 4, 0); |
|
|
540 | else |
|
|
541 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
542 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
543 | } |
|
|
544 | else |
|
|
545 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
546 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
312 | } |
547 | } |
313 | |
548 | |
314 | static void |
549 | static void |
315 | encode_sv (enc_t *enc, SV *sv) |
550 | encode_sv (enc_t *enc, SV *sv) |
316 | { |
551 | { |
|
|
552 | SvGETMAGIC (sv); |
|
|
553 | |
317 | if (SvPOKp (sv)) |
554 | if (SvPOKp (sv)) |
318 | { |
555 | { |
319 | STRLEN len; |
556 | STRLEN len; |
320 | char *str = SvPV (sv, len); |
557 | char *str = SvPV (sv, len); |
321 | encode_ch (enc, '"'); |
558 | encode_ch (enc, '"'); |
322 | encode_str (enc, str, len, SvUTF8 (sv)); |
559 | encode_str (enc, str, len, SvUTF8 (sv)); |
323 | encode_ch (enc, '"'); |
560 | encode_ch (enc, '"'); |
324 | } |
561 | } |
325 | else if (SvNOKp (sv)) |
562 | else if (SvNOKp (sv)) |
326 | { |
563 | { |
|
|
564 | // trust that perl will do the right thing w.r.t. JSON syntax. |
327 | need (enc, NV_DIG + 32); |
565 | need (enc, NV_DIG + 32); |
328 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
566 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
329 | enc->cur += strlen (enc->cur); |
567 | enc->cur += strlen (enc->cur); |
330 | } |
568 | } |
331 | else if (SvIOKp (sv)) |
569 | else if (SvIOKp (sv)) |
332 | { |
570 | { |
333 | need (enc, 64); |
571 | // we assume we can always read an IV as a UV |
|
|
572 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
573 | { |
|
|
574 | // large integer, use the (rather slow) snprintf way. |
|
|
575 | need (enc, sizeof (UV) * 3); |
334 | enc->cur += |
576 | enc->cur += |
335 | SvIsUV(sv) |
577 | SvIsUV(sv) |
336 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
578 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
337 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
579 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
580 | } |
|
|
581 | else |
|
|
582 | { |
|
|
583 | // optimise the "small number case" |
|
|
584 | // code will likely be branchless and use only a single multiplication |
|
|
585 | I32 i = SvIV (sv); |
|
|
586 | U32 u; |
|
|
587 | char digit, nz = 0; |
|
|
588 | |
|
|
589 | need (enc, 6); |
|
|
590 | |
|
|
591 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
592 | u = i < 0 ? -i : i; |
|
|
593 | |
|
|
594 | // convert to 4.28 fixed-point representation |
|
|
595 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
596 | |
|
|
597 | // now output digit by digit, each time masking out the integer part |
|
|
598 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
599 | // resulting in a net multiplication by 10. |
|
|
600 | // we always write the digit to memory but conditionally increment |
|
|
601 | // the pointer, to ease the usage of conditional move instructions. |
|
|
602 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
603 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
604 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
605 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
606 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
607 | } |
338 | } |
608 | } |
339 | else if (SvROK (sv)) |
609 | else if (SvROK (sv)) |
340 | { |
610 | encode_rv (enc, SvRV (sv)); |
341 | if (!--enc->max_recurse) |
|
|
342 | croak ("data structure too deep (hit recursion limit)"); |
|
|
343 | |
|
|
344 | sv = SvRV (sv); |
|
|
345 | |
|
|
346 | switch (SvTYPE (sv)) |
|
|
347 | { |
|
|
348 | case SVt_PVAV: encode_av (enc, (AV *)sv); break; |
|
|
349 | case SVt_PVHV: encode_hv (enc, (HV *)sv); break; |
|
|
350 | |
|
|
351 | default: |
|
|
352 | croak ("JSON can only represent references to arrays or hashes"); |
|
|
353 | } |
|
|
354 | } |
|
|
355 | else if (!SvOK (sv)) |
611 | else if (!SvOK (sv)) |
356 | encode_str (enc, "null", 4, 0); |
612 | encode_str (enc, "null", 4, 0); |
357 | else |
613 | else |
358 | croak ("encountered perl type that JSON cannot handle"); |
614 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
|
|
615 | SvPV_nolen (sv), SvFLAGS (sv)); |
359 | } |
616 | } |
360 | |
617 | |
361 | static SV * |
618 | static SV * |
362 | encode_json (SV *scalar, UV flags) |
619 | encode_json (SV *scalar, U32 flags) |
363 | { |
620 | { |
364 | enc_t enc; |
621 | enc_t enc; |
|
|
622 | |
|
|
623 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
|
|
624 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
|
|
625 | |
365 | enc.flags = flags; |
626 | enc.flags = flags; |
366 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
367 | enc.cur = SvPVX (enc.sv); |
628 | enc.cur = SvPVX (enc.sv); |
368 | enc.end = SvEND (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
369 | enc.max_recurse = 0; |
|
|
370 | enc.indent = 0; |
630 | enc.indent = 0; |
|
|
631 | enc.maxdepth = DEC_DEPTH (flags); |
371 | |
632 | |
372 | SvPOK_only (enc.sv); |
633 | SvPOK_only (enc.sv); |
373 | encode_sv (&enc, scalar); |
634 | encode_sv (&enc, scalar); |
374 | |
635 | |
|
|
636 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
637 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
638 | |
375 | if (!(flags & (F_ASCII | F_UTF8))) |
639 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
376 | SvUTF8_on (enc.sv); |
640 | SvUTF8_on (enc.sv); |
377 | |
641 | |
378 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
642 | if (enc.flags & F_SHRINK) |
|
|
643 | shrink (enc.sv); |
|
|
644 | |
379 | return enc.sv; |
645 | return enc.sv; |
380 | } |
646 | } |
381 | |
647 | |
382 | ///////////////////////////////////////////////////////////////////////////// |
648 | ///////////////////////////////////////////////////////////////////////////// |
|
|
649 | // decoder |
383 | |
650 | |
384 | #define WS \ |
651 | // structure used for decoding JSON |
|
|
652 | typedef struct |
|
|
653 | { |
|
|
654 | char *cur; // current parser pointer |
|
|
655 | char *end; // end of input string |
|
|
656 | const char *err; // parse error, if != 0 |
|
|
657 | U32 flags; // F_* |
|
|
658 | U32 depth; // recursion depth |
|
|
659 | U32 maxdepth; // recursion depth limit |
|
|
660 | } dec_t; |
|
|
661 | |
|
|
662 | inline void |
|
|
663 | decode_ws (dec_t *dec) |
|
|
664 | { |
385 | for (;;) \ |
665 | for (;;) |
386 | { \ |
666 | { |
387 | char ch = *dec->cur; \ |
667 | char ch = *dec->cur; |
|
|
668 | |
388 | if (ch > 0x20 \ |
669 | if (ch > 0x20 |
389 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) \ |
670 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
390 | break; \ |
671 | break; |
|
|
672 | |
391 | ++dec->cur; \ |
673 | ++dec->cur; |
392 | } |
674 | } |
|
|
675 | } |
393 | |
676 | |
394 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
677 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
678 | |
395 | #define EXPECT_CH(ch) SB \ |
679 | #define EXPECT_CH(ch) SB \ |
396 | if (*dec->cur != ch) \ |
680 | if (*dec->cur != ch) \ |
397 | ERR (# ch " expected"); \ |
681 | ERR (# ch " expected"); \ |
398 | ++dec->cur; \ |
682 | ++dec->cur; \ |
399 | SE |
683 | SE |
400 | |
684 | |
|
|
685 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
686 | #define DEC_DEC_DEPTH --dec->depth |
|
|
687 | |
401 | static SV *decode_sv (dec_t *dec); |
688 | static SV *decode_sv (dec_t *dec); |
402 | |
|
|
403 | #define APPEND_CH(ch) SB \ |
|
|
404 | SvGROW (sv, cur + 1 + 1); \ |
|
|
405 | SvPVX (sv)[cur++] = (ch); \ |
|
|
406 | SE |
|
|
407 | |
689 | |
408 | static signed char decode_hexdigit[256]; |
690 | static signed char decode_hexdigit[256]; |
409 | |
691 | |
410 | static UV |
692 | static UV |
411 | decode_4hex (dec_t *dec) |
693 | decode_4hex (dec_t *dec) |
412 | { |
694 | { |
413 | signed char d1, d2, d3, d4; |
695 | signed char d1, d2, d3, d4; |
|
|
696 | unsigned char *cur = (unsigned char *)dec->cur; |
414 | |
697 | |
415 | d1 = decode_hexdigit [((unsigned char *)dec->cur) [0]]; |
698 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
416 | if (d1 < 0) ERR ("four hexadecimal digits expected"); |
699 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
417 | d2 = decode_hexdigit [((unsigned char *)dec->cur) [1]]; |
700 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
418 | if (d2 < 0) ERR ("four hexadecimal digits expected"); |
701 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
419 | d3 = decode_hexdigit [((unsigned char *)dec->cur) [2]]; |
|
|
420 | if (d3 < 0) ERR ("four hexadecimal digits expected"); |
|
|
421 | d4 = decode_hexdigit [((unsigned char *)dec->cur) [3]]; |
|
|
422 | if (d4 < 0) ERR ("four hexadecimal digits expected"); |
|
|
423 | |
702 | |
424 | dec->cur += 4; |
703 | dec->cur += 4; |
425 | |
704 | |
426 | return ((UV)d1) << 12 |
705 | return ((UV)d1) << 12 |
427 | | ((UV)d2) << 8 |
706 | | ((UV)d2) << 8 |
… | |
… | |
433 | } |
712 | } |
434 | |
713 | |
435 | static SV * |
714 | static SV * |
436 | decode_str (dec_t *dec) |
715 | decode_str (dec_t *dec) |
437 | { |
716 | { |
438 | SV *sv = NEWSV (0,2); |
717 | SV *sv = 0; |
439 | STRLEN cur = 0; |
|
|
440 | int utf8 = 0; |
718 | int utf8 = 0; |
|
|
719 | char *dec_cur = dec->cur; |
441 | |
720 | |
442 | for (;;) |
721 | do |
443 | { |
722 | { |
444 | unsigned char ch = *(unsigned char *)dec->cur; |
723 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
|
|
724 | char *cur = buf; |
445 | |
725 | |
446 | if (ch == '"') |
726 | do |
447 | break; |
|
|
448 | else if (ch == '\\') |
|
|
449 | { |
727 | { |
450 | switch (*++dec->cur) |
728 | unsigned char ch = *(unsigned char *)dec_cur++; |
|
|
729 | |
|
|
730 | if (expect_false (ch == '"')) |
451 | { |
731 | { |
452 | case '\\': |
732 | --dec_cur; |
453 | case '/': |
733 | break; |
454 | case '"': APPEND_CH (*dec->cur++); break; |
734 | } |
455 | |
735 | else if (expect_false (ch == '\\')) |
456 | case 'b': APPEND_CH ('\010'); ++dec->cur; break; |
736 | { |
457 | case 't': APPEND_CH ('\011'); ++dec->cur; break; |
737 | switch (*dec_cur) |
458 | case 'n': APPEND_CH ('\012'); ++dec->cur; break; |
|
|
459 | case 'f': APPEND_CH ('\014'); ++dec->cur; break; |
|
|
460 | case 'r': APPEND_CH ('\015'); ++dec->cur; break; |
|
|
461 | |
|
|
462 | case 'u': |
|
|
463 | { |
738 | { |
464 | UV lo, hi; |
739 | case '\\': |
465 | ++dec->cur; |
740 | case '/': |
|
|
741 | case '"': *cur++ = *dec_cur++; break; |
466 | |
742 | |
467 | hi = decode_4hex (dec); |
743 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
468 | if (hi == (UV)-1) |
744 | case 't': ++dec_cur; *cur++ = '\011'; break; |
469 | goto fail; |
745 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
|
|
746 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
|
|
747 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
470 | |
748 | |
471 | // possibly a surrogate pair |
749 | case 'u': |
472 | if (hi >= 0xd800 && hi < 0xdc00) |
|
|
473 | { |
750 | { |
474 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
751 | UV lo, hi; |
475 | ERR ("illegal surrogate character"); |
752 | ++dec_cur; |
476 | |
753 | |
477 | dec->cur += 2; |
754 | dec->cur = dec_cur; |
478 | |
|
|
479 | lo = decode_4hex (dec); |
755 | hi = decode_4hex (dec); |
|
|
756 | dec_cur = dec->cur; |
480 | if (lo == (UV)-1) |
757 | if (hi == (UV)-1) |
481 | goto fail; |
758 | goto fail; |
482 | |
759 | |
|
|
760 | // possibly a surrogate pair |
|
|
761 | if (hi >= 0xd800) |
|
|
762 | if (hi < 0xdc00) |
|
|
763 | { |
|
|
764 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
|
|
765 | ERR ("missing low surrogate character in surrogate pair"); |
|
|
766 | |
|
|
767 | dec_cur += 2; |
|
|
768 | |
|
|
769 | dec->cur = dec_cur; |
|
|
770 | lo = decode_4hex (dec); |
|
|
771 | dec_cur = dec->cur; |
|
|
772 | if (lo == (UV)-1) |
|
|
773 | goto fail; |
|
|
774 | |
483 | if (lo < 0xdc00 || lo >= 0xe000) |
775 | if (lo < 0xdc00 || lo >= 0xe000) |
484 | ERR ("surrogate pair expected"); |
776 | ERR ("surrogate pair expected"); |
485 | |
777 | |
486 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
778 | hi = (hi - 0xD800) * 0x400 + (lo - 0xDC00) + 0x10000; |
|
|
779 | } |
|
|
780 | else if (hi < 0xe000) |
|
|
781 | ERR ("missing high surrogate character in surrogate pair"); |
|
|
782 | |
|
|
783 | if (hi >= 0x80) |
|
|
784 | { |
|
|
785 | utf8 = 1; |
|
|
786 | |
|
|
787 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
|
|
788 | } |
|
|
789 | else |
|
|
790 | *cur++ = hi; |
487 | } |
791 | } |
488 | else if (lo >= 0xdc00 && lo < 0xe000) |
|
|
489 | ERR ("illegal surrogate character"); |
|
|
490 | |
|
|
491 | if (hi >= 0x80) |
|
|
492 | { |
792 | break; |
493 | utf8 = 1; |
|
|
494 | |
793 | |
495 | SvGROW (sv, cur + 4 + 1); // at most 4 bytes for 21 bits |
|
|
496 | cur = (char *)uvuni_to_utf8_flags (SvPVX (sv) + cur, hi, 0) - SvPVX (sv); |
|
|
497 | } |
|
|
498 | else |
794 | default: |
499 | APPEND_CH (hi); |
795 | --dec_cur; |
|
|
796 | ERR ("illegal backslash escape sequence in string"); |
500 | } |
797 | } |
|
|
798 | } |
|
|
799 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
|
|
800 | *cur++ = ch; |
|
|
801 | else if (ch >= 0x80) |
|
|
802 | { |
|
|
803 | STRLEN clen; |
|
|
804 | UV uch; |
|
|
805 | |
|
|
806 | --dec_cur; |
|
|
807 | |
|
|
808 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
|
|
809 | if (clen == (STRLEN)-1) |
|
|
810 | ERR ("malformed UTF-8 character in JSON string"); |
|
|
811 | |
|
|
812 | do |
|
|
813 | *cur++ = *dec_cur++; |
|
|
814 | while (--clen); |
|
|
815 | |
|
|
816 | utf8 = 1; |
|
|
817 | } |
|
|
818 | else |
|
|
819 | { |
|
|
820 | --dec_cur; |
|
|
821 | |
|
|
822 | if (!ch) |
|
|
823 | ERR ("unexpected end of string while parsing JSON string"); |
501 | break; |
824 | else |
|
|
825 | ERR ("invalid character encountered while parsing JSON string"); |
502 | } |
826 | } |
503 | } |
827 | } |
504 | else if (ch >= 0x20 && ch <= 0x7f) |
828 | while (cur < buf + SHORT_STRING_LEN); |
505 | APPEND_CH (*dec->cur++); |
829 | |
506 | else if (ch >= 0x80) |
830 | { |
|
|
831 | STRLEN len = cur - buf; |
|
|
832 | |
|
|
833 | if (sv) |
507 | { |
834 | { |
508 | STRLEN clen; |
835 | SvGROW (sv, SvCUR (sv) + len + 1); |
509 | UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); |
836 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
510 | if (clen < 0) |
837 | SvCUR_set (sv, SvCUR (sv) + len); |
511 | ERR ("malformed UTF-8 character in string, cannot convert to JSON"); |
|
|
512 | |
|
|
513 | SvGROW (sv, cur + clen + 1); // at most 4 bytes for 21 bits |
|
|
514 | memcpy (SvPVX (sv) + cur, dec->cur, clen); |
|
|
515 | dec->cur += clen; |
|
|
516 | } |
838 | } |
517 | else |
839 | else |
518 | ERR ("invalid character encountered"); |
840 | sv = newSVpvn (buf, len); |
519 | } |
841 | } |
|
|
842 | } |
|
|
843 | while (*dec_cur != '"'); |
520 | |
844 | |
521 | ++dec->cur; |
845 | ++dec_cur; |
522 | |
846 | |
|
|
847 | if (sv) |
|
|
848 | { |
523 | SvPOK_only (sv); |
849 | SvPOK_only (sv); |
524 | |
|
|
525 | SvCUR_set (sv, cur); |
|
|
526 | *SvEND (sv) = 0; |
850 | *SvEND (sv) = 0; |
527 | |
851 | |
528 | if (utf8) |
852 | if (utf8) |
529 | SvUTF8_on (sv); |
853 | SvUTF8_on (sv); |
|
|
854 | } |
|
|
855 | else |
|
|
856 | sv = newSVpvn ("", 0); |
530 | |
857 | |
|
|
858 | dec->cur = dec_cur; |
531 | return sv; |
859 | return sv; |
532 | |
860 | |
533 | fail: |
861 | fail: |
534 | SvREFCNT_dec (sv); |
862 | dec->cur = dec_cur; |
535 | return 0; |
863 | return 0; |
536 | } |
864 | } |
537 | |
865 | |
538 | static SV * |
866 | static SV * |
539 | decode_num (dec_t *dec) |
867 | decode_num (dec_t *dec) |
… | |
… | |
549 | { |
877 | { |
550 | ++dec->cur; |
878 | ++dec->cur; |
551 | if (*dec->cur >= '0' && *dec->cur <= '9') |
879 | if (*dec->cur >= '0' && *dec->cur <= '9') |
552 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
880 | ERR ("malformed number (leading zero must not be followed by another digit)"); |
553 | } |
881 | } |
554 | |
882 | else if (*dec->cur < '0' || *dec->cur > '9') |
555 | // int |
883 | ERR ("malformed number (no digits after initial minus)"); |
|
|
884 | else |
|
|
885 | do |
|
|
886 | { |
|
|
887 | ++dec->cur; |
|
|
888 | } |
556 | while (*dec->cur >= '0' && *dec->cur <= '9') |
889 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
557 | ++dec->cur; |
|
|
558 | |
890 | |
559 | // [frac] |
891 | // [frac] |
560 | if (*dec->cur == '.') |
892 | if (*dec->cur == '.') |
561 | { |
893 | { |
562 | is_nv = 1; |
894 | ++dec->cur; |
|
|
895 | |
|
|
896 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
897 | ERR ("malformed number (no digits after decimal point)"); |
563 | |
898 | |
564 | do |
899 | do |
565 | { |
900 | { |
566 | ++dec->cur; |
901 | ++dec->cur; |
567 | } |
902 | } |
568 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
903 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
|
|
904 | |
|
|
905 | is_nv = 1; |
569 | } |
906 | } |
570 | |
907 | |
571 | // [exp] |
908 | // [exp] |
572 | if (*dec->cur == 'e' || *dec->cur == 'E') |
909 | if (*dec->cur == 'e' || *dec->cur == 'E') |
573 | { |
910 | { |
574 | is_nv = 1; |
|
|
575 | |
|
|
576 | ++dec->cur; |
911 | ++dec->cur; |
|
|
912 | |
577 | if (*dec->cur == '-' || *dec->cur == '+') |
913 | if (*dec->cur == '-' || *dec->cur == '+') |
578 | ++dec->cur; |
914 | ++dec->cur; |
579 | |
915 | |
|
|
916 | if (*dec->cur < '0' || *dec->cur > '9') |
|
|
917 | ERR ("malformed number (no digits after exp sign)"); |
|
|
918 | |
|
|
919 | do |
|
|
920 | { |
|
|
921 | ++dec->cur; |
|
|
922 | } |
580 | while (*dec->cur >= '0' && *dec->cur <= '9') |
923 | while (*dec->cur >= '0' && *dec->cur <= '9'); |
581 | ++dec->cur; |
924 | |
|
|
925 | is_nv = 1; |
582 | } |
926 | } |
583 | |
927 | |
584 | if (!is_nv) |
928 | if (!is_nv) |
585 | { |
929 | { |
586 | UV uv; |
930 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
587 | int numtype = grok_number (start, dec->cur - start, &uv); |
931 | if (*start == '-') |
588 | if (numtype & IS_NUMBER_IN_UV) |
932 | switch (dec->cur - start) |
589 | if (numtype & IS_NUMBER_NEG) |
|
|
590 | { |
933 | { |
591 | if (uv < (UV)IV_MIN) |
934 | case 2: return newSViv (-( start [1] - '0' * 1)); |
592 | return newSViv (-(IV)uv); |
935 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
936 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
937 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
593 | } |
938 | } |
|
|
939 | else |
|
|
940 | switch (dec->cur - start) |
|
|
941 | { |
|
|
942 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
943 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
944 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
945 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
946 | } |
|
|
947 | |
|
|
948 | { |
|
|
949 | UV uv; |
|
|
950 | int numtype = grok_number (start, dec->cur - start, &uv); |
|
|
951 | if (numtype & IS_NUMBER_IN_UV) |
|
|
952 | if (numtype & IS_NUMBER_NEG) |
|
|
953 | { |
|
|
954 | if (uv < (UV)IV_MIN) |
|
|
955 | return newSViv (-(IV)uv); |
|
|
956 | } |
594 | else |
957 | else |
595 | return newSVuv (uv); |
958 | return newSVuv (uv); |
|
|
959 | |
|
|
960 | // here would likely be the place for bigint support |
596 | } |
961 | } |
|
|
962 | } |
597 | |
963 | |
|
|
964 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
598 | return newSVnv (Atof (start)); |
965 | return newSVnv (Atof (start)); |
599 | |
966 | |
600 | fail: |
967 | fail: |
601 | return 0; |
968 | return 0; |
602 | } |
969 | } |
… | |
… | |
604 | static SV * |
971 | static SV * |
605 | decode_av (dec_t *dec) |
972 | decode_av (dec_t *dec) |
606 | { |
973 | { |
607 | AV *av = newAV (); |
974 | AV *av = newAV (); |
608 | |
975 | |
|
|
976 | DEC_INC_DEPTH; |
|
|
977 | decode_ws (dec); |
|
|
978 | |
|
|
979 | if (*dec->cur == ']') |
|
|
980 | ++dec->cur; |
|
|
981 | else |
609 | for (;;) |
982 | for (;;) |
610 | { |
983 | { |
611 | SV *value; |
984 | SV *value; |
612 | |
985 | |
613 | value = decode_sv (dec); |
986 | value = decode_sv (dec); |
614 | if (!value) |
987 | if (!value) |
615 | goto fail; |
988 | goto fail; |
616 | |
989 | |
617 | av_push (av, value); |
990 | av_push (av, value); |
618 | |
991 | |
619 | WS; |
992 | decode_ws (dec); |
620 | |
993 | |
621 | if (*dec->cur == ']') |
994 | if (*dec->cur == ']') |
622 | { |
995 | { |
623 | ++dec->cur; |
996 | ++dec->cur; |
624 | break; |
997 | break; |
|
|
998 | } |
625 | } |
999 | |
626 | |
|
|
627 | if (*dec->cur != ',') |
1000 | if (*dec->cur != ',') |
628 | ERR (", or ] expected while parsing array"); |
1001 | ERR (", or ] expected while parsing array"); |
629 | |
1002 | |
630 | ++dec->cur; |
1003 | ++dec->cur; |
631 | } |
1004 | } |
632 | |
1005 | |
|
|
1006 | DEC_DEC_DEPTH; |
633 | return newRV_noinc ((SV *)av); |
1007 | return newRV_noinc ((SV *)av); |
634 | |
1008 | |
635 | fail: |
1009 | fail: |
636 | SvREFCNT_dec (av); |
1010 | SvREFCNT_dec (av); |
|
|
1011 | DEC_DEC_DEPTH; |
637 | return 0; |
1012 | return 0; |
638 | } |
1013 | } |
639 | |
1014 | |
640 | static SV * |
1015 | static SV * |
641 | decode_hv (dec_t *dec) |
1016 | decode_hv (dec_t *dec) |
642 | { |
1017 | { |
643 | HV *hv = newHV (); |
1018 | HV *hv = newHV (); |
644 | |
1019 | |
|
|
1020 | DEC_INC_DEPTH; |
|
|
1021 | decode_ws (dec); |
|
|
1022 | |
|
|
1023 | if (*dec->cur == '}') |
|
|
1024 | ++dec->cur; |
|
|
1025 | else |
645 | for (;;) |
1026 | for (;;) |
646 | { |
1027 | { |
647 | SV *key, *value; |
1028 | SV *key, *value; |
648 | |
1029 | |
649 | WS; EXPECT_CH ('"'); |
1030 | decode_ws (dec); EXPECT_CH ('"'); |
650 | |
1031 | |
651 | key = decode_str (dec); |
1032 | key = decode_str (dec); |
652 | if (!key) |
1033 | if (!key) |
653 | goto fail; |
|
|
654 | |
|
|
655 | WS; EXPECT_CH (':'); |
|
|
656 | |
|
|
657 | value = decode_sv (dec); |
|
|
658 | if (!value) |
|
|
659 | { |
|
|
660 | SvREFCNT_dec (key); |
|
|
661 | goto fail; |
1034 | goto fail; |
|
|
1035 | |
|
|
1036 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1037 | |
|
|
1038 | value = decode_sv (dec); |
|
|
1039 | if (!value) |
|
|
1040 | { |
|
|
1041 | SvREFCNT_dec (key); |
|
|
1042 | goto fail; |
662 | } |
1043 | } |
663 | |
1044 | |
664 | //TODO: optimise |
|
|
665 | hv_store_ent (hv, key, value, 0); |
1045 | hv_store_ent (hv, key, value, 0); |
|
|
1046 | SvREFCNT_dec (key); |
666 | |
1047 | |
667 | WS; |
1048 | decode_ws (dec); |
668 | |
1049 | |
669 | if (*dec->cur == '}') |
1050 | if (*dec->cur == '}') |
670 | { |
1051 | { |
671 | ++dec->cur; |
1052 | ++dec->cur; |
672 | break; |
1053 | break; |
673 | } |
1054 | } |
674 | |
1055 | |
675 | if (*dec->cur != ',') |
1056 | if (*dec->cur != ',') |
676 | ERR (", or } expected while parsing object/hash"); |
1057 | ERR (", or } expected while parsing object/hash"); |
677 | |
1058 | |
678 | ++dec->cur; |
1059 | ++dec->cur; |
679 | } |
1060 | } |
680 | |
1061 | |
|
|
1062 | DEC_DEC_DEPTH; |
681 | return newRV_noinc ((SV *)hv); |
1063 | return newRV_noinc ((SV *)hv); |
682 | |
1064 | |
683 | fail: |
1065 | fail: |
684 | SvREFCNT_dec (hv); |
1066 | SvREFCNT_dec (hv); |
|
|
1067 | DEC_DEC_DEPTH; |
685 | return 0; |
1068 | return 0; |
686 | } |
1069 | } |
687 | |
1070 | |
688 | static SV * |
1071 | static SV * |
689 | decode_sv (dec_t *dec) |
1072 | decode_sv (dec_t *dec) |
690 | { |
1073 | { |
691 | WS; |
1074 | decode_ws (dec); |
|
|
1075 | |
|
|
1076 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1077 | // to parse anything. |
692 | switch (*dec->cur) |
1078 | switch (*dec->cur) |
693 | { |
1079 | { |
694 | case '"': ++dec->cur; return decode_str (dec); |
1080 | case '"': ++dec->cur; return decode_str (dec); |
695 | case '[': ++dec->cur; return decode_av (dec); |
1081 | case '[': ++dec->cur; return decode_av (dec); |
696 | case '{': ++dec->cur; return decode_hv (dec); |
1082 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
702 | |
1088 | |
703 | case 't': |
1089 | case 't': |
704 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1090 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
705 | { |
1091 | { |
706 | dec->cur += 4; |
1092 | dec->cur += 4; |
707 | return newSViv (1); |
1093 | return SvREFCNT_inc (json_true); |
708 | } |
1094 | } |
709 | else |
1095 | else |
710 | ERR ("'true' expected"); |
1096 | ERR ("'true' expected"); |
711 | |
1097 | |
712 | break; |
1098 | break; |
713 | |
1099 | |
714 | case 'f': |
1100 | case 'f': |
715 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1101 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
716 | { |
1102 | { |
717 | dec->cur += 5; |
1103 | dec->cur += 5; |
718 | return newSViv (0); |
1104 | return SvREFCNT_inc (json_false); |
719 | } |
1105 | } |
720 | else |
1106 | else |
721 | ERR ("'false' expected"); |
1107 | ERR ("'false' expected"); |
722 | |
1108 | |
723 | break; |
1109 | break; |
724 | |
1110 | |
725 | case 'n': |
1111 | case 'n': |
726 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
1112 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "null", 4)) |
727 | { |
1113 | { |
728 | dec->cur += 4; |
1114 | dec->cur += 4; |
729 | return newSViv (1); |
1115 | return newSVsv (&PL_sv_undef); |
730 | } |
1116 | } |
731 | else |
1117 | else |
732 | ERR ("'null' expected"); |
1118 | ERR ("'null' expected"); |
733 | |
1119 | |
734 | break; |
1120 | break; |
735 | |
1121 | |
736 | default: |
1122 | default: |
737 | ERR ("malformed json string"); |
1123 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
738 | break; |
1124 | break; |
739 | } |
1125 | } |
740 | |
1126 | |
741 | fail: |
1127 | fail: |
742 | return 0; |
1128 | return 0; |
743 | } |
1129 | } |
744 | |
1130 | |
745 | static SV * |
1131 | static SV * |
746 | decode_json (SV *string, UV flags) |
1132 | decode_json (SV *string, U32 flags, UV *offset_return) |
747 | { |
1133 | { |
|
|
1134 | dec_t dec; |
|
|
1135 | UV offset; |
748 | SV *sv; |
1136 | SV *sv; |
749 | |
1137 | |
|
|
1138 | SvGETMAGIC (string); |
|
|
1139 | SvUPGRADE (string, SVt_PV); |
|
|
1140 | |
|
|
1141 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
|
|
1142 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1143 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); |
|
|
1144 | |
750 | if (!(flags & F_UTF8)) |
1145 | if (flags & F_UTF8) |
|
|
1146 | sv_utf8_downgrade (string, 0); |
|
|
1147 | else |
751 | sv_utf8_upgrade (string); |
1148 | sv_utf8_upgrade (string); |
752 | |
1149 | |
753 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1150 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
754 | |
1151 | |
755 | dec_t dec; |
|
|
756 | dec.flags = flags; |
1152 | dec.flags = flags; |
757 | dec.cur = SvPVX (string); |
1153 | dec.cur = SvPVX (string); |
758 | dec.end = SvEND (string); |
1154 | dec.end = SvEND (string); |
759 | dec.err = 0; |
1155 | dec.err = 0; |
|
|
1156 | dec.depth = 0; |
|
|
1157 | dec.maxdepth = DEC_DEPTH (dec.flags); |
760 | |
1158 | |
761 | *dec.end = 1; // invalid anywhere |
1159 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
762 | sv = decode_sv (&dec); |
1160 | sv = decode_sv (&dec); |
763 | *dec.end = 0; |
1161 | |
|
|
1162 | if (!(offset_return || !sv)) |
|
|
1163 | { |
|
|
1164 | // check for trailing garbage |
|
|
1165 | decode_ws (&dec); |
|
|
1166 | |
|
|
1167 | if (*dec.cur) |
|
|
1168 | { |
|
|
1169 | dec.err = "garbage after JSON object"; |
|
|
1170 | SvREFCNT_dec (sv); |
|
|
1171 | sv = 0; |
|
|
1172 | } |
|
|
1173 | } |
|
|
1174 | |
|
|
1175 | if (offset_return || !sv) |
|
|
1176 | { |
|
|
1177 | offset = dec.flags & F_UTF8 |
|
|
1178 | ? dec.cur - SvPVX (string) |
|
|
1179 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1180 | |
|
|
1181 | if (offset_return) |
|
|
1182 | *offset_return = offset; |
|
|
1183 | } |
764 | |
1184 | |
765 | if (!sv) |
1185 | if (!sv) |
766 | { |
1186 | { |
767 | IV offset = utf8_distance (dec.cur, SvPVX (string)); |
|
|
768 | SV *uni = sv_newmortal (); |
1187 | SV *uni = sv_newmortal (); |
769 | |
1188 | |
|
|
1189 | // horrible hack to silence warning inside pv_uni_display |
|
|
1190 | COP cop = *PL_curcop; |
|
|
1191 | cop.cop_warnings = pWARN_NONE; |
|
|
1192 | ENTER; |
|
|
1193 | SAVEVPTR (PL_curcop); |
|
|
1194 | PL_curcop = &cop; |
770 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1195 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
|
|
1196 | LEAVE; |
|
|
1197 | |
771 | croak ("%s, at character %d (%s)", |
1198 | croak ("%s, at character offset %d [\"%s\"]", |
772 | dec.err, |
1199 | dec.err, |
773 | (int)offset, |
1200 | (int)offset, |
774 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1201 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
775 | } |
1202 | } |
776 | |
1203 | |
777 | sv_dump (sv);//D |
|
|
778 | return sv_2mortal (sv); |
1204 | sv = sv_2mortal (sv); |
|
|
1205 | |
|
|
1206 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
|
|
1207 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
|
|
1208 | |
|
|
1209 | return sv; |
779 | } |
1210 | } |
|
|
1211 | |
|
|
1212 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1213 | // XS interface functions |
780 | |
1214 | |
781 | MODULE = JSON::XS PACKAGE = JSON::XS |
1215 | MODULE = JSON::XS PACKAGE = JSON::XS |
782 | |
1216 | |
783 | BOOT: |
1217 | BOOT: |
784 | { |
1218 | { |
785 | int i; |
1219 | int i; |
786 | |
1220 | |
787 | memset (decode_hexdigit, 0xff, 256); |
|
|
788 | for (i = 10; i--; ) |
1221 | for (i = 0; i < 256; ++i) |
789 | decode_hexdigit ['0' + i] = i; |
1222 | decode_hexdigit [i] = |
|
|
1223 | i >= '0' && i <= '9' ? i - '0' |
|
|
1224 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
|
|
1225 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
|
|
1226 | : -1; |
790 | |
1227 | |
791 | for (i = 6; --i; ) |
|
|
792 | { |
|
|
793 | decode_hexdigit ['a' + i] = 10 + i; |
|
|
794 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
795 | } |
|
|
796 | |
|
|
797 | json_stash = gv_stashpv ("JSON::XS", 1); |
1228 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1229 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1230 | |
|
|
1231 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1232 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
798 | } |
1233 | } |
|
|
1234 | |
|
|
1235 | PROTOTYPES: DISABLE |
799 | |
1236 | |
800 | SV *new (char *dummy) |
1237 | SV *new (char *dummy) |
801 | CODE: |
1238 | CODE: |
802 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1239 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
803 | OUTPUT: |
1240 | OUTPUT: |
804 | RETVAL |
1241 | RETVAL |
805 | |
1242 | |
806 | SV *ascii (SV *self, int enable) |
1243 | SV *ascii (SV *self, int enable = 1) |
807 | ALIAS: |
1244 | ALIAS: |
808 | ascii = F_ASCII |
1245 | ascii = F_ASCII |
|
|
1246 | latin1 = F_LATIN1 |
809 | utf8 = F_UTF8 |
1247 | utf8 = F_UTF8 |
810 | indent = F_INDENT |
1248 | indent = F_INDENT |
811 | canonical = F_CANONICAL |
1249 | canonical = F_CANONICAL |
812 | space_before = F_SPACE_BEFORE |
1250 | space_before = F_SPACE_BEFORE |
813 | space_after = F_SPACE_AFTER |
1251 | space_after = F_SPACE_AFTER |
814 | json_rpc = F_JSON_RPC |
|
|
815 | pretty = F_PRETTY |
1252 | pretty = F_PRETTY |
|
|
1253 | allow_nonref = F_ALLOW_NONREF |
|
|
1254 | shrink = F_SHRINK |
|
|
1255 | allow_blessed = F_ALLOW_BLESSED |
|
|
1256 | convert_blessed = F_CONV_BLESSED |
816 | CODE: |
1257 | CODE: |
817 | { |
1258 | { |
818 | UV *uv = SvJSON (self); |
1259 | UV *uv = SvJSON (self); |
819 | if (enable) |
1260 | if (enable) |
820 | *uv |= ix; |
1261 | *uv |= ix; |
… | |
… | |
824 | RETVAL = newSVsv (self); |
1265 | RETVAL = newSVsv (self); |
825 | } |
1266 | } |
826 | OUTPUT: |
1267 | OUTPUT: |
827 | RETVAL |
1268 | RETVAL |
828 | |
1269 | |
|
|
1270 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1271 | CODE: |
|
|
1272 | { |
|
|
1273 | UV *uv = SvJSON (self); |
|
|
1274 | UV log2 = 0; |
|
|
1275 | |
|
|
1276 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1277 | |
|
|
1278 | while ((1UL << log2) < max_depth) |
|
|
1279 | ++log2; |
|
|
1280 | |
|
|
1281 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1282 | |
|
|
1283 | RETVAL = newSVsv (self); |
|
|
1284 | } |
|
|
1285 | OUTPUT: |
|
|
1286 | RETVAL |
|
|
1287 | |
|
|
1288 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1289 | CODE: |
|
|
1290 | { |
|
|
1291 | UV *uv = SvJSON (self); |
|
|
1292 | UV log2 = 0; |
|
|
1293 | |
|
|
1294 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1295 | if (max_size == 1) max_size = 2; |
|
|
1296 | |
|
|
1297 | while ((1UL << log2) < max_size) |
|
|
1298 | ++log2; |
|
|
1299 | |
|
|
1300 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1301 | |
|
|
1302 | RETVAL = newSVsv (self); |
|
|
1303 | } |
|
|
1304 | OUTPUT: |
|
|
1305 | RETVAL |
|
|
1306 | |
829 | void encode (SV *self, SV *scalar) |
1307 | void encode (SV *self, SV *scalar) |
830 | PPCODE: |
1308 | PPCODE: |
831 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1309 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
832 | |
1310 | |
833 | void decode (SV *self, SV *jsonstr) |
1311 | void decode (SV *self, SV *jsonstr) |
834 | PPCODE: |
1312 | PPCODE: |
835 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1313 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1314 | |
|
|
1315 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1316 | PPCODE: |
|
|
1317 | { |
|
|
1318 | UV offset; |
|
|
1319 | EXTEND (SP, 2); |
|
|
1320 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1321 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1322 | } |
|
|
1323 | |
|
|
1324 | PROTOTYPES: ENABLE |
836 | |
1325 | |
837 | void to_json (SV *scalar) |
1326 | void to_json (SV *scalar) |
|
|
1327 | ALIAS: |
|
|
1328 | objToJson = 0 |
838 | PPCODE: |
1329 | PPCODE: |
839 | XPUSHs (encode_json (scalar, F_UTF8)); |
1330 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
840 | |
1331 | |
841 | void from_json (SV *jsonstr) |
1332 | void from_json (SV *jsonstr) |
|
|
1333 | ALIAS: |
|
|
1334 | jsonToObj = 0 |
842 | PPCODE: |
1335 | PPCODE: |
843 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1336 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
844 | |
1337 | |