… | |
… | |
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 |
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 <= 0 && (json)->incr_mode == INCR_M_JSON) |
106 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
107 | |
… | |
… | |
182 | ptr_to_index (SV *sv, char *offset) |
204 | ptr_to_index (SV *sv, char *offset) |
183 | { |
205 | { |
184 | return SvUTF8 (sv) |
206 | return SvUTF8 (sv) |
185 | ? utf8_distance (offset, SvPVX (sv)) |
207 | ? utf8_distance (offset, SvPVX (sv)) |
186 | : 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; |
187 | } |
345 | } |
188 | |
346 | |
189 | ///////////////////////////////////////////////////////////////////////////// |
347 | ///////////////////////////////////////////////////////////////////////////// |
190 | // encoder |
348 | // encoder |
191 | |
349 | |
… | |
… | |
380 | |
538 | |
381 | if (enc->indent >= enc->json.max_depth) |
539 | if (enc->indent >= enc->json.max_depth) |
382 | croak (ERR_NESTING_EXCEEDED); |
540 | croak (ERR_NESTING_EXCEEDED); |
383 | |
541 | |
384 | encode_ch (enc, '['); |
542 | encode_ch (enc, '['); |
385 | |
543 | |
386 | if (len >= 0) |
544 | if (len >= 0) |
387 | { |
545 | { |
388 | encode_nl (enc); ++enc->indent; |
546 | encode_nl (enc); ++enc->indent; |
389 | |
547 | |
390 | for (i = 0; i <= len; ++i) |
548 | for (i = 0; i <= len; ++i) |
… | |
… | |
402 | encode_comma (enc); |
560 | encode_comma (enc); |
403 | } |
561 | } |
404 | |
562 | |
405 | encode_nl (enc); --enc->indent; encode_indent (enc); |
563 | encode_nl (enc); --enc->indent; encode_indent (enc); |
406 | } |
564 | } |
407 | |
565 | |
408 | encode_ch (enc, ']'); |
566 | encode_ch (enc, ']'); |
409 | } |
567 | } |
410 | |
568 | |
411 | static void |
569 | static void |
412 | encode_hk (enc_t *enc, HE *he) |
570 | encode_hk (enc_t *enc, HE *he) |
… | |
… | |
416 | if (HeKLEN (he) == HEf_SVKEY) |
574 | if (HeKLEN (he) == HEf_SVKEY) |
417 | { |
575 | { |
418 | SV *sv = HeSVKEY (he); |
576 | SV *sv = HeSVKEY (he); |
419 | STRLEN len; |
577 | STRLEN len; |
420 | char *str; |
578 | char *str; |
421 | |
579 | |
422 | SvGETMAGIC (sv); |
580 | SvGETMAGIC (sv); |
423 | str = SvPV (sv, len); |
581 | str = SvPV (sv, len); |
424 | |
582 | |
425 | encode_str (enc, str, len, SvUTF8 (sv)); |
583 | encode_str (enc, str, len, SvUTF8 (sv)); |
426 | } |
584 | } |
… | |
… | |
492 | } |
650 | } |
493 | |
651 | |
494 | if (count) |
652 | if (count) |
495 | { |
653 | { |
496 | int i, fast = 1; |
654 | int i, fast = 1; |
497 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
655 | HE *hes_stack [STACK_HES]; |
498 | HE **hes = _alloca (count * sizeof (HE)); |
656 | HE **hes = hes_stack; |
499 | #else |
657 | |
500 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
658 | // allocate larger arrays on the heap |
501 | #endif |
659 | if (count > STACK_HES) |
|
|
660 | { |
|
|
661 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
662 | hes = (HE **)SvPVX (sv); |
|
|
663 | } |
502 | |
664 | |
503 | i = 0; |
665 | i = 0; |
504 | while ((he = hv_iternext (hv))) |
666 | while ((he = hv_iternext (hv))) |
505 | { |
667 | { |
506 | hes [i++] = he; |
668 | hes [i++] = he; |
… | |
… | |
575 | // encode objects, arrays and special \0=false and \1=true values. |
737 | // encode objects, arrays and special \0=false and \1=true values. |
576 | static void |
738 | static void |
577 | encode_rv (enc_t *enc, SV *sv) |
739 | encode_rv (enc_t *enc, SV *sv) |
578 | { |
740 | { |
579 | svtype svt; |
741 | svtype svt; |
|
|
742 | GV *method; |
580 | |
743 | |
581 | SvGETMAGIC (sv); |
744 | SvGETMAGIC (sv); |
582 | svt = SvTYPE (sv); |
745 | svt = SvTYPE (sv); |
583 | |
746 | |
584 | if (expect_false (SvOBJECT (sv))) |
747 | if (expect_false (SvOBJECT (sv))) |
585 | { |
748 | { |
586 | HV *stash = !JSON_SLOW || json_boolean_stash |
749 | HV *stash = SvSTASH (sv); |
587 | ? json_boolean_stash |
|
|
588 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
589 | |
750 | |
590 | if (SvSTASH (sv) == stash) |
751 | if (stash == bool_stash) |
591 | { |
752 | { |
592 | if (SvIV (sv)) |
753 | if (SvIV (sv)) |
593 | encode_str (enc, "true", 4, 0); |
754 | encode_str (enc, "true", 4, 0); |
594 | else |
755 | else |
595 | encode_str (enc, "false", 5, 0); |
756 | encode_str (enc, "false", 5, 0); |
596 | } |
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); |
597 | else |
825 | else |
598 | { |
|
|
599 | #if 0 |
|
|
600 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
601 | { |
|
|
602 | // not yet |
|
|
603 | } |
|
|
604 | #endif |
|
|
605 | if (enc->json.flags & F_CONV_BLESSED) |
|
|
606 | { |
|
|
607 | // we re-bless the reference to get overload and other niceties right |
|
|
608 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
|
|
609 | |
|
|
610 | if (to_json) |
|
|
611 | { |
|
|
612 | dSP; |
|
|
613 | |
|
|
614 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
615 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
616 | |
|
|
617 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
618 | PUTBACK; |
|
|
619 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
620 | SPAGAIN; |
|
|
621 | |
|
|
622 | // catch this surprisingly common error |
|
|
623 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
624 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
625 | |
|
|
626 | sv = POPs; |
|
|
627 | PUTBACK; |
|
|
628 | |
|
|
629 | encode_sv (enc, sv); |
|
|
630 | |
|
|
631 | FREETMPS; LEAVE; |
|
|
632 | } |
|
|
633 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
634 | encode_str (enc, "null", 4, 0); |
|
|
635 | else |
|
|
636 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
637 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
638 | } |
|
|
639 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
640 | encode_str (enc, "null", 4, 0); |
|
|
641 | else |
|
|
642 | 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)", |
643 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
827 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
644 | } |
|
|
645 | } |
828 | } |
646 | else if (svt == SVt_PVHV) |
829 | else if (svt == SVt_PVHV) |
647 | encode_hv (enc, (HV *)sv); |
830 | encode_hv (enc, (HV *)sv); |
648 | else if (svt == SVt_PVAV) |
831 | else if (svt == SVt_PVAV) |
649 | encode_av (enc, (AV *)sv); |
832 | encode_av (enc, (AV *)sv); |
650 | else if (svt < SVt_PVAV) |
833 | else if (svt < SVt_PVAV) |
651 | { |
834 | { |
652 | STRLEN len = 0; |
835 | int bool_type = ref_bool_type (sv); |
653 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
654 | |
836 | |
655 | if (len == 1 && *pv == '1') |
837 | if (bool_type == 1) |
656 | encode_str (enc, "true", 4, 0); |
838 | encode_str (enc, "true", 4, 0); |
657 | else if (len == 1 && *pv == '0') |
839 | else if (bool_type == 0) |
658 | encode_str (enc, "false", 5, 0); |
840 | encode_str (enc, "false", 5, 0); |
659 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
841 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
660 | encode_str (enc, "null", 4, 0); |
842 | encode_str (enc, "null", 4, 0); |
661 | else |
843 | else |
662 | 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", |
… | |
… | |
725 | } |
907 | } |
726 | else |
908 | else |
727 | { |
909 | { |
728 | // large integer, use the (rather slow) snprintf way. |
910 | // large integer, use the (rather slow) snprintf way. |
729 | need (enc, IVUV_MAXCHARS); |
911 | need (enc, IVUV_MAXCHARS); |
730 | enc->cur += |
912 | enc->cur += |
731 | SvIsUV(sv) |
913 | SvIsUV(sv) |
732 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
914 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
733 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
915 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
734 | } |
916 | } |
735 | } |
917 | } |
736 | else if (SvROK (sv)) |
918 | else if (SvROK (sv)) |
737 | encode_rv (enc, SvRV (sv)); |
919 | encode_rv (enc, SvRV (sv)); |
738 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
920 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
739 | encode_str (enc, "null", 4, 0); |
921 | encode_str (enc, "null", 4, 0); |
740 | else |
922 | else |
741 | 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", |
742 | SvPV_nolen (sv), SvFLAGS (sv)); |
924 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
743 | } |
925 | } |
744 | |
926 | |
745 | static SV * |
927 | static SV * |
746 | encode_json (SV *scalar, JSON *json) |
928 | encode_json (SV *scalar, JSON *json) |
747 | { |
929 | { |
748 | enc_t enc; |
930 | enc_t enc; |
749 | |
931 | |
750 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
932 | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
751 | 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)"); |
752 | |
934 | |
753 | enc.json = *json; |
935 | enc.json = *json; |
754 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
936 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
755 | enc.cur = SvPVX (enc.sv); |
937 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
949 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1131 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
950 | *cur++ = ch; |
1132 | *cur++ = ch; |
951 | else if (ch >= 0x80) |
1133 | else if (ch >= 0x80) |
952 | { |
1134 | { |
953 | STRLEN clen; |
1135 | STRLEN clen; |
954 | UV uch; |
|
|
955 | |
1136 | |
956 | --dec_cur; |
1137 | --dec_cur; |
957 | |
1138 | |
958 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
1139 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
959 | if (clen == (STRLEN)-1) |
1140 | if (clen == (STRLEN)-1) |
960 | ERR ("malformed UTF-8 character in JSON string"); |
1141 | ERR ("malformed UTF-8 character in JSON string"); |
961 | |
1142 | |
962 | do |
1143 | do |
963 | *cur++ = *dec_cur++; |
1144 | *cur++ = *dec_cur++; |
964 | while (--clen); |
1145 | while (--clen); |
965 | |
1146 | |
966 | utf8 = 1; |
1147 | utf8 = 1; |
967 | } |
1148 | } |
|
|
1149 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1150 | *cur++ = ch; |
968 | else |
1151 | else |
969 | { |
1152 | { |
970 | --dec_cur; |
1153 | --dec_cur; |
971 | |
1154 | |
972 | if (!ch) |
1155 | if (!ch) |
… | |
… | |
1117 | } |
1300 | } |
1118 | |
1301 | |
1119 | len -= *start == '-' ? 1 : 0; |
1302 | len -= *start == '-' ? 1 : 0; |
1120 | |
1303 | |
1121 | // does not fit into IV or UV, try NV |
1304 | // does not fit into IV or UV, try NV |
1122 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1305 | 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 |
1306 | // fits into NV without loss of precision |
1128 | return newSVnv (Atof (start)); |
1307 | return newSVnv (json_atof (start)); |
1129 | |
1308 | |
1130 | // everything else fails, convert it to a string |
1309 | // everything else fails, convert it to a string |
1131 | return newSVpvn (start, dec->cur - start); |
1310 | return newSVpvn (start, dec->cur - start); |
1132 | } |
1311 | } |
1133 | |
1312 | |
1134 | // loss of precision here |
1313 | // loss of precision here |
1135 | return newSVnv (Atof (start)); |
1314 | return newSVnv (json_atof (start)); |
1136 | |
1315 | |
1137 | fail: |
1316 | fail: |
1138 | return 0; |
1317 | return 0; |
1139 | } |
1318 | } |
1140 | |
1319 | |
… | |
… | |
1164 | if (*dec->cur == ']') |
1343 | if (*dec->cur == ']') |
1165 | { |
1344 | { |
1166 | ++dec->cur; |
1345 | ++dec->cur; |
1167 | break; |
1346 | break; |
1168 | } |
1347 | } |
1169 | |
1348 | |
1170 | if (*dec->cur != ',') |
1349 | if (*dec->cur != ',') |
1171 | ERR (", or ] expected while parsing array"); |
1350 | ERR (", or ] expected while parsing array"); |
1172 | |
1351 | |
1173 | ++dec->cur; |
1352 | ++dec->cur; |
1174 | |
1353 | |
… | |
… | |
1298 | |
1477 | |
1299 | hv_iterinit (hv); |
1478 | hv_iterinit (hv); |
1300 | he = hv_iternext (hv); |
1479 | he = hv_iternext (hv); |
1301 | hv_iterinit (hv); |
1480 | hv_iterinit (hv); |
1302 | |
1481 | |
1303 | // the next line creates a mortal sv each time its called. |
1482 | // the next line creates a mortal sv each time it's called. |
1304 | // might want to optimise this for common cases. |
1483 | // might want to optimise this for common cases. |
1305 | 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); |
1306 | |
1485 | |
1307 | if (cb) |
1486 | if (cb) |
1308 | { |
1487 | { |
1309 | dSP; |
1488 | dSP; |
1310 | int count; |
1489 | int count; |
1311 | |
1490 | |
1312 | ENTER; SAVETMPS; PUSHMARK (SP); |
1491 | ENTER; SAVETMPS; |
|
|
1492 | SAVESTACK_POS (); |
|
|
1493 | PUSHMARK (SP); |
1313 | XPUSHs (HeVAL (he)); |
1494 | XPUSHs (HeVAL (he)); |
|
|
1495 | sv_2mortal (sv); |
1314 | |
1496 | |
1315 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1497 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1316 | |
1498 | |
1317 | if (count == 1) |
1499 | if (count == 1) |
1318 | { |
1500 | { |
1319 | sv = newSVsv (POPs); |
1501 | sv = newSVsv (POPs); |
1320 | FREETMPS; LEAVE; |
1502 | FREETMPS; LEAVE; |
1321 | return sv; |
1503 | return sv; |
1322 | } |
1504 | } |
1323 | |
1505 | |
|
|
1506 | SvREFCNT_inc (sv); |
1324 | FREETMPS; LEAVE; |
1507 | FREETMPS; LEAVE; |
1325 | } |
1508 | } |
1326 | } |
1509 | } |
1327 | |
1510 | |
1328 | if (dec->json.cb_object) |
1511 | if (dec->json.cb_object) |
1329 | { |
1512 | { |
1330 | dSP; |
1513 | dSP; |
1331 | int count; |
1514 | int count; |
1332 | |
1515 | |
1333 | ENTER; SAVETMPS; PUSHMARK (SP); |
1516 | ENTER; SAVETMPS; |
|
|
1517 | SAVESTACK_POS (); |
|
|
1518 | PUSHMARK (SP); |
1334 | XPUSHs (sv_2mortal (sv)); |
1519 | XPUSHs (sv_2mortal (sv)); |
1335 | |
1520 | |
1336 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1521 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1337 | |
1522 | |
1338 | if (count == 1) |
1523 | if (count == 1) |
… | |
… | |
1354 | DEC_DEC_DEPTH; |
1539 | DEC_DEC_DEPTH; |
1355 | return 0; |
1540 | return 0; |
1356 | } |
1541 | } |
1357 | |
1542 | |
1358 | 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 * |
1359 | decode_sv (dec_t *dec) |
1627 | decode_sv (dec_t *dec) |
1360 | { |
1628 | { |
1361 | // the beauty of JSON: you need exactly one character lookahead |
1629 | // the beauty of JSON: you need exactly one character lookahead |
1362 | // to parse everything. |
1630 | // to parse everything. |
1363 | switch (*dec->cur) |
1631 | switch (*dec->cur) |
1364 | { |
1632 | { |
1365 | case '"': ++dec->cur; return decode_str (dec); |
1633 | case '"': ++dec->cur; return decode_str (dec); |
1366 | case '[': ++dec->cur; return decode_av (dec); |
1634 | case '[': ++dec->cur; return decode_av (dec); |
1367 | case '{': ++dec->cur; return decode_hv (dec); |
1635 | case '{': ++dec->cur; return decode_hv (dec); |
|
|
1636 | case '(': return decode_tag (dec); |
1368 | |
1637 | |
1369 | case '-': |
1638 | case '-': |
1370 | case '0': case '1': case '2': case '3': case '4': |
1639 | case '0': case '1': case '2': case '3': case '4': |
1371 | case '5': case '6': case '7': case '8': case '9': |
1640 | case '5': case '6': case '7': case '8': case '9': |
1372 | return decode_num (dec); |
1641 | return decode_num (dec); |
… | |
… | |
1374 | case 't': |
1643 | case 't': |
1375 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1644 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1376 | { |
1645 | { |
1377 | dec->cur += 4; |
1646 | dec->cur += 4; |
1378 | #if JSON_SLOW |
1647 | #if JSON_SLOW |
1379 | json_true = get_bool ("JSON::XS::true"); |
1648 | bool_true = get_bool ("Types::Serialiser::true"); |
1380 | #endif |
1649 | #endif |
1381 | return newSVsv (json_true); |
1650 | return newSVsv (bool_true); |
1382 | } |
1651 | } |
1383 | else |
1652 | else |
1384 | ERR ("'true' expected"); |
1653 | ERR ("'true' expected"); |
1385 | |
1654 | |
1386 | break; |
1655 | break; |
… | |
… | |
1388 | case 'f': |
1657 | case 'f': |
1389 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1658 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1390 | { |
1659 | { |
1391 | dec->cur += 5; |
1660 | dec->cur += 5; |
1392 | #if JSON_SLOW |
1661 | #if JSON_SLOW |
1393 | json_false = get_bool ("JSON::XS::false"); |
1662 | bool_false = get_bool ("Types::Serialiser::false"); |
1394 | #endif |
1663 | #endif |
1395 | return newSVsv (json_false); |
1664 | return newSVsv (bool_false); |
1396 | } |
1665 | } |
1397 | else |
1666 | else |
1398 | ERR ("'false' expected"); |
1667 | ERR ("'false' expected"); |
1399 | |
1668 | |
1400 | break; |
1669 | break; |
… | |
… | |
1409 | ERR ("'null' expected"); |
1678 | ERR ("'null' expected"); |
1410 | |
1679 | |
1411 | break; |
1680 | break; |
1412 | |
1681 | |
1413 | default: |
1682 | default: |
1414 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1683 | ERR ("malformed JSON string, neither tag, array, object, number, string or atom"); |
1415 | break; |
1684 | break; |
1416 | } |
1685 | } |
1417 | |
1686 | |
1418 | fail: |
1687 | fail: |
1419 | return 0; |
1688 | return 0; |
… | |
… | |
1424 | { |
1693 | { |
1425 | dec_t dec; |
1694 | dec_t dec; |
1426 | SV *sv; |
1695 | SV *sv; |
1427 | |
1696 | |
1428 | /* work around bugs in 5.10 where manipulating magic values |
1697 | /* work around bugs in 5.10 where manipulating magic values |
1429 | * will perl ignore the magic in subsequent accesses |
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 | * |
|
|
1702 | * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16), |
|
|
1703 | * as reported by Reini Urban. |
1430 | */ |
1704 | */ |
1431 | /*SvGETMAGIC (string);*/ |
1705 | /*SvGETMAGIC (string);*/ |
1432 | if (SvMAGICAL (string)) |
1706 | if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
1433 | string = sv_2mortal (newSVsv (string)); |
1707 | string = sv_2mortal (newSVsv (string)); |
1434 | |
1708 | |
1435 | SvUPGRADE (string, SVt_PV); |
1709 | SvUPGRADE (string, SVt_PV); |
1436 | |
1710 | |
1437 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1711 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1454 | if (offset > json->max_size && json->max_size) |
1728 | 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", |
1729 | 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); |
1730 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1457 | } |
1731 | } |
1458 | |
1732 | |
1459 | if (json->flags & F_UTF8) |
1733 | if (DECODE_WANTS_OCTETS (json)) |
1460 | sv_utf8_downgrade (string, 0); |
1734 | sv_utf8_downgrade (string, 0); |
1461 | else |
1735 | else |
1462 | sv_utf8_upgrade (string); |
1736 | sv_utf8_upgrade (string); |
1463 | |
1737 | |
1464 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1738 | 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); |
1780 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1507 | LEAVE; |
1781 | LEAVE; |
1508 | |
1782 | |
1509 | croak ("%s, at character offset %d (before \"%s\")", |
1783 | croak ("%s, at character offset %d (before \"%s\")", |
1510 | dec.err, |
1784 | dec.err, |
1511 | ptr_to_index (string, dec.cur), |
1785 | (int)ptr_to_index (string, dec.cur), |
1512 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1786 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1513 | } |
1787 | } |
1514 | |
1788 | |
1515 | sv = sv_2mortal (sv); |
1789 | sv = sv_2mortal (sv); |
1516 | |
1790 | |
1517 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1791 | if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
1518 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1792 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1519 | |
1793 | |
1520 | return sv; |
1794 | return sv; |
1521 | } |
1795 | } |
1522 | |
1796 | |
… | |
… | |
1525 | |
1799 | |
1526 | static void |
1800 | static void |
1527 | incr_parse (JSON *self) |
1801 | incr_parse (JSON *self) |
1528 | { |
1802 | { |
1529 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1803 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1804 | |
|
|
1805 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1806 | // but this would make it slower, so... |
1530 | |
1807 | |
1531 | for (;;) |
1808 | for (;;) |
1532 | { |
1809 | { |
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 |
1810 | //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) |
1811 | switch (self->incr_mode) |
1535 | { |
1812 | { |
1536 | // only used for intiial whitespace skipping |
1813 | // only used for initial whitespace skipping |
1537 | case INCR_M_WS: |
1814 | case INCR_M_WS: |
1538 | for (;;) |
1815 | for (;;) |
1539 | { |
1816 | { |
1540 | if (*p > 0x20) |
1817 | if (*p > 0x20) |
1541 | { |
1818 | { |
|
|
1819 | if (*p == '#') |
|
|
1820 | { |
|
|
1821 | self->incr_mode = INCR_M_C0; |
|
|
1822 | goto incr_m_c; |
|
|
1823 | } |
|
|
1824 | else |
|
|
1825 | { |
1542 | self->incr_mode = INCR_M_JSON; |
1826 | self->incr_mode = INCR_M_JSON; |
1543 | goto incr_m_json; |
1827 | goto incr_m_json; |
|
|
1828 | } |
1544 | } |
1829 | } |
1545 | else if (!*p) |
1830 | else if (!*p) |
1546 | goto interrupt; |
1831 | goto interrupt; |
1547 | |
1832 | |
1548 | ++p; |
1833 | ++p; |
… | |
… | |
1554 | goto interrupt; |
1839 | goto interrupt; |
1555 | |
1840 | |
1556 | ++p; |
1841 | ++p; |
1557 | self->incr_mode = INCR_M_STR; |
1842 | self->incr_mode = INCR_M_STR; |
1558 | goto incr_m_str; |
1843 | goto incr_m_str; |
|
|
1844 | |
|
|
1845 | // inside #-style comments |
|
|
1846 | case INCR_M_C0: |
|
|
1847 | case INCR_M_C1: |
|
|
1848 | incr_m_c: |
|
|
1849 | for (;;) |
|
|
1850 | { |
|
|
1851 | if (*p == '\n') |
|
|
1852 | { |
|
|
1853 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1854 | break; |
|
|
1855 | } |
|
|
1856 | else if (!*p) |
|
|
1857 | goto interrupt; |
|
|
1858 | |
|
|
1859 | ++p; |
|
|
1860 | } |
|
|
1861 | |
|
|
1862 | break; |
1559 | |
1863 | |
1560 | // inside a string |
1864 | // inside a string |
1561 | case INCR_M_STR: |
1865 | case INCR_M_STR: |
1562 | incr_m_str: |
1866 | incr_m_str: |
1563 | for (;;) |
1867 | for (;;) |
… | |
… | |
1614 | self->incr_mode = INCR_M_STR; |
1918 | self->incr_mode = INCR_M_STR; |
1615 | goto incr_m_str; |
1919 | goto incr_m_str; |
1616 | |
1920 | |
1617 | case '[': |
1921 | case '[': |
1618 | case '{': |
1922 | case '{': |
|
|
1923 | case '(': |
1619 | if (++self->incr_nest > self->max_depth) |
1924 | if (++self->incr_nest > self->max_depth) |
1620 | croak (ERR_NESTING_EXCEEDED); |
1925 | croak (ERR_NESTING_EXCEEDED); |
1621 | break; |
1926 | break; |
1622 | |
1927 | |
1623 | case ']': |
1928 | case ']': |
1624 | case '}': |
1929 | case '}': |
1625 | if (--self->incr_nest <= 0) |
1930 | if (--self->incr_nest <= 0) |
1626 | goto interrupt; |
1931 | goto interrupt; |
|
|
1932 | break; |
|
|
1933 | |
|
|
1934 | case ')': |
|
|
1935 | --self->incr_nest; |
|
|
1936 | break; |
|
|
1937 | |
|
|
1938 | case '#': |
|
|
1939 | self->incr_mode = INCR_M_C1; |
|
|
1940 | goto incr_m_c; |
1627 | } |
1941 | } |
1628 | } |
1942 | } |
1629 | } |
1943 | } |
1630 | |
1944 | |
1631 | modechange: |
1945 | modechange: |
1632 | ; |
1946 | ; |
1633 | } |
1947 | } |
1634 | |
1948 | |
1635 | interrupt: |
1949 | interrupt: |
1636 | self->incr_pos = p - SvPVX (self->incr_text); |
1950 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1951 | //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 |
1952 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1638 | } |
1953 | } |
1639 | |
1954 | |
1640 | ///////////////////////////////////////////////////////////////////////////// |
1955 | ///////////////////////////////////////////////////////////////////////////// |
1641 | // XS interface functions |
1956 | // XS interface functions |
… | |
… | |
1651 | i >= '0' && i <= '9' ? i - '0' |
1966 | i >= '0' && i <= '9' ? i - '0' |
1652 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1967 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1653 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1968 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1654 | : -1; |
1969 | : -1; |
1655 | |
1970 | |
1656 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1971 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1657 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1972 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1658 | |
|
|
1659 | json_true = get_bool ("JSON::XS::true"); |
1973 | bool_true = get_bool ("Types::Serialiser::true"); |
1660 | json_false = get_bool ("JSON::XS::false"); |
1974 | bool_false = get_bool ("Types::Serialiser::false"); |
|
|
1975 | |
|
|
1976 | sv_json = newSVpv ("JSON", 0); |
|
|
1977 | SvREADONLY_on (sv_json); |
1661 | |
1978 | |
1662 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1979 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1663 | } |
1980 | } |
1664 | |
1981 | |
1665 | PROTOTYPES: DISABLE |
1982 | PROTOTYPES: DISABLE |
1666 | |
1983 | |
1667 | void CLONE (...) |
1984 | void CLONE (...) |
1668 | CODE: |
1985 | CODE: |
1669 | json_stash = 0; |
1986 | json_stash = 0; |
1670 | json_boolean_stash = 0; |
1987 | bool_stash = 0; |
1671 | |
1988 | |
1672 | void new (char *klass) |
1989 | void new (char *klass) |
1673 | PPCODE: |
1990 | PPCODE: |
1674 | { |
1991 | { |
1675 | SV *pv = NEWSV (0, sizeof (JSON)); |
1992 | SV *pv = NEWSV (0, sizeof (JSON)); |
1676 | SvPOK_only (pv); |
1993 | SvPOK_only (pv); |
1677 | json_init ((JSON *)SvPVX (pv)); |
1994 | json_init ((JSON *)SvPVX (pv)); |
1678 | XPUSHs (sv_2mortal (sv_bless ( |
1995 | XPUSHs (sv_2mortal (sv_bless ( |
1679 | newRV_noinc (pv), |
1996 | newRV_noinc (pv), |
1680 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1997 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
1695 | shrink = F_SHRINK |
2012 | shrink = F_SHRINK |
1696 | allow_blessed = F_ALLOW_BLESSED |
2013 | allow_blessed = F_ALLOW_BLESSED |
1697 | convert_blessed = F_CONV_BLESSED |
2014 | convert_blessed = F_CONV_BLESSED |
1698 | relaxed = F_RELAXED |
2015 | relaxed = F_RELAXED |
1699 | allow_unknown = F_ALLOW_UNKNOWN |
2016 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
2017 | allow_tags = F_ALLOW_TAGS |
1700 | PPCODE: |
2018 | PPCODE: |
1701 | { |
2019 | { |
1702 | if (enable) |
2020 | if (enable) |
1703 | self->flags |= ix; |
2021 | self->flags |= ix; |
1704 | else |
2022 | else |
… | |
… | |
1720 | get_shrink = F_SHRINK |
2038 | get_shrink = F_SHRINK |
1721 | get_allow_blessed = F_ALLOW_BLESSED |
2039 | get_allow_blessed = F_ALLOW_BLESSED |
1722 | get_convert_blessed = F_CONV_BLESSED |
2040 | get_convert_blessed = F_CONV_BLESSED |
1723 | get_relaxed = F_RELAXED |
2041 | get_relaxed = F_RELAXED |
1724 | get_allow_unknown = F_ALLOW_UNKNOWN |
2042 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
2043 | get_allow_tags = F_ALLOW_TAGS |
1725 | PPCODE: |
2044 | PPCODE: |
1726 | XPUSHs (boolSV (self->flags & ix)); |
2045 | XPUSHs (boolSV (self->flags & ix)); |
1727 | |
2046 | |
1728 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
2047 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1729 | PPCODE: |
2048 | PPCODE: |
… | |
… | |
1757 | } |
2076 | } |
1758 | |
2077 | |
1759 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
2078 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1760 | PPCODE: |
2079 | PPCODE: |
1761 | { |
2080 | { |
1762 | if (!self->cb_sk_object) |
2081 | if (!self->cb_sk_object) |
1763 | self->cb_sk_object = newHV (); |
2082 | self->cb_sk_object = newHV (); |
1764 | |
2083 | |
1765 | if (SvOK (cb)) |
2084 | if (SvOK (cb)) |
1766 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
2085 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1767 | else |
2086 | else |
… | |
… | |
1778 | XPUSHs (ST (0)); |
2097 | XPUSHs (ST (0)); |
1779 | } |
2098 | } |
1780 | |
2099 | |
1781 | void encode (JSON *self, SV *scalar) |
2100 | void encode (JSON *self, SV *scalar) |
1782 | PPCODE: |
2101 | PPCODE: |
1783 | XPUSHs (encode_json (scalar, self)); |
2102 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
2103 | XPUSHs (scalar); |
1784 | |
2104 | |
1785 | void decode (JSON *self, SV *jsonstr) |
2105 | void decode (JSON *self, SV *jsonstr) |
1786 | PPCODE: |
2106 | PPCODE: |
1787 | XPUSHs (decode_json (jsonstr, self, 0)); |
2107 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
2108 | XPUSHs (jsonstr); |
1788 | |
2109 | |
1789 | void decode_prefix (JSON *self, SV *jsonstr) |
2110 | void decode_prefix (JSON *self, SV *jsonstr) |
1790 | PPCODE: |
2111 | PPCODE: |
1791 | { |
2112 | { |
|
|
2113 | SV *sv; |
1792 | char *offset; |
2114 | char *offset; |
|
|
2115 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1793 | EXTEND (SP, 2); |
2116 | EXTEND (SP, 2); |
1794 | PUSHs (decode_json (jsonstr, self, &offset)); |
2117 | PUSHs (sv); |
1795 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
2118 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1796 | } |
2119 | } |
1797 | |
2120 | |
1798 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2121 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1799 | PPCODE: |
2122 | PPCODE: |
1800 | { |
2123 | { |
1801 | if (!self->incr_text) |
2124 | if (!self->incr_text) |
1802 | self->incr_text = newSVpvn ("", 0); |
2125 | self->incr_text = newSVpvn ("", 0); |
|
|
2126 | |
|
|
2127 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
2128 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
2129 | if (DECODE_WANTS_OCTETS (self)) |
|
|
2130 | { |
|
|
2131 | if (self->incr_pos) |
|
|
2132 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
2133 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
2134 | |
|
|
2135 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
2136 | } |
|
|
2137 | else |
|
|
2138 | { |
|
|
2139 | sv_utf8_upgrade (self->incr_text); |
|
|
2140 | |
|
|
2141 | if (self->incr_pos) |
|
|
2142 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
2143 | - (U8 *)SvPVX (self->incr_text); |
|
|
2144 | } |
1803 | |
2145 | |
1804 | // append data, if any |
2146 | // append data, if any |
1805 | if (jsonstr) |
2147 | if (jsonstr) |
1806 | { |
2148 | { |
|
|
2149 | /* make sure both strings have same encoding */ |
|
|
2150 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
1807 | if (SvUTF8 (jsonstr)) |
2151 | if (SvUTF8 (jsonstr)) |
|
|
2152 | sv_utf8_downgrade (jsonstr, 0); |
1808 | { |
2153 | 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); |
2154 | sv_utf8_upgrade (jsonstr); |
1821 | |
2155 | |
|
|
2156 | /* and then just blindly append */ |
1822 | { |
2157 | { |
1823 | STRLEN len; |
2158 | STRLEN len; |
1824 | const char *str = SvPV (jsonstr, len); |
2159 | const char *str = SvPV (jsonstr, len); |
1825 | STRLEN cur = SvCUR (self->incr_text); |
2160 | STRLEN cur = SvCUR (self->incr_text); |
1826 | |
2161 | |
… | |
… | |
1834 | } |
2169 | } |
1835 | |
2170 | |
1836 | if (GIMME_V != G_VOID) |
2171 | if (GIMME_V != G_VOID) |
1837 | do |
2172 | do |
1838 | { |
2173 | { |
|
|
2174 | SV *sv; |
1839 | char *offset; |
2175 | char *offset; |
1840 | |
2176 | |
1841 | if (!INCR_DONE (self)) |
2177 | if (!INCR_DONE (self)) |
1842 | { |
2178 | { |
1843 | incr_parse (self); |
2179 | incr_parse (self); |
… | |
… | |
1845 | if (self->incr_pos > self->max_size && self->max_size) |
2181 | 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", |
2182 | 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); |
2183 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1848 | |
2184 | |
1849 | if (!INCR_DONE (self)) |
2185 | if (!INCR_DONE (self)) |
|
|
2186 | { |
|
|
2187 | // as an optimisation, do not accumulate white space in the incr buffer |
|
|
2188 | if (self->incr_mode == INCR_M_WS && self->incr_pos) |
|
|
2189 | { |
|
|
2190 | self->incr_pos = 0; |
|
|
2191 | SvCUR_set (self->incr_text, 0); |
|
|
2192 | } |
|
|
2193 | |
1850 | break; |
2194 | break; |
|
|
2195 | } |
1851 | } |
2196 | } |
1852 | |
2197 | |
1853 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2198 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2199 | XPUSHs (sv); |
1854 | |
2200 | |
1855 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2201 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1856 | self->incr_nest = 0; |
2202 | self->incr_nest = 0; |
1857 | self->incr_mode = 0; |
2203 | self->incr_mode = 0; |
1858 | |
2204 | |
… | |
… | |
1902 | SvREFCNT_dec (self->incr_text); |
2248 | SvREFCNT_dec (self->incr_text); |
1903 | |
2249 | |
1904 | PROTOTYPES: ENABLE |
2250 | PROTOTYPES: ENABLE |
1905 | |
2251 | |
1906 | void encode_json (SV *scalar) |
2252 | void encode_json (SV *scalar) |
1907 | ALIAS: |
|
|
1908 | to_json_ = 0 |
|
|
1909 | encode_json = F_UTF8 |
|
|
1910 | PPCODE: |
2253 | PPCODE: |
1911 | { |
2254 | { |
1912 | JSON json; |
2255 | JSON json; |
1913 | json_init (&json); |
2256 | json_init (&json); |
1914 | json.flags |= ix; |
2257 | json.flags |= F_UTF8; |
1915 | XPUSHs (encode_json (scalar, &json)); |
2258 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2259 | XPUSHs (scalar); |
1916 | } |
2260 | } |
1917 | |
2261 | |
1918 | void decode_json (SV *jsonstr) |
2262 | void decode_json (SV *jsonstr) |
1919 | ALIAS: |
|
|
1920 | from_json_ = 0 |
|
|
1921 | decode_json = F_UTF8 |
|
|
1922 | PPCODE: |
2263 | PPCODE: |
1923 | { |
2264 | { |
1924 | JSON json; |
2265 | JSON json; |
1925 | json_init (&json); |
2266 | json_init (&json); |
1926 | json.flags |= ix; |
2267 | json.flags |= F_UTF8; |
1927 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2268 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2269 | XPUSHs (jsonstr); |
1928 | } |
2270 | } |
1929 | |
2271 | |
1930 | |
|
|