… | |
… | |
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 |
… | |
… | |
65 | #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?)" |
66 | |
80 | |
67 | #ifdef USE_ITHREADS |
81 | #ifdef USE_ITHREADS |
68 | # define JSON_SLOW 1 |
82 | # define JSON_SLOW 1 |
69 | # 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)) |
70 | #else |
85 | #else |
71 | # define JSON_SLOW 0 |
86 | # define JSON_SLOW 0 |
72 | # define JSON_STASH json_stash |
87 | # define JSON_STASH json_stash |
|
|
88 | # define BOOL_STASH bool_stash |
73 | #endif |
89 | #endif |
74 | |
90 | |
75 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
91 | // the amount of HEs to allocate on the stack, when sorting keys |
76 | 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; |
77 | |
96 | |
78 | enum { |
97 | enum { |
79 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
98 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
80 | INCR_M_STR, // inside string |
99 | INCR_M_STR, // inside string |
81 | 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 |
82 | INCR_M_JSON // outside anything, count nesting |
103 | INCR_M_JSON // outside anything, count nesting |
83 | }; |
104 | }; |
84 | |
105 | |
85 | #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) |
86 | |
107 | |
… | |
… | |
183 | ptr_to_index (SV *sv, char *offset) |
204 | ptr_to_index (SV *sv, char *offset) |
184 | { |
205 | { |
185 | return SvUTF8 (sv) |
206 | return SvUTF8 (sv) |
186 | ? utf8_distance (offset, SvPVX (sv)) |
207 | ? utf8_distance (offset, SvPVX (sv)) |
187 | : 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 | return -1; |
|
|
326 | } |
|
|
327 | |
|
|
328 | // returns whether scalar is not a reference in the sense of allow_nonref |
|
|
329 | static int |
|
|
330 | json_nonref (SV *scalar) |
|
|
331 | { |
|
|
332 | if (!SvROK (scalar)) |
|
|
333 | return 1; |
|
|
334 | |
|
|
335 | scalar = SvRV (scalar); |
|
|
336 | |
|
|
337 | if (SvTYPE (scalar) >= SVt_PVMG) |
|
|
338 | { |
|
|
339 | if (SvSTASH (scalar) == bool_stash) |
|
|
340 | return 1; |
|
|
341 | |
|
|
342 | if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
|
|
343 | return 1; |
|
|
344 | } |
|
|
345 | |
|
|
346 | return 0; |
188 | } |
347 | } |
189 | |
348 | |
190 | ///////////////////////////////////////////////////////////////////////////// |
349 | ///////////////////////////////////////////////////////////////////////////// |
191 | // encoder |
350 | // encoder |
192 | |
351 | |
… | |
… | |
381 | |
540 | |
382 | if (enc->indent >= enc->json.max_depth) |
541 | if (enc->indent >= enc->json.max_depth) |
383 | croak (ERR_NESTING_EXCEEDED); |
542 | croak (ERR_NESTING_EXCEEDED); |
384 | |
543 | |
385 | encode_ch (enc, '['); |
544 | encode_ch (enc, '['); |
386 | |
545 | |
387 | if (len >= 0) |
546 | if (len >= 0) |
388 | { |
547 | { |
389 | encode_nl (enc); ++enc->indent; |
548 | encode_nl (enc); ++enc->indent; |
390 | |
549 | |
391 | for (i = 0; i <= len; ++i) |
550 | for (i = 0; i <= len; ++i) |
… | |
… | |
403 | encode_comma (enc); |
562 | encode_comma (enc); |
404 | } |
563 | } |
405 | |
564 | |
406 | encode_nl (enc); --enc->indent; encode_indent (enc); |
565 | encode_nl (enc); --enc->indent; encode_indent (enc); |
407 | } |
566 | } |
408 | |
567 | |
409 | encode_ch (enc, ']'); |
568 | encode_ch (enc, ']'); |
410 | } |
569 | } |
411 | |
570 | |
412 | static void |
571 | static void |
413 | encode_hk (enc_t *enc, HE *he) |
572 | encode_hk (enc_t *enc, HE *he) |
… | |
… | |
417 | if (HeKLEN (he) == HEf_SVKEY) |
576 | if (HeKLEN (he) == HEf_SVKEY) |
418 | { |
577 | { |
419 | SV *sv = HeSVKEY (he); |
578 | SV *sv = HeSVKEY (he); |
420 | STRLEN len; |
579 | STRLEN len; |
421 | char *str; |
580 | char *str; |
422 | |
581 | |
423 | SvGETMAGIC (sv); |
582 | SvGETMAGIC (sv); |
424 | str = SvPV (sv, len); |
583 | str = SvPV (sv, len); |
425 | |
584 | |
426 | encode_str (enc, str, len, SvUTF8 (sv)); |
585 | encode_str (enc, str, len, SvUTF8 (sv)); |
427 | } |
586 | } |
… | |
… | |
493 | } |
652 | } |
494 | |
653 | |
495 | if (count) |
654 | if (count) |
496 | { |
655 | { |
497 | int i, fast = 1; |
656 | int i, fast = 1; |
498 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
657 | HE *hes_stack [STACK_HES]; |
499 | HE **hes = _alloca (count * sizeof (HE)); |
658 | HE **hes = hes_stack; |
500 | #else |
659 | |
501 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
660 | // allocate larger arrays on the heap |
502 | #endif |
661 | if (count > STACK_HES) |
|
|
662 | { |
|
|
663 | SV *sv = sv_2mortal (NEWSV (0, count * sizeof (*hes))); |
|
|
664 | hes = (HE **)SvPVX (sv); |
|
|
665 | } |
503 | |
666 | |
504 | i = 0; |
667 | i = 0; |
505 | while ((he = hv_iternext (hv))) |
668 | while ((he = hv_iternext (hv))) |
506 | { |
669 | { |
507 | hes [i++] = he; |
670 | hes [i++] = he; |
… | |
… | |
576 | // encode objects, arrays and special \0=false and \1=true values. |
739 | // encode objects, arrays and special \0=false and \1=true values. |
577 | static void |
740 | static void |
578 | encode_rv (enc_t *enc, SV *sv) |
741 | encode_rv (enc_t *enc, SV *sv) |
579 | { |
742 | { |
580 | svtype svt; |
743 | svtype svt; |
|
|
744 | GV *method; |
581 | |
745 | |
582 | SvGETMAGIC (sv); |
746 | SvGETMAGIC (sv); |
583 | svt = SvTYPE (sv); |
747 | svt = SvTYPE (sv); |
584 | |
748 | |
585 | if (expect_false (SvOBJECT (sv))) |
749 | if (expect_false (SvOBJECT (sv))) |
586 | { |
750 | { |
587 | HV *stash = !JSON_SLOW || json_boolean_stash |
751 | HV *stash = SvSTASH (sv); |
588 | ? json_boolean_stash |
|
|
589 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
590 | |
752 | |
591 | if (SvSTASH (sv) == stash) |
753 | if (stash == bool_stash) |
592 | { |
754 | { |
593 | if (SvIV (sv)) |
755 | if (SvIV (sv)) |
594 | encode_str (enc, "true", 4, 0); |
756 | encode_str (enc, "true", 4, 0); |
595 | else |
757 | else |
596 | encode_str (enc, "false", 5, 0); |
758 | encode_str (enc, "false", 5, 0); |
597 | } |
759 | } |
|
|
760 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
|
|
761 | { |
|
|
762 | int count; |
|
|
763 | dSP; |
|
|
764 | |
|
|
765 | ENTER; SAVETMPS; |
|
|
766 | SAVESTACK_POS (); |
|
|
767 | PUSHMARK (SP); |
|
|
768 | EXTEND (SP, 2); |
|
|
769 | // we re-bless the reference to get overload and other niceties right |
|
|
770 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
771 | PUSHs (sv_json); |
|
|
772 | |
|
|
773 | PUTBACK; |
|
|
774 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
775 | SPAGAIN; |
|
|
776 | |
|
|
777 | // catch this surprisingly common error |
|
|
778 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
779 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
780 | |
|
|
781 | encode_ch (enc, '('); |
|
|
782 | encode_ch (enc, '"'); |
|
|
783 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
784 | encode_ch (enc, '"'); |
|
|
785 | encode_ch (enc, ')'); |
|
|
786 | encode_ch (enc, '['); |
|
|
787 | |
|
|
788 | while (count) |
|
|
789 | { |
|
|
790 | encode_sv (enc, SP[1 - count--]); |
|
|
791 | |
|
|
792 | if (count) |
|
|
793 | encode_ch (enc, ','); |
|
|
794 | } |
|
|
795 | |
|
|
796 | encode_ch (enc, ']'); |
|
|
797 | |
|
|
798 | FREETMPS; LEAVE; |
|
|
799 | } |
|
|
800 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
|
|
801 | { |
|
|
802 | dSP; |
|
|
803 | |
|
|
804 | ENTER; SAVETMPS; |
|
|
805 | PUSHMARK (SP); |
|
|
806 | // we re-bless the reference to get overload and other niceties right |
|
|
807 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
808 | |
|
|
809 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
810 | PUTBACK; |
|
|
811 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
812 | SPAGAIN; |
|
|
813 | |
|
|
814 | // catch this surprisingly common error |
|
|
815 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
816 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
817 | |
|
|
818 | sv = POPs; |
|
|
819 | PUTBACK; |
|
|
820 | |
|
|
821 | encode_sv (enc, sv); |
|
|
822 | |
|
|
823 | FREETMPS; LEAVE; |
|
|
824 | } |
|
|
825 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
826 | encode_str (enc, "null", 4, 0); |
598 | else |
827 | 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", |
828 | 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)))); |
829 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
645 | } |
|
|
646 | } |
830 | } |
647 | else if (svt == SVt_PVHV) |
831 | else if (svt == SVt_PVHV) |
648 | encode_hv (enc, (HV *)sv); |
832 | encode_hv (enc, (HV *)sv); |
649 | else if (svt == SVt_PVAV) |
833 | else if (svt == SVt_PVAV) |
650 | encode_av (enc, (AV *)sv); |
834 | encode_av (enc, (AV *)sv); |
651 | else if (svt < SVt_PVAV) |
835 | else if (svt < SVt_PVAV) |
652 | { |
836 | { |
653 | STRLEN len = 0; |
837 | int bool_type = ref_bool_type (sv); |
654 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
655 | |
838 | |
656 | if (len == 1 && *pv == '1') |
839 | if (bool_type == 1) |
657 | encode_str (enc, "true", 4, 0); |
840 | encode_str (enc, "true", 4, 0); |
658 | else if (len == 1 && *pv == '0') |
841 | else if (bool_type == 0) |
659 | encode_str (enc, "false", 5, 0); |
842 | encode_str (enc, "false", 5, 0); |
660 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
843 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
661 | encode_str (enc, "null", 4, 0); |
844 | encode_str (enc, "null", 4, 0); |
662 | else |
845 | else |
663 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
846 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
… | |
… | |
726 | } |
909 | } |
727 | else |
910 | else |
728 | { |
911 | { |
729 | // large integer, use the (rather slow) snprintf way. |
912 | // large integer, use the (rather slow) snprintf way. |
730 | need (enc, IVUV_MAXCHARS); |
913 | need (enc, IVUV_MAXCHARS); |
731 | enc->cur += |
914 | enc->cur += |
732 | SvIsUV(sv) |
915 | SvIsUV(sv) |
733 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
916 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
734 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
917 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
735 | } |
918 | } |
736 | } |
919 | } |
737 | else if (SvROK (sv)) |
920 | else if (SvROK (sv)) |
738 | encode_rv (enc, SvRV (sv)); |
921 | encode_rv (enc, SvRV (sv)); |
739 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
922 | else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) |
740 | encode_str (enc, "null", 4, 0); |
923 | encode_str (enc, "null", 4, 0); |
741 | else |
924 | else |
742 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
925 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, check your input data", |
743 | SvPV_nolen (sv), SvFLAGS (sv)); |
926 | SvPV_nolen (sv), (unsigned int)SvFLAGS (sv)); |
744 | } |
927 | } |
745 | |
928 | |
746 | static SV * |
929 | static SV * |
747 | encode_json (SV *scalar, JSON *json) |
930 | encode_json (SV *scalar, JSON *json) |
748 | { |
931 | { |
749 | enc_t enc; |
932 | enc_t enc; |
750 | |
933 | |
751 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
934 | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
752 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
935 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
753 | |
936 | |
754 | enc.json = *json; |
937 | enc.json = *json; |
755 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
938 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
756 | enc.cur = SvPVX (enc.sv); |
939 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
963 | *cur++ = *dec_cur++; |
1146 | *cur++ = *dec_cur++; |
964 | while (--clen); |
1147 | while (--clen); |
965 | |
1148 | |
966 | utf8 = 1; |
1149 | utf8 = 1; |
967 | } |
1150 | } |
|
|
1151 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1152 | *cur++ = ch; |
968 | else |
1153 | else |
969 | { |
1154 | { |
970 | --dec_cur; |
1155 | --dec_cur; |
971 | |
1156 | |
972 | if (!ch) |
1157 | if (!ch) |
… | |
… | |
1117 | } |
1302 | } |
1118 | |
1303 | |
1119 | len -= *start == '-' ? 1 : 0; |
1304 | len -= *start == '-' ? 1 : 0; |
1120 | |
1305 | |
1121 | // does not fit into IV or UV, try NV |
1306 | // does not fit into IV or UV, try NV |
1122 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1307 | 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 |
1308 | // fits into NV without loss of precision |
1128 | return newSVnv (Atof (start)); |
1309 | return newSVnv (json_atof (start)); |
1129 | |
1310 | |
1130 | // everything else fails, convert it to a string |
1311 | // everything else fails, convert it to a string |
1131 | return newSVpvn (start, dec->cur - start); |
1312 | return newSVpvn (start, dec->cur - start); |
1132 | } |
1313 | } |
1133 | |
1314 | |
1134 | // loss of precision here |
1315 | // loss of precision here |
1135 | return newSVnv (Atof (start)); |
1316 | return newSVnv (json_atof (start)); |
1136 | |
1317 | |
1137 | fail: |
1318 | fail: |
1138 | return 0; |
1319 | return 0; |
1139 | } |
1320 | } |
1140 | |
1321 | |
… | |
… | |
1164 | if (*dec->cur == ']') |
1345 | if (*dec->cur == ']') |
1165 | { |
1346 | { |
1166 | ++dec->cur; |
1347 | ++dec->cur; |
1167 | break; |
1348 | break; |
1168 | } |
1349 | } |
1169 | |
1350 | |
1170 | if (*dec->cur != ',') |
1351 | if (*dec->cur != ',') |
1171 | ERR (", or ] expected while parsing array"); |
1352 | ERR (", or ] expected while parsing array"); |
1172 | |
1353 | |
1173 | ++dec->cur; |
1354 | ++dec->cur; |
1174 | |
1355 | |
… | |
… | |
1298 | |
1479 | |
1299 | hv_iterinit (hv); |
1480 | hv_iterinit (hv); |
1300 | he = hv_iternext (hv); |
1481 | he = hv_iternext (hv); |
1301 | hv_iterinit (hv); |
1482 | hv_iterinit (hv); |
1302 | |
1483 | |
1303 | // the next line creates a mortal sv each time its called. |
1484 | // the next line creates a mortal sv each time it's called. |
1304 | // might want to optimise this for common cases. |
1485 | // might want to optimise this for common cases. |
1305 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1486 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1306 | |
1487 | |
1307 | if (cb) |
1488 | if (cb) |
1308 | { |
1489 | { |
1309 | dSP; |
1490 | dSP; |
1310 | int count; |
1491 | int count; |
1311 | |
1492 | |
1312 | ENTER; SAVETMPS; PUSHMARK (SP); |
1493 | ENTER; SAVETMPS; |
|
|
1494 | SAVESTACK_POS (); |
|
|
1495 | PUSHMARK (SP); |
1313 | XPUSHs (HeVAL (he)); |
1496 | XPUSHs (HeVAL (he)); |
|
|
1497 | sv_2mortal (sv); |
1314 | |
1498 | |
1315 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1499 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1316 | |
1500 | |
1317 | if (count == 1) |
1501 | if (count == 1) |
1318 | { |
1502 | { |
1319 | sv = newSVsv (POPs); |
1503 | sv = newSVsv (POPs); |
1320 | FREETMPS; LEAVE; |
1504 | FREETMPS; LEAVE; |
1321 | return sv; |
1505 | return sv; |
1322 | } |
1506 | } |
1323 | |
1507 | |
|
|
1508 | SvREFCNT_inc (sv); |
1324 | FREETMPS; LEAVE; |
1509 | FREETMPS; LEAVE; |
1325 | } |
1510 | } |
1326 | } |
1511 | } |
1327 | |
1512 | |
1328 | if (dec->json.cb_object) |
1513 | if (dec->json.cb_object) |
1329 | { |
1514 | { |
1330 | dSP; |
1515 | dSP; |
1331 | int count; |
1516 | int count; |
1332 | |
1517 | |
1333 | ENTER; SAVETMPS; PUSHMARK (SP); |
1518 | ENTER; SAVETMPS; |
|
|
1519 | SAVESTACK_POS (); |
|
|
1520 | PUSHMARK (SP); |
1334 | XPUSHs (sv_2mortal (sv)); |
1521 | XPUSHs (sv_2mortal (sv)); |
1335 | |
1522 | |
1336 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1523 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1337 | |
1524 | |
1338 | if (count == 1) |
1525 | if (count == 1) |
… | |
… | |
1354 | DEC_DEC_DEPTH; |
1541 | DEC_DEC_DEPTH; |
1355 | return 0; |
1542 | return 0; |
1356 | } |
1543 | } |
1357 | |
1544 | |
1358 | static SV * |
1545 | static SV * |
|
|
1546 | decode_tag (dec_t *dec) |
|
|
1547 | { |
|
|
1548 | SV *tag = 0; |
|
|
1549 | SV *val = 0; |
|
|
1550 | |
|
|
1551 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
|
|
1552 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
|
|
1553 | |
|
|
1554 | ++dec->cur; |
|
|
1555 | |
|
|
1556 | decode_ws (dec); |
|
|
1557 | |
|
|
1558 | tag = decode_sv (dec); |
|
|
1559 | if (!tag) |
|
|
1560 | goto fail; |
|
|
1561 | |
|
|
1562 | if (!SvPOK (tag)) |
|
|
1563 | ERR ("malformed JSON string, (tag) must be a string"); |
|
|
1564 | |
|
|
1565 | decode_ws (dec); |
|
|
1566 | |
|
|
1567 | if (*dec->cur != ')') |
|
|
1568 | ERR (") expected after tag"); |
|
|
1569 | |
|
|
1570 | ++dec->cur; |
|
|
1571 | |
|
|
1572 | decode_ws (dec); |
|
|
1573 | |
|
|
1574 | val = decode_sv (dec); |
|
|
1575 | if (!val) |
|
|
1576 | goto fail; |
|
|
1577 | |
|
|
1578 | if (!SvROK (val) || SvTYPE (SvRV (val)) != SVt_PVAV) |
|
|
1579 | ERR ("malformed JSON string, tag value must be an array"); |
|
|
1580 | |
|
|
1581 | { |
|
|
1582 | AV *av = (AV *)SvRV (val); |
|
|
1583 | int i, len = av_len (av) + 1; |
|
|
1584 | HV *stash = gv_stashsv (tag, 0); |
|
|
1585 | SV *sv; |
|
|
1586 | |
|
|
1587 | if (!stash) |
|
|
1588 | ERR ("cannot decode perl-object (package does not exist)"); |
|
|
1589 | |
|
|
1590 | GV *method = gv_fetchmethod_autoload (stash, "THAW", 0); |
|
|
1591 | |
|
|
1592 | if (!method) |
|
|
1593 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
|
|
1594 | |
|
|
1595 | dSP; |
|
|
1596 | |
|
|
1597 | ENTER; SAVETMPS; |
|
|
1598 | PUSHMARK (SP); |
|
|
1599 | EXTEND (SP, len + 2); |
|
|
1600 | // we re-bless the reference to get overload and other niceties right |
|
|
1601 | PUSHs (tag); |
|
|
1602 | PUSHs (sv_json); |
|
|
1603 | |
|
|
1604 | for (i = 0; i < len; ++i) |
|
|
1605 | PUSHs (*av_fetch (av, i, 1)); |
|
|
1606 | |
|
|
1607 | PUTBACK; |
|
|
1608 | call_sv ((SV *)GvCV (method), G_SCALAR); |
|
|
1609 | SPAGAIN; |
|
|
1610 | |
|
|
1611 | SvREFCNT_dec (tag); |
|
|
1612 | SvREFCNT_dec (val); |
|
|
1613 | sv = SvREFCNT_inc (POPs); |
|
|
1614 | |
|
|
1615 | PUTBACK; |
|
|
1616 | |
|
|
1617 | FREETMPS; LEAVE; |
|
|
1618 | |
|
|
1619 | return sv; |
|
|
1620 | } |
|
|
1621 | |
|
|
1622 | fail: |
|
|
1623 | SvREFCNT_dec (tag); |
|
|
1624 | SvREFCNT_dec (val); |
|
|
1625 | return 0; |
|
|
1626 | } |
|
|
1627 | |
|
|
1628 | static SV * |
1359 | decode_sv (dec_t *dec) |
1629 | decode_sv (dec_t *dec) |
1360 | { |
1630 | { |
1361 | // the beauty of JSON: you need exactly one character lookahead |
1631 | // the beauty of JSON: you need exactly one character lookahead |
1362 | // to parse everything. |
1632 | // to parse everything. |
1363 | switch (*dec->cur) |
1633 | switch (*dec->cur) |
1364 | { |
1634 | { |
1365 | case '"': ++dec->cur; return decode_str (dec); |
1635 | case '"': ++dec->cur; return decode_str (dec); |
1366 | case '[': ++dec->cur; return decode_av (dec); |
1636 | case '[': ++dec->cur; return decode_av (dec); |
1367 | case '{': ++dec->cur; return decode_hv (dec); |
1637 | case '{': ++dec->cur; return decode_hv (dec); |
|
|
1638 | case '(': return decode_tag (dec); |
1368 | |
1639 | |
1369 | case '-': |
1640 | case '-': |
1370 | case '0': case '1': case '2': case '3': case '4': |
1641 | case '0': case '1': case '2': case '3': case '4': |
1371 | case '5': case '6': case '7': case '8': case '9': |
1642 | case '5': case '6': case '7': case '8': case '9': |
1372 | return decode_num (dec); |
1643 | return decode_num (dec); |
… | |
… | |
1374 | case 't': |
1645 | case 't': |
1375 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1646 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1376 | { |
1647 | { |
1377 | dec->cur += 4; |
1648 | dec->cur += 4; |
1378 | #if JSON_SLOW |
1649 | #if JSON_SLOW |
1379 | json_true = get_bool ("JSON::XS::true"); |
1650 | bool_true = get_bool ("Types::Serialiser::true"); |
1380 | #endif |
1651 | #endif |
1381 | return newSVsv (json_true); |
1652 | return newSVsv (bool_true); |
1382 | } |
1653 | } |
1383 | else |
1654 | else |
1384 | ERR ("'true' expected"); |
1655 | ERR ("'true' expected"); |
1385 | |
1656 | |
1386 | break; |
1657 | break; |
… | |
… | |
1388 | case 'f': |
1659 | case 'f': |
1389 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1660 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1390 | { |
1661 | { |
1391 | dec->cur += 5; |
1662 | dec->cur += 5; |
1392 | #if JSON_SLOW |
1663 | #if JSON_SLOW |
1393 | json_false = get_bool ("JSON::XS::false"); |
1664 | bool_false = get_bool ("Types::Serialiser::false"); |
1394 | #endif |
1665 | #endif |
1395 | return newSVsv (json_false); |
1666 | return newSVsv (bool_false); |
1396 | } |
1667 | } |
1397 | else |
1668 | else |
1398 | ERR ("'false' expected"); |
1669 | ERR ("'false' expected"); |
1399 | |
1670 | |
1400 | break; |
1671 | break; |
… | |
… | |
1409 | ERR ("'null' expected"); |
1680 | ERR ("'null' expected"); |
1410 | |
1681 | |
1411 | break; |
1682 | break; |
1412 | |
1683 | |
1413 | default: |
1684 | default: |
1414 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1685 | ERR ("malformed JSON string, neither tag, array, object, number, string or atom"); |
1415 | break; |
1686 | break; |
1416 | } |
1687 | } |
1417 | |
1688 | |
1418 | fail: |
1689 | fail: |
1419 | return 0; |
1690 | return 0; |
… | |
… | |
1424 | { |
1695 | { |
1425 | dec_t dec; |
1696 | dec_t dec; |
1426 | SV *sv; |
1697 | SV *sv; |
1427 | |
1698 | |
1428 | /* work around bugs in 5.10 where manipulating magic values |
1699 | /* work around bugs in 5.10 where manipulating magic values |
1429 | * will perl ignore the magic in subsequent accesses |
1700 | * makes perl ignore the magic in subsequent accesses. |
|
|
1701 | * also make a copy of non-PV values, to get them into a clean |
|
|
1702 | * state (SvPV should do that, but it's buggy, see below). |
|
|
1703 | * |
|
|
1704 | * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16), |
|
|
1705 | * as reported by Reini Urban. |
1430 | */ |
1706 | */ |
1431 | /*SvGETMAGIC (string);*/ |
1707 | /*SvGETMAGIC (string);*/ |
1432 | if (SvMAGICAL (string)) |
1708 | if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
1433 | string = sv_2mortal (newSVsv (string)); |
1709 | string = sv_2mortal (newSVsv (string)); |
1434 | |
1710 | |
1435 | SvUPGRADE (string, SVt_PV); |
1711 | SvUPGRADE (string, SVt_PV); |
1436 | |
1712 | |
1437 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1713 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1454 | if (offset > json->max_size && json->max_size) |
1730 | 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", |
1731 | 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); |
1732 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1457 | } |
1733 | } |
1458 | |
1734 | |
1459 | if (json->flags & F_UTF8) |
1735 | if (DECODE_WANTS_OCTETS (json)) |
1460 | sv_utf8_downgrade (string, 0); |
1736 | sv_utf8_downgrade (string, 0); |
1461 | else |
1737 | else |
1462 | sv_utf8_upgrade (string); |
1738 | sv_utf8_upgrade (string); |
1463 | |
1739 | |
1464 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1740 | 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); |
1782 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1507 | LEAVE; |
1783 | LEAVE; |
1508 | |
1784 | |
1509 | croak ("%s, at character offset %d (before \"%s\")", |
1785 | croak ("%s, at character offset %d (before \"%s\")", |
1510 | dec.err, |
1786 | dec.err, |
1511 | ptr_to_index (string, dec.cur), |
1787 | (int)ptr_to_index (string, dec.cur), |
1512 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1788 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1513 | } |
1789 | } |
1514 | |
1790 | |
1515 | sv = sv_2mortal (sv); |
1791 | sv = sv_2mortal (sv); |
1516 | |
1792 | |
1517 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1793 | 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)"); |
1794 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1519 | |
1795 | |
1520 | return sv; |
1796 | return sv; |
1521 | } |
1797 | } |
1522 | |
1798 | |
… | |
… | |
1525 | |
1801 | |
1526 | static void |
1802 | static void |
1527 | incr_parse (JSON *self) |
1803 | incr_parse (JSON *self) |
1528 | { |
1804 | { |
1529 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1805 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1806 | |
|
|
1807 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1808 | // but this would make it slower, so... |
1530 | |
1809 | |
1531 | for (;;) |
1810 | for (;;) |
1532 | { |
1811 | { |
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 |
1812 | //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) |
1813 | switch (self->incr_mode) |
1535 | { |
1814 | { |
1536 | // only used for intiial whitespace skipping |
1815 | // only used for initial whitespace skipping |
1537 | case INCR_M_WS: |
1816 | case INCR_M_WS: |
1538 | for (;;) |
1817 | for (;;) |
1539 | { |
1818 | { |
1540 | if (*p > 0x20) |
1819 | if (*p > 0x20) |
1541 | { |
1820 | { |
|
|
1821 | if (*p == '#') |
|
|
1822 | { |
|
|
1823 | self->incr_mode = INCR_M_C0; |
|
|
1824 | goto incr_m_c; |
|
|
1825 | } |
|
|
1826 | else |
|
|
1827 | { |
1542 | self->incr_mode = INCR_M_JSON; |
1828 | self->incr_mode = INCR_M_JSON; |
1543 | goto incr_m_json; |
1829 | goto incr_m_json; |
|
|
1830 | } |
1544 | } |
1831 | } |
1545 | else if (!*p) |
1832 | else if (!*p) |
1546 | goto interrupt; |
1833 | goto interrupt; |
1547 | |
1834 | |
1548 | ++p; |
1835 | ++p; |
… | |
… | |
1554 | goto interrupt; |
1841 | goto interrupt; |
1555 | |
1842 | |
1556 | ++p; |
1843 | ++p; |
1557 | self->incr_mode = INCR_M_STR; |
1844 | self->incr_mode = INCR_M_STR; |
1558 | goto incr_m_str; |
1845 | goto incr_m_str; |
|
|
1846 | |
|
|
1847 | // inside #-style comments |
|
|
1848 | case INCR_M_C0: |
|
|
1849 | case INCR_M_C1: |
|
|
1850 | incr_m_c: |
|
|
1851 | for (;;) |
|
|
1852 | { |
|
|
1853 | if (*p == '\n') |
|
|
1854 | { |
|
|
1855 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1856 | break; |
|
|
1857 | } |
|
|
1858 | else if (!*p) |
|
|
1859 | goto interrupt; |
|
|
1860 | |
|
|
1861 | ++p; |
|
|
1862 | } |
|
|
1863 | |
|
|
1864 | break; |
1559 | |
1865 | |
1560 | // inside a string |
1866 | // inside a string |
1561 | case INCR_M_STR: |
1867 | case INCR_M_STR: |
1562 | incr_m_str: |
1868 | incr_m_str: |
1563 | for (;;) |
1869 | for (;;) |
… | |
… | |
1614 | self->incr_mode = INCR_M_STR; |
1920 | self->incr_mode = INCR_M_STR; |
1615 | goto incr_m_str; |
1921 | goto incr_m_str; |
1616 | |
1922 | |
1617 | case '[': |
1923 | case '[': |
1618 | case '{': |
1924 | case '{': |
|
|
1925 | case '(': |
1619 | if (++self->incr_nest > self->max_depth) |
1926 | if (++self->incr_nest > self->max_depth) |
1620 | croak (ERR_NESTING_EXCEEDED); |
1927 | croak (ERR_NESTING_EXCEEDED); |
1621 | break; |
1928 | break; |
1622 | |
1929 | |
1623 | case ']': |
1930 | case ']': |
1624 | case '}': |
1931 | case '}': |
1625 | if (--self->incr_nest <= 0) |
1932 | if (--self->incr_nest <= 0) |
1626 | goto interrupt; |
1933 | goto interrupt; |
|
|
1934 | break; |
|
|
1935 | |
|
|
1936 | case ')': |
|
|
1937 | --self->incr_nest; |
|
|
1938 | break; |
|
|
1939 | |
|
|
1940 | case '#': |
|
|
1941 | self->incr_mode = INCR_M_C1; |
|
|
1942 | goto incr_m_c; |
1627 | } |
1943 | } |
1628 | } |
1944 | } |
1629 | } |
1945 | } |
1630 | |
1946 | |
1631 | modechange: |
1947 | modechange: |
1632 | ; |
1948 | ; |
1633 | } |
1949 | } |
1634 | |
1950 | |
1635 | interrupt: |
1951 | interrupt: |
1636 | self->incr_pos = p - SvPVX (self->incr_text); |
1952 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1953 | //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 |
1954 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1638 | } |
1955 | } |
1639 | |
1956 | |
1640 | ///////////////////////////////////////////////////////////////////////////// |
1957 | ///////////////////////////////////////////////////////////////////////////// |
1641 | // XS interface functions |
1958 | // XS interface functions |
… | |
… | |
1651 | i >= '0' && i <= '9' ? i - '0' |
1968 | i >= '0' && i <= '9' ? i - '0' |
1652 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1969 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1653 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1970 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1654 | : -1; |
1971 | : -1; |
1655 | |
1972 | |
1656 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1973 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1657 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1974 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1658 | |
|
|
1659 | json_true = get_bool ("JSON::XS::true"); |
1975 | bool_true = get_bool ("Types::Serialiser::true"); |
1660 | json_false = get_bool ("JSON::XS::false"); |
1976 | bool_false = get_bool ("Types::Serialiser::false"); |
|
|
1977 | |
|
|
1978 | sv_json = newSVpv ("JSON", 0); |
|
|
1979 | SvREADONLY_on (sv_json); |
1661 | |
1980 | |
1662 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1981 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1663 | } |
1982 | } |
1664 | |
1983 | |
1665 | PROTOTYPES: DISABLE |
1984 | PROTOTYPES: DISABLE |
1666 | |
1985 | |
1667 | void CLONE (...) |
1986 | void CLONE (...) |
1668 | CODE: |
1987 | CODE: |
1669 | json_stash = 0; |
1988 | json_stash = 0; |
1670 | json_boolean_stash = 0; |
1989 | bool_stash = 0; |
1671 | |
1990 | |
1672 | void new (char *klass) |
1991 | void new (char *klass) |
1673 | PPCODE: |
1992 | PPCODE: |
1674 | { |
1993 | { |
1675 | SV *pv = NEWSV (0, sizeof (JSON)); |
1994 | SV *pv = NEWSV (0, sizeof (JSON)); |
1676 | SvPOK_only (pv); |
1995 | SvPOK_only (pv); |
1677 | json_init ((JSON *)SvPVX (pv)); |
1996 | json_init ((JSON *)SvPVX (pv)); |
1678 | XPUSHs (sv_2mortal (sv_bless ( |
1997 | XPUSHs (sv_2mortal (sv_bless ( |
1679 | newRV_noinc (pv), |
1998 | newRV_noinc (pv), |
1680 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
1999 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
… | |
… | |
1695 | shrink = F_SHRINK |
2014 | shrink = F_SHRINK |
1696 | allow_blessed = F_ALLOW_BLESSED |
2015 | allow_blessed = F_ALLOW_BLESSED |
1697 | convert_blessed = F_CONV_BLESSED |
2016 | convert_blessed = F_CONV_BLESSED |
1698 | relaxed = F_RELAXED |
2017 | relaxed = F_RELAXED |
1699 | allow_unknown = F_ALLOW_UNKNOWN |
2018 | allow_unknown = F_ALLOW_UNKNOWN |
|
|
2019 | allow_tags = F_ALLOW_TAGS |
1700 | PPCODE: |
2020 | PPCODE: |
1701 | { |
2021 | { |
1702 | if (enable) |
2022 | if (enable) |
1703 | self->flags |= ix; |
2023 | self->flags |= ix; |
1704 | else |
2024 | else |
… | |
… | |
1720 | get_shrink = F_SHRINK |
2040 | get_shrink = F_SHRINK |
1721 | get_allow_blessed = F_ALLOW_BLESSED |
2041 | get_allow_blessed = F_ALLOW_BLESSED |
1722 | get_convert_blessed = F_CONV_BLESSED |
2042 | get_convert_blessed = F_CONV_BLESSED |
1723 | get_relaxed = F_RELAXED |
2043 | get_relaxed = F_RELAXED |
1724 | get_allow_unknown = F_ALLOW_UNKNOWN |
2044 | get_allow_unknown = F_ALLOW_UNKNOWN |
|
|
2045 | get_allow_tags = F_ALLOW_TAGS |
1725 | PPCODE: |
2046 | PPCODE: |
1726 | XPUSHs (boolSV (self->flags & ix)); |
2047 | XPUSHs (boolSV (self->flags & ix)); |
1727 | |
2048 | |
1728 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
2049 | void max_depth (JSON *self, U32 max_depth = 0x80000000UL) |
1729 | PPCODE: |
2050 | PPCODE: |
… | |
… | |
1757 | } |
2078 | } |
1758 | |
2079 | |
1759 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
2080 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1760 | PPCODE: |
2081 | PPCODE: |
1761 | { |
2082 | { |
1762 | if (!self->cb_sk_object) |
2083 | if (!self->cb_sk_object) |
1763 | self->cb_sk_object = newHV (); |
2084 | self->cb_sk_object = newHV (); |
1764 | |
2085 | |
1765 | if (SvOK (cb)) |
2086 | if (SvOK (cb)) |
1766 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
2087 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1767 | else |
2088 | else |
… | |
… | |
1778 | XPUSHs (ST (0)); |
2099 | XPUSHs (ST (0)); |
1779 | } |
2100 | } |
1780 | |
2101 | |
1781 | void encode (JSON *self, SV *scalar) |
2102 | void encode (JSON *self, SV *scalar) |
1782 | PPCODE: |
2103 | PPCODE: |
1783 | XPUSHs (encode_json (scalar, self)); |
2104 | PUTBACK; scalar = encode_json (scalar, self); SPAGAIN; |
|
|
2105 | XPUSHs (scalar); |
1784 | |
2106 | |
1785 | void decode (JSON *self, SV *jsonstr) |
2107 | void decode (JSON *self, SV *jsonstr) |
1786 | PPCODE: |
2108 | PPCODE: |
1787 | XPUSHs (decode_json (jsonstr, self, 0)); |
2109 | PUTBACK; jsonstr = decode_json (jsonstr, self, 0); SPAGAIN; |
|
|
2110 | XPUSHs (jsonstr); |
1788 | |
2111 | |
1789 | void decode_prefix (JSON *self, SV *jsonstr) |
2112 | void decode_prefix (JSON *self, SV *jsonstr) |
1790 | PPCODE: |
2113 | PPCODE: |
1791 | { |
2114 | { |
|
|
2115 | SV *sv; |
1792 | char *offset; |
2116 | char *offset; |
|
|
2117 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
1793 | EXTEND (SP, 2); |
2118 | EXTEND (SP, 2); |
1794 | PUSHs (decode_json (jsonstr, self, &offset)); |
2119 | PUSHs (sv); |
1795 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
2120 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1796 | } |
2121 | } |
1797 | |
2122 | |
1798 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2123 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1799 | PPCODE: |
2124 | PPCODE: |
1800 | { |
2125 | { |
1801 | if (!self->incr_text) |
2126 | if (!self->incr_text) |
1802 | self->incr_text = newSVpvn ("", 0); |
2127 | self->incr_text = newSVpvn ("", 0); |
|
|
2128 | |
|
|
2129 | /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */ |
|
|
2130 | if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text)) |
|
|
2131 | if (DECODE_WANTS_OCTETS (self)) |
|
|
2132 | { |
|
|
2133 | if (self->incr_pos) |
|
|
2134 | self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text), |
|
|
2135 | (U8 *)SvPVX (self->incr_text) + self->incr_pos); |
|
|
2136 | |
|
|
2137 | sv_utf8_downgrade (self->incr_text, 0); |
|
|
2138 | } |
|
|
2139 | else |
|
|
2140 | { |
|
|
2141 | sv_utf8_upgrade (self->incr_text); |
|
|
2142 | |
|
|
2143 | if (self->incr_pos) |
|
|
2144 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
2145 | - (U8 *)SvPVX (self->incr_text); |
|
|
2146 | } |
1803 | |
2147 | |
1804 | // append data, if any |
2148 | // append data, if any |
1805 | if (jsonstr) |
2149 | if (jsonstr) |
1806 | { |
2150 | { |
|
|
2151 | /* make sure both strings have same encoding */ |
|
|
2152 | if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text)) |
1807 | if (SvUTF8 (jsonstr)) |
2153 | if (SvUTF8 (jsonstr)) |
|
|
2154 | sv_utf8_downgrade (jsonstr, 0); |
1808 | { |
2155 | 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); |
2156 | sv_utf8_upgrade (jsonstr); |
1821 | |
2157 | |
|
|
2158 | /* and then just blindly append */ |
1822 | { |
2159 | { |
1823 | STRLEN len; |
2160 | STRLEN len; |
1824 | const char *str = SvPV (jsonstr, len); |
2161 | const char *str = SvPV (jsonstr, len); |
1825 | STRLEN cur = SvCUR (self->incr_text); |
2162 | STRLEN cur = SvCUR (self->incr_text); |
1826 | |
2163 | |
… | |
… | |
1834 | } |
2171 | } |
1835 | |
2172 | |
1836 | if (GIMME_V != G_VOID) |
2173 | if (GIMME_V != G_VOID) |
1837 | do |
2174 | do |
1838 | { |
2175 | { |
|
|
2176 | SV *sv; |
1839 | char *offset; |
2177 | char *offset; |
1840 | |
2178 | |
1841 | if (!INCR_DONE (self)) |
2179 | if (!INCR_DONE (self)) |
1842 | { |
2180 | { |
1843 | incr_parse (self); |
2181 | incr_parse (self); |
… | |
… | |
1845 | if (self->incr_pos > self->max_size && self->max_size) |
2183 | 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", |
2184 | 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); |
2185 | (unsigned long)self->incr_pos, (unsigned long)self->max_size); |
1848 | |
2186 | |
1849 | if (!INCR_DONE (self)) |
2187 | if (!INCR_DONE (self)) |
|
|
2188 | { |
|
|
2189 | // as an optimisation, do not accumulate white space in the incr buffer |
|
|
2190 | if (self->incr_mode == INCR_M_WS && self->incr_pos) |
|
|
2191 | { |
|
|
2192 | self->incr_pos = 0; |
|
|
2193 | SvCUR_set (self->incr_text, 0); |
|
|
2194 | } |
|
|
2195 | |
1850 | break; |
2196 | break; |
|
|
2197 | } |
1851 | } |
2198 | } |
1852 | |
2199 | |
1853 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
2200 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
|
|
2201 | XPUSHs (sv); |
1854 | |
2202 | |
1855 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2203 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1856 | self->incr_nest = 0; |
2204 | self->incr_nest = 0; |
1857 | self->incr_mode = 0; |
2205 | self->incr_mode = 0; |
1858 | |
2206 | |
… | |
… | |
1902 | SvREFCNT_dec (self->incr_text); |
2250 | SvREFCNT_dec (self->incr_text); |
1903 | |
2251 | |
1904 | PROTOTYPES: ENABLE |
2252 | PROTOTYPES: ENABLE |
1905 | |
2253 | |
1906 | void encode_json (SV *scalar) |
2254 | void encode_json (SV *scalar) |
1907 | ALIAS: |
|
|
1908 | to_json_ = 0 |
|
|
1909 | encode_json = F_UTF8 |
|
|
1910 | PPCODE: |
2255 | PPCODE: |
1911 | { |
2256 | { |
1912 | JSON json; |
2257 | JSON json; |
1913 | json_init (&json); |
2258 | json_init (&json); |
1914 | json.flags |= ix; |
2259 | json.flags |= F_UTF8; |
1915 | XPUSHs (encode_json (scalar, &json)); |
2260 | PUTBACK; scalar = encode_json (scalar, &json); SPAGAIN; |
|
|
2261 | XPUSHs (scalar); |
1916 | } |
2262 | } |
1917 | |
2263 | |
1918 | void decode_json (SV *jsonstr) |
2264 | void decode_json (SV *jsonstr) |
1919 | ALIAS: |
|
|
1920 | from_json_ = 0 |
|
|
1921 | decode_json = F_UTF8 |
|
|
1922 | PPCODE: |
2265 | PPCODE: |
1923 | { |
2266 | { |
1924 | JSON json; |
2267 | JSON json; |
1925 | json_init (&json); |
2268 | json_init (&json); |
1926 | json.flags |= ix; |
2269 | json.flags |= F_UTF8; |
1927 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2270 | PUTBACK; jsonstr = decode_json (jsonstr, &json, 0); SPAGAIN; |
|
|
2271 | XPUSHs (jsonstr); |
1928 | } |
2272 | } |
1929 | |
2273 | |
1930 | |
|
|