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" |
8 | #include <stdio.h> |
|
|
9 | #include <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
14 | // some old perls do not have this, try to make it work, no |
15 | // some old perls do not have this, try to make it work, no |
15 | // guarentees, though. |
16 | // guarentees, though. if it breaks, you get to keep the pieces. |
16 | #ifndef UTF8_MAXBYTES |
17 | #ifndef UTF8_MAXBYTES |
17 | # define UTF8_MAXBYTES 13 |
18 | # define UTF8_MAXBYTES 13 |
18 | #endif |
19 | #endif |
19 | |
20 | |
20 | #define F_ASCII 0x00000001UL |
21 | #define F_ASCII 0x00000001UL |
21 | #define F_LATIN1 0x00000002UL |
22 | #define F_LATIN1 0x00000002UL |
22 | #define F_UTF8 0x00000004UL |
23 | #define F_UTF8 0x00000004UL |
23 | #define F_INDENT 0x00000008UL |
24 | #define F_INDENT 0x00000008UL |
24 | #define F_CANONICAL 0x00000010UL |
25 | #define F_CANONICAL 0x00000010UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
|
|
30 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
31 | #define F_CONV_BLESSED 0x00000800UL |
|
|
32 | #define F_RELAXED 0x00001000UL |
|
|
33 | |
29 | #define F_MAXDEPTH 0xf8000000UL |
34 | #define F_MAXDEPTH 0xf8000000UL |
30 | #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 |
31 | |
39 | |
32 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
40 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
33 | |
41 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
34 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
35 | // F_BLESSED? <=> { $__class__$ => } |
|
|
36 | |
42 | |
37 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
43 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
38 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
44 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
39 | |
45 | |
40 | #define INIT_SIZE 32 // initial scalar size to be allocated |
46 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
44 | |
50 | |
45 | #define SB do { |
51 | #define SB do { |
46 | #define SE } while (0) |
52 | #define SE } while (0) |
47 | |
53 | |
48 | #if __GNUC__ >= 3 |
54 | #if __GNUC__ >= 3 |
49 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
55 | # define expect(expr,value) __builtin_expect ((expr), (value)) |
50 | # define inline inline |
56 | # define INLINE static inline |
51 | #else |
57 | #else |
52 | # define expect(expr,value) (expr) |
58 | # define expect(expr,value) (expr) |
53 | # define inline static |
59 | # define INLINE static |
54 | #endif |
60 | #endif |
55 | |
61 | |
56 | #define expect_false(expr) expect ((expr) != 0, 0) |
62 | #define expect_false(expr) expect ((expr) != 0, 0) |
57 | #define expect_true(expr) expect ((expr) != 0, 1) |
63 | #define expect_true(expr) expect ((expr) != 0, 1) |
58 | |
64 | |
|
|
65 | #define IN_RANGE_INC(type,val,beg,end) \ |
|
|
66 | ((unsigned type)((unsigned type)(val) - (unsigned type)(beg)) \ |
|
|
67 | <= (unsigned type)((unsigned type)(end) - (unsigned type)(beg))) |
|
|
68 | |
|
|
69 | #ifdef USE_ITHREADS |
|
|
70 | # define JSON_SLOW 1 |
|
|
71 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
72 | #else |
|
|
73 | # define JSON_SLOW 0 |
|
|
74 | # define JSON_STASH json_stash |
|
|
75 | #endif |
|
|
76 | |
59 | static HV *json_stash; // JSON::XS:: |
77 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
78 | static SV *json_true, *json_false; |
|
|
79 | |
|
|
80 | typedef struct { |
|
|
81 | U32 flags; |
|
|
82 | SV *cb_object; |
|
|
83 | HV *cb_sk_object; |
|
|
84 | } JSON; |
60 | |
85 | |
61 | ///////////////////////////////////////////////////////////////////////////// |
86 | ///////////////////////////////////////////////////////////////////////////// |
62 | // utility functions |
87 | // utility functions |
63 | |
88 | |
64 | static UV * |
89 | INLINE void |
65 | SvJSON (SV *sv) |
|
|
66 | { |
|
|
67 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
68 | croak ("object is not of type JSON::XS"); |
|
|
69 | |
|
|
70 | return &SvUVX (SvRV (sv)); |
|
|
71 | } |
|
|
72 | |
|
|
73 | static void |
|
|
74 | shrink (SV *sv) |
90 | shrink (SV *sv) |
75 | { |
91 | { |
76 | sv_utf8_downgrade (sv, 1); |
92 | sv_utf8_downgrade (sv, 1); |
77 | if (SvLEN (sv) > SvCUR (sv) + 1) |
93 | if (SvLEN (sv) > SvCUR (sv) + 1) |
78 | { |
94 | { |
… | |
… | |
87 | // decode an utf-8 character and return it, or (UV)-1 in |
103 | // decode an utf-8 character and return it, or (UV)-1 in |
88 | // case of an error. |
104 | // case of an error. |
89 | // we special-case "safe" characters from U+80 .. U+7FF, |
105 | // we special-case "safe" characters from U+80 .. U+7FF, |
90 | // but use the very good perl function to parse anything else. |
106 | // but use the very good perl function to parse anything else. |
91 | // note that we never call this function for a ascii codepoints |
107 | // note that we never call this function for a ascii codepoints |
92 | inline UV |
108 | INLINE UV |
93 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
109 | decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen) |
94 | { |
110 | { |
95 | if (expect_false (s[0] > 0xdf || s[0] < 0xc2)) |
111 | if (expect_true (len >= 2 |
96 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
112 | && IN_RANGE_INC (char, s[0], 0xc2, 0xdf) |
97 | else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf) |
113 | && IN_RANGE_INC (char, s[1], 0x80, 0xbf))) |
98 | { |
114 | { |
99 | *clen = 2; |
115 | *clen = 2; |
100 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
116 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
101 | } |
117 | } |
102 | else |
118 | else |
103 | { |
119 | return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY); |
104 | *clen = (STRLEN)-1; |
120 | } |
105 | return (UV)-1; |
121 | |
|
|
122 | // likewise for encoding, also never called for ascii codepoints |
|
|
123 | // this function takes advantage of this fact, although current gccs |
|
|
124 | // seem to optimise the check for >= 0x80 away anyways |
|
|
125 | INLINE unsigned char * |
|
|
126 | encode_utf8 (unsigned char *s, UV ch) |
|
|
127 | { |
|
|
128 | if (ch <= 0x7FF) |
106 | } |
129 | { |
|
|
130 | *s++ = (ch >> 6) | 0xc0; |
|
|
131 | *s++ = (ch & 0x3f) | 0x80; |
|
|
132 | } |
|
|
133 | else |
|
|
134 | s = uvuni_to_utf8_flags (s, ch, 0); |
|
|
135 | |
|
|
136 | return s; |
107 | } |
137 | } |
108 | |
138 | |
109 | ///////////////////////////////////////////////////////////////////////////// |
139 | ///////////////////////////////////////////////////////////////////////////// |
110 | // encoder |
140 | // encoder |
111 | |
141 | |
… | |
… | |
113 | typedef struct |
143 | typedef struct |
114 | { |
144 | { |
115 | char *cur; // SvPVX (sv) + current output position |
145 | char *cur; // SvPVX (sv) + current output position |
116 | char *end; // SvEND (sv) |
146 | char *end; // SvEND (sv) |
117 | SV *sv; // result scalar |
147 | SV *sv; // result scalar |
118 | U32 flags; // F_* |
148 | JSON json; |
119 | U32 indent; // indentation level |
149 | U32 indent; // indentation level |
120 | U32 maxdepth; // max. indentation/recursion level |
150 | U32 maxdepth; // max. indentation/recursion level |
|
|
151 | UV limit; // escape character values >= this value when encoding |
121 | } enc_t; |
152 | } enc_t; |
122 | |
153 | |
123 | inline void |
154 | INLINE void |
124 | need (enc_t *enc, STRLEN len) |
155 | need (enc_t *enc, STRLEN len) |
125 | { |
156 | { |
126 | if (expect_false (enc->cur + len >= enc->end)) |
157 | if (expect_false (enc->cur + len >= enc->end)) |
127 | { |
158 | { |
128 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
159 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
… | |
… | |
130 | enc->cur = SvPVX (enc->sv) + cur; |
161 | enc->cur = SvPVX (enc->sv) + cur; |
131 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
162 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
132 | } |
163 | } |
133 | } |
164 | } |
134 | |
165 | |
135 | inline void |
166 | INLINE void |
136 | encode_ch (enc_t *enc, char ch) |
167 | encode_ch (enc_t *enc, char ch) |
137 | { |
168 | { |
138 | need (enc, 1); |
169 | need (enc, 1); |
139 | *enc->cur++ = ch; |
170 | *enc->cur++ = ch; |
140 | } |
171 | } |
… | |
… | |
194 | { |
225 | { |
195 | uch = ch; |
226 | uch = ch; |
196 | clen = 1; |
227 | clen = 1; |
197 | } |
228 | } |
198 | |
229 | |
199 | if (uch > 0x10FFFFUL) |
230 | if (uch < 0x80/*0x20*/ || uch >= enc->limit) |
200 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
201 | |
|
|
202 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
|
|
203 | { |
231 | { |
204 | if (uch > 0xFFFFUL) |
232 | if (uch > 0xFFFFUL) |
205 | { |
233 | { |
|
|
234 | if (uch > 0x10FFFFUL) |
|
|
235 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
|
|
236 | |
206 | need (enc, len += 11); |
237 | need (enc, len += 11); |
207 | sprintf (enc->cur, "\\u%04x\\u%04x", |
238 | sprintf (enc->cur, "\\u%04x\\u%04x", |
208 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
239 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
209 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
240 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
210 | enc->cur += 12; |
241 | enc->cur += 12; |
… | |
… | |
221 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
252 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
222 | } |
253 | } |
223 | |
254 | |
224 | str += clen; |
255 | str += clen; |
225 | } |
256 | } |
226 | else if (enc->flags & F_LATIN1) |
257 | else if (enc->json.flags & F_LATIN1) |
227 | { |
258 | { |
228 | *enc->cur++ = uch; |
259 | *enc->cur++ = uch; |
229 | str += clen; |
260 | str += clen; |
230 | } |
261 | } |
231 | else if (is_utf8) |
262 | else if (is_utf8) |
… | |
… | |
238 | while (--clen); |
269 | while (--clen); |
239 | } |
270 | } |
240 | else |
271 | else |
241 | { |
272 | { |
242 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
273 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
243 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
274 | enc->cur = encode_utf8 (enc->cur, uch); |
244 | ++str; |
275 | ++str; |
245 | } |
276 | } |
246 | } |
277 | } |
247 | } |
278 | } |
248 | } |
279 | } |
249 | |
280 | |
250 | --len; |
281 | --len; |
251 | } |
282 | } |
252 | } |
283 | } |
253 | |
284 | |
254 | inline void |
285 | INLINE void |
255 | encode_indent (enc_t *enc) |
286 | encode_indent (enc_t *enc) |
256 | { |
287 | { |
257 | if (enc->flags & F_INDENT) |
288 | if (enc->json.flags & F_INDENT) |
258 | { |
289 | { |
259 | int spaces = enc->indent * INDENT_STEP; |
290 | int spaces = enc->indent * INDENT_STEP; |
260 | |
291 | |
261 | need (enc, spaces); |
292 | need (enc, spaces); |
262 | memset (enc->cur, ' ', spaces); |
293 | memset (enc->cur, ' ', spaces); |
263 | enc->cur += spaces; |
294 | enc->cur += spaces; |
264 | } |
295 | } |
265 | } |
296 | } |
266 | |
297 | |
267 | inline void |
298 | INLINE void |
268 | encode_space (enc_t *enc) |
299 | encode_space (enc_t *enc) |
269 | { |
300 | { |
270 | need (enc, 1); |
301 | need (enc, 1); |
271 | encode_ch (enc, ' '); |
302 | encode_ch (enc, ' '); |
272 | } |
303 | } |
273 | |
304 | |
274 | inline void |
305 | INLINE void |
275 | encode_nl (enc_t *enc) |
306 | encode_nl (enc_t *enc) |
276 | { |
307 | { |
277 | if (enc->flags & F_INDENT) |
308 | if (enc->json.flags & F_INDENT) |
278 | { |
309 | { |
279 | need (enc, 1); |
310 | need (enc, 1); |
280 | encode_ch (enc, '\n'); |
311 | encode_ch (enc, '\n'); |
281 | } |
312 | } |
282 | } |
313 | } |
283 | |
314 | |
284 | inline void |
315 | INLINE void |
285 | encode_comma (enc_t *enc) |
316 | encode_comma (enc_t *enc) |
286 | { |
317 | { |
287 | encode_ch (enc, ','); |
318 | encode_ch (enc, ','); |
288 | |
319 | |
289 | if (enc->flags & F_INDENT) |
320 | if (enc->json.flags & F_INDENT) |
290 | encode_nl (enc); |
321 | encode_nl (enc); |
291 | else if (enc->flags & F_SPACE_AFTER) |
322 | else if (enc->json.flags & F_SPACE_AFTER) |
292 | encode_space (enc); |
323 | encode_space (enc); |
293 | } |
324 | } |
294 | |
325 | |
295 | static void encode_sv (enc_t *enc, SV *sv); |
326 | static void encode_sv (enc_t *enc, SV *sv); |
296 | |
327 | |
… | |
… | |
300 | int i, len = av_len (av); |
331 | int i, len = av_len (av); |
301 | |
332 | |
302 | if (enc->indent >= enc->maxdepth) |
333 | if (enc->indent >= enc->maxdepth) |
303 | croak ("data structure too deep (hit recursion limit)"); |
334 | croak ("data structure too deep (hit recursion limit)"); |
304 | |
335 | |
305 | encode_ch (enc, '['); encode_nl (enc); |
336 | encode_ch (enc, '['); |
306 | ++enc->indent; |
337 | |
|
|
338 | if (len >= 0) |
|
|
339 | { |
|
|
340 | encode_nl (enc); ++enc->indent; |
307 | |
341 | |
308 | for (i = 0; i <= len; ++i) |
342 | for (i = 0; i <= len; ++i) |
309 | { |
343 | { |
|
|
344 | SV **svp = av_fetch (av, i, 0); |
|
|
345 | |
310 | encode_indent (enc); |
346 | encode_indent (enc); |
311 | encode_sv (enc, *av_fetch (av, i, 0)); |
|
|
312 | |
347 | |
|
|
348 | if (svp) |
|
|
349 | encode_sv (enc, *svp); |
|
|
350 | else |
|
|
351 | encode_str (enc, "null", 4, 0); |
|
|
352 | |
313 | if (i < len) |
353 | if (i < len) |
314 | encode_comma (enc); |
354 | encode_comma (enc); |
315 | } |
355 | } |
316 | |
356 | |
|
|
357 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
358 | } |
|
|
359 | |
317 | encode_nl (enc); |
360 | encode_ch (enc, ']'); |
318 | |
|
|
319 | --enc->indent; |
|
|
320 | encode_indent (enc); encode_ch (enc, ']'); |
|
|
321 | } |
361 | } |
322 | |
362 | |
323 | static void |
363 | static void |
324 | encode_he (enc_t *enc, HE *he) |
364 | encode_hk (enc_t *enc, HE *he) |
325 | { |
365 | { |
326 | encode_ch (enc, '"'); |
366 | encode_ch (enc, '"'); |
327 | |
367 | |
328 | if (HeKLEN (he) == HEf_SVKEY) |
368 | if (HeKLEN (he) == HEf_SVKEY) |
329 | { |
369 | { |
… | |
… | |
339 | else |
379 | else |
340 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
380 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
341 | |
381 | |
342 | encode_ch (enc, '"'); |
382 | encode_ch (enc, '"'); |
343 | |
383 | |
344 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
384 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
345 | encode_ch (enc, ':'); |
385 | encode_ch (enc, ':'); |
346 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
386 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
347 | encode_sv (enc, HeVAL (he)); |
|
|
348 | } |
387 | } |
349 | |
388 | |
350 | // compare hash entries, used when all keys are bytestrings |
389 | // compare hash entries, used when all keys are bytestrings |
351 | static int |
390 | static int |
352 | he_cmp_fast (const void *a_, const void *b_) |
391 | he_cmp_fast (const void *a_, const void *b_) |
… | |
… | |
357 | HE *b = *(HE **)b_; |
396 | HE *b = *(HE **)b_; |
358 | |
397 | |
359 | STRLEN la = HeKLEN (a); |
398 | STRLEN la = HeKLEN (a); |
360 | STRLEN lb = HeKLEN (b); |
399 | STRLEN lb = HeKLEN (b); |
361 | |
400 | |
362 | if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) |
401 | if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la))) |
363 | cmp = la - lb; |
402 | cmp = lb - la; |
364 | |
403 | |
365 | return cmp; |
404 | return cmp; |
366 | } |
405 | } |
367 | |
406 | |
368 | // compare hash entries, used when some keys are sv's or utf-x |
407 | // compare hash entries, used when some keys are sv's or utf-x |
369 | static int |
408 | static int |
370 | he_cmp_slow (const void *a, const void *b) |
409 | he_cmp_slow (const void *a, const void *b) |
371 | { |
410 | { |
372 | return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); |
411 | return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a)); |
373 | } |
412 | } |
374 | |
413 | |
375 | static void |
414 | static void |
376 | encode_hv (enc_t *enc, HV *hv) |
415 | encode_hv (enc_t *enc, HV *hv) |
377 | { |
416 | { |
|
|
417 | HE *he; |
378 | int count, i; |
418 | int count; |
379 | |
419 | |
380 | if (enc->indent >= enc->maxdepth) |
420 | if (enc->indent >= enc->maxdepth) |
381 | croak ("data structure too deep (hit recursion limit)"); |
421 | croak ("data structure too deep (hit recursion limit)"); |
382 | |
422 | |
383 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
423 | encode_ch (enc, '{'); |
384 | |
424 | |
385 | if ((count = hv_iterinit (hv))) |
|
|
386 | { |
|
|
387 | // for canonical output we have to sort by keys first |
425 | // for canonical output we have to sort by keys first |
388 | // actually, this is mostly due to the stupid so-called |
426 | // actually, this is mostly due to the stupid so-called |
389 | // security workaround added somewhere in 5.8.x. |
427 | // security workaround added somewhere in 5.8.x. |
390 | // that randomises hash orderings |
428 | // that randomises hash orderings |
391 | if (enc->flags & F_CANONICAL) |
429 | if (enc->json.flags & F_CANONICAL) |
|
|
430 | { |
|
|
431 | int count = hv_iterinit (hv); |
|
|
432 | |
|
|
433 | if (SvMAGICAL (hv)) |
392 | { |
434 | { |
|
|
435 | // need to count by iterating. could improve by dynamically building the vector below |
|
|
436 | // but I don't care for the speed of this special case. |
|
|
437 | // note also that we will run into undefined behaviour when the two iterations |
|
|
438 | // do not result in the same count, something I might care for in some later release. |
|
|
439 | |
|
|
440 | count = 0; |
|
|
441 | while (hv_iternext (hv)) |
|
|
442 | ++count; |
|
|
443 | |
|
|
444 | hv_iterinit (hv); |
|
|
445 | } |
|
|
446 | |
|
|
447 | if (count) |
|
|
448 | { |
393 | int fast = 1; |
449 | int i, fast = 1; |
394 | HE *he; |
|
|
395 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
450 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
396 | HE **hes = _alloca (count * sizeof (HE)); |
451 | HE **hes = _alloca (count * sizeof (HE)); |
397 | #else |
452 | #else |
398 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
453 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
399 | #endif |
454 | #endif |
… | |
… | |
426 | |
481 | |
427 | FREETMPS; |
482 | FREETMPS; |
428 | LEAVE; |
483 | LEAVE; |
429 | } |
484 | } |
430 | |
485 | |
431 | for (i = 0; i < count; ++i) |
486 | encode_nl (enc); ++enc->indent; |
|
|
487 | |
|
|
488 | while (count--) |
432 | { |
489 | { |
433 | encode_indent (enc); |
490 | encode_indent (enc); |
|
|
491 | he = hes [count]; |
434 | encode_he (enc, hes [i]); |
492 | encode_hk (enc, he); |
|
|
493 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
435 | |
494 | |
436 | if (i < count - 1) |
495 | if (count) |
437 | encode_comma (enc); |
496 | encode_comma (enc); |
438 | } |
497 | } |
439 | |
498 | |
|
|
499 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
500 | } |
|
|
501 | } |
|
|
502 | else |
|
|
503 | { |
|
|
504 | if (hv_iterinit (hv) || SvMAGICAL (hv)) |
|
|
505 | if ((he = hv_iternext (hv))) |
|
|
506 | { |
|
|
507 | encode_nl (enc); ++enc->indent; |
|
|
508 | |
|
|
509 | for (;;) |
|
|
510 | { |
440 | encode_nl (enc); |
511 | encode_indent (enc); |
|
|
512 | encode_hk (enc, he); |
|
|
513 | encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he)); |
|
|
514 | |
|
|
515 | if (!(he = hv_iternext (hv))) |
|
|
516 | break; |
|
|
517 | |
|
|
518 | encode_comma (enc); |
|
|
519 | } |
|
|
520 | |
|
|
521 | encode_nl (enc); --enc->indent; encode_indent (enc); |
|
|
522 | } |
|
|
523 | } |
|
|
524 | |
|
|
525 | encode_ch (enc, '}'); |
|
|
526 | } |
|
|
527 | |
|
|
528 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
529 | static void |
|
|
530 | encode_rv (enc_t *enc, SV *sv) |
|
|
531 | { |
|
|
532 | svtype svt; |
|
|
533 | |
|
|
534 | SvGETMAGIC (sv); |
|
|
535 | svt = SvTYPE (sv); |
|
|
536 | |
|
|
537 | if (expect_false (SvOBJECT (sv))) |
|
|
538 | { |
|
|
539 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
540 | ? json_boolean_stash |
|
|
541 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
542 | |
|
|
543 | if (SvSTASH (sv) == stash) |
|
|
544 | { |
|
|
545 | if (SvIV (sv)) |
|
|
546 | encode_str (enc, "true", 4, 0); |
|
|
547 | else |
|
|
548 | encode_str (enc, "false", 5, 0); |
441 | } |
549 | } |
442 | else |
550 | else |
443 | { |
551 | { |
444 | HE *he = hv_iternext (hv); |
552 | #if 0 |
445 | |
553 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
446 | for (;;) |
|
|
447 | { |
554 | { |
448 | encode_indent (enc); |
555 | // not yet |
449 | encode_he (enc, he); |
|
|
450 | |
|
|
451 | if (!(he = hv_iternext (hv))) |
|
|
452 | break; |
|
|
453 | |
|
|
454 | encode_comma (enc); |
|
|
455 | } |
556 | } |
|
|
557 | #endif |
|
|
558 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
559 | { |
|
|
560 | // we re-bless the reference to get overload and other niceties right |
|
|
561 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
456 | |
562 | |
|
|
563 | if (to_json) |
|
|
564 | { |
|
|
565 | dSP; |
|
|
566 | |
|
|
567 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
568 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
569 | |
|
|
570 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
571 | PUTBACK; |
|
|
572 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
573 | SPAGAIN; |
|
|
574 | |
|
|
575 | // catch this surprisingly common error |
|
|
576 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
577 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
578 | |
|
|
579 | sv = POPs; |
|
|
580 | PUTBACK; |
|
|
581 | |
457 | encode_nl (enc); |
582 | encode_sv (enc, sv); |
|
|
583 | |
|
|
584 | FREETMPS; LEAVE; |
|
|
585 | } |
|
|
586 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
587 | encode_str (enc, "null", 4, 0); |
|
|
588 | else |
|
|
589 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
590 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
591 | } |
|
|
592 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
593 | encode_str (enc, "null", 4, 0); |
|
|
594 | else |
|
|
595 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
596 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
458 | } |
597 | } |
459 | } |
598 | } |
460 | |
|
|
461 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
462 | } |
|
|
463 | |
|
|
464 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
465 | static void |
|
|
466 | encode_rv (enc_t *enc, SV *sv) |
|
|
467 | { |
|
|
468 | svtype svt; |
|
|
469 | |
|
|
470 | SvGETMAGIC (sv); |
|
|
471 | svt = SvTYPE (sv); |
|
|
472 | |
|
|
473 | if (svt == SVt_PVHV) |
599 | else if (svt == SVt_PVHV) |
474 | encode_hv (enc, (HV *)sv); |
600 | encode_hv (enc, (HV *)sv); |
475 | else if (svt == SVt_PVAV) |
601 | else if (svt == SVt_PVAV) |
476 | encode_av (enc, (AV *)sv); |
602 | encode_av (enc, (AV *)sv); |
477 | else if (svt < SVt_PVAV) |
603 | else if (svt < SVt_PVAV) |
478 | { |
604 | { |
479 | if (SvNIOK (sv) && SvIV (sv) == 0) |
605 | STRLEN len = 0; |
|
|
606 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
607 | |
|
|
608 | if (len == 1 && *pv == '1') |
|
|
609 | encode_str (enc, "true", 4, 0); |
|
|
610 | else if (len == 1 && *pv == '0') |
480 | encode_str (enc, "false", 5, 0); |
611 | encode_str (enc, "false", 5, 0); |
481 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
482 | encode_str (enc, "true", 4, 0); |
|
|
483 | else |
612 | else |
484 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
613 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
485 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
614 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
486 | } |
615 | } |
487 | else |
616 | else |
… | |
… | |
557 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
686 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
558 | SvPV_nolen (sv), SvFLAGS (sv)); |
687 | SvPV_nolen (sv), SvFLAGS (sv)); |
559 | } |
688 | } |
560 | |
689 | |
561 | static SV * |
690 | static SV * |
562 | encode_json (SV *scalar, U32 flags) |
691 | encode_json (SV *scalar, JSON *json) |
563 | { |
692 | { |
564 | enc_t enc; |
693 | enc_t enc; |
565 | |
694 | |
566 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
695 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
567 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
696 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
568 | |
697 | |
569 | enc.flags = flags; |
698 | enc.json = *json; |
570 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
699 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
571 | enc.cur = SvPVX (enc.sv); |
700 | enc.cur = SvPVX (enc.sv); |
572 | enc.end = SvEND (enc.sv); |
701 | enc.end = SvEND (enc.sv); |
573 | enc.indent = 0; |
702 | enc.indent = 0; |
574 | enc.maxdepth = DEC_DEPTH (flags); |
703 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
|
|
704 | enc.limit = enc.json.flags & F_ASCII ? 0x000080UL |
|
|
705 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
|
|
706 | : 0x10FFFFUL; |
575 | |
707 | |
576 | SvPOK_only (enc.sv); |
708 | SvPOK_only (enc.sv); |
577 | encode_sv (&enc, scalar); |
709 | encode_sv (&enc, scalar); |
578 | |
710 | |
579 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
711 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
580 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
712 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
581 | |
713 | |
582 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
714 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
583 | SvUTF8_on (enc.sv); |
715 | SvUTF8_on (enc.sv); |
584 | |
716 | |
585 | if (enc.flags & F_SHRINK) |
717 | if (enc.json.flags & F_SHRINK) |
586 | shrink (enc.sv); |
718 | shrink (enc.sv); |
587 | |
719 | |
588 | return enc.sv; |
720 | return enc.sv; |
589 | } |
721 | } |
590 | |
722 | |
… | |
… | |
595 | typedef struct |
727 | typedef struct |
596 | { |
728 | { |
597 | char *cur; // current parser pointer |
729 | char *cur; // current parser pointer |
598 | char *end; // end of input string |
730 | char *end; // end of input string |
599 | const char *err; // parse error, if != 0 |
731 | const char *err; // parse error, if != 0 |
600 | U32 flags; // F_* |
732 | JSON json; |
601 | U32 depth; // recursion depth |
733 | U32 depth; // recursion depth |
602 | U32 maxdepth; // recursion depth limit |
734 | U32 maxdepth; // recursion depth limit |
603 | } dec_t; |
735 | } dec_t; |
604 | |
736 | |
605 | inline void |
737 | INLINE void |
|
|
738 | decode_comment (dec_t *dec) |
|
|
739 | { |
|
|
740 | // only '#'-style comments allowed a.t.m. |
|
|
741 | |
|
|
742 | while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d) |
|
|
743 | ++dec->cur; |
|
|
744 | } |
|
|
745 | |
|
|
746 | INLINE void |
606 | decode_ws (dec_t *dec) |
747 | decode_ws (dec_t *dec) |
607 | { |
748 | { |
608 | for (;;) |
749 | for (;;) |
609 | { |
750 | { |
610 | char ch = *dec->cur; |
751 | char ch = *dec->cur; |
611 | |
752 | |
612 | if (ch > 0x20 |
753 | if (ch > 0x20) |
|
|
754 | { |
|
|
755 | if (expect_false (ch == '#')) |
|
|
756 | { |
|
|
757 | if (dec->json.flags & F_RELAXED) |
|
|
758 | decode_comment (dec); |
|
|
759 | else |
|
|
760 | break; |
|
|
761 | } |
|
|
762 | else |
|
|
763 | break; |
|
|
764 | } |
613 | || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) |
765 | else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09) |
614 | break; |
766 | break; // parse error, but let higher level handle it, gives better error messages |
615 | |
767 | |
616 | ++dec->cur; |
768 | ++dec->cur; |
617 | } |
769 | } |
618 | } |
770 | } |
619 | |
771 | |
… | |
… | |
725 | |
877 | |
726 | if (hi >= 0x80) |
878 | if (hi >= 0x80) |
727 | { |
879 | { |
728 | utf8 = 1; |
880 | utf8 = 1; |
729 | |
881 | |
730 | cur = (char *)uvuni_to_utf8_flags (cur, hi, 0); |
882 | cur = encode_utf8 (cur, hi); |
731 | } |
883 | } |
732 | else |
884 | else |
733 | *cur++ = hi; |
885 | *cur++ = hi; |
734 | } |
886 | } |
735 | break; |
887 | break; |
… | |
… | |
868 | is_nv = 1; |
1020 | is_nv = 1; |
869 | } |
1021 | } |
870 | |
1022 | |
871 | if (!is_nv) |
1023 | if (!is_nv) |
872 | { |
1024 | { |
|
|
1025 | int len = dec->cur - start; |
|
|
1026 | |
873 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1027 | // special case the rather common 1..5-digit-int case |
874 | if (*start == '-') |
1028 | if (*start == '-') |
875 | switch (dec->cur - start) |
1029 | switch (len) |
876 | { |
1030 | { |
877 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1031 | case 2: return newSViv (-( start [1] - '0' * 1)); |
878 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1032 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
879 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1033 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
880 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1034 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1035 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
881 | } |
1036 | } |
882 | else |
1037 | else |
883 | switch (dec->cur - start) |
1038 | switch (len) |
884 | { |
1039 | { |
885 | case 1: return newSViv ( start [0] - '0' * 1); |
1040 | case 1: return newSViv ( start [0] - '0' * 1); |
886 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1041 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
887 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1042 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
888 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1043 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1044 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
889 | } |
1045 | } |
890 | |
1046 | |
891 | { |
1047 | { |
892 | UV uv; |
1048 | UV uv; |
893 | int numtype = grok_number (start, dec->cur - start, &uv); |
1049 | int numtype = grok_number (start, len, &uv); |
894 | if (numtype & IS_NUMBER_IN_UV) |
1050 | if (numtype & IS_NUMBER_IN_UV) |
895 | if (numtype & IS_NUMBER_NEG) |
1051 | if (numtype & IS_NUMBER_NEG) |
896 | { |
1052 | { |
897 | if (uv < (UV)IV_MIN) |
1053 | if (uv < (UV)IV_MIN) |
898 | return newSViv (-(IV)uv); |
1054 | return newSViv (-(IV)uv); |
899 | } |
1055 | } |
900 | else |
1056 | else |
901 | return newSVuv (uv); |
1057 | return newSVuv (uv); |
902 | |
|
|
903 | // here would likely be the place for bigint support |
|
|
904 | } |
1058 | } |
905 | } |
|
|
906 | |
1059 | |
907 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
1060 | len -= *start == '-' ? 1 : 0; |
|
|
1061 | |
|
|
1062 | // does not fit into IV or UV, try NV |
|
|
1063 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
1064 | #if defined (LDBL_DIG) |
|
|
1065 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1066 | #endif |
|
|
1067 | ) |
|
|
1068 | // fits into NV without loss of precision |
|
|
1069 | return newSVnv (Atof (start)); |
|
|
1070 | |
|
|
1071 | // everything else fails, convert it to a string |
|
|
1072 | return newSVpvn (start, dec->cur - start); |
|
|
1073 | } |
|
|
1074 | |
|
|
1075 | // loss of precision here |
908 | return newSVnv (Atof (start)); |
1076 | return newSVnv (Atof (start)); |
909 | |
1077 | |
910 | fail: |
1078 | fail: |
911 | return 0; |
1079 | return 0; |
912 | } |
1080 | } |
… | |
… | |
942 | |
1110 | |
943 | if (*dec->cur != ',') |
1111 | if (*dec->cur != ',') |
944 | ERR (", or ] expected while parsing array"); |
1112 | ERR (", or ] expected while parsing array"); |
945 | |
1113 | |
946 | ++dec->cur; |
1114 | ++dec->cur; |
|
|
1115 | |
|
|
1116 | decode_ws (dec); |
|
|
1117 | |
|
|
1118 | if (*dec->cur == ']' && dec->json.flags & F_RELAXED) |
|
|
1119 | { |
|
|
1120 | ++dec->cur; |
|
|
1121 | break; |
|
|
1122 | } |
947 | } |
1123 | } |
948 | |
1124 | |
949 | DEC_DEC_DEPTH; |
1125 | DEC_DEC_DEPTH; |
950 | return newRV_noinc ((SV *)av); |
1126 | return newRV_noinc ((SV *)av); |
951 | |
1127 | |
… | |
… | |
956 | } |
1132 | } |
957 | |
1133 | |
958 | static SV * |
1134 | static SV * |
959 | decode_hv (dec_t *dec) |
1135 | decode_hv (dec_t *dec) |
960 | { |
1136 | { |
|
|
1137 | SV *sv; |
961 | HV *hv = newHV (); |
1138 | HV *hv = newHV (); |
962 | |
1139 | |
963 | DEC_INC_DEPTH; |
1140 | DEC_INC_DEPTH; |
964 | decode_ws (dec); |
1141 | decode_ws (dec); |
965 | |
1142 | |
966 | if (*dec->cur == '}') |
1143 | if (*dec->cur == '}') |
967 | ++dec->cur; |
1144 | ++dec->cur; |
968 | else |
1145 | else |
969 | for (;;) |
1146 | for (;;) |
970 | { |
1147 | { |
|
|
1148 | EXPECT_CH ('"'); |
|
|
1149 | |
|
|
1150 | // heuristic: assume that |
|
|
1151 | // a) decode_str + hv_store_ent are abysmally slow. |
|
|
1152 | // b) most hash keys are short, simple ascii text. |
|
|
1153 | // => try to "fast-match" such strings to avoid |
|
|
1154 | // the overhead of decode_str + hv_store_ent. |
|
|
1155 | { |
971 | SV *key, *value; |
1156 | SV *value; |
|
|
1157 | char *p = dec->cur; |
|
|
1158 | char *e = p + 24; // only try up to 24 bytes |
972 | |
1159 | |
973 | decode_ws (dec); EXPECT_CH ('"'); |
1160 | for (;;) |
974 | |
|
|
975 | key = decode_str (dec); |
|
|
976 | if (!key) |
|
|
977 | goto fail; |
|
|
978 | |
|
|
979 | decode_ws (dec); EXPECT_CH (':'); |
|
|
980 | |
|
|
981 | value = decode_sv (dec); |
|
|
982 | if (!value) |
|
|
983 | { |
1161 | { |
|
|
1162 | // the >= 0x80 is true on most architectures |
|
|
1163 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1164 | { |
|
|
1165 | // slow path, back up and use decode_str |
|
|
1166 | SV *key = decode_str (dec); |
|
|
1167 | if (!key) |
|
|
1168 | goto fail; |
|
|
1169 | |
|
|
1170 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1171 | |
|
|
1172 | decode_ws (dec); |
|
|
1173 | value = decode_sv (dec); |
|
|
1174 | if (!value) |
|
|
1175 | { |
|
|
1176 | SvREFCNT_dec (key); |
|
|
1177 | goto fail; |
|
|
1178 | } |
|
|
1179 | |
|
|
1180 | hv_store_ent (hv, key, value, 0); |
984 | SvREFCNT_dec (key); |
1181 | SvREFCNT_dec (key); |
|
|
1182 | |
|
|
1183 | break; |
|
|
1184 | } |
|
|
1185 | else if (*p == '"') |
|
|
1186 | { |
|
|
1187 | // fast path, got a simple key |
|
|
1188 | char *key = dec->cur; |
|
|
1189 | int len = p - key; |
|
|
1190 | dec->cur = p + 1; |
|
|
1191 | |
|
|
1192 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1193 | |
|
|
1194 | decode_ws (dec); |
|
|
1195 | value = decode_sv (dec); |
|
|
1196 | if (!value) |
985 | goto fail; |
1197 | goto fail; |
|
|
1198 | |
|
|
1199 | hv_store (hv, key, len, value, 0); |
|
|
1200 | |
|
|
1201 | break; |
|
|
1202 | } |
|
|
1203 | |
|
|
1204 | ++p; |
986 | } |
1205 | } |
987 | |
1206 | } |
988 | hv_store_ent (hv, key, value, 0); |
|
|
989 | SvREFCNT_dec (key); |
|
|
990 | |
1207 | |
991 | decode_ws (dec); |
1208 | decode_ws (dec); |
992 | |
1209 | |
993 | if (*dec->cur == '}') |
1210 | if (*dec->cur == '}') |
994 | { |
1211 | { |
… | |
… | |
998 | |
1215 | |
999 | if (*dec->cur != ',') |
1216 | if (*dec->cur != ',') |
1000 | ERR (", or } expected while parsing object/hash"); |
1217 | ERR (", or } expected while parsing object/hash"); |
1001 | |
1218 | |
1002 | ++dec->cur; |
1219 | ++dec->cur; |
|
|
1220 | |
|
|
1221 | decode_ws (dec); |
|
|
1222 | |
|
|
1223 | if (*dec->cur == '}' && dec->json.flags & F_RELAXED) |
|
|
1224 | { |
|
|
1225 | ++dec->cur; |
|
|
1226 | break; |
|
|
1227 | } |
1003 | } |
1228 | } |
1004 | |
1229 | |
1005 | DEC_DEC_DEPTH; |
1230 | DEC_DEC_DEPTH; |
1006 | return newRV_noinc ((SV *)hv); |
1231 | sv = newRV_noinc ((SV *)hv); |
|
|
1232 | |
|
|
1233 | // check filter callbacks |
|
|
1234 | if (dec->json.flags & F_HOOK) |
|
|
1235 | { |
|
|
1236 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1237 | { |
|
|
1238 | HE *cb, *he; |
|
|
1239 | |
|
|
1240 | hv_iterinit (hv); |
|
|
1241 | he = hv_iternext (hv); |
|
|
1242 | hv_iterinit (hv); |
|
|
1243 | |
|
|
1244 | // the next line creates a mortal sv each time its called. |
|
|
1245 | // might want to optimise this for common cases. |
|
|
1246 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1247 | |
|
|
1248 | if (cb) |
|
|
1249 | { |
|
|
1250 | dSP; |
|
|
1251 | int count; |
|
|
1252 | |
|
|
1253 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1254 | XPUSHs (HeVAL (he)); |
|
|
1255 | |
|
|
1256 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1257 | |
|
|
1258 | if (count == 1) |
|
|
1259 | { |
|
|
1260 | sv = newSVsv (POPs); |
|
|
1261 | FREETMPS; LEAVE; |
|
|
1262 | return sv; |
|
|
1263 | } |
|
|
1264 | |
|
|
1265 | FREETMPS; LEAVE; |
|
|
1266 | } |
|
|
1267 | } |
|
|
1268 | |
|
|
1269 | if (dec->json.cb_object) |
|
|
1270 | { |
|
|
1271 | dSP; |
|
|
1272 | int count; |
|
|
1273 | |
|
|
1274 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1275 | XPUSHs (sv_2mortal (sv)); |
|
|
1276 | |
|
|
1277 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1278 | |
|
|
1279 | if (count == 1) |
|
|
1280 | { |
|
|
1281 | sv = newSVsv (POPs); |
|
|
1282 | FREETMPS; LEAVE; |
|
|
1283 | return sv; |
|
|
1284 | } |
|
|
1285 | |
|
|
1286 | SvREFCNT_inc (sv); |
|
|
1287 | FREETMPS; LEAVE; |
|
|
1288 | } |
|
|
1289 | } |
|
|
1290 | |
|
|
1291 | return sv; |
1007 | |
1292 | |
1008 | fail: |
1293 | fail: |
1009 | SvREFCNT_dec (hv); |
1294 | SvREFCNT_dec (hv); |
1010 | DEC_DEC_DEPTH; |
1295 | DEC_DEC_DEPTH; |
1011 | return 0; |
1296 | return 0; |
1012 | } |
1297 | } |
1013 | |
1298 | |
1014 | static SV * |
1299 | static SV * |
1015 | decode_sv (dec_t *dec) |
1300 | decode_sv (dec_t *dec) |
1016 | { |
1301 | { |
1017 | decode_ws (dec); |
|
|
1018 | |
|
|
1019 | // the beauty of JSON: you need exactly one character lookahead |
1302 | // the beauty of JSON: you need exactly one character lookahead |
1020 | // to parse anything. |
1303 | // to parse anything. |
1021 | switch (*dec->cur) |
1304 | switch (*dec->cur) |
1022 | { |
1305 | { |
1023 | case '"': ++dec->cur; return decode_str (dec); |
1306 | case '"': ++dec->cur; return decode_str (dec); |
… | |
… | |
1031 | |
1314 | |
1032 | case 't': |
1315 | case 't': |
1033 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1316 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1034 | { |
1317 | { |
1035 | dec->cur += 4; |
1318 | dec->cur += 4; |
1036 | return newSViv (1); |
1319 | #if JSON_SLOW |
|
|
1320 | json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true); |
|
|
1321 | #endif |
|
|
1322 | return SvREFCNT_inc (json_true); |
1037 | } |
1323 | } |
1038 | else |
1324 | else |
1039 | ERR ("'true' expected"); |
1325 | ERR ("'true' expected"); |
1040 | |
1326 | |
1041 | break; |
1327 | break; |
1042 | |
1328 | |
1043 | case 'f': |
1329 | case 'f': |
1044 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1330 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1045 | { |
1331 | { |
1046 | dec->cur += 5; |
1332 | dec->cur += 5; |
1047 | return newSViv (0); |
1333 | #if JSON_SLOW |
|
|
1334 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
|
|
1335 | #endif |
|
|
1336 | return SvREFCNT_inc (json_false); |
1048 | } |
1337 | } |
1049 | else |
1338 | else |
1050 | ERR ("'false' expected"); |
1339 | ERR ("'false' expected"); |
1051 | |
1340 | |
1052 | break; |
1341 | break; |
… | |
… | |
1070 | fail: |
1359 | fail: |
1071 | return 0; |
1360 | return 0; |
1072 | } |
1361 | } |
1073 | |
1362 | |
1074 | static SV * |
1363 | static SV * |
1075 | decode_json (SV *string, U32 flags, UV *offset_return) |
1364 | decode_json (SV *string, JSON *json, UV *offset_return) |
1076 | { |
1365 | { |
1077 | dec_t dec; |
1366 | dec_t dec; |
1078 | UV offset; |
1367 | UV offset; |
1079 | SV *sv; |
1368 | SV *sv; |
1080 | |
1369 | |
1081 | SvGETMAGIC (string); |
1370 | SvGETMAGIC (string); |
1082 | SvUPGRADE (string, SVt_PV); |
1371 | SvUPGRADE (string, SVt_PV); |
1083 | |
1372 | |
|
|
1373 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1374 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1375 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1376 | |
1084 | if (flags & F_UTF8) |
1377 | if (json->flags & F_UTF8) |
1085 | sv_utf8_downgrade (string, 0); |
1378 | sv_utf8_downgrade (string, 0); |
1086 | else |
1379 | else |
1087 | sv_utf8_upgrade (string); |
1380 | sv_utf8_upgrade (string); |
1088 | |
1381 | |
1089 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1382 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1090 | |
1383 | |
1091 | dec.flags = flags; |
1384 | dec.json = *json; |
1092 | dec.cur = SvPVX (string); |
1385 | dec.cur = SvPVX (string); |
1093 | dec.end = SvEND (string); |
1386 | dec.end = SvEND (string); |
1094 | dec.err = 0; |
1387 | dec.err = 0; |
1095 | dec.depth = 0; |
1388 | dec.depth = 0; |
1096 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1389 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1390 | |
|
|
1391 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1392 | dec.json.flags |= F_HOOK; |
1097 | |
1393 | |
1098 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1394 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1395 | |
|
|
1396 | decode_ws (&dec); |
1099 | sv = decode_sv (&dec); |
1397 | sv = decode_sv (&dec); |
1100 | |
1398 | |
1101 | if (!(offset_return || !sv)) |
1399 | if (!(offset_return || !sv)) |
1102 | { |
1400 | { |
1103 | // check for trailing garbage |
1401 | // check for trailing garbage |
… | |
… | |
1111 | } |
1409 | } |
1112 | } |
1410 | } |
1113 | |
1411 | |
1114 | if (offset_return || !sv) |
1412 | if (offset_return || !sv) |
1115 | { |
1413 | { |
1116 | offset = dec.flags & F_UTF8 |
1414 | offset = dec.json.flags & F_UTF8 |
1117 | ? dec.cur - SvPVX (string) |
1415 | ? dec.cur - SvPVX (string) |
1118 | : utf8_distance (dec.cur, SvPVX (string)); |
1416 | : utf8_distance (dec.cur, SvPVX (string)); |
1119 | |
1417 | |
1120 | if (offset_return) |
1418 | if (offset_return) |
1121 | *offset_return = offset; |
1419 | *offset_return = offset; |
… | |
… | |
1140 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1438 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1141 | } |
1439 | } |
1142 | |
1440 | |
1143 | sv = sv_2mortal (sv); |
1441 | sv = sv_2mortal (sv); |
1144 | |
1442 | |
1145 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1443 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1146 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1444 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1147 | |
1445 | |
1148 | return sv; |
1446 | return sv; |
1149 | } |
1447 | } |
1150 | |
1448 | |
… | |
… | |
1162 | i >= '0' && i <= '9' ? i - '0' |
1460 | i >= '0' && i <= '9' ? i - '0' |
1163 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1461 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1164 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1462 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1165 | : -1; |
1463 | : -1; |
1166 | |
1464 | |
1167 | json_stash = gv_stashpv ("JSON::XS", 1); |
1465 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1466 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1467 | |
|
|
1468 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1469 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1168 | } |
1470 | } |
1169 | |
1471 | |
1170 | PROTOTYPES: DISABLE |
1472 | PROTOTYPES: DISABLE |
1171 | |
1473 | |
1172 | SV *new (char *dummy) |
1474 | void CLONE (...) |
1173 | CODE: |
1475 | CODE: |
1174 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1476 | json_stash = 0; |
|
|
1477 | json_boolean_stash = 0; |
|
|
1478 | |
|
|
1479 | void new (char *klass) |
|
|
1480 | PPCODE: |
|
|
1481 | { |
|
|
1482 | SV *pv = NEWSV (0, sizeof (JSON)); |
|
|
1483 | SvPOK_only (pv); |
|
|
1484 | Zero (SvPVX (pv), 1, JSON); |
|
|
1485 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1486 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1487 | newRV_noinc (pv), |
|
|
1488 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1489 | ))); |
|
|
1490 | } |
|
|
1491 | |
|
|
1492 | void ascii (JSON *self, int enable = 1) |
|
|
1493 | ALIAS: |
|
|
1494 | ascii = F_ASCII |
|
|
1495 | latin1 = F_LATIN1 |
|
|
1496 | utf8 = F_UTF8 |
|
|
1497 | indent = F_INDENT |
|
|
1498 | canonical = F_CANONICAL |
|
|
1499 | space_before = F_SPACE_BEFORE |
|
|
1500 | space_after = F_SPACE_AFTER |
|
|
1501 | pretty = F_PRETTY |
|
|
1502 | allow_nonref = F_ALLOW_NONREF |
|
|
1503 | shrink = F_SHRINK |
|
|
1504 | allow_blessed = F_ALLOW_BLESSED |
|
|
1505 | convert_blessed = F_CONV_BLESSED |
|
|
1506 | relaxed = F_RELAXED |
|
|
1507 | PPCODE: |
|
|
1508 | { |
|
|
1509 | if (enable) |
|
|
1510 | self->flags |= ix; |
|
|
1511 | else |
|
|
1512 | self->flags &= ~ix; |
|
|
1513 | |
|
|
1514 | XPUSHs (ST (0)); |
|
|
1515 | } |
|
|
1516 | |
|
|
1517 | void get_ascii (JSON *self) |
|
|
1518 | ALIAS: |
|
|
1519 | get_ascii = F_ASCII |
|
|
1520 | get_latin1 = F_LATIN1 |
|
|
1521 | get_utf8 = F_UTF8 |
|
|
1522 | get_indent = F_INDENT |
|
|
1523 | get_canonical = F_CANONICAL |
|
|
1524 | get_space_before = F_SPACE_BEFORE |
|
|
1525 | get_space_after = F_SPACE_AFTER |
|
|
1526 | get_allow_nonref = F_ALLOW_NONREF |
|
|
1527 | get_shrink = F_SHRINK |
|
|
1528 | get_allow_blessed = F_ALLOW_BLESSED |
|
|
1529 | get_convert_blessed = F_CONV_BLESSED |
|
|
1530 | get_relaxed = F_RELAXED |
|
|
1531 | PPCODE: |
|
|
1532 | XPUSHs (boolSV (self->flags & ix)); |
|
|
1533 | |
|
|
1534 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
|
|
1535 | PPCODE: |
|
|
1536 | { |
|
|
1537 | UV log2 = 0; |
|
|
1538 | |
|
|
1539 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1540 | |
|
|
1541 | while ((1UL << log2) < max_depth) |
|
|
1542 | ++log2; |
|
|
1543 | |
|
|
1544 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1545 | |
|
|
1546 | XPUSHs (ST (0)); |
|
|
1547 | } |
|
|
1548 | |
|
|
1549 | U32 get_max_depth (JSON *self) |
|
|
1550 | CODE: |
|
|
1551 | RETVAL = DEC_DEPTH (self->flags); |
1175 | OUTPUT: |
1552 | OUTPUT: |
1176 | RETVAL |
1553 | RETVAL |
1177 | |
1554 | |
1178 | SV *ascii (SV *self, int enable = 1) |
1555 | void max_size (JSON *self, UV max_size = 0) |
1179 | ALIAS: |
1556 | PPCODE: |
1180 | ascii = F_ASCII |
1557 | { |
1181 | latin1 = F_LATIN1 |
1558 | UV log2 = 0; |
1182 | utf8 = F_UTF8 |
1559 | |
1183 | indent = F_INDENT |
1560 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1184 | canonical = F_CANONICAL |
1561 | if (max_size == 1) max_size = 2; |
1185 | space_before = F_SPACE_BEFORE |
1562 | |
1186 | space_after = F_SPACE_AFTER |
1563 | while ((1UL << log2) < max_size) |
1187 | pretty = F_PRETTY |
1564 | ++log2; |
1188 | allow_nonref = F_ALLOW_NONREF |
1565 | |
1189 | shrink = F_SHRINK |
1566 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1567 | |
|
|
1568 | XPUSHs (ST (0)); |
|
|
1569 | } |
|
|
1570 | |
|
|
1571 | int get_max_size (JSON *self) |
1190 | CODE: |
1572 | CODE: |
1191 | { |
1573 | RETVAL = DEC_SIZE (self->flags); |
1192 | UV *uv = SvJSON (self); |
|
|
1193 | if (enable) |
|
|
1194 | *uv |= ix; |
|
|
1195 | else |
|
|
1196 | *uv &= ~ix; |
|
|
1197 | |
|
|
1198 | RETVAL = newSVsv (self); |
|
|
1199 | } |
|
|
1200 | OUTPUT: |
1574 | OUTPUT: |
1201 | RETVAL |
1575 | RETVAL |
1202 | |
1576 | |
1203 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1577 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1204 | CODE: |
|
|
1205 | { |
|
|
1206 | UV *uv = SvJSON (self); |
|
|
1207 | UV log2 = 0; |
|
|
1208 | |
|
|
1209 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1210 | |
|
|
1211 | while ((1UL << log2) < max_depth) |
|
|
1212 | ++log2; |
|
|
1213 | |
|
|
1214 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1215 | |
|
|
1216 | RETVAL = newSVsv (self); |
|
|
1217 | } |
|
|
1218 | OUTPUT: |
|
|
1219 | RETVAL |
|
|
1220 | |
|
|
1221 | void encode (SV *self, SV *scalar) |
|
|
1222 | PPCODE: |
1578 | PPCODE: |
1223 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1579 | { |
|
|
1580 | SvREFCNT_dec (self->cb_object); |
|
|
1581 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1224 | |
1582 | |
1225 | void decode (SV *self, SV *jsonstr) |
1583 | XPUSHs (ST (0)); |
|
|
1584 | } |
|
|
1585 | |
|
|
1586 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1226 | PPCODE: |
1587 | PPCODE: |
|
|
1588 | { |
|
|
1589 | if (!self->cb_sk_object) |
|
|
1590 | self->cb_sk_object = newHV (); |
|
|
1591 | |
|
|
1592 | if (SvOK (cb)) |
|
|
1593 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1594 | else |
|
|
1595 | { |
|
|
1596 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1597 | |
|
|
1598 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1599 | { |
|
|
1600 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1601 | self->cb_sk_object = 0; |
|
|
1602 | } |
|
|
1603 | } |
|
|
1604 | |
|
|
1605 | XPUSHs (ST (0)); |
|
|
1606 | } |
|
|
1607 | |
|
|
1608 | void encode (JSON *self, SV *scalar) |
|
|
1609 | PPCODE: |
|
|
1610 | XPUSHs (encode_json (scalar, self)); |
|
|
1611 | |
|
|
1612 | void decode (JSON *self, SV *jsonstr) |
|
|
1613 | PPCODE: |
1227 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1614 | XPUSHs (decode_json (jsonstr, self, 0)); |
1228 | |
1615 | |
1229 | void decode_prefix (SV *self, SV *jsonstr) |
1616 | void decode_prefix (JSON *self, SV *jsonstr) |
1230 | PPCODE: |
1617 | PPCODE: |
1231 | { |
1618 | { |
1232 | UV offset; |
1619 | UV offset; |
1233 | EXTEND (SP, 2); |
1620 | EXTEND (SP, 2); |
1234 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1621 | PUSHs (decode_json (jsonstr, self, &offset)); |
1235 | PUSHs (sv_2mortal (newSVuv (offset))); |
1622 | PUSHs (sv_2mortal (newSVuv (offset))); |
1236 | } |
1623 | } |
1237 | |
1624 | |
|
|
1625 | void DESTROY (JSON *self) |
|
|
1626 | CODE: |
|
|
1627 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1628 | SvREFCNT_dec (self->cb_object); |
|
|
1629 | |
1238 | PROTOTYPES: ENABLE |
1630 | PROTOTYPES: ENABLE |
1239 | |
1631 | |
1240 | void to_json (SV *scalar) |
1632 | void encode_json (SV *scalar) |
1241 | ALIAS: |
|
|
1242 | objToJson = 0 |
|
|
1243 | PPCODE: |
1633 | PPCODE: |
|
|
1634 | { |
|
|
1635 | JSON json = { F_DEFAULT | F_UTF8 }; |
1244 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1636 | XPUSHs (encode_json (scalar, &json)); |
|
|
1637 | } |
1245 | |
1638 | |
1246 | void from_json (SV *jsonstr) |
1639 | void decode_json (SV *jsonstr) |
1247 | ALIAS: |
|
|
1248 | jsonToObj = 0 |
|
|
1249 | PPCODE: |
1640 | PPCODE: |
|
|
1641 | { |
|
|
1642 | JSON json = { F_DEFAULT | F_UTF8 }; |
1250 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1643 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1644 | } |
1251 | |
1645 | |