1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
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> |
|
|
9 | #include <float.h> |
8 | |
10 | |
|
|
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
13 | #endif |
|
|
14 | |
|
|
15 | // some old perls do not have this, try to make it work, no |
|
|
16 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
17 | #ifndef UTF8_MAXBYTES |
|
|
18 | # define UTF8_MAXBYTES 13 |
|
|
19 | #endif |
|
|
20 | |
9 | #define F_ASCII 0x00000001UL |
21 | #define F_ASCII 0x00000001UL |
|
|
22 | #define F_LATIN1 0x00000002UL |
10 | #define F_UTF8 0x00000002UL |
23 | #define F_UTF8 0x00000004UL |
11 | #define F_INDENT 0x00000004UL |
24 | #define F_INDENT 0x00000008UL |
12 | #define F_CANONICAL 0x00000008UL |
25 | #define F_CANONICAL 0x00000010UL |
13 | #define F_SPACE_BEFORE 0x00000010UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
14 | #define F_SPACE_AFTER 0x00000020UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
15 | #define F_ALLOW_NONREF 0x00000080UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
16 | #define F_SHRINK 0x00000100UL |
29 | #define F_SHRINK 0x00000200UL |
|
|
30 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
31 | #define F_CONV_BLESSED 0x00000800UL |
|
|
32 | #define F_RELAXED 0x00001000UL |
|
|
33 | |
17 | #define F_MAXDEPTH 0xf8000000UL |
34 | #define F_MAXDEPTH 0xf8000000UL |
18 | #define S_MAXDEPTH 27 |
35 | #define S_MAXDEPTH 27 |
|
|
36 | #define F_MAXSIZE 0x01f00000UL |
|
|
37 | #define S_MAXSIZE 20 |
|
|
38 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
19 | |
39 | |
20 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
40 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
21 | |
41 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
22 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
23 | // F_BLESSED? <=> { $__class__$ => } |
|
|
24 | |
42 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
43 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (12UL << S_MAXDEPTH) |
44 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
45 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
46 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
47 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
48 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
49 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
33 | |
50 | |
34 | #define SB do { |
51 | #define SB do { |
35 | #define SE } while (0) |
52 | #define SE } while (0) |
36 | |
53 | |
|
|
54 | #if __GNUC__ >= 3 |
|
|
55 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
56 | # define inline inline |
|
|
57 | #else |
|
|
58 | # define expect(expr,value) (expr) |
|
|
59 | # define inline static |
|
|
60 | #endif |
|
|
61 | |
|
|
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
64 | |
|
|
65 | #ifdef USE_ITHREADS |
|
|
66 | # define JSON_SLOW 1 |
|
|
67 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
68 | #else |
|
|
69 | # define JSON_SLOW 0 |
|
|
70 | # define JSON_STASH json_stash |
|
|
71 | #endif |
|
|
72 | |
37 | static HV *json_stash; // JSON::XS:: |
73 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
74 | static SV *json_true, *json_false; |
|
|
75 | |
|
|
76 | typedef struct { |
|
|
77 | U32 flags; |
|
|
78 | SV *cb_object; |
|
|
79 | HV *cb_sk_object; |
|
|
80 | } JSON; |
38 | |
81 | |
39 | ///////////////////////////////////////////////////////////////////////////// |
82 | ///////////////////////////////////////////////////////////////////////////// |
40 | // utility functions |
83 | // utility functions |
41 | |
84 | |
42 | static UV * |
85 | inline void |
43 | SvJSON (SV *sv) |
|
|
44 | { |
|
|
45 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
46 | croak ("object is not of type JSON::XS"); |
|
|
47 | |
|
|
48 | return &SvUVX (SvRV (sv)); |
|
|
49 | } |
|
|
50 | |
|
|
51 | static void |
|
|
52 | shrink (SV *sv) |
86 | shrink (SV *sv) |
53 | { |
87 | { |
54 | sv_utf8_downgrade (sv, 1); |
88 | sv_utf8_downgrade (sv, 1); |
55 | if (SvLEN (sv) > SvCUR (sv) + 1) |
89 | if (SvLEN (sv) > SvCUR (sv) + 1) |
56 | { |
90 | { |
… | |
… | |
65 | // decode an utf-8 character and return it, or (UV)-1 in |
99 | // decode an utf-8 character and return it, or (UV)-1 in |
66 | // case of an error. |
100 | // case of an error. |
67 | // we special-case "safe" characters from U+80 .. U+7FF, |
101 | // we special-case "safe" characters from U+80 .. U+7FF, |
68 | // but use the very good perl function to parse anything else. |
102 | // but use the very good perl function to parse anything else. |
69 | // note that we never call this function for a ascii codepoints |
103 | // note that we never call this function for a ascii codepoints |
70 | static UV |
104 | inline UV |
71 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
105 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
72 | { |
106 | { |
73 | if (s[0] > 0xdf || s[0] < 0xc2) |
107 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
74 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
108 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
75 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
109 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
76 | { |
110 | { |
77 | *clen = 2; |
111 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
112 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
113 | } |
80 | else |
114 | else |
|
|
115 | { |
|
|
116 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
117 | return (UV)-1; |
|
|
118 | } |
82 | } |
119 | } |
83 | |
120 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
121 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
122 | // encoder |
86 | |
123 | |
… | |
… | |
88 | typedef struct |
125 | typedef struct |
89 | { |
126 | { |
90 | char *cur; // SvPVX (sv) + current output position |
127 | char *cur; // SvPVX (sv) + current output position |
91 | char *end; // SvEND (sv) |
128 | char *end; // SvEND (sv) |
92 | SV *sv; // result scalar |
129 | SV *sv; // result scalar |
93 | U32 flags; // F_* |
130 | JSON json; |
94 | U32 indent; // indentation level |
131 | U32 indent; // indentation level |
95 | U32 maxdepth; // max. indentation/recursion level |
132 | U32 maxdepth; // max. indentation/recursion level |
96 | } enc_t; |
133 | } enc_t; |
97 | |
134 | |
98 | static void |
135 | inline void |
99 | need (enc_t *enc, STRLEN len) |
136 | need (enc_t *enc, STRLEN len) |
100 | { |
137 | { |
101 | if (enc->cur + len >= enc->end) |
138 | if (expect_false (enc->cur + len >= enc->end)) |
102 | { |
139 | { |
103 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
140 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
104 | SvGROW (enc->sv, cur + len + 1); |
141 | SvGROW (enc->sv, cur + len + 1); |
105 | enc->cur = SvPVX (enc->sv) + cur; |
142 | enc->cur = SvPVX (enc->sv) + cur; |
106 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
143 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
107 | } |
144 | } |
108 | } |
145 | } |
109 | |
146 | |
110 | static void |
147 | inline void |
111 | encode_ch (enc_t *enc, char ch) |
148 | encode_ch (enc_t *enc, char ch) |
112 | { |
149 | { |
113 | need (enc, 1); |
150 | need (enc, 1); |
114 | *enc->cur++ = ch; |
151 | *enc->cur++ = ch; |
115 | } |
152 | } |
… | |
… | |
123 | |
160 | |
124 | while (str < end) |
161 | while (str < end) |
125 | { |
162 | { |
126 | unsigned char ch = *(unsigned char *)str; |
163 | unsigned char ch = *(unsigned char *)str; |
127 | |
164 | |
128 | if (ch >= 0x20 && ch < 0x80) // most common case |
165 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
129 | { |
166 | { |
130 | if (ch == '"') // but with slow exceptions |
167 | if (expect_false (ch == '"')) // but with slow exceptions |
131 | { |
168 | { |
132 | need (enc, len += 1); |
169 | need (enc, len += 1); |
133 | *enc->cur++ = '\\'; |
170 | *enc->cur++ = '\\'; |
134 | *enc->cur++ = '"'; |
171 | *enc->cur++ = '"'; |
135 | } |
172 | } |
136 | else if (ch == '\\') |
173 | else if (expect_false (ch == '\\')) |
137 | { |
174 | { |
138 | need (enc, len += 1); |
175 | need (enc, len += 1); |
139 | *enc->cur++ = '\\'; |
176 | *enc->cur++ = '\\'; |
140 | *enc->cur++ = '\\'; |
177 | *enc->cur++ = '\\'; |
141 | } |
178 | } |
… | |
… | |
159 | STRLEN clen; |
196 | STRLEN clen; |
160 | UV uch; |
197 | UV uch; |
161 | |
198 | |
162 | if (is_utf8) |
199 | if (is_utf8) |
163 | { |
200 | { |
164 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
165 | uch = decode_utf8 (str, end - str, &clen); |
201 | uch = decode_utf8 (str, end - str, &clen); |
166 | if (clen == (STRLEN)-1) |
202 | if (clen == (STRLEN)-1) |
167 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
203 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
168 | } |
204 | } |
169 | else |
205 | else |
… | |
… | |
173 | } |
209 | } |
174 | |
210 | |
175 | if (uch > 0x10FFFFUL) |
211 | if (uch > 0x10FFFFUL) |
176 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
212 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
177 | |
213 | |
178 | if (uch < 0x80 || enc->flags & F_ASCII) |
214 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
179 | { |
215 | { |
180 | if (uch > 0xFFFFUL) |
216 | if (uch > 0xFFFFUL) |
181 | { |
217 | { |
182 | need (enc, len += 11); |
218 | need (enc, len += 11); |
183 | sprintf (enc->cur, "\\u%04x\\u%04x", |
219 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
197 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
233 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
198 | } |
234 | } |
199 | |
235 | |
200 | str += clen; |
236 | str += clen; |
201 | } |
237 | } |
|
|
238 | else if (enc->json.flags & F_LATIN1) |
|
|
239 | { |
|
|
240 | *enc->cur++ = uch; |
|
|
241 | str += clen; |
|
|
242 | } |
202 | else if (is_utf8) |
243 | else if (is_utf8) |
203 | { |
244 | { |
204 | need (enc, len += clen); |
245 | need (enc, len += clen); |
205 | do |
246 | do |
206 | { |
247 | { |
… | |
… | |
208 | } |
249 | } |
209 | while (--clen); |
250 | while (--clen); |
210 | } |
251 | } |
211 | else |
252 | else |
212 | { |
253 | { |
213 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
254 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
214 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
255 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
215 | ++str; |
256 | ++str; |
216 | } |
257 | } |
217 | } |
258 | } |
218 | } |
259 | } |
… | |
… | |
220 | |
261 | |
221 | --len; |
262 | --len; |
222 | } |
263 | } |
223 | } |
264 | } |
224 | |
265 | |
225 | static void |
266 | inline void |
226 | encode_indent (enc_t *enc) |
267 | encode_indent (enc_t *enc) |
227 | { |
268 | { |
228 | if (enc->flags & F_INDENT) |
269 | if (enc->json.flags & F_INDENT) |
229 | { |
270 | { |
230 | int spaces = enc->indent * INDENT_STEP; |
271 | int spaces = enc->indent * INDENT_STEP; |
231 | |
272 | |
232 | need (enc, spaces); |
273 | need (enc, spaces); |
233 | memset (enc->cur, ' ', spaces); |
274 | memset (enc->cur, ' ', spaces); |
234 | enc->cur += spaces; |
275 | enc->cur += spaces; |
235 | } |
276 | } |
236 | } |
277 | } |
237 | |
278 | |
238 | static void |
279 | inline void |
239 | encode_space (enc_t *enc) |
280 | encode_space (enc_t *enc) |
240 | { |
281 | { |
241 | need (enc, 1); |
282 | need (enc, 1); |
242 | encode_ch (enc, ' '); |
283 | encode_ch (enc, ' '); |
243 | } |
284 | } |
244 | |
285 | |
245 | static void |
286 | inline void |
246 | encode_nl (enc_t *enc) |
287 | encode_nl (enc_t *enc) |
247 | { |
288 | { |
248 | if (enc->flags & F_INDENT) |
289 | if (enc->json.flags & F_INDENT) |
249 | { |
290 | { |
250 | need (enc, 1); |
291 | need (enc, 1); |
251 | encode_ch (enc, '\n'); |
292 | encode_ch (enc, '\n'); |
252 | } |
293 | } |
253 | } |
294 | } |
254 | |
295 | |
255 | static void |
296 | inline void |
256 | encode_comma (enc_t *enc) |
297 | encode_comma (enc_t *enc) |
257 | { |
298 | { |
258 | encode_ch (enc, ','); |
299 | encode_ch (enc, ','); |
259 | |
300 | |
260 | if (enc->flags & F_INDENT) |
301 | if (enc->json.flags & F_INDENT) |
261 | encode_nl (enc); |
302 | encode_nl (enc); |
262 | else if (enc->flags & F_SPACE_AFTER) |
303 | else if (enc->json.flags & F_SPACE_AFTER) |
263 | encode_space (enc); |
304 | encode_space (enc); |
264 | } |
305 | } |
265 | |
306 | |
266 | static void encode_sv (enc_t *enc, SV *sv); |
307 | static void encode_sv (enc_t *enc, SV *sv); |
267 | |
308 | |
… | |
… | |
271 | int i, len = av_len (av); |
312 | int i, len = av_len (av); |
272 | |
313 | |
273 | if (enc->indent >= enc->maxdepth) |
314 | if (enc->indent >= enc->maxdepth) |
274 | croak ("data structure too deep (hit recursion limit)"); |
315 | croak ("data structure too deep (hit recursion limit)"); |
275 | |
316 | |
276 | encode_ch (enc, '['); encode_nl (enc); |
317 | encode_ch (enc, '['); |
277 | ++enc->indent; |
318 | |
|
|
319 | if (len >= 0) |
|
|
320 | { |
|
|
321 | encode_nl (enc); ++enc->indent; |
278 | |
322 | |
279 | for (i = 0; i <= len; ++i) |
323 | for (i = 0; i <= len; ++i) |
280 | { |
324 | { |
|
|
325 | SV **svp = av_fetch (av, i, 0); |
|
|
326 | |
281 | encode_indent (enc); |
327 | encode_indent (enc); |
282 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
283 | |
328 | |
|
|
329 | if (svp) |
|
|
330 | encode_sv (enc, *svp); |
|
|
331 | else |
|
|
332 | encode_str (enc, "null", 4, 0); |
|
|
333 | |
284 | if (i < len) |
334 | if (i < len) |
285 | encode_comma (enc); |
335 | encode_comma (enc); |
286 | } |
336 | } |
287 | |
337 | |
|
|
338 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
339 | } |
|
|
340 | |
288 | encode_nl (enc); |
341 | encode_ch (enc, ']'); |
289 | |
|
|
290 | --enc->indent; |
|
|
291 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
292 | } |
342 | } |
293 | |
343 | |
294 | static void |
344 | static void |
295 | encode_he (enc_t *enc, HE *he) |
345 | encode_hk (enc_t *enc, HE *he) |
296 | { |
346 | { |
297 | encode_ch (enc, '"'); |
347 | encode_ch (enc, '"'); |
298 | |
348 | |
299 | if (HeKLEN (he) == HEf_SVKEY) |
349 | if (HeKLEN (he) == HEf_SVKEY) |
300 | { |
350 | { |
… | |
… | |
310 | else |
360 | else |
311 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
361 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
312 | |
362 | |
313 | encode_ch (enc, '"'); |
363 | encode_ch (enc, '"'); |
314 | |
364 | |
315 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
365 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
316 | encode_ch (enc, ':'); |
366 | encode_ch (enc, ':'); |
317 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
367 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
318 | encode_sv (enc, HeVAL (he)); |
|
|
319 | } |
368 | } |
320 | |
369 | |
321 | // compare hash entries, used when all keys are bytestrings |
370 | // compare hash entries, used when all keys are bytestrings |
322 | static int |
371 | static int |
323 | he_cmp_fast (const void *a_, const void *b_) |
372 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
328 | HE *b = *(HE **)b_; |
377 | HE *b = *(HE **)b_; |
329 | |
378 | |
330 | STRLEN la = HeKLEN (a); |
379 | STRLEN la = HeKLEN (a); |
331 | STRLEN lb = HeKLEN (b); |
380 | STRLEN lb = HeKLEN (b); |
332 | |
381 | |
333 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
382 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
334 | cmp = la - lb; |
383 | cmp = lb - la; |
335 | |
384 | |
336 | return cmp; |
385 | return cmp; |
337 | } |
386 | } |
338 | |
387 | |
339 | // compare hash entries, used when some keys are sv's or utf-x |
388 | // compare hash entries, used when some keys are sv's or utf-x |
340 | static int |
389 | static int |
341 | he_cmp_slow (const void *a, const void *b) |
390 | he_cmp_slow (const void *a, const void *b) |
342 | { |
391 | { |
343 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
392 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
344 | } |
393 | } |
345 | |
394 | |
346 | static void |
395 | static void |
347 | encode_hv (enc_t *enc, HV *hv) |
396 | encode_hv (enc_t *enc, HV *hv) |
348 | { |
397 | { |
|
|
398 | HE *he; |
349 | int count, i; |
399 | int count; |
350 | |
400 | |
351 | if (enc->indent >= enc->maxdepth) |
401 | if (enc->indent >= enc->maxdepth) |
352 | croak ("data structure too deep (hit recursion limit)"); |
402 | croak ("data structure too deep (hit recursion limit)"); |
353 | |
403 | |
354 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
404 | encode_ch (enc, '{'); |
355 | |
405 | |
356 | if ((count = hv_iterinit (hv))) |
|
|
357 | { |
|
|
358 | // for canonical output we have to sort by keys first |
406 | // for canonical output we have to sort by keys first |
359 | // actually, this is mostly due to the stupid so-called |
407 | // actually, this is mostly due to the stupid so-called |
360 | // security workaround added somewhere in 5.8.x. |
408 | // security workaround added somewhere in 5.8.x. |
361 | // that randomises hash orderings |
409 | // that randomises hash orderings |
362 | if (enc->flags & F_CANONICAL) |
410 | if (enc->json.flags & F_CANONICAL) |
|
|
411 | { |
|
|
412 | int count = hv_iterinit (hv); |
|
|
413 | |
|
|
414 | if (SvMAGICAL (hv)) |
363 | { |
415 | { |
|
|
416 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
417 | // but I don't care for the speed of this special case. |
|
|
418 | // note also that we will run into undefined behaviour when the two iterations |
|
|
419 | // do not result in the same count, something I might care for in some later release. |
|
|
420 | |
|
|
421 | count = 0; |
|
|
422 | while (hv_iternext (hv)) |
|
|
423 | ++count; |
|
|
424 | |
|
|
425 | hv_iterinit (hv); |
|
|
426 | } |
|
|
427 | |
|
|
428 | if (count) |
|
|
429 | { |
|
|
430 | int i, fast = 1; |
|
|
431 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
432 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
433 | #else |
364 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
434 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
365 | int fast = 1; |
435 | #endif |
366 | |
436 | |
367 | i = 0; |
437 | i = 0; |
368 | while ((he = hv_iternext (hv))) |
438 | while ((he = hv_iternext (hv))) |
369 | { |
439 | { |
370 | hes [i++] = he; |
440 | hes [i++] = he; |
… | |
… | |
392 | |
462 | |
393 | FREETMPS; |
463 | FREETMPS; |
394 | LEAVE; |
464 | LEAVE; |
395 | } |
465 | } |
396 | |
466 | |
397 | for (i = 0; i < count; ++i) |
467 | encode_nl (enc); ++enc->indent; |
|
|
468 | |
|
|
469 | while (count--) |
398 | { |
470 | { |
399 | encode_indent (enc); |
471 | encode_indent (enc); |
|
|
472 | he = hes [count]; |
400 | encode_he (enc, hes [i]); |
473 | encode_hk (enc, he); |
|
|
474 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
401 | |
475 | |
402 | if (i < count - 1) |
476 | if (count) |
403 | encode_comma (enc); |
477 | encode_comma (enc); |
404 | } |
478 | } |
405 | |
479 | |
|
|
480 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
481 | } |
|
|
482 | } |
|
|
483 | else |
|
|
484 | { |
|
|
485 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
486 | if ((he = hv_iternext (hv))) |
|
|
487 | { |
|
|
488 | encode_nl (enc); ++enc->indent; |
|
|
489 | |
|
|
490 | for (;;) |
|
|
491 | { |
406 | encode_nl (enc); |
492 | encode_indent (enc); |
|
|
493 | encode_hk (enc, he); |
|
|
494 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
|
|
495 | |
|
|
496 | if (!(he = hv_iternext (hv))) |
|
|
497 | break; |
|
|
498 | |
|
|
499 | encode_comma (enc); |
|
|
500 | } |
|
|
501 | |
|
|
502 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
503 | } |
|
|
504 | } |
|
|
505 | |
|
|
506 | encode_ch (enc, '}'); |
|
|
507 | } |
|
|
508 | |
|
|
509 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
510 | static void |
|
|
511 | encode_rv (enc_t *enc, SV *sv) |
|
|
512 | { |
|
|
513 | svtype svt; |
|
|
514 | |
|
|
515 | SvGETMAGIC (sv); |
|
|
516 | svt = SvTYPE (sv); |
|
|
517 | |
|
|
518 | if (expect_false (SvOBJECT (sv))) |
|
|
519 | { |
|
|
520 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
521 | ? json_boolean_stash |
|
|
522 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
523 | |
|
|
524 | if (SvSTASH (sv) == stash) |
|
|
525 | { |
|
|
526 | if (SvIV (sv)) |
|
|
527 | encode_str (enc, "true", 4, 0); |
|
|
528 | else |
|
|
529 | encode_str (enc, "false", 5, 0); |
407 | } |
530 | } |
408 | else |
531 | else |
409 | { |
532 | { |
410 | SV *sv; |
533 | #if 0 |
411 | HE *he = hv_iternext (hv); |
534 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
412 | |
|
|
413 | for (;;) |
|
|
414 | { |
535 | { |
415 | encode_indent (enc); |
536 | // not yet |
416 | encode_he (enc, he); |
|
|
417 | |
|
|
418 | if (!(he = hv_iternext (hv))) |
|
|
419 | break; |
|
|
420 | |
|
|
421 | encode_comma (enc); |
|
|
422 | } |
537 | } |
|
|
538 | #endif |
|
|
539 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
540 | { |
|
|
541 | // we re-bless the reference to get overload and other niceties right |
|
|
542 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
423 | |
543 | |
|
|
544 | if (to_json) |
|
|
545 | { |
|
|
546 | dSP; |
|
|
547 | |
|
|
548 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
549 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
550 | |
|
|
551 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
552 | PUTBACK; |
|
|
553 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
554 | SPAGAIN; |
|
|
555 | |
|
|
556 | // catch this surprisingly common error |
|
|
557 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
558 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
559 | |
|
|
560 | sv = POPs; |
|
|
561 | PUTBACK; |
|
|
562 | |
424 | encode_nl (enc); |
563 | encode_sv (enc, sv); |
|
|
564 | |
|
|
565 | FREETMPS; LEAVE; |
|
|
566 | } |
|
|
567 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
568 | encode_str (enc, "null", 4, 0); |
|
|
569 | else |
|
|
570 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
571 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
572 | } |
|
|
573 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
574 | encode_str (enc, "null", 4, 0); |
|
|
575 | else |
|
|
576 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
577 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
425 | } |
578 | } |
426 | } |
579 | } |
427 | |
|
|
428 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
429 | } |
|
|
430 | |
|
|
431 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
432 | static void |
|
|
433 | encode_rv (enc_t *enc, SV *sv) |
|
|
434 | { |
|
|
435 | SvGETMAGIC (sv); |
|
|
436 | |
|
|
437 | svtype svt = SvTYPE (sv); |
|
|
438 | |
|
|
439 | if (svt == SVt_PVHV) |
580 | else if (svt == SVt_PVHV) |
440 | encode_hv (enc, (HV *)sv); |
581 | encode_hv (enc, (HV *)sv); |
441 | else if (svt == SVt_PVAV) |
582 | else if (svt == SVt_PVAV) |
442 | encode_av (enc, (AV *)sv); |
583 | encode_av (enc, (AV *)sv); |
443 | else if (svt < SVt_PVAV) |
584 | else if (svt < SVt_PVAV) |
444 | { |
585 | { |
445 | if (SvNIOK (sv) && SvIV (sv) == 0) |
586 | STRLEN len = 0; |
|
|
587 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
588 | |
|
|
589 | if (len == 1 && *pv == '1') |
|
|
590 | encode_str (enc, "true", 4, 0); |
|
|
591 | else if (len == 1 && *pv == '0') |
446 | encode_str (enc, "false", 5, 0); |
592 | encode_str (enc, "false", 5, 0); |
447 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
448 | encode_str (enc, "true", 4, 0); |
|
|
449 | else |
593 | else |
450 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
594 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
451 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
595 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
452 | } |
596 | } |
453 | else |
597 | else |
… | |
… | |
468 | encode_str (enc, str, len, SvUTF8 (sv)); |
612 | encode_str (enc, str, len, SvUTF8 (sv)); |
469 | encode_ch (enc, '"'); |
613 | encode_ch (enc, '"'); |
470 | } |
614 | } |
471 | else if (SvNOKp (sv)) |
615 | else if (SvNOKp (sv)) |
472 | { |
616 | { |
|
|
617 | // trust that perl will do the right thing w.r.t. JSON syntax. |
473 | need (enc, NV_DIG + 32); |
618 | need (enc, NV_DIG + 32); |
474 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
619 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
475 | enc->cur += strlen (enc->cur); |
620 | enc->cur += strlen (enc->cur); |
476 | } |
621 | } |
477 | else if (SvIOKp (sv)) |
622 | else if (SvIOKp (sv)) |
478 | { |
623 | { |
479 | need (enc, 64); |
624 | // we assume we can always read an IV as a UV |
|
|
625 | if (SvUV (sv) & ~(UV)0x7fff) |
|
|
626 | { |
|
|
627 | // large integer, use the (rather slow) snprintf way. |
|
|
628 | need (enc, sizeof (UV) * 3); |
480 | enc->cur += |
629 | enc->cur += |
481 | SvIsUV(sv) |
630 | SvIsUV(sv) |
482 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
631 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
483 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
632 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
633 | } |
|
|
634 | else |
|
|
635 | { |
|
|
636 | // optimise the "small number case" |
|
|
637 | // code will likely be branchless and use only a single multiplication |
|
|
638 | I32 i = SvIV (sv); |
|
|
639 | U32 u; |
|
|
640 | char digit, nz = 0; |
|
|
641 | |
|
|
642 | need (enc, 6); |
|
|
643 | |
|
|
644 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
|
|
645 | u = i < 0 ? -i : i; |
|
|
646 | |
|
|
647 | // convert to 4.28 fixed-point representation |
|
|
648 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
|
|
649 | |
|
|
650 | // now output digit by digit, each time masking out the integer part |
|
|
651 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
652 | // resulting in a net multiplication by 10. |
|
|
653 | // we always write the digit to memory but conditionally increment |
|
|
654 | // the pointer, to ease the usage of conditional move instructions. |
|
|
655 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
|
|
656 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
|
|
657 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
|
|
658 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
|
|
659 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
660 | } |
484 | } |
661 | } |
485 | else if (SvROK (sv)) |
662 | else if (SvROK (sv)) |
486 | encode_rv (enc, SvRV (sv)); |
663 | encode_rv (enc, SvRV (sv)); |
487 | else if (!SvOK (sv)) |
664 | else if (!SvOK (sv)) |
488 | encode_str (enc, "null", 4, 0); |
665 | encode_str (enc, "null", 4, 0); |
… | |
… | |
490 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
667 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
491 | SvPV_nolen (sv), SvFLAGS (sv)); |
668 | SvPV_nolen (sv), SvFLAGS (sv)); |
492 | } |
669 | } |
493 | |
670 | |
494 | static SV * |
671 | static SV * |
495 | encode_json (SV *scalar, U32 flags) |
672 | encode_json (SV *scalar, JSON *json) |
496 | { |
673 | { |
|
|
674 | enc_t enc; |
|
|
675 | |
497 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
676 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
498 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
677 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
499 | |
678 | |
500 | enc_t enc; |
679 | enc.json = *json; |
501 | enc.flags = flags; |
|
|
502 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
680 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
503 | enc.cur = SvPVX (enc.sv); |
681 | enc.cur = SvPVX (enc.sv); |
504 | enc.end = SvEND (enc.sv); |
682 | enc.end = SvEND (enc.sv); |
505 | enc.indent = 0; |
683 | enc.indent = 0; |
506 | enc.maxdepth = DEC_DEPTH (flags); |
684 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
507 | |
685 | |
508 | SvPOK_only (enc.sv); |
686 | SvPOK_only (enc.sv); |
509 | encode_sv (&enc, scalar); |
687 | encode_sv (&enc, scalar); |
510 | |
688 | |
|
|
689 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
690 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
691 | |
511 | if (!(flags & (F_ASCII | F_UTF8))) |
692 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
512 | SvUTF8_on (enc.sv); |
693 | SvUTF8_on (enc.sv); |
513 | |
694 | |
514 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
515 | |
|
|
516 | if (enc.flags & F_SHRINK) |
695 | if (enc.json.flags & F_SHRINK) |
517 | shrink (enc.sv); |
696 | shrink (enc.sv); |
518 | |
697 | |
519 | return enc.sv; |
698 | return enc.sv; |
520 | } |
699 | } |
521 | |
700 | |
… | |
… | |
526 | typedef struct |
705 | typedef struct |
527 | { |
706 | { |
528 | char *cur; // current parser pointer |
707 | char *cur; // current parser pointer |
529 | char *end; // end of input string |
708 | char *end; // end of input string |
530 | const char *err; // parse error, if != 0 |
709 | const char *err; // parse error, if != 0 |
531 | U32 flags; // F_* |
710 | JSON json; |
532 | U32 depth; // recursion depth |
711 | U32 depth; // recursion depth |
533 | U32 maxdepth; // recursion depth limit |
712 | U32 maxdepth; // recursion depth limit |
534 | } dec_t; |
713 | } dec_t; |
535 | |
714 | |
536 | static void |
715 | inline void |
|
|
716 | decode_comment (dec_t *dec) |
|
|
717 | { |
|
|
718 | // only '#'-style comments allowed a.t.m. |
|
|
719 | |
|
|
720 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
721 | ++dec->cur; |
|
|
722 | } |
|
|
723 | |
|
|
724 | inline void |
537 | decode_ws (dec_t *dec) |
725 | decode_ws (dec_t *dec) |
538 | { |
726 | { |
539 | for (;;) |
727 | for (;;) |
540 | { |
728 | { |
541 | char ch = *dec->cur; |
729 | char ch = *dec->cur; |
542 | |
730 | |
543 | if (ch > 0x20 |
731 | if (ch > 0x20) |
|
|
732 | { |
|
|
733 | if (expect_false (ch == '#')) |
|
|
734 | { |
|
|
735 | if (dec->json.flags & F_RELAXED) |
|
|
736 | decode_comment (dec); |
|
|
737 | else |
|
|
738 | break; |
|
|
739 | } |
|
|
740 | else |
|
|
741 | break; |
|
|
742 | } |
544 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
743 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
545 | break; |
744 | break; // parse error, but let higher level handle it, gives better error messages |
546 | |
745 | |
547 | ++dec->cur; |
746 | ++dec->cur; |
548 | } |
747 | } |
549 | } |
748 | } |
550 | |
749 | |
… | |
… | |
567 | decode_4hex (dec_t *dec) |
766 | decode_4hex (dec_t *dec) |
568 | { |
767 | { |
569 | signed char d1, d2, d3, d4; |
768 | signed char d1, d2, d3, d4; |
570 | unsigned char *cur = (unsigned char *)dec->cur; |
769 | unsigned char *cur = (unsigned char *)dec->cur; |
571 | |
770 | |
572 | d1 = decode_hexdigit [cur [0]]; if (d1 < 0) ERR ("four hexadecimal digits expected"); |
771 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
573 | d2 = decode_hexdigit [cur [1]]; if (d2 < 0) ERR ("four hexadecimal digits expected"); |
772 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
574 | d3 = decode_hexdigit [cur [2]]; if (d3 < 0) ERR ("four hexadecimal digits expected"); |
773 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
575 | d4 = decode_hexdigit [cur [3]]; if (d4 < 0) ERR ("four hexadecimal digits expected"); |
774 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
576 | |
775 | |
577 | dec->cur += 4; |
776 | dec->cur += 4; |
578 | |
777 | |
579 | return ((UV)d1) << 12 |
778 | return ((UV)d1) << 12 |
580 | | ((UV)d2) << 8 |
779 | | ((UV)d2) << 8 |
… | |
… | |
588 | static SV * |
787 | static SV * |
589 | decode_str (dec_t *dec) |
788 | decode_str (dec_t *dec) |
590 | { |
789 | { |
591 | SV *sv = 0; |
790 | SV *sv = 0; |
592 | int utf8 = 0; |
791 | int utf8 = 0; |
|
|
792 | char *dec_cur = dec->cur; |
593 | |
793 | |
594 | do |
794 | do |
595 | { |
795 | { |
596 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
796 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
597 | char *cur = buf; |
797 | char *cur = buf; |
598 | |
798 | |
599 | do |
799 | do |
600 | { |
800 | { |
601 | unsigned char ch = *(unsigned char *)dec->cur++; |
801 | unsigned char ch = *(unsigned char *)dec_cur++; |
602 | |
802 | |
603 | if (ch == '"') |
803 | if (expect_false (ch == '"')) |
604 | { |
804 | { |
605 | --dec->cur; |
805 | --dec_cur; |
606 | break; |
806 | break; |
607 | } |
807 | } |
608 | else if (ch == '\\') |
808 | else if (expect_false (ch == '\\')) |
609 | { |
809 | { |
610 | switch (*dec->cur) |
810 | switch (*dec_cur) |
611 | { |
811 | { |
612 | case '\\': |
812 | case '\\': |
613 | case '/': |
813 | case '/': |
614 | case '"': *cur++ = *dec->cur++; break; |
814 | case '"': *cur++ = *dec_cur++; break; |
615 | |
815 | |
616 | case 'b': ++dec->cur; *cur++ = '\010'; break; |
816 | case 'b': ++dec_cur; *cur++ = '\010'; break; |
617 | case 't': ++dec->cur; *cur++ = '\011'; break; |
817 | case 't': ++dec_cur; *cur++ = '\011'; break; |
618 | case 'n': ++dec->cur; *cur++ = '\012'; break; |
818 | case 'n': ++dec_cur; *cur++ = '\012'; break; |
619 | case 'f': ++dec->cur; *cur++ = '\014'; break; |
819 | case 'f': ++dec_cur; *cur++ = '\014'; break; |
620 | case 'r': ++dec->cur; *cur++ = '\015'; break; |
820 | case 'r': ++dec_cur; *cur++ = '\015'; break; |
621 | |
821 | |
622 | case 'u': |
822 | case 'u': |
623 | { |
823 | { |
624 | UV lo, hi; |
824 | UV lo, hi; |
625 | ++dec->cur; |
825 | ++dec_cur; |
626 | |
826 | |
|
|
827 | dec->cur = dec_cur; |
627 | hi = decode_4hex (dec); |
828 | hi = decode_4hex (dec); |
|
|
829 | dec_cur = dec->cur; |
628 | if (hi == (UV)-1) |
830 | if (hi == (UV)-1) |
629 | goto fail; |
831 | goto fail; |
630 | |
832 | |
631 | // possibly a surrogate pair |
833 | // possibly a surrogate pair |
632 | if (hi >= 0xd800) |
834 | if (hi >= 0xd800) |
633 | if (hi < 0xdc00) |
835 | if (hi < 0xdc00) |
634 | { |
836 | { |
635 | if (dec->cur [0] != '\\' || dec->cur [1] != 'u') |
837 | if (dec_cur [0] != '\\' || dec_cur [1] != 'u') |
636 | ERR ("missing low surrogate character in surrogate pair"); |
838 | ERR ("missing low surrogate character in surrogate pair"); |
637 | |
839 | |
638 | dec->cur += 2; |
840 | dec_cur += 2; |
639 | |
841 | |
|
|
842 | dec->cur = dec_cur; |
640 | lo = decode_4hex (dec); |
843 | lo = decode_4hex (dec); |
|
|
844 | dec_cur = dec->cur; |
641 | if (lo == (UV)-1) |
845 | if (lo == (UV)-1) |
642 | goto fail; |
846 | goto fail; |
643 | |
847 | |
644 | if (lo < 0xdc00 || lo >= 0xe000) |
848 | if (lo < 0xdc00 || lo >= 0xe000) |
645 | ERR ("surrogate pair expected"); |
849 | ERR ("surrogate pair expected"); |
… | |
… | |
659 | *cur++ = hi; |
863 | *cur++ = hi; |
660 | } |
864 | } |
661 | break; |
865 | break; |
662 | |
866 | |
663 | default: |
867 | default: |
664 | --dec->cur; |
868 | --dec_cur; |
665 | ERR ("illegal backslash escape sequence in string"); |
869 | ERR ("illegal backslash escape sequence in string"); |
666 | } |
870 | } |
667 | } |
871 | } |
668 | else if (ch >= 0x20 && ch <= 0x7f) |
872 | else if (expect_true (ch >= 0x20 && ch <= 0x7f)) |
669 | *cur++ = ch; |
873 | *cur++ = ch; |
670 | else if (ch >= 0x80) |
874 | else if (ch >= 0x80) |
671 | { |
875 | { |
672 | --dec->cur; |
|
|
673 | |
|
|
674 | STRLEN clen; |
876 | STRLEN clen; |
|
|
877 | UV uch; |
|
|
878 | |
|
|
879 | --dec_cur; |
|
|
880 | |
675 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
881 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
676 | if (clen == (STRLEN)-1) |
882 | if (clen == (STRLEN)-1) |
677 | ERR ("malformed UTF-8 character in JSON string"); |
883 | ERR ("malformed UTF-8 character in JSON string"); |
678 | |
884 | |
679 | do |
885 | do |
680 | { |
|
|
681 | *cur++ = *dec->cur++; |
886 | *cur++ = *dec_cur++; |
682 | } |
|
|
683 | while (--clen); |
887 | while (--clen); |
684 | |
888 | |
685 | utf8 = 1; |
889 | utf8 = 1; |
686 | } |
890 | } |
687 | else if (!ch) |
|
|
688 | ERR ("unexpected end of string while parsing json string"); |
|
|
689 | else |
891 | else |
|
|
892 | { |
|
|
893 | --dec_cur; |
|
|
894 | |
|
|
895 | if (!ch) |
|
|
896 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
897 | else |
690 | ERR ("invalid character encountered"); |
898 | ERR ("invalid character encountered while parsing JSON string"); |
691 | |
899 | } |
692 | } |
900 | } |
693 | while (cur < buf + SHORT_STRING_LEN); |
901 | while (cur < buf + SHORT_STRING_LEN); |
694 | |
902 | |
|
|
903 | { |
695 | STRLEN len = cur - buf; |
904 | STRLEN len = cur - buf; |
696 | |
905 | |
697 | if (sv) |
906 | if (sv) |
698 | { |
907 | { |
699 | SvGROW (sv, SvCUR (sv) + len + 1); |
908 | SvGROW (sv, SvCUR (sv) + len + 1); |
700 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
909 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
701 | SvCUR_set (sv, SvCUR (sv) + len); |
910 | SvCUR_set (sv, SvCUR (sv) + len); |
702 | } |
911 | } |
703 | else |
912 | else |
704 | sv = newSVpvn (buf, len); |
913 | sv = newSVpvn (buf, len); |
705 | } |
914 | } |
|
|
915 | } |
706 | while (*dec->cur != '"'); |
916 | while (*dec_cur != '"'); |
707 | |
917 | |
708 | ++dec->cur; |
918 | ++dec_cur; |
709 | |
919 | |
710 | if (sv) |
920 | if (sv) |
711 | { |
921 | { |
712 | SvPOK_only (sv); |
922 | SvPOK_only (sv); |
713 | *SvEND (sv) = 0; |
923 | *SvEND (sv) = 0; |
… | |
… | |
716 | SvUTF8_on (sv); |
926 | SvUTF8_on (sv); |
717 | } |
927 | } |
718 | else |
928 | else |
719 | sv = newSVpvn ("", 0); |
929 | sv = newSVpvn ("", 0); |
720 | |
930 | |
|
|
931 | dec->cur = dec_cur; |
721 | return sv; |
932 | return sv; |
722 | |
933 | |
723 | fail: |
934 | fail: |
|
|
935 | dec->cur = dec_cur; |
724 | return 0; |
936 | return 0; |
725 | } |
937 | } |
726 | |
938 | |
727 | static SV * |
939 | static SV * |
728 | decode_num (dec_t *dec) |
940 | decode_num (dec_t *dec) |
… | |
… | |
786 | is_nv = 1; |
998 | is_nv = 1; |
787 | } |
999 | } |
788 | |
1000 | |
789 | if (!is_nv) |
1001 | if (!is_nv) |
790 | { |
1002 | { |
791 | UV uv; |
1003 | int len = dec->cur - start; |
792 | int numtype = grok_number (start, dec->cur - start, &uv); |
1004 | |
793 | if (numtype & IS_NUMBER_IN_UV) |
1005 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
794 | if (numtype & IS_NUMBER_NEG) |
1006 | if (*start == '-') |
|
|
1007 | switch (len) |
795 | { |
1008 | { |
796 | if (uv < (UV)IV_MIN) |
1009 | case 2: return newSViv (-( start [1] - '0' * 1)); |
797 | return newSViv (-(IV)uv); |
1010 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
|
|
1011 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
|
|
1012 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
798 | } |
1013 | } |
|
|
1014 | else |
|
|
1015 | switch (len) |
|
|
1016 | { |
|
|
1017 | case 1: return newSViv ( start [0] - '0' * 1); |
|
|
1018 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
|
|
1019 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
|
|
1020 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1021 | } |
|
|
1022 | |
|
|
1023 | { |
|
|
1024 | UV uv; |
|
|
1025 | int numtype = grok_number (start, len, &uv); |
|
|
1026 | if (numtype & IS_NUMBER_IN_UV) |
|
|
1027 | if (numtype & IS_NUMBER_NEG) |
|
|
1028 | { |
|
|
1029 | if (uv < (UV)IV_MIN) |
|
|
1030 | return newSViv (-(IV)uv); |
|
|
1031 | } |
799 | else |
1032 | else |
800 | return newSVuv (uv); |
1033 | return newSVuv (uv); |
801 | } |
1034 | } |
802 | |
1035 | |
|
|
1036 | len -= *start == '-' ? 1 : 0; |
|
|
1037 | |
|
|
1038 | // does not fit into IV or UV, try NV |
|
|
1039 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1040 | #if defined (LDBL_DIG) |
|
|
1041 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1042 | #endif |
|
|
1043 | ) |
|
|
1044 | // fits into NV without loss of precision |
|
|
1045 | return newSVnv (Atof (start)); |
|
|
1046 | |
|
|
1047 | // everything else fails, convert it to a string |
|
|
1048 | return newSVpvn (start, dec->cur - start); |
|
|
1049 | } |
|
|
1050 | |
|
|
1051 | // loss of precision here |
803 | return newSVnv (Atof (start)); |
1052 | return newSVnv (Atof (start)); |
804 | |
1053 | |
805 | fail: |
1054 | fail: |
806 | return 0; |
1055 | return 0; |
807 | } |
1056 | } |
… | |
… | |
837 | |
1086 | |
838 | if (*dec->cur != ',') |
1087 | if (*dec->cur != ',') |
839 | ERR (", or ] expected while parsing array"); |
1088 | ERR (", or ] expected while parsing array"); |
840 | |
1089 | |
841 | ++dec->cur; |
1090 | ++dec->cur; |
|
|
1091 | |
|
|
1092 | decode_ws (dec); |
|
|
1093 | |
|
|
1094 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1095 | { |
|
|
1096 | ++dec->cur; |
|
|
1097 | break; |
|
|
1098 | } |
842 | } |
1099 | } |
843 | |
1100 | |
844 | DEC_DEC_DEPTH; |
1101 | DEC_DEC_DEPTH; |
845 | return newRV_noinc ((SV *)av); |
1102 | return newRV_noinc ((SV *)av); |
846 | |
1103 | |
… | |
… | |
851 | } |
1108 | } |
852 | |
1109 | |
853 | static SV * |
1110 | static SV * |
854 | decode_hv (dec_t *dec) |
1111 | decode_hv (dec_t *dec) |
855 | { |
1112 | { |
|
|
1113 | SV *sv; |
856 | HV *hv = newHV (); |
1114 | HV *hv = newHV (); |
857 | |
1115 | |
858 | DEC_INC_DEPTH; |
1116 | DEC_INC_DEPTH; |
859 | decode_ws (dec); |
1117 | decode_ws (dec); |
860 | |
1118 | |
861 | if (*dec->cur == '}') |
1119 | if (*dec->cur == '}') |
862 | ++dec->cur; |
1120 | ++dec->cur; |
863 | else |
1121 | else |
864 | for (;;) |
1122 | for (;;) |
865 | { |
1123 | { |
|
|
1124 | EXPECT_CH ('"'); |
|
|
1125 | |
|
|
1126 | // heuristic: assume that |
|
|
1127 | // a) decode_str + hv_store_ent are abysmally slow. |
|
|
1128 | // b) most hash keys are short, simple ascii text. |
|
|
1129 | // => try to "fast-match" such strings to avoid |
|
|
1130 | // the overhead of decode_str + hv_store_ent. |
|
|
1131 | { |
866 | SV *key, *value; |
1132 | SV *value; |
|
|
1133 | char *p = dec->cur; |
|
|
1134 | char *e = p + 24; // only try up to 24 bytes |
867 | |
1135 | |
868 | decode_ws (dec); EXPECT_CH ('"'); |
1136 | for (;;) |
869 | |
|
|
870 | key = decode_str (dec); |
|
|
871 | if (!key) |
|
|
872 | goto fail; |
|
|
873 | |
|
|
874 | decode_ws (dec); EXPECT_CH (':'); |
|
|
875 | |
|
|
876 | value = decode_sv (dec); |
|
|
877 | if (!value) |
|
|
878 | { |
1137 | { |
|
|
1138 | // the >= 0x80 is true on most architectures |
|
|
1139 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1140 | { |
|
|
1141 | // slow path, back up and use decode_str |
|
|
1142 | SV *key = decode_str (dec); |
|
|
1143 | if (!key) |
|
|
1144 | goto fail; |
|
|
1145 | |
|
|
1146 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1147 | |
|
|
1148 | decode_ws (dec); |
|
|
1149 | value = decode_sv (dec); |
|
|
1150 | if (!value) |
|
|
1151 | { |
|
|
1152 | SvREFCNT_dec (key); |
|
|
1153 | goto fail; |
|
|
1154 | } |
|
|
1155 | |
|
|
1156 | hv_store_ent (hv, key, value, 0); |
879 | SvREFCNT_dec (key); |
1157 | SvREFCNT_dec (key); |
|
|
1158 | |
|
|
1159 | break; |
|
|
1160 | } |
|
|
1161 | else if (*p == '"') |
|
|
1162 | { |
|
|
1163 | // fast path, got a simple key |
|
|
1164 | char *key = dec->cur; |
|
|
1165 | int len = p - key; |
|
|
1166 | dec->cur = p + 1; |
|
|
1167 | |
|
|
1168 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1169 | |
|
|
1170 | decode_ws (dec); |
|
|
1171 | value = decode_sv (dec); |
|
|
1172 | if (!value) |
880 | goto fail; |
1173 | goto fail; |
|
|
1174 | |
|
|
1175 | hv_store (hv, key, len, value, 0); |
|
|
1176 | |
|
|
1177 | break; |
|
|
1178 | } |
|
|
1179 | |
|
|
1180 | ++p; |
881 | } |
1181 | } |
882 | |
1182 | } |
883 | hv_store_ent (hv, key, value, 0); |
|
|
884 | SvREFCNT_dec (key); |
|
|
885 | |
1183 | |
886 | decode_ws (dec); |
1184 | decode_ws (dec); |
887 | |
1185 | |
888 | if (*dec->cur == '}') |
1186 | if (*dec->cur == '}') |
889 | { |
1187 | { |
… | |
… | |
893 | |
1191 | |
894 | if (*dec->cur != ',') |
1192 | if (*dec->cur != ',') |
895 | ERR (", or } expected while parsing object/hash"); |
1193 | ERR (", or } expected while parsing object/hash"); |
896 | |
1194 | |
897 | ++dec->cur; |
1195 | ++dec->cur; |
|
|
1196 | |
|
|
1197 | decode_ws (dec); |
|
|
1198 | |
|
|
1199 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1200 | { |
|
|
1201 | ++dec->cur; |
|
|
1202 | break; |
|
|
1203 | } |
898 | } |
1204 | } |
899 | |
1205 | |
900 | DEC_DEC_DEPTH; |
1206 | DEC_DEC_DEPTH; |
901 | return newRV_noinc ((SV *)hv); |
1207 | sv = newRV_noinc ((SV *)hv); |
|
|
1208 | |
|
|
1209 | // check filter callbacks |
|
|
1210 | if (dec->json.flags & F_HOOK) |
|
|
1211 | { |
|
|
1212 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1213 | { |
|
|
1214 | HE *cb, *he; |
|
|
1215 | |
|
|
1216 | hv_iterinit (hv); |
|
|
1217 | he = hv_iternext (hv); |
|
|
1218 | hv_iterinit (hv); |
|
|
1219 | |
|
|
1220 | // the next line creates a mortal sv each time its called. |
|
|
1221 | // might want to optimise this for common cases. |
|
|
1222 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1223 | |
|
|
1224 | if (cb) |
|
|
1225 | { |
|
|
1226 | dSP; |
|
|
1227 | int count; |
|
|
1228 | |
|
|
1229 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1230 | XPUSHs (HeVAL (he)); |
|
|
1231 | |
|
|
1232 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1233 | |
|
|
1234 | if (count == 1) |
|
|
1235 | { |
|
|
1236 | sv = newSVsv (POPs); |
|
|
1237 | FREETMPS; LEAVE; |
|
|
1238 | return sv; |
|
|
1239 | } |
|
|
1240 | |
|
|
1241 | FREETMPS; LEAVE; |
|
|
1242 | } |
|
|
1243 | } |
|
|
1244 | |
|
|
1245 | if (dec->json.cb_object) |
|
|
1246 | { |
|
|
1247 | dSP; |
|
|
1248 | int count; |
|
|
1249 | |
|
|
1250 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1251 | XPUSHs (sv_2mortal (sv)); |
|
|
1252 | |
|
|
1253 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1254 | |
|
|
1255 | if (count == 1) |
|
|
1256 | { |
|
|
1257 | sv = newSVsv (POPs); |
|
|
1258 | FREETMPS; LEAVE; |
|
|
1259 | return sv; |
|
|
1260 | } |
|
|
1261 | |
|
|
1262 | SvREFCNT_inc (sv); |
|
|
1263 | FREETMPS; LEAVE; |
|
|
1264 | } |
|
|
1265 | } |
|
|
1266 | |
|
|
1267 | return sv; |
902 | |
1268 | |
903 | fail: |
1269 | fail: |
904 | SvREFCNT_dec (hv); |
1270 | SvREFCNT_dec (hv); |
905 | DEC_DEC_DEPTH; |
1271 | DEC_DEC_DEPTH; |
906 | return 0; |
1272 | return 0; |
907 | } |
1273 | } |
908 | |
1274 | |
909 | static SV * |
1275 | static SV * |
910 | decode_sv (dec_t *dec) |
1276 | decode_sv (dec_t *dec) |
911 | { |
1277 | { |
912 | decode_ws (dec); |
1278 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1279 | // to parse anything. |
913 | switch (*dec->cur) |
1280 | switch (*dec->cur) |
914 | { |
1281 | { |
915 | case '"': ++dec->cur; return decode_str (dec); |
1282 | case '"': ++dec->cur; return decode_str (dec); |
916 | case '[': ++dec->cur; return decode_av (dec); |
1283 | case '[': ++dec->cur; return decode_av (dec); |
917 | case '{': ++dec->cur; return decode_hv (dec); |
1284 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
923 | |
1290 | |
924 | case 't': |
1291 | case 't': |
925 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1292 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
926 | { |
1293 | { |
927 | dec->cur += 4; |
1294 | dec->cur += 4; |
928 | return newSViv (1); |
1295 | #if JSON_SLOW |
|
|
1296 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1297 | #endif |
|
|
1298 | return SvREFCNT_inc (json_true); |
929 | } |
1299 | } |
930 | else |
1300 | else |
931 | ERR ("'true' expected"); |
1301 | ERR ("'true' expected"); |
932 | |
1302 | |
933 | break; |
1303 | break; |
934 | |
1304 | |
935 | case 'f': |
1305 | case 'f': |
936 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1306 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
937 | { |
1307 | { |
938 | dec->cur += 5; |
1308 | dec->cur += 5; |
939 | return newSViv (0); |
1309 | #if JSON_SLOW |
|
|
1310 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1311 | #endif |
|
|
1312 | return SvREFCNT_inc (json_false); |
940 | } |
1313 | } |
941 | else |
1314 | else |
942 | ERR ("'false' expected"); |
1315 | ERR ("'false' expected"); |
943 | |
1316 | |
944 | break; |
1317 | break; |
… | |
… | |
953 | ERR ("'null' expected"); |
1326 | ERR ("'null' expected"); |
954 | |
1327 | |
955 | break; |
1328 | break; |
956 | |
1329 | |
957 | default: |
1330 | default: |
958 | ERR ("malformed json string, neither array, object, number, string or atom"); |
1331 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
959 | break; |
1332 | break; |
960 | } |
1333 | } |
961 | |
1334 | |
962 | fail: |
1335 | fail: |
963 | return 0; |
1336 | return 0; |
964 | } |
1337 | } |
965 | |
1338 | |
966 | static SV * |
1339 | static SV * |
967 | decode_json (SV *string, U32 flags) |
1340 | decode_json (SV *string, JSON *json, UV *offset_return) |
968 | { |
1341 | { |
|
|
1342 | dec_t dec; |
|
|
1343 | UV offset; |
969 | SV *sv; |
1344 | SV *sv; |
970 | |
1345 | |
|
|
1346 | SvGETMAGIC (string); |
|
|
1347 | SvUPGRADE (string, SVt_PV); |
|
|
1348 | |
|
|
1349 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1350 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1351 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1352 | |
971 | if (flags & F_UTF8) |
1353 | if (json->flags & F_UTF8) |
972 | sv_utf8_downgrade (string, 0); |
1354 | sv_utf8_downgrade (string, 0); |
973 | else |
1355 | else |
974 | sv_utf8_upgrade (string); |
1356 | sv_utf8_upgrade (string); |
975 | |
1357 | |
976 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1358 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
977 | |
1359 | |
978 | dec_t dec; |
1360 | dec.json = *json; |
979 | dec.flags = flags; |
|
|
980 | dec.cur = SvPVX (string); |
1361 | dec.cur = SvPVX (string); |
981 | dec.end = SvEND (string); |
1362 | dec.end = SvEND (string); |
982 | dec.err = 0; |
1363 | dec.err = 0; |
983 | dec.depth = 0; |
1364 | dec.depth = 0; |
984 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1365 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
985 | |
1366 | |
|
|
1367 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1368 | dec.json.flags |= F_HOOK; |
|
|
1369 | |
986 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1370 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1371 | |
|
|
1372 | decode_ws (&dec); |
987 | sv = decode_sv (&dec); |
1373 | sv = decode_sv (&dec); |
988 | |
1374 | |
|
|
1375 | if (!(offset_return || !sv)) |
|
|
1376 | { |
|
|
1377 | // check for trailing garbage |
|
|
1378 | decode_ws (&dec); |
|
|
1379 | |
|
|
1380 | if (*dec.cur) |
|
|
1381 | { |
|
|
1382 | dec.err = "garbage after JSON object"; |
|
|
1383 | SvREFCNT_dec (sv); |
|
|
1384 | sv = 0; |
|
|
1385 | } |
|
|
1386 | } |
|
|
1387 | |
|
|
1388 | if (offset_return || !sv) |
|
|
1389 | { |
|
|
1390 | offset = dec.json.flags & F_UTF8 |
|
|
1391 | ? dec.cur - SvPVX (string) |
|
|
1392 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1393 | |
|
|
1394 | if (offset_return) |
|
|
1395 | *offset_return = offset; |
|
|
1396 | } |
|
|
1397 | |
989 | if (!sv) |
1398 | if (!sv) |
990 | { |
1399 | { |
991 | IV offset = dec.flags & F_UTF8 |
|
|
992 | ? dec.cur - SvPVX (string) |
|
|
993 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
994 | SV *uni = sv_newmortal (); |
1400 | SV *uni = sv_newmortal (); |
995 | |
1401 | |
996 | // horrible hack to silence warning inside pv_uni_display |
1402 | // horrible hack to silence warning inside pv_uni_display |
997 | COP cop = *PL_curcop; |
1403 | COP cop = *PL_curcop; |
998 | cop.cop_warnings = pWARN_NONE; |
1404 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1000 | SAVEVPTR (PL_curcop); |
1406 | SAVEVPTR (PL_curcop); |
1001 | PL_curcop = &cop; |
1407 | PL_curcop = &cop; |
1002 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1408 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1003 | LEAVE; |
1409 | LEAVE; |
1004 | |
1410 | |
1005 | croak ("%s, at character offset %d (%s)", |
1411 | croak ("%s, at character offset %d [\"%s\"]", |
1006 | dec.err, |
1412 | dec.err, |
1007 | (int)offset, |
1413 | (int)offset, |
1008 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1414 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1009 | } |
1415 | } |
1010 | |
1416 | |
1011 | sv = sv_2mortal (sv); |
1417 | sv = sv_2mortal (sv); |
1012 | |
1418 | |
1013 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1419 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1014 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1420 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1015 | |
1421 | |
1016 | return sv; |
1422 | return sv; |
1017 | } |
1423 | } |
1018 | |
1424 | |
… | |
… | |
1022 | MODULE = JSON::XS PACKAGE = JSON::XS |
1428 | MODULE = JSON::XS PACKAGE = JSON::XS |
1023 | |
1429 | |
1024 | BOOT: |
1430 | BOOT: |
1025 | { |
1431 | { |
1026 | int i; |
1432 | int i; |
1027 | |
|
|
1028 | memset (decode_hexdigit, 0xff, 256); |
|
|
1029 | |
1433 | |
1030 | for (i = 0; i < 256; ++i) |
1434 | for (i = 0; i < 256; ++i) |
1031 | decode_hexdigit [i] = |
1435 | decode_hexdigit [i] = |
1032 | i >= '0' && i <= '9' ? i - '0' |
1436 | i >= '0' && i <= '9' ? i - '0' |
1033 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1437 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1034 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1438 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1035 | : -1; |
1439 | : -1; |
1036 | |
1440 | |
1037 | json_stash = gv_stashpv ("JSON::XS", 1); |
1441 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1442 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1443 | |
|
|
1444 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1445 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1038 | } |
1446 | } |
1039 | |
1447 | |
1040 | PROTOTYPES: DISABLE |
1448 | PROTOTYPES: DISABLE |
1041 | |
1449 | |
1042 | SV *new (char *dummy) |
1450 | void CLONE (...) |
1043 | CODE: |
1451 | CODE: |
1044 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1452 | json_stash = 0; |
|
|
1453 | json_boolean_stash = 0; |
|
|
1454 | |
|
|
1455 | void new (char *klass) |
|
|
1456 | PPCODE: |
|
|
1457 | { |
|
|
1458 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1459 | SvPOK_only (pv); |
|
|
1460 | Zero (SvPVX (pv), 1, JSON); |
|
|
1461 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1462 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
|
|
1463 | } |
|
|
1464 | |
|
|
1465 | void ascii (JSON *self, int enable = 1) |
|
|
1466 | ALIAS: |
|
|
1467 | ascii = F_ASCII |
|
|
1468 | latin1 = F_LATIN1 |
|
|
1469 | utf8 = F_UTF8 |
|
|
1470 | indent = F_INDENT |
|
|
1471 | canonical = F_CANONICAL |
|
|
1472 | space_before = F_SPACE_BEFORE |
|
|
1473 | space_after = F_SPACE_AFTER |
|
|
1474 | pretty = F_PRETTY |
|
|
1475 | allow_nonref = F_ALLOW_NONREF |
|
|
1476 | shrink = F_SHRINK |
|
|
1477 | allow_blessed = F_ALLOW_BLESSED |
|
|
1478 | convert_blessed = F_CONV_BLESSED |
|
|
1479 | relaxed = F_RELAXED |
|
|
1480 | PPCODE: |
|
|
1481 | { |
|
|
1482 | if (enable) |
|
|
1483 | self->flags |= ix; |
|
|
1484 | else |
|
|
1485 | self->flags &= ~ix; |
|
|
1486 | |
|
|
1487 | XPUSHs (ST (0)); |
|
|
1488 | } |
|
|
1489 | |
|
|
1490 | void get_ascii (JSON *self) |
|
|
1491 | ALIAS: |
|
|
1492 | get_ascii = F_ASCII |
|
|
1493 | get_latin1 = F_LATIN1 |
|
|
1494 | get_utf8 = F_UTF8 |
|
|
1495 | get_indent = F_INDENT |
|
|
1496 | get_canonical = F_CANONICAL |
|
|
1497 | get_space_before = F_SPACE_BEFORE |
|
|
1498 | get_space_after = F_SPACE_AFTER |
|
|
1499 | get_pretty = F_PRETTY |
|
|
1500 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1501 | get_shrink = F_SHRINK |
|
|
1502 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1503 | get_convert_blessed = F_CONV_BLESSED |
|
|
1504 | get_relaxed = F_RELAXED |
|
|
1505 | PPCODE: |
|
|
1506 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1507 | |
|
|
1508 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
|
|
1509 | PPCODE: |
|
|
1510 | { |
|
|
1511 | UV log2 = 0; |
|
|
1512 | |
|
|
1513 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1514 | |
|
|
1515 | while ((1UL << log2) < max_depth) |
|
|
1516 | ++log2; |
|
|
1517 | |
|
|
1518 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1519 | |
|
|
1520 | XPUSHs (ST (0)); |
|
|
1521 | } |
|
|
1522 | |
|
|
1523 | U32 get_max_depth (JSON *self) |
|
|
1524 | CODE: |
|
|
1525 | RETVAL = DEC_DEPTH (self->flags); |
1045 | OUTPUT: |
1526 | OUTPUT: |
1046 | RETVAL |
1527 | RETVAL |
1047 | |
1528 | |
1048 | SV *ascii (SV *self, int enable = 1) |
1529 | void max_size (JSON *self, UV max_size = 0) |
1049 | ALIAS: |
1530 | PPCODE: |
1050 | ascii = F_ASCII |
1531 | { |
1051 | utf8 = F_UTF8 |
1532 | UV log2 = 0; |
1052 | indent = F_INDENT |
1533 | |
1053 | canonical = F_CANONICAL |
1534 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1054 | space_before = F_SPACE_BEFORE |
1535 | if (max_size == 1) max_size = 2; |
1055 | space_after = F_SPACE_AFTER |
1536 | |
1056 | pretty = F_PRETTY |
1537 | while ((1UL << log2) < max_size) |
1057 | allow_nonref = F_ALLOW_NONREF |
1538 | ++log2; |
1058 | shrink = F_SHRINK |
1539 | |
|
|
1540 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1541 | |
|
|
1542 | XPUSHs (ST (0)); |
|
|
1543 | } |
|
|
1544 | |
|
|
1545 | int get_max_size (JSON *self) |
1059 | CODE: |
1546 | CODE: |
1060 | { |
1547 | RETVAL = DEC_SIZE (self->flags); |
1061 | UV *uv = SvJSON (self); |
|
|
1062 | if (enable) |
|
|
1063 | *uv |= ix; |
|
|
1064 | else |
|
|
1065 | *uv &= ~ix; |
|
|
1066 | |
|
|
1067 | RETVAL = newSVsv (self); |
|
|
1068 | } |
|
|
1069 | OUTPUT: |
1548 | OUTPUT: |
1070 | RETVAL |
1549 | RETVAL |
1071 | |
1550 | |
1072 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1551 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
|
|
1552 | PPCODE: |
|
|
1553 | { |
|
|
1554 | SvREFCNT_dec (self->cb_object); |
|
|
1555 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1556 | |
|
|
1557 | XPUSHs (ST (0)); |
|
|
1558 | } |
|
|
1559 | |
|
|
1560 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1561 | PPCODE: |
|
|
1562 | { |
|
|
1563 | if (!self->cb_sk_object) |
|
|
1564 | self->cb_sk_object = newHV (); |
|
|
1565 | |
|
|
1566 | if (SvOK (cb)) |
|
|
1567 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1568 | else |
|
|
1569 | { |
|
|
1570 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1571 | |
|
|
1572 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1573 | { |
|
|
1574 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1575 | self->cb_sk_object = 0; |
|
|
1576 | } |
|
|
1577 | } |
|
|
1578 | |
|
|
1579 | XPUSHs (ST (0)); |
|
|
1580 | } |
|
|
1581 | |
|
|
1582 | void encode (JSON *self, SV *scalar) |
|
|
1583 | PPCODE: |
|
|
1584 | XPUSHs (encode_json (scalar, self)); |
|
|
1585 | |
|
|
1586 | void decode (JSON *self, SV *jsonstr) |
|
|
1587 | PPCODE: |
|
|
1588 | XPUSHs (decode_json (jsonstr, self, 0)); |
|
|
1589 | |
|
|
1590 | void decode_prefix (JSON *self, SV *jsonstr) |
|
|
1591 | PPCODE: |
|
|
1592 | { |
|
|
1593 | UV offset; |
|
|
1594 | EXTEND (SP, 2); |
|
|
1595 | PUSHs (decode_json (jsonstr, self, &offset)); |
|
|
1596 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1597 | } |
|
|
1598 | |
|
|
1599 | void DESTROY (JSON *self) |
1073 | CODE: |
1600 | CODE: |
1074 | { |
1601 | SvREFCNT_dec (self->cb_sk_object); |
1075 | UV *uv = SvJSON (self); |
1602 | SvREFCNT_dec (self->cb_object); |
1076 | UV log2 = 0; |
|
|
1077 | |
1603 | |
1078 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1604 | PROTOTYPES: ENABLE |
1079 | |
1605 | |
1080 | while ((1UL << log2) < max_depth) |
1606 | void to_json (SV *scalar) |
1081 | ++log2; |
|
|
1082 | |
|
|
1083 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1084 | |
|
|
1085 | RETVAL = newSVsv (self); |
|
|
1086 | } |
|
|
1087 | OUTPUT: |
|
|
1088 | RETVAL |
|
|
1089 | |
|
|
1090 | void encode (SV *self, SV *scalar) |
|
|
1091 | PPCODE: |
1607 | PPCODE: |
|
|
1608 | { |
|
|
1609 | JSON json = { F_DEFAULT | F_UTF8 }; |
1092 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1610 | XPUSHs (encode_json (scalar, &json)); |
|
|
1611 | } |
1093 | |
1612 | |
1094 | void decode (SV *self, SV *jsonstr) |
1613 | void from_json (SV *jsonstr) |
1095 | PPCODE: |
1614 | PPCODE: |
|
|
1615 | { |
|
|
1616 | JSON json = { F_DEFAULT | F_UTF8 }; |
1096 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1617 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1618 | } |
1097 | |
1619 | |
1098 | PROTOTYPES: ENABLE |
|
|
1099 | |
|
|
1100 | void to_json (SV *scalar) |
|
|
1101 | ALIAS: |
|
|
1102 | objToJson = 0 |
|
|
1103 | PPCODE: |
|
|
1104 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
|
|
1105 | |
|
|
1106 | void from_json (SV *jsonstr) |
|
|
1107 | ALIAS: |
|
|
1108 | jsonToObj = 0 |
|
|
1109 | PPCODE: |
|
|
1110 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
|
|
1111 | |
|
|