… | |
… | |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | #endif |
14 | #endif |
15 | |
15 | |
16 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
20 | #endif |
20 | #endif |
21 | |
21 | |
|
|
22 | // compatibility with perl <5.18 |
|
|
23 | #ifndef HvNAMELEN_get |
|
|
24 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
25 | #endif |
|
|
26 | #ifndef HvNAMELEN |
|
|
27 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
28 | #endif |
|
|
29 | #ifndef HvNAMEUTF8 |
|
|
30 | # define HvNAMEUTF8(hv) 0 |
|
|
31 | #endif |
|
|
32 | |
|
|
33 | // three extra for rounding, sign, and end of string |
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
34 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
23 | |
35 | |
24 | #define F_ASCII 0x00000001UL |
36 | #define F_ASCII 0x00000001UL |
25 | #define F_LATIN1 0x00000002UL |
37 | #define F_LATIN1 0x00000002UL |
26 | #define F_UTF8 0x00000004UL |
38 | #define F_UTF8 0x00000004UL |
27 | #define F_INDENT 0x00000008UL |
39 | #define F_INDENT 0x00000008UL |
… | |
… | |
32 | #define F_SHRINK 0x00000200UL |
44 | #define F_SHRINK 0x00000200UL |
33 | #define F_ALLOW_BLESSED 0x00000400UL |
45 | #define F_ALLOW_BLESSED 0x00000400UL |
34 | #define F_CONV_BLESSED 0x00000800UL |
46 | #define F_CONV_BLESSED 0x00000800UL |
35 | #define F_RELAXED 0x00001000UL |
47 | #define F_RELAXED 0x00001000UL |
36 | #define F_ALLOW_UNKNOWN 0x00002000UL |
48 | #define F_ALLOW_UNKNOWN 0x00002000UL |
|
|
49 | #define F_ALLOW_TAGS 0x00004000UL |
37 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
50 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
38 | |
51 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
52 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
40 | |
53 | |
41 | #define INIT_SIZE 32 // initial scalar size to be allocated |
54 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INDENT_STEP 3 // spaces per indentation level |
55 | #define INDENT_STEP 3 // spaces per indentation level |
43 | |
56 | |
44 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
57 | #define SHORT_STRING_LEN 16384 // special-case strings of up to this size |
|
|
58 | |
|
|
59 | #define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8) |
45 | |
60 | |
46 | #define SB do { |
61 | #define SB do { |
47 | #define SE } while (0) |
62 | #define SE } while (0) |
48 | |
63 | |
49 | #if __GNUC__ >= 3 |
64 | #if __GNUC__ >= 3 |
… | |
… | |
64 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
79 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
65 | |
80 | |
66 | #ifdef USE_ITHREADS |
81 | #ifdef USE_ITHREADS |
67 | # define JSON_SLOW 1 |
82 | # define JSON_SLOW 1 |
68 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
83 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
84 | # define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1)) |
69 | #else |
85 | #else |
70 | # define JSON_SLOW 0 |
86 | # define JSON_SLOW 0 |
71 | # define JSON_STASH json_stash |
87 | # define JSON_STASH json_stash |
|
|
88 | # define BOOL_STASH bool_stash |
72 | #endif |
89 | #endif |
73 | |
90 | |
74 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
91 | // the amount of HEs to allocate on the stack, when sorting keys |
75 | static SV *json_true, *json_false; |
92 | #define STACK_HES 64 |
|
|
93 | |
|
|
94 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
|
|
95 | static SV *bool_true, *bool_false, *sv_json; |
76 | |
96 | |
77 | enum { |
97 | enum { |
78 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
98 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_STR, // inside string |
99 | INCR_M_STR, // inside string |
80 | INCR_M_BS, // inside backslash |
100 | INCR_M_BS, // inside backslash |
|
|
101 | INCR_M_C0, // inside comment in initial whitespace sequence |
|
|
102 | INCR_M_C1, // inside comment in other places |
81 | INCR_M_JSON // outside anything, count nesting |
103 | INCR_M_JSON // outside anything, count nesting |
82 | }; |
104 | }; |
83 | |
105 | |
84 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
106 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
107 | |
86 | typedef struct { |
108 | typedef struct { |
87 | U32 flags; |
109 | U32 flags; |
88 | U32 max_depth; |
110 | U32 max_depth; |
89 | STRLEN max_size; |
111 | STRLEN max_size; |
… | |
… | |
92 | HV *cb_sk_object; |
114 | HV *cb_sk_object; |
93 | |
115 | |
94 | // for the incremental parser |
116 | // for the incremental parser |
95 | SV *incr_text; // the source text so far |
117 | SV *incr_text; // the source text so far |
96 | STRLEN incr_pos; // the current offset into the text |
118 | STRLEN incr_pos; // the current offset into the text |
97 | unsigned char incr_nest; // {[]}-nesting level |
119 | int incr_nest; // {[]}-nesting level |
98 | unsigned char incr_mode; |
120 | unsigned char incr_mode; |
99 | } JSON; |
121 | } JSON; |
100 | |
122 | |
101 | INLINE void |
123 | INLINE void |
102 | json_init (JSON *json) |
124 | json_init (JSON *json) |
… | |
… | |
175 | *s++ = 0x80 | ( ch & 0x3f); |
197 | *s++ = 0x80 | ( ch & 0x3f); |
176 | |
198 | |
177 | return s; |
199 | return s; |
178 | } |
200 | } |
179 | |
201 | |
|
|
202 | // convert offset pointer to character index, sv must be string |
|
|
203 | static STRLEN |
|
|
204 | ptr_to_index (SV *sv, char *offset) |
|
|
205 | { |
|
|
206 | return SvUTF8 (sv) |
|
|
207 | ? utf8_distance (offset, SvPVX (sv)) |
|
|
208 | : offset - SvPVX (sv); |
|
|
209 | } |
|
|
210 | |
|
|
211 | ///////////////////////////////////////////////////////////////////////////// |
|
|
212 | // fp hell |
|
|
213 | |
|
|
214 | // scan a group of digits, and a trailing exponent |
|
|
215 | static void |
|
|
216 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
|
|
217 | { |
|
|
218 | UV uaccum = 0; |
|
|
219 | int eaccum = 0; |
|
|
220 | |
|
|
221 | // if we recurse too deep, skip all remaining digits |
|
|
222 | // to avoid a stack overflow attack |
|
|
223 | if (expect_false (--maxdepth <= 0)) |
|
|
224 | while (((U8)*s - '0') < 10) |
|
|
225 | ++s; |
|
|
226 | |
|
|
227 | for (;;) |
|
|
228 | { |
|
|
229 | U8 dig = (U8)*s - '0'; |
|
|
230 | |
|
|
231 | if (expect_false (dig >= 10)) |
|
|
232 | { |
|
|
233 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
234 | { |
|
|
235 | ++s; |
|
|
236 | json_atof_scan1 (s, accum, expo, 1, maxdepth); |
|
|
237 | } |
|
|
238 | else if ((dig | ' ') == 'e' - '0') |
|
|
239 | { |
|
|
240 | int exp2 = 0; |
|
|
241 | int neg = 0; |
|
|
242 | |
|
|
243 | ++s; |
|
|
244 | |
|
|
245 | if (*s == '-') |
|
|
246 | { |
|
|
247 | ++s; |
|
|
248 | neg = 1; |
|
|
249 | } |
|
|
250 | else if (*s == '+') |
|
|
251 | ++s; |
|
|
252 | |
|
|
253 | while ((dig = (U8)*s - '0') < 10) |
|
|
254 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
255 | |
|
|
256 | *expo += neg ? -exp2 : exp2; |
|
|
257 | } |
|
|
258 | |
|
|
259 | break; |
|
|
260 | } |
|
|
261 | |
|
|
262 | ++s; |
|
|
263 | |
|
|
264 | uaccum = uaccum * 10 + dig; |
|
|
265 | ++eaccum; |
|
|
266 | |
|
|
267 | // if we have too many digits, then recurse for more |
|
|
268 | // we actually do this for rather few digits |
|
|
269 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
270 | { |
|
|
271 | if (postdp) *expo -= eaccum; |
|
|
272 | json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
|
|
273 | if (postdp) *expo += eaccum; |
|
|
274 | |
|
|
275 | break; |
|
|
276 | } |
|
|
277 | } |
|
|
278 | |
|
|
279 | // this relies greatly on the quality of the pow () |
|
|
280 | // implementation of the platform, but a good |
|
|
281 | // implementation is hard to beat. |
|
|
282 | // (IEEE 754 conformant ones are required to be exact) |
|
|
283 | if (postdp) *expo -= eaccum; |
|
|
284 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
285 | *expo += eaccum; |
|
|
286 | } |
|
|
287 | |
|
|
288 | static NV |
|
|
289 | json_atof (const char *s) |
|
|
290 | { |
|
|
291 | NV accum = 0.; |
|
|
292 | int expo = 0; |
|
|
293 | int neg = 0; |
|
|
294 | |
|
|
295 | if (*s == '-') |
|
|
296 | { |
|
|
297 | ++s; |
|
|
298 | neg = 1; |
|
|
299 | } |
|
|
300 | |
|
|
301 | // a recursion depth of ten gives us >>500 bits |
|
|
302 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
|
|
303 | |
|
|
304 | return neg ? -accum : accum; |
|
|
305 | } |
|
|
306 | |
|
|
307 | // target of scalar reference is bool? -1 == nope, 0 == false, 1 == true |
|
|
308 | static int |
|
|
309 | ref_bool_type (SV *sv) |
|
|
310 | { |
|
|
311 | svtype svt = SvTYPE (sv); |
|
|
312 | |
|
|
313 | if (svt < SVt_PVAV) |
|
|
314 | { |
|
|
315 | STRLEN len = 0; |
|
|
316 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
317 | |
|
|
318 | if (len == 1) |
|
|
319 | if (*pv == '1') |
|
|
320 | return 1; |
|
|
321 | else if (*pv == '0') |
|
|
322 | return 0; |
|
|
323 | |
|
|
324 | } |
|
|
325 | |
|
|
326 | return -1; |
|
|
327 | } |
|
|
328 | |
|
|
329 | // returns whether scalar is not a reference in the sense of allow_nonref |
|
|
330 | static int |
|
|
331 | json_nonref (SV *scalar) |
|
|
332 | { |
|
|
333 | if (!SvROK (scalar)) |
|
|
334 | return 1; |
|
|
335 | |
|
|
336 | scalar = SvRV (scalar); |
|
|
337 | |
|
|
338 | if (SvSTASH (scalar) == bool_stash) |
|
|
339 | return 1; |
|
|
340 | |
|
|
341 | if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
|
|
342 | return 1; |
|
|
343 | |
|
|
344 | return 0; |
|
|
345 | } |
|
|
346 | |
180 | ///////////////////////////////////////////////////////////////////////////// |
347 | ///////////////////////////////////////////////////////////////////////////// |
181 | // encoder |
348 | // encoder |
182 | |
349 | |
183 | // structure used for encoding JSON |
350 | // structure used for encoding JSON |
184 | typedef struct |
351 | typedef struct |
… | |
… | |
194 | INLINE void |
361 | INLINE void |
195 | need (enc_t *enc, STRLEN len) |
362 | need (enc_t *enc, STRLEN len) |
196 | { |
363 | { |
197 | if (expect_false (enc->cur + len >= enc->end)) |
364 | if (expect_false (enc->cur + len >= enc->end)) |
198 | { |
365 | { |
199 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
366 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
200 | SvGROW (enc->sv, cur + len + 1); |
367 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
201 | enc->cur = SvPVX (enc->sv) + cur; |
368 | enc->cur = SvPVX (enc->sv) + cur; |
202 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
369 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
203 | } |
370 | } |
204 | } |
371 | } |
205 | |
372 | |
… | |
… | |
280 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
447 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
281 | enc->cur += 12; |
448 | enc->cur += 12; |
282 | } |
449 | } |
283 | else |
450 | else |
284 | { |
451 | { |
285 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
286 | need (enc, len += 5); |
452 | need (enc, len += 5); |
287 | *enc->cur++ = '\\'; |
453 | *enc->cur++ = '\\'; |
288 | *enc->cur++ = 'u'; |
454 | *enc->cur++ = 'u'; |
289 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
455 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
290 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
456 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
291 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
457 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
292 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
458 | *enc->cur++ = PL_hexdigit [(uch >> 0) & 15]; |
293 | } |
459 | } |
294 | |
460 | |
295 | str += clen; |
461 | str += clen; |
296 | } |
462 | } |
297 | else if (enc->json.flags & F_LATIN1) |
463 | else if (enc->json.flags & F_LATIN1) |
… | |
… | |
372 | |
538 | |
373 | if (enc->indent >= enc->json.max_depth) |
539 | if (enc->indent >= enc->json.max_depth) |
374 | croak (ERR_NESTING_EXCEEDED); |
540 | croak (ERR_NESTING_EXCEEDED); |
375 | |
541 | |
376 | encode_ch (enc, '['); |
542 | encode_ch (enc, '['); |
377 | |
543 | |
378 | if (len >= 0) |
544 | if (len >= 0) |
379 | { |
545 | { |
380 | encode_nl (enc); ++enc->indent; |
546 | encode_nl (enc); ++enc->indent; |
381 | |
547 | |
382 | for (i = 0; i <= len; ++i) |
548 | for (i = 0; i <= len; ++i) |
… | |
… | |
394 | encode_comma (enc); |
560 | encode_comma (enc); |
395 | } |
561 | } |
396 | |
562 | |
397 | encode_nl (enc); --enc->indent; encode_indent (enc); |
563 | encode_nl (enc); --enc->indent; encode_indent (enc); |
398 | } |
564 | } |
399 | |
565 | |
400 | encode_ch (enc, ']'); |
566 | encode_ch (enc, ']'); |
401 | } |
567 | } |
402 | |
568 | |
403 | static void |
569 | static void |
404 | encode_hk (enc_t *enc, HE *he) |
570 | encode_hk (enc_t *enc, HE *he) |
… | |
… | |
408 | if (HeKLEN (he) == HEf_SVKEY) |
574 | if (HeKLEN (he) == HEf_SVKEY) |
409 | { |
575 | { |
410 | SV *sv = HeSVKEY (he); |
576 | SV *sv = HeSVKEY (he); |
411 | STRLEN len; |
577 | STRLEN len; |
412 | char *str; |
578 | char *str; |
413 | |
579 | |
414 | SvGETMAGIC (sv); |
580 | SvGETMAGIC (sv); |
415 | str = SvPV (sv, len); |
581 | str = SvPV (sv, len); |
416 | |
582 | |
417 | encode_str (enc, str, len, SvUTF8 (sv)); |
583 | encode_str (enc, str, len, SvUTF8 (sv)); |
418 | } |
584 | } |
… | |
… | |
461 | |
627 | |
462 | encode_ch (enc, '{'); |
628 | encode_ch (enc, '{'); |
463 | |
629 | |
464 | // for canonical output we have to sort by keys first |
630 | // for canonical output we have to sort by keys first |
465 | // actually, this is mostly due to the stupid so-called |
631 | // actually, this is mostly due to the stupid so-called |
466 | // security workaround added somewhere in 5.8.x. |
632 | // security workaround added somewhere in 5.8.x |
467 | // that randomises hash orderings |
633 | // that randomises hash orderings |
468 | if (enc->json.flags & F_CANONICAL) |
634 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
469 | { |
635 | { |
470 | int count = hv_iterinit (hv); |
636 | int count = hv_iterinit (hv); |
471 | |
637 | |
472 | if (SvMAGICAL (hv)) |
638 | if (SvMAGICAL (hv)) |
473 | { |
639 | { |
… | |
… | |
484 | } |
650 | } |
485 | |
651 | |
486 | if (count) |
652 | if (count) |
487 | { |
653 | { |
488 | int i, fast = 1; |
654 | int i, fast = 1; |
489 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
655 | HE *hes_stack [STACK_HES]; |
490 | HE **hes = _alloca (count * sizeof (HE)); |
656 | HE **hes = hes_stack; |
491 | #else |
657 | |
492 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
658 | // allocate larger arrays on the heap |
493 | #endif |
659 | if (count > STACK_HES) |
|
|
660 | { |
|
|
661 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
662 | hes = (HE **)SvPVX (sv); |
|
|
663 | } |
494 | |
664 | |
495 | i = 0; |
665 | i = 0; |
496 | while ((he = hv_iternext (hv))) |
666 | while ((he = hv_iternext (hv))) |
497 | { |
667 | { |
498 | hes [i++] = he; |
668 | hes [i++] = he; |
… | |
… | |
567 | // encode objects, arrays and special \0=false and \1=true values. |
737 | // encode objects, arrays and special \0=false and \1=true values. |
568 | static void |
738 | static void |
569 | encode_rv (enc_t *enc, SV *sv) |
739 | encode_rv (enc_t *enc, SV *sv) |
570 | { |
740 | { |
571 | svtype svt; |
741 | svtype svt; |
|
|
742 | GV *method; |
572 | |
743 | |
573 | SvGETMAGIC (sv); |
744 | SvGETMAGIC (sv); |
574 | svt = SvTYPE (sv); |
745 | svt = SvTYPE (sv); |
575 | |
746 | |
576 | if (expect_false (SvOBJECT (sv))) |
747 | if (expect_false (SvOBJECT (sv))) |
577 | { |
748 | { |
578 | HV *stash = !JSON_SLOW || json_boolean_stash |
749 | HV *stash = SvSTASH (sv); |
579 | ? json_boolean_stash |
|
|
580 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
581 | |
750 | |
582 | if (SvSTASH (sv) == stash) |
751 | if (stash == bool_stash) |
583 | { |
752 | { |
584 | if (SvIV (sv)) |
753 | if (SvIV (sv)) |
585 | encode_str (enc, "true", 4, 0); |
754 | encode_str (enc, "true", 4, 0); |
586 | else |
755 | else |
587 | encode_str (enc, "false", 5, 0); |
756 | encode_str (enc, "false", 5, 0); |
588 | } |
757 | } |
|
|
758 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
|
|
759 | { |
|
|
760 | int count; |
|
|
761 | dSP; |
|
|
762 | |
|
|
763 | ENTER; SAVETMPS; |
|
|
764 | SAVESTACK_POS (); |
|
|
765 | PUSHMARK (SP); |
|
|
766 | EXTEND (SP, 2); |
|
|
767 | // we re-bless the reference to get overload and other niceties right |
|
|
768 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
769 | PUSHs (sv_json); |
|
|
770 | |
|
|
771 | PUTBACK; |
|
|
772 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
773 | SPAGAIN; |
|
|
774 | |
|
|
775 | // catch this surprisingly common error |
|
|
776 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
777 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
778 | |
|
|
779 | encode_ch (enc, '('); |
|
|
780 | encode_ch (enc, '"'); |
|
|
781 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
782 | encode_ch (enc, '"'); |
|
|
783 | encode_ch (enc, ')'); |
|
|
784 | encode_ch (enc, '['); |
|
|
785 | |
|
|
786 | while (count) |
|
|
787 | { |
|
|
788 | encode_sv (enc, SP[1 - count--]); |
|
|
789 | |
|
|
790 | if (count) |
|
|
791 | encode_ch (enc, ','); |
|
|
792 | } |
|
|
793 | |
|
|
794 | encode_ch (enc, ']'); |
|
|
795 | |
|
|
796 | FREETMPS; LEAVE; |
|
|
797 | } |
|
|
798 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
|
|
799 | { |
|
|
800 | dSP; |
|
|
801 | |
|
|
802 | ENTER; SAVETMPS; |
|
|
803 | PUSHMARK (SP); |
|
|
804 | // we re-bless the reference to get overload and other niceties right |
|
|
805 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
806 | |
|
|
807 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
808 | PUTBACK; |
|
|
809 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
810 | SPAGAIN; |
|
|
811 | |
|
|
812 | // catch this surprisingly common error |
|
|
813 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
814 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
815 | |
|
|
816 | sv = POPs; |
|
|
817 | PUTBACK; |
|
|
818 | |
|
|
819 | encode_sv (enc, sv); |
|
|
820 | |
|
|
821 | FREETMPS; LEAVE; |
|
|
822 | } |
|
|
823 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
824 | encode_str (enc, "null", 4, 0); |
589 | else |
825 | else |
590 | { |
|
|
591 | #if 0 |
|
|
592 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
593 | { |
|
|
594 | // not yet |
|
|
595 | } |
|
|
596 | #endif |
|
|
597 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
598 | { |
|
|
599 | // we re-bless the reference to get overload and other niceties right |
|
|
600 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
|
|
601 | |
|
|
602 | if (to_json) |
|
|
603 | { |
|
|
604 | dSP; |
|
|
605 | |
|
|
606 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
607 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
608 | |
|
|
609 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
610 | PUTBACK; |
|
|
611 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
612 | SPAGAIN; |
|
|
613 | |
|
|
614 | // catch this surprisingly common error |
|
|
615 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
616 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
617 | |
|
|
618 | sv = POPs; |
|
|
619 | PUTBACK; |
|
|
620 | |
|
|
621 | encode_sv (enc, sv); |
|
|
622 | |
|
|
623 | FREETMPS; LEAVE; |
|
|
624 | } |
|
|
625 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
626 | encode_str (enc, "null", 4, 0); |
|
|
627 | else |
|
|
628 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
629 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
630 | } |
|
|
631 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
632 | encode_str (enc, "null", 4, 0); |
|
|
633 | else |
|
|
634 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
826 | croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", |
635 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
827 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
636 | } |
|
|
637 | } |
828 | } |
638 | else if (svt == SVt_PVHV) |
829 | else if (svt == SVt_PVHV) |
639 | encode_hv (enc, (HV *)sv); |
830 | encode_hv (enc, (HV *)sv); |
640 | else if (svt == SVt_PVAV) |
831 | else if (svt == SVt_PVAV) |
641 | encode_av (enc, (AV *)sv); |
832 | encode_av (enc, (AV *)sv); |
642 | else if (svt < SVt_PVAV) |
833 | else if (svt < SVt_PVAV) |
643 | { |
834 | { |
644 | STRLEN len = 0; |
835 | int bool_type = ref_bool_type (sv); |
645 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
646 | |
836 | |
647 | if (len == 1 && *pv == '1') |
837 | if (bool_type == 1) |
648 | encode_str (enc, "true", 4, 0); |
838 | encode_str (enc, "true", 4, 0); |
649 | else if (len == 1 && *pv == '0') |
839 | else if (bool_type == 0) |
650 | encode_str (enc, "false", 5, 0); |
840 | encode_str (enc, "false", 5, 0); |
651 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
841 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
652 | encode_str (enc, "null", 4, 0); |
842 | encode_str (enc, "null", 4, 0); |
653 | else |
843 | else |
654 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
844 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
… | |
… | |
717 | } |
907 | } |
718 | else |
908 | else |
719 | { |
909 | { |
720 | // large integer, use the (rather slow) snprintf way. |
910 | // large integer, use the (rather slow) snprintf way. |
721 | need (enc, IVUV_MAXCHARS); |
911 | need (enc, IVUV_MAXCHARS); |
722 | enc->cur += |
912 | enc->cur += |
723 | SvIsUV(sv) |
913 | SvIsUV(sv) |
724 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
914 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
725 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
915 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
726 | } |
916 | } |
727 | } |
917 | } |
728 | else if (SvROK (sv)) |
918 | else if (SvROK (sv)) |
729 | encode_rv (enc, SvRV (sv)); |
919 | encode_rv (enc, SvRV (sv)); |
730 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
920 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
731 | encode_str (enc, "null", 4, 0); |
921 | encode_str (enc, "null", 4, 0); |
732 | else |
922 | else |
733 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
923 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", |
734 | SvPV_nolen (sv), SvFLAGS (sv)); |
924 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
735 | } |
925 | } |
736 | |
926 | |
737 | static SV * |
927 | static SV * |
738 | encode_json (SV *scalar, JSON *json) |
928 | encode_json (SV *scalar, JSON *json) |
739 | { |
929 | { |
740 | enc_t enc; |
930 | enc_t enc; |
741 | |
931 | |
742 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
932 | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
743 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
933 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
744 | |
934 | |
745 | enc.json = *json; |
935 | enc.json = *json; |
746 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
936 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
747 | enc.cur = SvPVX (enc.sv); |
937 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
751 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
941 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
752 | : 0x110000UL; |
942 | : 0x110000UL; |
753 | |
943 | |
754 | SvPOK_only (enc.sv); |
944 | SvPOK_only (enc.sv); |
755 | encode_sv (&enc, scalar); |
945 | encode_sv (&enc, scalar); |
|
|
946 | encode_nl (&enc); |
756 | |
947 | |
757 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
948 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
758 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
949 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
759 | |
950 | |
760 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
951 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
940 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1131 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
941 | *cur++ = ch; |
1132 | *cur++ = ch; |
942 | else if (ch >= 0x80) |
1133 | else if (ch >= 0x80) |
943 | { |
1134 | { |
944 | STRLEN clen; |
1135 | STRLEN clen; |
945 | UV uch; |
|
|
946 | |
1136 | |
947 | --dec_cur; |
1137 | --dec_cur; |
948 | |
1138 | |
949 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
1139 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
950 | if (clen == (STRLEN)-1) |
1140 | if (clen == (STRLEN)-1) |
951 | ERR ("malformed UTF-8 character in JSON string"); |
1141 | ERR ("malformed UTF-8 character in JSON string"); |
952 | |
1142 | |
953 | do |
1143 | do |
954 | *cur++ = *dec_cur++; |
1144 | *cur++ = *dec_cur++; |
955 | while (--clen); |
1145 | while (--clen); |
956 | |
1146 | |
957 | utf8 = 1; |
1147 | utf8 = 1; |
958 | } |
1148 | } |
|
|
1149 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1150 | *cur++ = ch; |
959 | else |
1151 | else |
960 | { |
1152 | { |
961 | --dec_cur; |
1153 | --dec_cur; |
962 | |
1154 | |
963 | if (!ch) |
1155 | if (!ch) |
… | |
… | |
971 | { |
1163 | { |
972 | STRLEN len = cur - buf; |
1164 | STRLEN len = cur - buf; |
973 | |
1165 | |
974 | if (sv) |
1166 | if (sv) |
975 | { |
1167 | { |
976 | SvGROW (sv, SvCUR (sv) + len + 1); |
1168 | STRLEN cur = SvCUR (sv); |
|
|
1169 | |
|
|
1170 | if (SvLEN (sv) <= cur + len) |
|
|
1171 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1172 | |
977 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1173 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
978 | SvCUR_set (sv, SvCUR (sv) + len); |
1174 | SvCUR_set (sv, SvCUR (sv) + len); |
979 | } |
1175 | } |
980 | else |
1176 | else |
981 | sv = newSVpvn (buf, len); |
1177 | sv = newSVpvn (buf, len); |
… | |
… | |
1072 | |
1268 | |
1073 | // special case the rather common 1..5-digit-int case |
1269 | // special case the rather common 1..5-digit-int case |
1074 | if (*start == '-') |
1270 | if (*start == '-') |
1075 | switch (len) |
1271 | switch (len) |
1076 | { |
1272 | { |
1077 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1273 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1078 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1274 | case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11)); |
1079 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1275 | case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1080 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1276 | case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1081 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1277 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1082 | } |
1278 | } |
1083 | else |
1279 | else |
1084 | switch (len) |
1280 | switch (len) |
1085 | { |
1281 | { |
1086 | case 1: return newSViv ( start [0] - '0' * 1); |
1282 | case 1: return newSViv ( start [0] - '0' * 1); |
1087 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1283 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1088 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1284 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1089 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1285 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1090 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1286 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1091 | } |
1287 | } |
1092 | |
1288 | |
1093 | { |
1289 | { |
1094 | UV uv; |
1290 | UV uv; |
1095 | int numtype = grok_number (start, len, &uv); |
1291 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1104 | } |
1300 | } |
1105 | |
1301 | |
1106 | len -= *start == '-' ? 1 : 0; |
1302 | len -= *start == '-' ? 1 : 0; |
1107 | |
1303 | |
1108 | // does not fit into IV or UV, try NV |
1304 | // does not fit into IV or UV, try NV |
1109 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1305 | if (len <= NV_DIG) |
1110 | #if defined (LDBL_DIG) |
|
|
1111 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1112 | #endif |
|
|
1113 | ) |
|
|
1114 | // fits into NV without loss of precision |
1306 | // fits into NV without loss of precision |
1115 | return newSVnv (Atof (start)); |
1307 | return newSVnv (json_atof (start)); |
1116 | |
1308 | |
1117 | // everything else fails, convert it to a string |
1309 | // everything else fails, convert it to a string |
1118 | return newSVpvn (start, dec->cur - start); |
1310 | return newSVpvn (start, dec->cur - start); |
1119 | } |
1311 | } |
1120 | |
1312 | |
1121 | // loss of precision here |
1313 | // loss of precision here |
1122 | return newSVnv (Atof (start)); |
1314 | return newSVnv (json_atof (start)); |
1123 | |
1315 | |
1124 | fail: |
1316 | fail: |
1125 | return 0; |
1317 | return 0; |
1126 | } |
1318 | } |
1127 | |
1319 | |
… | |
… | |
1151 | if (*dec->cur == ']') |
1343 | if (*dec->cur == ']') |
1152 | { |
1344 | { |
1153 | ++dec->cur; |
1345 | ++dec->cur; |
1154 | break; |
1346 | break; |
1155 | } |
1347 | } |
1156 | |
1348 | |
1157 | if (*dec->cur != ',') |
1349 | if (*dec->cur != ',') |
1158 | ERR (", or ] expected while parsing array"); |
1350 | ERR (", or ] expected while parsing array"); |
1159 | |
1351 | |
1160 | ++dec->cur; |
1352 | ++dec->cur; |
1161 | |
1353 | |
… | |
… | |
1285 | |
1477 | |
1286 | hv_iterinit (hv); |
1478 | hv_iterinit (hv); |
1287 | he = hv_iternext (hv); |
1479 | he = hv_iternext (hv); |
1288 | hv_iterinit (hv); |
1480 | hv_iterinit (hv); |
1289 | |
1481 | |
1290 | // the next line creates a mortal sv each time its called. |
1482 | // the next line creates a mortal sv each time it's called. |
1291 | // might want to optimise this for common cases. |
1483 | // might want to optimise this for common cases. |
1292 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1484 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1293 | |
1485 | |
1294 | if (cb) |
1486 | if (cb) |
1295 | { |
1487 | { |
1296 | dSP; |
1488 | dSP; |
1297 | int count; |
1489 | int count; |
1298 | |
1490 | |
1299 | ENTER; SAVETMPS; PUSHMARK (SP); |
1491 | ENTER; SAVETMPS; |
|
|
1492 | SAVESTACK_POS (); |
|
|
1493 | PUSHMARK (SP); |
1300 | XPUSHs (HeVAL (he)); |
1494 | XPUSHs (HeVAL (he)); |
|
|
1495 | sv_2mortal (sv); |
1301 | |
1496 | |
1302 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1497 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1303 | |
1498 | |
1304 | if (count == 1) |
1499 | if (count == 1) |
1305 | { |
1500 | { |
1306 | sv = newSVsv (POPs); |
1501 | sv = newSVsv (POPs); |
1307 | FREETMPS; LEAVE; |
1502 | FREETMPS; LEAVE; |
1308 | return sv; |
1503 | return sv; |
1309 | } |
1504 | } |
1310 | |
1505 | |
|
|
1506 | SvREFCNT_inc (sv); |
1311 | FREETMPS; LEAVE; |
1507 | FREETMPS; LEAVE; |
1312 | } |
1508 | } |
1313 | } |
1509 | } |
1314 | |
1510 | |
1315 | if (dec->json.cb_object) |
1511 | if (dec->json.cb_object) |
1316 | { |
1512 | { |
1317 | dSP; |
1513 | dSP; |
1318 | int count; |
1514 | int count; |
1319 | |
1515 | |
1320 | ENTER; SAVETMPS; PUSHMARK (SP); |
1516 | ENTER; SAVETMPS; |
|
|
1517 | SAVESTACK_POS (); |
|
|
1518 | PUSHMARK (SP); |
1321 | XPUSHs (sv_2mortal (sv)); |
1519 | XPUSHs (sv_2mortal (sv)); |
1322 | |
1520 | |
1323 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1521 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1324 | |
1522 | |
1325 | if (count == 1) |
1523 | if (count == 1) |
… | |
… | |
1341 | DEC_DEC_DEPTH; |
1539 | DEC_DEC_DEPTH; |
1342 | return 0; |
1540 | return 0; |
1343 | } |
1541 | } |
1344 | |
1542 | |
1345 | static SV * |
1543 | static SV * |
|
|
1544 | decode_tag (dec_t *dec) |
|
|
1545 | { |
|
|
1546 | SV *tag = 0; |
|
|
1547 | SV *val = 0; |
|
|
1548 | |
|
|
1549 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
|
|
1550 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
|
|
1551 | |
|
|
1552 | ++dec->cur; |
|
|
1553 | |
|
|
1554 | decode_ws (dec); |
|
|
1555 | |
|
|
1556 | tag = decode_sv (dec); |
|
|
1557 | if (!tag) |
|
|
1558 | goto fail; |
|
|
1559 | |
|
|
1560 | if (!SvPOK (tag)) |
|
|
1561 | ERR ("malformed JSON string, (tag) must be a string"); |
|
|
1562 | |
|
|
1563 | decode_ws (dec); |
|
|
1564 | |
|
|
1565 | if (*dec->cur != ')') |
|
|
1566 | ERR (") expected after tag"); |
|
|
1567 | |
|
|
1568 | ++dec->cur; |
|
|
1569 | |
|
|
1570 | decode_ws (dec); |
|
|
1571 | |
|
|
1572 | val = decode_sv (dec); |
|
|
1573 | if (!val) |
|
|
1574 | goto fail; |
|
|
1575 | |
|
|
1576 | if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) |
|
|
1577 | ERR ("malformed JSON string, tag value must be an array"); |
|
|
1578 | |
|
|
1579 | { |
|
|
1580 | AV *av = (AV *)SvRV (val); |
|
|
1581 | int i, len = av_len (av) + 1; |
|
|
1582 | HV *stash = gv_stashsv (tag, 0); |
|
|
1583 | SV *sv; |
|
|
1584 | |
|
|
1585 | if (!stash) |
|
|
1586 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
1587 | |
|
|
1588 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
1589 | |
|
|
1590 | if (!method) |
|
|
1591 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
1592 | |
|
|
1593 | dSP; |
|
|
1594 | |
|
|
1595 | ENTER; SAVETMPS; |
|
|
1596 | PUSHMARK (SP); |
|
|
1597 | EXTEND (SP, len + 2); |
|
|
1598 | // we re-bless the reference to get overload and other niceties right |
|
|
1599 | PUSHs (tag); |
|
|
1600 | PUSHs (sv_json); |
|
|
1601 | |
|
|
1602 | for (i = 0; i < len; ++i) |
|
|
1603 | PUSHs (*av_fetch (av, i, 1)); |
|
|
1604 | |
|
|
1605 | PUTBACK; |
|
|
1606 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
1607 | SPAGAIN; |
|
|
1608 | |
|
|
1609 | SvREFCNT_dec (tag); |
|
|
1610 | SvREFCNT_dec (val); |
|
|
1611 | sv = SvREFCNT_inc (POPs); |
|
|
1612 | |
|
|
1613 | PUTBACK; |
|
|
1614 | |
|
|
1615 | FREETMPS; LEAVE; |
|
|
1616 | |
|
|
1617 | return sv; |
|
|
1618 | } |
|
|
1619 | |
|
|
1620 | fail: |
|
|
1621 | SvREFCNT_dec (tag); |
|
|
1622 | SvREFCNT_dec (val); |
|
|
1623 | return 0; |
|
|
1624 | } |
|
|
1625 | |
|
|
1626 | static SV * |
1346 | decode_sv (dec_t *dec) |
1627 | decode_sv (dec_t *dec) |
1347 | { |
1628 | { |
1348 | // the beauty of JSON: you need exactly one character lookahead |
1629 | // the beauty of JSON: you need exactly one character lookahead |
1349 | // to parse everything. |
1630 | // to parse everything. |
1350 | switch (*dec->cur) |
1631 | switch (*dec->cur) |
1351 | { |
1632 | { |
1352 | case '"': ++dec->cur; return decode_str (dec); |
1633 | case '"': ++dec->cur; return decode_str (dec); |
1353 | case '[': ++dec->cur; return decode_av (dec); |
1634 | case '[': ++dec->cur; return decode_av (dec); |
1354 | case '{': ++dec->cur; return decode_hv (dec); |
1635 | case '{': ++dec->cur; return decode_hv (dec); |
|
|
1636 | case '(': return decode_tag (dec); |
1355 | |
1637 | |
1356 | case '-': |
1638 | case '-': |
1357 | case '0': case '1': case '2': case '3': case '4': |
1639 | case '0': case '1': case '2': case '3': case '4': |
1358 | case '5': case '6': case '7': case '8': case '9': |
1640 | case '5': case '6': case '7': case '8': case '9': |
1359 | return decode_num (dec); |
1641 | return decode_num (dec); |
… | |
… | |
1361 | case 't': |
1643 | case 't': |
1362 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1644 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1363 | { |
1645 | { |
1364 | dec->cur += 4; |
1646 | dec->cur += 4; |
1365 | #if JSON_SLOW |
1647 | #if JSON_SLOW |
1366 | json_true = get_bool ("JSON::XS::true"); |
1648 | bool_true = get_bool ("Types::Serialiser::true"); |
1367 | #endif |
1649 | #endif |
1368 | return newSVsv (json_true); |
1650 | return newSVsv (bool_true); |
1369 | } |
1651 | } |
1370 | else |
1652 | else |
1371 | ERR ("'true' expected"); |
1653 | ERR ("'true' expected"); |
1372 | |
1654 | |
1373 | break; |
1655 | break; |
… | |
… | |
1375 | case 'f': |
1657 | case 'f': |
1376 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1658 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1377 | { |
1659 | { |
1378 | dec->cur += 5; |
1660 | dec->cur += 5; |
1379 | #if JSON_SLOW |
1661 | #if JSON_SLOW |
1380 | json_false = get_bool ("JSON::XS::false"); |
1662 | bool_false = get_bool ("Types::Serialiser::false"); |
1381 | #endif |
1663 | #endif |
1382 | return newSVsv (json_false); |
1664 | return newSVsv (bool_false); |
1383 | } |
1665 | } |
1384 | else |
1666 | else |
1385 | ERR ("'false' expected"); |
1667 | ERR ("'false' expected"); |
1386 | |
1668 | |
1387 | break; |
1669 | break; |
… | |
… | |
1396 | ERR ("'null' expected"); |
1678 | ERR ("'null' expected"); |
1397 | |
1679 | |
1398 | break; |
1680 | break; |
1399 | |
1681 | |
1400 | default: |
1682 | default: |
1401 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1683 | ERR ("malformed JSON string, neither tag, array, object, number, string or atom"); |
1402 | break; |
1684 | break; |
1403 | } |
1685 | } |
1404 | |
1686 | |
1405 | fail: |
1687 | fail: |
1406 | return 0; |
1688 | return 0; |
1407 | } |
1689 | } |
1408 | |
1690 | |
1409 | static SV * |
1691 | static SV * |
1410 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1692 | decode_json (SV *string, JSON *json, char **offset_return) |
1411 | { |
1693 | { |
1412 | dec_t dec; |
1694 | dec_t dec; |
1413 | STRLEN offset; |
|
|
1414 | SV *sv; |
1695 | SV *sv; |
1415 | |
1696 | |
|
|
1697 | /* work around bugs in 5.10 where manipulating magic values |
|
|
1698 | * makes perl ignore the magic in subsequent accesses. |
|
|
1699 | * also make a copy of non-PV values, to get them into a clean |
|
|
1700 | * state (SvPV should do that, but it's buggy, see below). |
|
|
1701 | */ |
1416 | SvGETMAGIC (string); |
1702 | /*SvGETMAGIC (string);*/ |
|
|
1703 | if (SvMAGICAL (string) || !SvPOK (string)) |
|
|
1704 | string = sv_2mortal (newSVsv (string)); |
|
|
1705 | |
1417 | SvUPGRADE (string, SVt_PV); |
1706 | SvUPGRADE (string, SVt_PV); |
1418 | |
1707 | |
1419 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1708 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1420 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1709 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1421 | * return the xpv_cur field which certainly exists after upgrading. |
1710 | * return the xpv_cur field which certainly exists after upgrading. |
1422 | * according to nicholas clark, calling SvPOK fixes this. |
1711 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1712 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
1713 | * and hope for the best. |
|
|
1714 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
1715 | * assertion business is seriously broken, try yet another workaround |
|
|
1716 | * for the broken -DDEBUGGING. |
1423 | */ |
1717 | */ |
1424 | SvPOK (string); |
1718 | { |
|
|
1719 | #ifdef DEBUGGING |
|
|
1720 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
1721 | #else |
|
|
1722 | STRLEN offset = SvCUR (string); |
|
|
1723 | #endif |
1425 | |
1724 | |
1426 | if (SvCUR (string) > json->max_size && json->max_size) |
1725 | if (offset > json->max_size && json->max_size) |
1427 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1726 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1428 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1727 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1728 | } |
1429 | |
1729 | |
1430 | if (json->flags & F_UTF8) |
1730 | if (DECODE_WANTS_OCTETS (json)) |
1431 | sv_utf8_downgrade (string, 0); |
1731 | sv_utf8_downgrade (string, 0); |
1432 | else |
1732 | else |
1433 | sv_utf8_upgrade (string); |
1733 | sv_utf8_upgrade (string); |
1434 | |
1734 | |
1435 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1735 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1445 | |
1745 | |
1446 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1746 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1447 | |
1747 | |
1448 | decode_ws (&dec); |
1748 | decode_ws (&dec); |
1449 | sv = decode_sv (&dec); |
1749 | sv = decode_sv (&dec); |
|
|
1750 | |
|
|
1751 | if (offset_return) |
|
|
1752 | *offset_return = dec.cur; |
1450 | |
1753 | |
1451 | if (!(offset_return || !sv)) |
1754 | if (!(offset_return || !sv)) |
1452 | { |
1755 | { |
1453 | // check for trailing garbage |
1756 | // check for trailing garbage |
1454 | decode_ws (&dec); |
1757 | decode_ws (&dec); |
… | |
… | |
1457 | { |
1760 | { |
1458 | dec.err = "garbage after JSON object"; |
1761 | dec.err = "garbage after JSON object"; |
1459 | SvREFCNT_dec (sv); |
1762 | SvREFCNT_dec (sv); |
1460 | sv = 0; |
1763 | sv = 0; |
1461 | } |
1764 | } |
1462 | } |
|
|
1463 | |
|
|
1464 | if (offset_return || !sv) |
|
|
1465 | { |
|
|
1466 | offset = dec.json.flags & F_UTF8 |
|
|
1467 | ? dec.cur - SvPVX (string) |
|
|
1468 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1469 | |
|
|
1470 | if (offset_return) |
|
|
1471 | *offset_return = offset; |
|
|
1472 | } |
1765 | } |
1473 | |
1766 | |
1474 | if (!sv) |
1767 | if (!sv) |
1475 | { |
1768 | { |
1476 | SV *uni = sv_newmortal (); |
1769 | SV *uni = sv_newmortal (); |
… | |
… | |
1482 | SAVEVPTR (PL_curcop); |
1775 | SAVEVPTR (PL_curcop); |
1483 | PL_curcop = &cop; |
1776 | PL_curcop = &cop; |
1484 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1777 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1485 | LEAVE; |
1778 | LEAVE; |
1486 | |
1779 | |
1487 | croak ("%s, at character offset %d [\"%s\"]", |
1780 | croak ("%s, at character offset %d (before \"%s\")", |
1488 | dec.err, |
1781 | dec.err, |
1489 | (int)offset, |
1782 | (int)ptr_to_index (string, dec.cur), |
1490 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1783 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1491 | } |
1784 | } |
1492 | |
1785 | |
1493 | sv = sv_2mortal (sv); |
1786 | sv = sv_2mortal (sv); |
1494 | |
1787 | |
1495 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1788 | if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
1496 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1789 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1497 | |
1790 | |
1498 | return sv; |
1791 | return sv; |
1499 | } |
1792 | } |
1500 | |
1793 | |
… | |
… | |
1503 | |
1796 | |
1504 | static void |
1797 | static void |
1505 | incr_parse (JSON *self) |
1798 | incr_parse (JSON *self) |
1506 | { |
1799 | { |
1507 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1800 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1801 | |
|
|
1802 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1803 | // but this would make it slower, so... |
1508 | |
1804 | |
1509 | for (;;) |
1805 | for (;;) |
1510 | { |
1806 | { |
1511 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1807 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1512 | switch (self->incr_mode) |
1808 | switch (self->incr_mode) |
1513 | { |
1809 | { |
1514 | // only used for intiial whitespace skipping |
1810 | // only used for initial whitespace skipping |
1515 | case INCR_M_WS: |
1811 | case INCR_M_WS: |
1516 | for (;;) |
1812 | for (;;) |
1517 | { |
1813 | { |
1518 | if (*p > 0x20) |
1814 | if (*p > 0x20) |
1519 | { |
1815 | { |
|
|
1816 | if (*p == '#') |
|
|
1817 | { |
|
|
1818 | self->incr_mode = INCR_M_C0; |
|
|
1819 | goto incr_m_c; |
|
|
1820 | } |
|
|
1821 | else |
|
|
1822 | { |
1520 | self->incr_mode = INCR_M_JSON; |
1823 | self->incr_mode = INCR_M_JSON; |
1521 | goto incr_m_json; |
1824 | goto incr_m_json; |
|
|
1825 | } |
1522 | } |
1826 | } |
1523 | else if (!*p) |
1827 | else if (!*p) |
1524 | goto interrupt; |
1828 | goto interrupt; |
1525 | |
1829 | |
1526 | ++p; |
1830 | ++p; |
… | |
… | |
1532 | goto interrupt; |
1836 | goto interrupt; |
1533 | |
1837 | |
1534 | ++p; |
1838 | ++p; |
1535 | self->incr_mode = INCR_M_STR; |
1839 | self->incr_mode = INCR_M_STR; |
1536 | goto incr_m_str; |
1840 | goto incr_m_str; |
|
|
1841 | |
|
|
1842 | // inside #-style comments |
|
|
1843 | case INCR_M_C0: |
|
|
1844 | case INCR_M_C1: |
|
|
1845 | incr_m_c: |
|
|
1846 | for (;;) |
|
|
1847 | { |
|
|
1848 | if (*p == '\n') |
|
|
1849 | { |
|
|
1850 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1851 | break; |
|
|
1852 | } |
|
|
1853 | else if (!*p) |
|
|
1854 | goto interrupt; |
|
|
1855 | |
|
|
1856 | ++p; |
|
|
1857 | } |
|
|
1858 | |
|
|
1859 | break; |
1537 | |
1860 | |
1538 | // inside a string |
1861 | // inside a string |
1539 | case INCR_M_STR: |
1862 | case INCR_M_STR: |
1540 | incr_m_str: |
1863 | incr_m_str: |
1541 | for (;;) |
1864 | for (;;) |
… | |
… | |
1592 | self->incr_mode = INCR_M_STR; |
1915 | self->incr_mode = INCR_M_STR; |
1593 | goto incr_m_str; |
1916 | goto incr_m_str; |
1594 | |
1917 | |
1595 | case '[': |
1918 | case '[': |
1596 | case '{': |
1919 | case '{': |
|
|
1920 | case '(': |
1597 | if (++self->incr_nest > self->max_depth) |
1921 | if (++self->incr_nest > self->max_depth) |
1598 | croak (ERR_NESTING_EXCEEDED); |
1922 | croak (ERR_NESTING_EXCEEDED); |
1599 | break; |
1923 | break; |
1600 | |
1924 | |
1601 | case ']': |
1925 | case ']': |
1602 | case '}': |
1926 | case '}': |
1603 | if (!--self->incr_nest) |
1927 | if (--self->incr_nest <= 0) |
1604 | goto interrupt; |
1928 | goto interrupt; |
|
|
1929 | break; |
|
|
1930 | |
|
|
1931 | case ')': |
|
|
1932 | --self->incr_nest; |
|
|
1933 | break; |
|
|
1934 | |
|
|
1935 | case '#': |
|
|
1936 | self->incr_mode = INCR_M_C1; |
|
|
1937 | goto incr_m_c; |
1605 | } |
1938 | } |
1606 | } |
1939 | } |
1607 | } |
1940 | } |
1608 | |
1941 | |
1609 | modechange: |
1942 | modechange: |
1610 | ; |
1943 | ; |
1611 | } |
1944 | } |
1612 | |
1945 | |
1613 | interrupt: |
1946 | interrupt: |
1614 | self->incr_pos = p - SvPVX (self->incr_text); |
1947 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1948 | //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
1615 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1949 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1616 | } |
1950 | } |
1617 | |
1951 | |
1618 | ///////////////////////////////////////////////////////////////////////////// |
1952 | ///////////////////////////////////////////////////////////////////////////// |
1619 | // XS interface functions |
1953 | // XS interface functions |
… | |
… | |
1629 | i >= '0' && i <= '9' ? i - '0' |
1963 | i >= '0' && i <= '9' ? i - '0' |
1630 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1964 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1631 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1965 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1632 | : -1; |
1966 | : -1; |
1633 | |
1967 | |
1634 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1968 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1635 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1969 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1636 | |
|
|
1637 | json_true = get_bool ("JSON::XS::true"); |
1970 | bool_true = get_bool ("Types::Serialiser::true"); |
1638 | json_false = get_bool ("JSON::XS::false"); |
1971 | bool_false = get_bool ("Types::Serialiser::false"); |
|
|
1972 | |
|
|
1973 | sv_json = newSVpv ("JSON", 0); |
|
|
1974 | SvREADONLY_on (sv_json); |
|
|
1975 | |
|
|
1976 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1639 | } |
1977 | } |
1640 | |
1978 | |
1641 | PROTOTYPES: DISABLE |
1979 | PROTOTYPES: DISABLE |
1642 | |
1980 | |
1643 | void CLONE (...) |
1981 | void CLONE (...) |
1644 | CODE: |
1982 | CODE: |
1645 | json_stash = 0; |
1983 | json_stash = 0; |
1646 | json_boolean_stash = 0; |
1984 | bool_stash = 0; |
1647 | |
1985 | |
1648 | void new (char *klass) |
1986 | void new (char *klass) |
1649 | PPCODE: |
1987 | PPCODE: |
1650 | { |
1988 | { |
1651 | SV *pv = NEWSV (0, sizeof (JSON)); |
1989 | SV *pv = NEWSV (0, sizeof (JSON)); |
1652 | SvPOK_only (pv); |
1990 | SvPOK_only (pv); |
1653 | json_init ((JSON *)SvPVX (pv)); |
1991 | json_init ((JSON *)SvPVX (pv)); |
1654 | XPUSHs (sv_2mortal (sv_bless ( |
1992 | XPUSHs (sv_2mortal (sv_bless ( |
1655 | newRV_noinc (pv), |
1993 | newRV_noinc (pv), |
1656 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1994 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
1671 | shrink = F_SHRINK |
2009 | shrink = F_SHRINK |
1672 | allow_blessed = F_ALLOW_BLESSED |
2010 | allow_blessed = F_ALLOW_BLESSED |
1673 | convert_blessed = F_CONV_BLESSED |
2011 | convert_blessed = F_CONV_BLESSED |
1674 | relaxed = F_RELAXED |
2012 | relaxed = F_RELAXED |
1675 | allow_unknown = F_ALLOW_UNKNOWN |
2013 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
2014 | allow_tags = F_ALLOW_TAGS |
1676 | PPCODE: |
2015 | PPCODE: |
1677 | { |
2016 | { |
1678 | if (enable) |
2017 | if (enable) |
1679 | self->flags |= ix; |
2018 | self->flags |= ix; |
1680 | else |
2019 | else |
… | |
… | |
1696 | get_shrink = F_SHRINK |
2035 | get_shrink = F_SHRINK |
1697 | get_allow_blessed = F_ALLOW_BLESSED |
2036 | get_allow_blessed = F_ALLOW_BLESSED |
1698 | get_convert_blessed = F_CONV_BLESSED |
2037 | get_convert_blessed = F_CONV_BLESSED |
1699 | get_relaxed = F_RELAXED |
2038 | get_relaxed = F_RELAXED |
1700 | get_allow_unknown = F_ALLOW_UNKNOWN |
2039 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
2040 | get_allow_tags = F_ALLOW_TAGS |
1701 | PPCODE: |
2041 | PPCODE: |
1702 | XPUSHs (boolSV (self->flags & ix)); |
2042 | XPUSHs (boolSV (self->flags & ix)); |
1703 | |
2043 | |
1704 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
2044 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1705 | PPCODE: |
2045 | PPCODE: |
… | |
… | |
1733 | } |
2073 | } |
1734 | |
2074 | |
1735 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
2075 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1736 | PPCODE: |
2076 | PPCODE: |
1737 | { |
2077 | { |
1738 | if (!self->cb_sk_object) |
2078 | if (!self->cb_sk_object) |
1739 | self->cb_sk_object = newHV (); |
2079 | self->cb_sk_object = newHV (); |
1740 | |
2080 | |
1741 | if (SvOK (cb)) |
2081 | if (SvOK (cb)) |
1742 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
2082 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1743 | else |
2083 | else |
… | |
… | |
1754 | XPUSHs (ST (0)); |
2094 | XPUSHs (ST (0)); |
1755 | } |
2095 | } |
1756 | |
2096 | |
1757 | void encode (JSON *self, SV *scalar) |
2097 | void encode (JSON *self, SV *scalar) |
1758 | PPCODE: |
2098 | PPCODE: |
1759 | XPUSHs (encode_json (scalar, self)); |
2099 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
2100 | XPUSHs (scalar); |
1760 | |
2101 | |
1761 | void decode (JSON *self, SV *jsonstr) |
2102 | void decode (JSON *self, SV *jsonstr) |
1762 | PPCODE: |
2103 | PPCODE: |
1763 | XPUSHs (decode_json (jsonstr, self, 0)); |
2104 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
2105 | XPUSHs (jsonstr); |
1764 | |
2106 | |
1765 | void decode_prefix (JSON *self, SV *jsonstr) |
2107 | void decode_prefix (JSON *self, SV *jsonstr) |
1766 | PPCODE: |
2108 | PPCODE: |
1767 | { |
2109 | { |
1768 | STRLEN offset; |
2110 | SV *sv; |
|
|
2111 | char *offset; |
|
|
2112 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1769 | EXTEND (SP, 2); |
2113 | EXTEND (SP, 2); |
1770 | PUSHs (decode_json (jsonstr, self, &offset)); |
2114 | PUSHs (sv); |
1771 | PUSHs (sv_2mortal (newSVuv (offset))); |
2115 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1772 | } |
2116 | } |
1773 | |
2117 | |
1774 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2118 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1775 | PPCODE: |
2119 | PPCODE: |
1776 | { |
2120 | { |
1777 | if (!self->incr_text) |
2121 | if (!self->incr_text) |
1778 | self->incr_text = newSVpvn ("", 0); |
2122 | self->incr_text = newSVpvn ("", 0); |
|
|
2123 | |
|
|
2124 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
2125 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
2126 | if (DECODE_WANTS_OCTETS (self)) |
|
|
2127 | { |
|
|
2128 | if (self->incr_pos) |
|
|
2129 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
2130 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
2131 | |
|
|
2132 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
2133 | } |
|
|
2134 | else |
|
|
2135 | { |
|
|
2136 | sv_utf8_upgrade (self->incr_text); |
|
|
2137 | |
|
|
2138 | if (self->incr_pos) |
|
|
2139 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
2140 | - (U8 *)SvPVX (self->incr_text); |
|
|
2141 | } |
1779 | |
2142 | |
1780 | // append data, if any |
2143 | // append data, if any |
1781 | if (jsonstr) |
2144 | if (jsonstr) |
1782 | { |
2145 | { |
|
|
2146 | /* make sure both strings have same encoding */ |
1783 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
2147 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
|
|
2148 | if (SvUTF8 (jsonstr)) |
|
|
2149 | sv_utf8_downgrade (jsonstr, 0); |
1784 | { |
2150 | else |
1785 | /* utf-8-ness differs, need to upgrade */ |
|
|
1786 | sv_utf8_upgrade (self->incr_text); |
2151 | sv_utf8_upgrade (jsonstr); |
1787 | |
2152 | |
1788 | if (self->incr_pos) |
2153 | /* and then just blindly append */ |
1789 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1790 | - (U8 *)SvPVX (self->incr_text); |
|
|
1791 | } |
|
|
1792 | |
|
|
1793 | { |
2154 | { |
1794 | STRLEN len; |
2155 | STRLEN len; |
1795 | const char *str = SvPV (jsonstr, len); |
2156 | const char *str = SvPV (jsonstr, len); |
1796 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
2157 | STRLEN cur = SvCUR (self->incr_text); |
|
|
2158 | |
|
|
2159 | if (SvLEN (self->incr_text) <= cur + len) |
|
|
2160 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
2161 | |
1797 | Move (str, SvEND (self->incr_text), len, char); |
2162 | Move (str, SvEND (self->incr_text), len, char); |
1798 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
2163 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1799 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
2164 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1800 | } |
2165 | } |
1801 | } |
2166 | } |
1802 | |
2167 | |
1803 | if (GIMME_V != G_VOID) |
2168 | if (GIMME_V != G_VOID) |
1804 | do |
2169 | do |
1805 | { |
2170 | { |
|
|
2171 | SV *sv; |
1806 | STRLEN offset; |
2172 | char *offset; |
1807 | |
2173 | |
1808 | if (!INCR_DONE (self)) |
2174 | if (!INCR_DONE (self)) |
1809 | { |
2175 | { |
1810 | incr_parse (self); |
2176 | incr_parse (self); |
1811 | |
2177 | |
1812 | if (self->incr_pos > self->max_size && self->max_size) |
2178 | if (self->incr_pos > self->max_size && self->max_size) |
1813 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
2179 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1814 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
2180 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1815 | |
2181 | |
1816 | if (!INCR_DONE (self)) |
2182 | if (!INCR_DONE (self)) |
|
|
2183 | { |
|
|
2184 | // as an optimisation, do not accumulate white space in the incr buffer |
|
|
2185 | if (self->incr_mode == INCR_M_WS && self->incr_pos) |
|
|
2186 | { |
|
|
2187 | self->incr_pos = 0; |
|
|
2188 | SvCUR_set (self->incr_text, 0); |
|
|
2189 | } |
|
|
2190 | |
1817 | break; |
2191 | break; |
|
|
2192 | } |
1818 | } |
2193 | } |
1819 | |
2194 | |
1820 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2195 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2196 | XPUSHs (sv); |
1821 | |
2197 | |
1822 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1823 | self->incr_pos -= offset; |
2198 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1824 | self->incr_nest = 0; |
2199 | self->incr_nest = 0; |
1825 | self->incr_mode = 0; |
2200 | self->incr_mode = 0; |
|
|
2201 | |
|
|
2202 | sv_chop (self->incr_text, offset); |
1826 | } |
2203 | } |
1827 | while (GIMME_V == G_ARRAY); |
2204 | while (GIMME_V == G_ARRAY); |
1828 | } |
2205 | } |
1829 | |
2206 | |
1830 | SV *incr_text (JSON *self) |
2207 | SV *incr_text (JSON *self) |
… | |
… | |
1868 | SvREFCNT_dec (self->incr_text); |
2245 | SvREFCNT_dec (self->incr_text); |
1869 | |
2246 | |
1870 | PROTOTYPES: ENABLE |
2247 | PROTOTYPES: ENABLE |
1871 | |
2248 | |
1872 | void encode_json (SV *scalar) |
2249 | void encode_json (SV *scalar) |
1873 | ALIAS: |
|
|
1874 | to_json_ = 0 |
|
|
1875 | encode_json = F_UTF8 |
|
|
1876 | PPCODE: |
2250 | PPCODE: |
1877 | { |
2251 | { |
1878 | JSON json; |
2252 | JSON json; |
1879 | json_init (&json); |
2253 | json_init (&json); |
1880 | json.flags |= ix; |
2254 | json.flags |= F_UTF8; |
1881 | XPUSHs (encode_json (scalar, &json)); |
2255 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2256 | XPUSHs (scalar); |
1882 | } |
2257 | } |
1883 | |
2258 | |
1884 | void decode_json (SV *jsonstr) |
2259 | void decode_json (SV *jsonstr) |
1885 | ALIAS: |
|
|
1886 | from_json_ = 0 |
|
|
1887 | decode_json = F_UTF8 |
|
|
1888 | PPCODE: |
2260 | PPCODE: |
1889 | { |
2261 | { |
1890 | JSON json; |
2262 | JSON json; |
1891 | json_init (&json); |
2263 | json_init (&json); |
1892 | json.flags |= ix; |
2264 | json.flags |= F_UTF8; |
1893 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2265 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2266 | XPUSHs (jsonstr); |
1894 | } |
2267 | } |
1895 | |
2268 | |
1896 | |
|
|