… | |
… | |
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 | // guarantees, 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 |
|
|
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 |
20 | #endif |
31 | #endif |
21 | |
32 | |
22 | // three extra for rounding, sign, and end of string |
33 | // three extra for rounding, sign, and end of string |
23 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
34 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
24 | |
35 | |
… | |
… | |
33 | #define F_SHRINK 0x00000200UL |
44 | #define F_SHRINK 0x00000200UL |
34 | #define F_ALLOW_BLESSED 0x00000400UL |
45 | #define F_ALLOW_BLESSED 0x00000400UL |
35 | #define F_CONV_BLESSED 0x00000800UL |
46 | #define F_CONV_BLESSED 0x00000800UL |
36 | #define F_RELAXED 0x00001000UL |
47 | #define F_RELAXED 0x00001000UL |
37 | #define F_ALLOW_UNKNOWN 0x00002000UL |
48 | #define F_ALLOW_UNKNOWN 0x00002000UL |
|
|
49 | #define F_ALLOW_TAGS 0x00004000UL |
38 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
50 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
39 | |
51 | |
40 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
52 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | |
53 | |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
54 | #define INIT_SIZE 32 // initial scalar size to be allocated |
43 | #define INDENT_STEP 3 // spaces per indentation level |
55 | #define INDENT_STEP 3 // spaces per indentation level |
44 | |
56 | |
45 | #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) |
46 | |
60 | |
47 | #define SB do { |
61 | #define SB do { |
48 | #define SE } while (0) |
62 | #define SE } while (0) |
49 | |
63 | |
50 | #if __GNUC__ >= 3 |
64 | #if __GNUC__ >= 3 |
… | |
… | |
70 | #else |
84 | #else |
71 | # define JSON_SLOW 0 |
85 | # define JSON_SLOW 0 |
72 | # define JSON_STASH json_stash |
86 | # define JSON_STASH json_stash |
73 | #endif |
87 | #endif |
74 | |
88 | |
|
|
89 | // the amount of HEs to allocate on the stack, when sorting keys |
|
|
90 | #define STACK_HES 64 |
|
|
91 | |
75 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
92 | static HV *json_stash, *types_boolean_stash; // JSON::XS:: |
76 | static SV *json_true, *json_false; |
93 | static SV *types_true, *types_false, *sv_json; |
77 | |
94 | |
78 | enum { |
95 | enum { |
79 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
96 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
80 | INCR_M_STR, // inside string |
97 | INCR_M_STR, // inside string |
81 | INCR_M_BS, // inside backslash |
98 | INCR_M_BS, // inside backslash |
|
|
99 | INCR_M_C0, // inside comment in initial whitespace sequence |
|
|
100 | INCR_M_C1, // inside comment in other places |
82 | INCR_M_JSON // outside anything, count nesting |
101 | INCR_M_JSON // outside anything, count nesting |
83 | }; |
102 | }; |
84 | |
103 | |
85 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
104 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
86 | |
105 | |
… | |
… | |
185 | return SvUTF8 (sv) |
204 | return SvUTF8 (sv) |
186 | ? utf8_distance (offset, SvPVX (sv)) |
205 | ? utf8_distance (offset, SvPVX (sv)) |
187 | : offset - SvPVX (sv); |
206 | : offset - SvPVX (sv); |
188 | } |
207 | } |
189 | |
208 | |
|
|
209 | ///////////////////////////////////////////////////////////////////////////// |
|
|
210 | // fp hell |
|
|
211 | |
|
|
212 | // scan a group of digits, and a trailing exponent |
|
|
213 | static void |
|
|
214 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth) |
|
|
215 | { |
|
|
216 | UV uaccum = 0; |
|
|
217 | int eaccum = 0; |
|
|
218 | |
|
|
219 | // if we recurse too deep, skip all remaining digits |
|
|
220 | // to avoid a stack overflow attack |
|
|
221 | if (expect_false (--maxdepth <= 0)) |
|
|
222 | while (((U8)*s - '0') < 10) |
|
|
223 | ++s; |
|
|
224 | |
|
|
225 | for (;;) |
|
|
226 | { |
|
|
227 | U8 dig = (U8)*s - '0'; |
|
|
228 | |
|
|
229 | if (expect_false (dig >= 10)) |
|
|
230 | { |
|
|
231 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
232 | { |
|
|
233 | ++s; |
|
|
234 | json_atof_scan1 (s, accum, expo, 1, maxdepth); |
|
|
235 | } |
|
|
236 | else if ((dig | ' ') == 'e' - '0') |
|
|
237 | { |
|
|
238 | int exp2 = 0; |
|
|
239 | int neg = 0; |
|
|
240 | |
|
|
241 | ++s; |
|
|
242 | |
|
|
243 | if (*s == '-') |
|
|
244 | { |
|
|
245 | ++s; |
|
|
246 | neg = 1; |
|
|
247 | } |
|
|
248 | else if (*s == '+') |
|
|
249 | ++s; |
|
|
250 | |
|
|
251 | while ((dig = (U8)*s - '0') < 10) |
|
|
252 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
253 | |
|
|
254 | *expo += neg ? -exp2 : exp2; |
|
|
255 | } |
|
|
256 | |
|
|
257 | break; |
|
|
258 | } |
|
|
259 | |
|
|
260 | ++s; |
|
|
261 | |
|
|
262 | uaccum = uaccum * 10 + dig; |
|
|
263 | ++eaccum; |
|
|
264 | |
|
|
265 | // if we have too many digits, then recurse for more |
|
|
266 | // we actually do this for rather few digits |
|
|
267 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
268 | { |
|
|
269 | if (postdp) *expo -= eaccum; |
|
|
270 | json_atof_scan1 (s, accum, expo, postdp, maxdepth); |
|
|
271 | if (postdp) *expo += eaccum; |
|
|
272 | |
|
|
273 | break; |
|
|
274 | } |
|
|
275 | } |
|
|
276 | |
|
|
277 | // this relies greatly on the quality of the pow () |
|
|
278 | // implementation of the platform, but a good |
|
|
279 | // implementation is hard to beat. |
|
|
280 | // (IEEE 754 conformant ones are required to be exact) |
|
|
281 | if (postdp) *expo -= eaccum; |
|
|
282 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
283 | *expo += eaccum; |
|
|
284 | } |
|
|
285 | |
|
|
286 | static NV |
|
|
287 | json_atof (const char *s) |
|
|
288 | { |
|
|
289 | NV accum = 0.; |
|
|
290 | int expo = 0; |
|
|
291 | int neg = 0; |
|
|
292 | |
|
|
293 | if (*s == '-') |
|
|
294 | { |
|
|
295 | ++s; |
|
|
296 | neg = 1; |
|
|
297 | } |
|
|
298 | |
|
|
299 | // a recursion depth of ten gives us >>500 bits |
|
|
300 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
|
|
301 | |
|
|
302 | return neg ? -accum : accum; |
|
|
303 | } |
190 | ///////////////////////////////////////////////////////////////////////////// |
304 | ///////////////////////////////////////////////////////////////////////////// |
191 | // encoder |
305 | // encoder |
192 | |
306 | |
193 | // structure used for encoding JSON |
307 | // structure used for encoding JSON |
194 | typedef struct |
308 | typedef struct |
… | |
… | |
381 | |
495 | |
382 | if (enc->indent >= enc->json.max_depth) |
496 | if (enc->indent >= enc->json.max_depth) |
383 | croak (ERR_NESTING_EXCEEDED); |
497 | croak (ERR_NESTING_EXCEEDED); |
384 | |
498 | |
385 | encode_ch (enc, '['); |
499 | encode_ch (enc, '['); |
386 | |
500 | |
387 | if (len >= 0) |
501 | if (len >= 0) |
388 | { |
502 | { |
389 | encode_nl (enc); ++enc->indent; |
503 | encode_nl (enc); ++enc->indent; |
390 | |
504 | |
391 | for (i = 0; i <= len; ++i) |
505 | for (i = 0; i <= len; ++i) |
… | |
… | |
403 | encode_comma (enc); |
517 | encode_comma (enc); |
404 | } |
518 | } |
405 | |
519 | |
406 | encode_nl (enc); --enc->indent; encode_indent (enc); |
520 | encode_nl (enc); --enc->indent; encode_indent (enc); |
407 | } |
521 | } |
408 | |
522 | |
409 | encode_ch (enc, ']'); |
523 | encode_ch (enc, ']'); |
410 | } |
524 | } |
411 | |
525 | |
412 | static void |
526 | static void |
413 | encode_hk (enc_t *enc, HE *he) |
527 | encode_hk (enc_t *enc, HE *he) |
… | |
… | |
417 | if (HeKLEN (he) == HEf_SVKEY) |
531 | if (HeKLEN (he) == HEf_SVKEY) |
418 | { |
532 | { |
419 | SV *sv = HeSVKEY (he); |
533 | SV *sv = HeSVKEY (he); |
420 | STRLEN len; |
534 | STRLEN len; |
421 | char *str; |
535 | char *str; |
422 | |
536 | |
423 | SvGETMAGIC (sv); |
537 | SvGETMAGIC (sv); |
424 | str = SvPV (sv, len); |
538 | str = SvPV (sv, len); |
425 | |
539 | |
426 | encode_str (enc, str, len, SvUTF8 (sv)); |
540 | encode_str (enc, str, len, SvUTF8 (sv)); |
427 | } |
541 | } |
… | |
… | |
493 | } |
607 | } |
494 | |
608 | |
495 | if (count) |
609 | if (count) |
496 | { |
610 | { |
497 | int i, fast = 1; |
611 | int i, fast = 1; |
498 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
612 | HE *hes_stack [STACK_HES]; |
499 | HE **hes = _alloca (count * sizeof (HE)); |
613 | HE **hes = hes_stack; |
500 | #else |
614 | |
501 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
615 | // allocate larger arrays on the heap |
502 | #endif |
616 | if (count > STACK_HES) |
|
|
617 | { |
|
|
618 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
619 | hes = (HE **)SvPVX (sv); |
|
|
620 | } |
503 | |
621 | |
504 | i = 0; |
622 | i = 0; |
505 | while ((he = hv_iternext (hv))) |
623 | while ((he = hv_iternext (hv))) |
506 | { |
624 | { |
507 | hes [i++] = he; |
625 | hes [i++] = he; |
… | |
… | |
576 | // encode objects, arrays and special \0=false and \1=true values. |
694 | // encode objects, arrays and special \0=false and \1=true values. |
577 | static void |
695 | static void |
578 | encode_rv (enc_t *enc, SV *sv) |
696 | encode_rv (enc_t *enc, SV *sv) |
579 | { |
697 | { |
580 | svtype svt; |
698 | svtype svt; |
|
|
699 | GV *method; |
581 | |
700 | |
582 | SvGETMAGIC (sv); |
701 | SvGETMAGIC (sv); |
583 | svt = SvTYPE (sv); |
702 | svt = SvTYPE (sv); |
584 | |
703 | |
585 | if (expect_false (SvOBJECT (sv))) |
704 | if (expect_false (SvOBJECT (sv))) |
586 | { |
705 | { |
587 | HV *stash = !JSON_SLOW || json_boolean_stash |
706 | HV *boolean_stash = !JSON_SLOW || types_boolean_stash |
588 | ? json_boolean_stash |
707 | ? types_boolean_stash |
589 | : gv_stashpv ("JSON::XS::Boolean", 1); |
708 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
709 | HV *stash = SvSTASH (sv); |
590 | |
710 | |
591 | if (SvSTASH (sv) == stash) |
711 | if (stash == boolean_stash) |
592 | { |
712 | { |
593 | if (SvIV (sv)) |
713 | if (SvIV (sv)) |
594 | encode_str (enc, "true", 4, 0); |
714 | encode_str (enc, "true", 4, 0); |
595 | else |
715 | else |
596 | encode_str (enc, "false", 5, 0); |
716 | encode_str (enc, "false", 5, 0); |
597 | } |
717 | } |
|
|
718 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
|
|
719 | { |
|
|
720 | int count; |
|
|
721 | dSP; |
|
|
722 | |
|
|
723 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
724 | EXTEND (SP, 2); |
|
|
725 | // we re-bless the reference to get overload and other niceties right |
|
|
726 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
727 | PUSHs (sv_json); |
|
|
728 | |
|
|
729 | PUTBACK; |
|
|
730 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
731 | SPAGAIN; |
|
|
732 | |
|
|
733 | // catch this surprisingly common error |
|
|
734 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
735 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
736 | |
|
|
737 | encode_ch (enc, '('); |
|
|
738 | encode_ch (enc, '"'); |
|
|
739 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
740 | encode_ch (enc, '"'); |
|
|
741 | encode_ch (enc, ')'); |
|
|
742 | encode_ch (enc, '['); |
|
|
743 | |
|
|
744 | while (count) |
|
|
745 | { |
|
|
746 | encode_sv (enc, SP[1 - count--]); |
|
|
747 | |
|
|
748 | if (count) |
|
|
749 | encode_ch (enc, ','); |
|
|
750 | } |
|
|
751 | |
|
|
752 | encode_ch (enc, ']'); |
|
|
753 | |
|
|
754 | PUTBACK; |
|
|
755 | |
|
|
756 | FREETMPS; LEAVE; |
|
|
757 | } |
|
|
758 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
|
|
759 | { |
|
|
760 | dSP; |
|
|
761 | |
|
|
762 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
763 | // we re-bless the reference to get overload and other niceties right |
|
|
764 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
765 | |
|
|
766 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
767 | PUTBACK; |
|
|
768 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
769 | SPAGAIN; |
|
|
770 | |
|
|
771 | // catch this surprisingly common error |
|
|
772 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
773 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
774 | |
|
|
775 | sv = POPs; |
|
|
776 | PUTBACK; |
|
|
777 | |
|
|
778 | encode_sv (enc, sv); |
|
|
779 | |
|
|
780 | FREETMPS; LEAVE; |
|
|
781 | } |
|
|
782 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
783 | encode_str (enc, "null", 4, 0); |
598 | else |
784 | else |
599 | { |
|
|
600 | #if 0 |
|
|
601 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
602 | { |
|
|
603 | // not yet |
|
|
604 | } |
|
|
605 | #endif |
|
|
606 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
607 | { |
|
|
608 | // we re-bless the reference to get overload and other niceties right |
|
|
609 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
|
|
610 | |
|
|
611 | if (to_json) |
|
|
612 | { |
|
|
613 | dSP; |
|
|
614 | |
|
|
615 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
616 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
617 | |
|
|
618 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
619 | PUTBACK; |
|
|
620 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
621 | SPAGAIN; |
|
|
622 | |
|
|
623 | // catch this surprisingly common error |
|
|
624 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
625 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
626 | |
|
|
627 | sv = POPs; |
|
|
628 | PUTBACK; |
|
|
629 | |
|
|
630 | encode_sv (enc, sv); |
|
|
631 | |
|
|
632 | FREETMPS; LEAVE; |
|
|
633 | } |
|
|
634 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
635 | encode_str (enc, "null", 4, 0); |
|
|
636 | else |
|
|
637 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
638 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
639 | } |
|
|
640 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
641 | encode_str (enc, "null", 4, 0); |
|
|
642 | else |
|
|
643 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
785 | croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", |
644 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
786 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
645 | } |
|
|
646 | } |
787 | } |
647 | else if (svt == SVt_PVHV) |
788 | else if (svt == SVt_PVHV) |
648 | encode_hv (enc, (HV *)sv); |
789 | encode_hv (enc, (HV *)sv); |
649 | else if (svt == SVt_PVAV) |
790 | else if (svt == SVt_PVAV) |
650 | encode_av (enc, (AV *)sv); |
791 | encode_av (enc, (AV *)sv); |
… | |
… | |
726 | } |
867 | } |
727 | else |
868 | else |
728 | { |
869 | { |
729 | // large integer, use the (rather slow) snprintf way. |
870 | // large integer, use the (rather slow) snprintf way. |
730 | need (enc, IVUV_MAXCHARS); |
871 | need (enc, IVUV_MAXCHARS); |
731 | enc->cur += |
872 | enc->cur += |
732 | SvIsUV(sv) |
873 | SvIsUV(sv) |
733 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
874 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
734 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
875 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
735 | } |
876 | } |
736 | } |
877 | } |
737 | else if (SvROK (sv)) |
878 | else if (SvROK (sv)) |
738 | encode_rv (enc, SvRV (sv)); |
879 | encode_rv (enc, SvRV (sv)); |
739 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
880 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
740 | encode_str (enc, "null", 4, 0); |
881 | encode_str (enc, "null", 4, 0); |
741 | else |
882 | else |
742 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
883 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", |
743 | SvPV_nolen (sv), SvFLAGS (sv)); |
884 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
744 | } |
885 | } |
745 | |
886 | |
746 | static SV * |
887 | static SV * |
747 | encode_json (SV *scalar, JSON *json) |
888 | encode_json (SV *scalar, JSON *json) |
748 | { |
889 | { |
… | |
… | |
1117 | } |
1258 | } |
1118 | |
1259 | |
1119 | len -= *start == '-' ? 1 : 0; |
1260 | len -= *start == '-' ? 1 : 0; |
1120 | |
1261 | |
1121 | // does not fit into IV or UV, try NV |
1262 | // does not fit into IV or UV, try NV |
1122 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1263 | if (len <= NV_DIG) |
1123 | #if defined (LDBL_DIG) |
|
|
1124 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1125 | #endif |
|
|
1126 | ) |
|
|
1127 | // fits into NV without loss of precision |
1264 | // fits into NV without loss of precision |
1128 | return newSVnv (Atof (start)); |
1265 | return newSVnv (json_atof (start)); |
1129 | |
1266 | |
1130 | // everything else fails, convert it to a string |
1267 | // everything else fails, convert it to a string |
1131 | return newSVpvn (start, dec->cur - start); |
1268 | return newSVpvn (start, dec->cur - start); |
1132 | } |
1269 | } |
1133 | |
1270 | |
1134 | // loss of precision here |
1271 | // loss of precision here |
1135 | return newSVnv (Atof (start)); |
1272 | return newSVnv (json_atof (start)); |
1136 | |
1273 | |
1137 | fail: |
1274 | fail: |
1138 | return 0; |
1275 | return 0; |
1139 | } |
1276 | } |
1140 | |
1277 | |
… | |
… | |
1164 | if (*dec->cur == ']') |
1301 | if (*dec->cur == ']') |
1165 | { |
1302 | { |
1166 | ++dec->cur; |
1303 | ++dec->cur; |
1167 | break; |
1304 | break; |
1168 | } |
1305 | } |
1169 | |
1306 | |
1170 | if (*dec->cur != ',') |
1307 | if (*dec->cur != ',') |
1171 | ERR (", or ] expected while parsing array"); |
1308 | ERR (", or ] expected while parsing array"); |
1172 | |
1309 | |
1173 | ++dec->cur; |
1310 | ++dec->cur; |
1174 | |
1311 | |
… | |
… | |
1309 | dSP; |
1446 | dSP; |
1310 | int count; |
1447 | int count; |
1311 | |
1448 | |
1312 | ENTER; SAVETMPS; PUSHMARK (SP); |
1449 | ENTER; SAVETMPS; PUSHMARK (SP); |
1313 | XPUSHs (HeVAL (he)); |
1450 | XPUSHs (HeVAL (he)); |
|
|
1451 | sv_2mortal (sv); |
1314 | |
1452 | |
1315 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1453 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1316 | |
1454 | |
1317 | if (count == 1) |
1455 | if (count == 1) |
1318 | { |
1456 | { |
1319 | sv = newSVsv (POPs); |
1457 | sv = newSVsv (POPs); |
1320 | FREETMPS; LEAVE; |
1458 | FREETMPS; LEAVE; |
1321 | return sv; |
1459 | return sv; |
1322 | } |
1460 | } |
1323 | |
1461 | |
|
|
1462 | SvREFCNT_inc (sv); |
1324 | FREETMPS; LEAVE; |
1463 | FREETMPS; LEAVE; |
1325 | } |
1464 | } |
1326 | } |
1465 | } |
1327 | |
1466 | |
1328 | if (dec->json.cb_object) |
1467 | if (dec->json.cb_object) |
… | |
… | |
1354 | DEC_DEC_DEPTH; |
1493 | DEC_DEC_DEPTH; |
1355 | return 0; |
1494 | return 0; |
1356 | } |
1495 | } |
1357 | |
1496 | |
1358 | static SV * |
1497 | static SV * |
|
|
1498 | decode_tag (dec_t *dec) |
|
|
1499 | { |
|
|
1500 | SV *tag = 0; |
|
|
1501 | SV *val = 0; |
|
|
1502 | |
|
|
1503 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
|
|
1504 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
|
|
1505 | |
|
|
1506 | ++dec->cur; |
|
|
1507 | |
|
|
1508 | decode_ws (dec); |
|
|
1509 | |
|
|
1510 | tag = decode_sv (dec); |
|
|
1511 | if (!tag) |
|
|
1512 | goto fail; |
|
|
1513 | |
|
|
1514 | if (!SvPOK (tag)) |
|
|
1515 | ERR ("malformed JSON string, (tag) must be a string"); |
|
|
1516 | |
|
|
1517 | decode_ws (dec); |
|
|
1518 | |
|
|
1519 | if (*dec->cur != ')') |
|
|
1520 | ERR (") expected after tag"); |
|
|
1521 | |
|
|
1522 | ++dec->cur; |
|
|
1523 | |
|
|
1524 | decode_ws (dec); |
|
|
1525 | |
|
|
1526 | val = decode_sv (dec); |
|
|
1527 | if (!val) |
|
|
1528 | goto fail; |
|
|
1529 | |
|
|
1530 | if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) |
|
|
1531 | ERR ("malformed JSON string, tag value must be an array"); |
|
|
1532 | |
|
|
1533 | { |
|
|
1534 | AV *av = (AV *)SvRV (val); |
|
|
1535 | int i, len = av_len (av) + 1; |
|
|
1536 | HV *stash = gv_stashsv (tag, 0); |
|
|
1537 | SV *sv; |
|
|
1538 | |
|
|
1539 | if (!stash) |
|
|
1540 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
1541 | |
|
|
1542 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
1543 | |
|
|
1544 | if (!method) |
|
|
1545 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
1546 | |
|
|
1547 | dSP; |
|
|
1548 | |
|
|
1549 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1550 | EXTEND (SP, len + 2); |
|
|
1551 | // we re-bless the reference to get overload and other niceties right |
|
|
1552 | PUSHs (tag); |
|
|
1553 | PUSHs (sv_json); |
|
|
1554 | |
|
|
1555 | for (i = 0; i < len; ++i) |
|
|
1556 | PUSHs (*av_fetch (av, i, 1)); |
|
|
1557 | |
|
|
1558 | PUTBACK; |
|
|
1559 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
1560 | SPAGAIN; |
|
|
1561 | |
|
|
1562 | SvREFCNT_dec (tag); |
|
|
1563 | SvREFCNT_dec (val); |
|
|
1564 | sv = SvREFCNT_inc (POPs); |
|
|
1565 | |
|
|
1566 | PUTBACK; |
|
|
1567 | |
|
|
1568 | FREETMPS; LEAVE; |
|
|
1569 | |
|
|
1570 | return sv; |
|
|
1571 | } |
|
|
1572 | |
|
|
1573 | fail: |
|
|
1574 | SvREFCNT_dec (tag); |
|
|
1575 | SvREFCNT_dec (val); |
|
|
1576 | return 0; |
|
|
1577 | } |
|
|
1578 | |
|
|
1579 | static SV * |
1359 | decode_sv (dec_t *dec) |
1580 | decode_sv (dec_t *dec) |
1360 | { |
1581 | { |
1361 | // the beauty of JSON: you need exactly one character lookahead |
1582 | // the beauty of JSON: you need exactly one character lookahead |
1362 | // to parse everything. |
1583 | // to parse everything. |
1363 | switch (*dec->cur) |
1584 | switch (*dec->cur) |
1364 | { |
1585 | { |
1365 | case '"': ++dec->cur; return decode_str (dec); |
1586 | case '"': ++dec->cur; return decode_str (dec); |
1366 | case '[': ++dec->cur; return decode_av (dec); |
1587 | case '[': ++dec->cur; return decode_av (dec); |
1367 | case '{': ++dec->cur; return decode_hv (dec); |
1588 | case '{': ++dec->cur; return decode_hv (dec); |
|
|
1589 | case '(': return decode_tag (dec); |
1368 | |
1590 | |
1369 | case '-': |
1591 | case '-': |
1370 | case '0': case '1': case '2': case '3': case '4': |
1592 | case '0': case '1': case '2': case '3': case '4': |
1371 | case '5': case '6': case '7': case '8': case '9': |
1593 | case '5': case '6': case '7': case '8': case '9': |
1372 | return decode_num (dec); |
1594 | return decode_num (dec); |
… | |
… | |
1374 | case 't': |
1596 | case 't': |
1375 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1597 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1376 | { |
1598 | { |
1377 | dec->cur += 4; |
1599 | dec->cur += 4; |
1378 | #if JSON_SLOW |
1600 | #if JSON_SLOW |
1379 | json_true = get_bool ("JSON::XS::true"); |
1601 | types_true = get_bool ("Types::Serialiser::true"); |
1380 | #endif |
1602 | #endif |
1381 | return newSVsv (json_true); |
1603 | return newSVsv (types_true); |
1382 | } |
1604 | } |
1383 | else |
1605 | else |
1384 | ERR ("'true' expected"); |
1606 | ERR ("'true' expected"); |
1385 | |
1607 | |
1386 | break; |
1608 | break; |
… | |
… | |
1388 | case 'f': |
1610 | case 'f': |
1389 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1611 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1390 | { |
1612 | { |
1391 | dec->cur += 5; |
1613 | dec->cur += 5; |
1392 | #if JSON_SLOW |
1614 | #if JSON_SLOW |
1393 | json_false = get_bool ("JSON::XS::false"); |
1615 | types_false = get_bool ("Types::Serialiser::false"); |
1394 | #endif |
1616 | #endif |
1395 | return newSVsv (json_false); |
1617 | return newSVsv (types_false); |
1396 | } |
1618 | } |
1397 | else |
1619 | else |
1398 | ERR ("'false' expected"); |
1620 | ERR ("'false' expected"); |
1399 | |
1621 | |
1400 | break; |
1622 | break; |
… | |
… | |
1409 | ERR ("'null' expected"); |
1631 | ERR ("'null' expected"); |
1410 | |
1632 | |
1411 | break; |
1633 | break; |
1412 | |
1634 | |
1413 | default: |
1635 | default: |
1414 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1636 | ERR ("malformed JSON string, neither tag, array, object, number, string or atom"); |
1415 | break; |
1637 | break; |
1416 | } |
1638 | } |
1417 | |
1639 | |
1418 | fail: |
1640 | fail: |
1419 | return 0; |
1641 | return 0; |
… | |
… | |
1424 | { |
1646 | { |
1425 | dec_t dec; |
1647 | dec_t dec; |
1426 | SV *sv; |
1648 | SV *sv; |
1427 | |
1649 | |
1428 | /* work around bugs in 5.10 where manipulating magic values |
1650 | /* work around bugs in 5.10 where manipulating magic values |
1429 | * will perl ignore the magic in subsequent accesses |
1651 | * makes perl ignore the magic in subsequent accesses. |
|
|
1652 | * also make a copy of non-PV values, to get them into a clean |
|
|
1653 | * state (SvPV should do that, but it's buggy, see below). |
1430 | */ |
1654 | */ |
1431 | /*SvGETMAGIC (string);*/ |
1655 | /*SvGETMAGIC (string);*/ |
1432 | if (SvMAGICAL (string)) |
1656 | if (SvMAGICAL (string) || !SvPOK (string)) |
1433 | string = sv_2mortal (newSVsv (string)); |
1657 | string = sv_2mortal (newSVsv (string)); |
1434 | |
1658 | |
1435 | SvUPGRADE (string, SVt_PV); |
1659 | SvUPGRADE (string, SVt_PV); |
1436 | |
1660 | |
1437 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1661 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1454 | if (offset > json->max_size && json->max_size) |
1678 | if (offset > json->max_size && json->max_size) |
1455 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1679 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1456 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1680 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1457 | } |
1681 | } |
1458 | |
1682 | |
1459 | if (json->flags & F_UTF8) |
1683 | if (DECODE_WANTS_OCTETS (json)) |
1460 | sv_utf8_downgrade (string, 0); |
1684 | sv_utf8_downgrade (string, 0); |
1461 | else |
1685 | else |
1462 | sv_utf8_upgrade (string); |
1686 | sv_utf8_upgrade (string); |
1463 | |
1687 | |
1464 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1688 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
… | |
… | |
1506 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1730 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1507 | LEAVE; |
1731 | LEAVE; |
1508 | |
1732 | |
1509 | croak ("%s, at character offset %d (before \"%s\")", |
1733 | croak ("%s, at character offset %d (before \"%s\")", |
1510 | dec.err, |
1734 | dec.err, |
1511 | ptr_to_index (string, dec.cur), |
1735 | (int)ptr_to_index (string, dec.cur), |
1512 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1736 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1513 | } |
1737 | } |
1514 | |
1738 | |
1515 | sv = sv_2mortal (sv); |
1739 | sv = sv_2mortal (sv); |
1516 | |
1740 | |
… | |
… | |
1525 | |
1749 | |
1526 | static void |
1750 | static void |
1527 | incr_parse (JSON *self) |
1751 | incr_parse (JSON *self) |
1528 | { |
1752 | { |
1529 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1753 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1754 | |
|
|
1755 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1756 | // but this would make it slower, so... |
1530 | |
1757 | |
1531 | for (;;) |
1758 | for (;;) |
1532 | { |
1759 | { |
1533 | //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 |
1760 | //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 |
1534 | switch (self->incr_mode) |
1761 | switch (self->incr_mode) |
1535 | { |
1762 | { |
1536 | // only used for intiial whitespace skipping |
1763 | // only used for initial whitespace skipping |
1537 | case INCR_M_WS: |
1764 | case INCR_M_WS: |
1538 | for (;;) |
1765 | for (;;) |
1539 | { |
1766 | { |
1540 | if (*p > 0x20) |
1767 | if (*p > 0x20) |
1541 | { |
1768 | { |
|
|
1769 | if (*p == '#') |
|
|
1770 | { |
|
|
1771 | self->incr_mode = INCR_M_C0; |
|
|
1772 | goto incr_m_c; |
|
|
1773 | } |
|
|
1774 | else |
|
|
1775 | { |
1542 | self->incr_mode = INCR_M_JSON; |
1776 | self->incr_mode = INCR_M_JSON; |
1543 | goto incr_m_json; |
1777 | goto incr_m_json; |
|
|
1778 | } |
1544 | } |
1779 | } |
1545 | else if (!*p) |
1780 | else if (!*p) |
1546 | goto interrupt; |
1781 | goto interrupt; |
1547 | |
1782 | |
1548 | ++p; |
1783 | ++p; |
… | |
… | |
1554 | goto interrupt; |
1789 | goto interrupt; |
1555 | |
1790 | |
1556 | ++p; |
1791 | ++p; |
1557 | self->incr_mode = INCR_M_STR; |
1792 | self->incr_mode = INCR_M_STR; |
1558 | goto incr_m_str; |
1793 | goto incr_m_str; |
|
|
1794 | |
|
|
1795 | // inside #-style comments |
|
|
1796 | case INCR_M_C0: |
|
|
1797 | case INCR_M_C1: |
|
|
1798 | incr_m_c: |
|
|
1799 | for (;;) |
|
|
1800 | { |
|
|
1801 | if (*p == '\n') |
|
|
1802 | { |
|
|
1803 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1804 | break; |
|
|
1805 | } |
|
|
1806 | else if (!*p) |
|
|
1807 | goto interrupt; |
|
|
1808 | |
|
|
1809 | ++p; |
|
|
1810 | } |
|
|
1811 | |
|
|
1812 | break; |
1559 | |
1813 | |
1560 | // inside a string |
1814 | // inside a string |
1561 | case INCR_M_STR: |
1815 | case INCR_M_STR: |
1562 | incr_m_str: |
1816 | incr_m_str: |
1563 | for (;;) |
1817 | for (;;) |
… | |
… | |
1614 | self->incr_mode = INCR_M_STR; |
1868 | self->incr_mode = INCR_M_STR; |
1615 | goto incr_m_str; |
1869 | goto incr_m_str; |
1616 | |
1870 | |
1617 | case '[': |
1871 | case '[': |
1618 | case '{': |
1872 | case '{': |
|
|
1873 | case '(': |
1619 | if (++self->incr_nest > self->max_depth) |
1874 | if (++self->incr_nest > self->max_depth) |
1620 | croak (ERR_NESTING_EXCEEDED); |
1875 | croak (ERR_NESTING_EXCEEDED); |
1621 | break; |
1876 | break; |
1622 | |
1877 | |
1623 | case ']': |
1878 | case ']': |
1624 | case '}': |
1879 | case '}': |
1625 | if (--self->incr_nest <= 0) |
1880 | if (--self->incr_nest <= 0) |
1626 | goto interrupt; |
1881 | goto interrupt; |
|
|
1882 | break; |
|
|
1883 | |
|
|
1884 | case ')': |
|
|
1885 | --self->incr_nest; |
|
|
1886 | break; |
|
|
1887 | |
|
|
1888 | case '#': |
|
|
1889 | self->incr_mode = INCR_M_C1; |
|
|
1890 | goto incr_m_c; |
1627 | } |
1891 | } |
1628 | } |
1892 | } |
1629 | } |
1893 | } |
1630 | |
1894 | |
1631 | modechange: |
1895 | modechange: |
1632 | ; |
1896 | ; |
1633 | } |
1897 | } |
1634 | |
1898 | |
1635 | interrupt: |
1899 | interrupt: |
1636 | self->incr_pos = p - SvPVX (self->incr_text); |
1900 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1901 | //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
1637 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1902 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1638 | } |
1903 | } |
1639 | |
1904 | |
1640 | ///////////////////////////////////////////////////////////////////////////// |
1905 | ///////////////////////////////////////////////////////////////////////////// |
1641 | // XS interface functions |
1906 | // XS interface functions |
… | |
… | |
1651 | i >= '0' && i <= '9' ? i - '0' |
1916 | i >= '0' && i <= '9' ? i - '0' |
1652 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1917 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1653 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1918 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1654 | : -1; |
1919 | : -1; |
1655 | |
1920 | |
1656 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1921 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1657 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1922 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1658 | |
1923 | |
1659 | json_true = get_bool ("JSON::XS::true"); |
1924 | types_true = get_bool ("Types::Serialiser::true"); |
1660 | json_false = get_bool ("JSON::XS::false"); |
1925 | types_false = get_bool ("Types::Serialiser::false"); |
|
|
1926 | |
|
|
1927 | sv_json = newSVpv ("JSON", 0); |
|
|
1928 | SvREADONLY_on (sv_json); |
1661 | |
1929 | |
1662 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1930 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1663 | } |
1931 | } |
1664 | |
1932 | |
1665 | PROTOTYPES: DISABLE |
1933 | PROTOTYPES: DISABLE |
1666 | |
1934 | |
1667 | void CLONE (...) |
1935 | void CLONE (...) |
1668 | CODE: |
1936 | CODE: |
1669 | json_stash = 0; |
1937 | json_stash = 0; |
1670 | json_boolean_stash = 0; |
1938 | types_boolean_stash = 0; |
1671 | |
1939 | |
1672 | void new (char *klass) |
1940 | void new (char *klass) |
1673 | PPCODE: |
1941 | PPCODE: |
1674 | { |
1942 | { |
1675 | SV *pv = NEWSV (0, sizeof (JSON)); |
1943 | SV *pv = NEWSV (0, sizeof (JSON)); |
1676 | SvPOK_only (pv); |
1944 | SvPOK_only (pv); |
1677 | json_init ((JSON *)SvPVX (pv)); |
1945 | json_init ((JSON *)SvPVX (pv)); |
1678 | XPUSHs (sv_2mortal (sv_bless ( |
1946 | XPUSHs (sv_2mortal (sv_bless ( |
1679 | newRV_noinc (pv), |
1947 | newRV_noinc (pv), |
1680 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1948 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
1695 | shrink = F_SHRINK |
1963 | shrink = F_SHRINK |
1696 | allow_blessed = F_ALLOW_BLESSED |
1964 | allow_blessed = F_ALLOW_BLESSED |
1697 | convert_blessed = F_CONV_BLESSED |
1965 | convert_blessed = F_CONV_BLESSED |
1698 | relaxed = F_RELAXED |
1966 | relaxed = F_RELAXED |
1699 | allow_unknown = F_ALLOW_UNKNOWN |
1967 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
1968 | allow_tags = F_ALLOW_TAGS |
1700 | PPCODE: |
1969 | PPCODE: |
1701 | { |
1970 | { |
1702 | if (enable) |
1971 | if (enable) |
1703 | self->flags |= ix; |
1972 | self->flags |= ix; |
1704 | else |
1973 | else |
… | |
… | |
1720 | get_shrink = F_SHRINK |
1989 | get_shrink = F_SHRINK |
1721 | get_allow_blessed = F_ALLOW_BLESSED |
1990 | get_allow_blessed = F_ALLOW_BLESSED |
1722 | get_convert_blessed = F_CONV_BLESSED |
1991 | get_convert_blessed = F_CONV_BLESSED |
1723 | get_relaxed = F_RELAXED |
1992 | get_relaxed = F_RELAXED |
1724 | get_allow_unknown = F_ALLOW_UNKNOWN |
1993 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
1994 | get_allow_tags = F_ALLOW_TAGS |
1725 | PPCODE: |
1995 | PPCODE: |
1726 | XPUSHs (boolSV (self->flags & ix)); |
1996 | XPUSHs (boolSV (self->flags & ix)); |
1727 | |
1997 | |
1728 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1998 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1729 | PPCODE: |
1999 | PPCODE: |
… | |
… | |
1757 | } |
2027 | } |
1758 | |
2028 | |
1759 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
2029 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1760 | PPCODE: |
2030 | PPCODE: |
1761 | { |
2031 | { |
1762 | if (!self->cb_sk_object) |
2032 | if (!self->cb_sk_object) |
1763 | self->cb_sk_object = newHV (); |
2033 | self->cb_sk_object = newHV (); |
1764 | |
2034 | |
1765 | if (SvOK (cb)) |
2035 | if (SvOK (cb)) |
1766 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
2036 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1767 | else |
2037 | else |
… | |
… | |
1778 | XPUSHs (ST (0)); |
2048 | XPUSHs (ST (0)); |
1779 | } |
2049 | } |
1780 | |
2050 | |
1781 | void encode (JSON *self, SV *scalar) |
2051 | void encode (JSON *self, SV *scalar) |
1782 | PPCODE: |
2052 | PPCODE: |
1783 | XPUSHs (encode_json (scalar, self)); |
2053 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
2054 | XPUSHs (scalar); |
1784 | |
2055 | |
1785 | void decode (JSON *self, SV *jsonstr) |
2056 | void decode (JSON *self, SV *jsonstr) |
1786 | PPCODE: |
2057 | PPCODE: |
1787 | XPUSHs (decode_json (jsonstr, self, 0)); |
2058 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
2059 | XPUSHs (jsonstr); |
1788 | |
2060 | |
1789 | void decode_prefix (JSON *self, SV *jsonstr) |
2061 | void decode_prefix (JSON *self, SV *jsonstr) |
1790 | PPCODE: |
2062 | PPCODE: |
1791 | { |
2063 | { |
|
|
2064 | SV *sv; |
1792 | char *offset; |
2065 | char *offset; |
|
|
2066 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1793 | EXTEND (SP, 2); |
2067 | EXTEND (SP, 2); |
1794 | PUSHs (decode_json (jsonstr, self, &offset)); |
2068 | PUSHs (sv); |
1795 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
2069 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1796 | } |
2070 | } |
1797 | |
2071 | |
1798 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2072 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1799 | PPCODE: |
2073 | PPCODE: |
1800 | { |
2074 | { |
1801 | if (!self->incr_text) |
2075 | if (!self->incr_text) |
1802 | self->incr_text = newSVpvn ("", 0); |
2076 | self->incr_text = newSVpvn ("", 0); |
|
|
2077 | |
|
|
2078 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
2079 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
2080 | if (DECODE_WANTS_OCTETS (self)) |
|
|
2081 | { |
|
|
2082 | if (self->incr_pos) |
|
|
2083 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
2084 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
2085 | |
|
|
2086 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
2087 | } |
|
|
2088 | else |
|
|
2089 | { |
|
|
2090 | sv_utf8_upgrade (self->incr_text); |
|
|
2091 | |
|
|
2092 | if (self->incr_pos) |
|
|
2093 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
2094 | - (U8 *)SvPVX (self->incr_text); |
|
|
2095 | } |
1803 | |
2096 | |
1804 | // append data, if any |
2097 | // append data, if any |
1805 | if (jsonstr) |
2098 | if (jsonstr) |
1806 | { |
2099 | { |
|
|
2100 | /* make sure both strings have same encoding */ |
|
|
2101 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
1807 | if (SvUTF8 (jsonstr)) |
2102 | if (SvUTF8 (jsonstr)) |
|
|
2103 | sv_utf8_downgrade (jsonstr, 0); |
1808 | { |
2104 | else |
1809 | if (!SvUTF8 (self->incr_text)) |
|
|
1810 | { |
|
|
1811 | /* utf-8-ness differs, need to upgrade */ |
|
|
1812 | sv_utf8_upgrade (self->incr_text); |
|
|
1813 | |
|
|
1814 | if (self->incr_pos) |
|
|
1815 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1816 | - (U8 *)SvPVX (self->incr_text); |
|
|
1817 | } |
|
|
1818 | } |
|
|
1819 | else if (SvUTF8 (self->incr_text)) |
|
|
1820 | sv_utf8_upgrade (jsonstr); |
2105 | sv_utf8_upgrade (jsonstr); |
1821 | |
2106 | |
|
|
2107 | /* and then just blindly append */ |
1822 | { |
2108 | { |
1823 | STRLEN len; |
2109 | STRLEN len; |
1824 | const char *str = SvPV (jsonstr, len); |
2110 | const char *str = SvPV (jsonstr, len); |
1825 | STRLEN cur = SvCUR (self->incr_text); |
2111 | STRLEN cur = SvCUR (self->incr_text); |
1826 | |
2112 | |
… | |
… | |
1834 | } |
2120 | } |
1835 | |
2121 | |
1836 | if (GIMME_V != G_VOID) |
2122 | if (GIMME_V != G_VOID) |
1837 | do |
2123 | do |
1838 | { |
2124 | { |
|
|
2125 | SV *sv; |
1839 | char *offset; |
2126 | char *offset; |
1840 | |
2127 | |
1841 | if (!INCR_DONE (self)) |
2128 | if (!INCR_DONE (self)) |
1842 | { |
2129 | { |
1843 | incr_parse (self); |
2130 | incr_parse (self); |
… | |
… | |
1845 | if (self->incr_pos > self->max_size && self->max_size) |
2132 | if (self->incr_pos > self->max_size && self->max_size) |
1846 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
2133 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1847 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
2134 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1848 | |
2135 | |
1849 | if (!INCR_DONE (self)) |
2136 | if (!INCR_DONE (self)) |
|
|
2137 | { |
|
|
2138 | // as an optimisation, do not accumulate white space in the incr buffer |
|
|
2139 | if (self->incr_mode == INCR_M_WS && self->incr_pos) |
|
|
2140 | { |
|
|
2141 | self->incr_pos = 0; |
|
|
2142 | SvCUR_set (self->incr_text, 0); |
|
|
2143 | } |
|
|
2144 | |
1850 | break; |
2145 | break; |
|
|
2146 | } |
1851 | } |
2147 | } |
1852 | |
2148 | |
1853 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2149 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2150 | XPUSHs (sv); |
1854 | |
2151 | |
1855 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2152 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1856 | self->incr_nest = 0; |
2153 | self->incr_nest = 0; |
1857 | self->incr_mode = 0; |
2154 | self->incr_mode = 0; |
1858 | |
2155 | |
… | |
… | |
1902 | SvREFCNT_dec (self->incr_text); |
2199 | SvREFCNT_dec (self->incr_text); |
1903 | |
2200 | |
1904 | PROTOTYPES: ENABLE |
2201 | PROTOTYPES: ENABLE |
1905 | |
2202 | |
1906 | void encode_json (SV *scalar) |
2203 | void encode_json (SV *scalar) |
1907 | ALIAS: |
|
|
1908 | to_json_ = 0 |
|
|
1909 | encode_json = F_UTF8 |
|
|
1910 | PPCODE: |
2204 | PPCODE: |
1911 | { |
2205 | { |
1912 | JSON json; |
2206 | JSON json; |
1913 | json_init (&json); |
2207 | json_init (&json); |
1914 | json.flags |= ix; |
2208 | json.flags |= F_UTF8; |
1915 | XPUSHs (encode_json (scalar, &json)); |
2209 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2210 | XPUSHs (scalar); |
1916 | } |
2211 | } |
1917 | |
2212 | |
1918 | void decode_json (SV *jsonstr) |
2213 | void decode_json (SV *jsonstr) |
1919 | ALIAS: |
|
|
1920 | from_json_ = 0 |
|
|
1921 | decode_json = F_UTF8 |
|
|
1922 | PPCODE: |
2214 | PPCODE: |
1923 | { |
2215 | { |
1924 | JSON json; |
2216 | JSON json; |
1925 | json_init (&json); |
2217 | json_init (&json); |
1926 | json.flags |= ix; |
2218 | json.flags |= F_UTF8; |
1927 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2219 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2220 | XPUSHs (jsonstr); |
1928 | } |
2221 | } |
1929 | |
2222 | |
1930 | |
|
|