… | |
… | |
6 | #include <string.h> |
6 | #include <string.h> |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
9 | #include <limits.h> |
9 | #include <limits.h> |
10 | #include <float.h> |
10 | #include <float.h> |
|
|
11 | #include <inttypes.h> |
11 | |
12 | |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | #endif |
15 | #endif |
15 | |
16 | |
16 | // some old perls do not have this, try to make it work, no |
17 | // 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. |
18 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
19 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
20 | # define UTF8_MAXBYTES 13 |
|
|
21 | #endif |
|
|
22 | |
|
|
23 | // compatibility with perl <5.18 |
|
|
24 | #ifndef HvNAMELEN_get |
|
|
25 | # define HvNAMELEN_get(hv) strlen (HvNAME (hv)) |
|
|
26 | #endif |
|
|
27 | #ifndef HvNAMELEN |
|
|
28 | # define HvNAMELEN(hv) HvNAMELEN_get (hv) |
|
|
29 | #endif |
|
|
30 | #ifndef HvNAMEUTF8 |
|
|
31 | # define HvNAMEUTF8(hv) 0 |
20 | #endif |
32 | #endif |
21 | |
33 | |
22 | // three extra for rounding, sign, and end of string |
34 | // three extra for rounding, sign, and end of string |
23 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
35 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
24 | |
36 | |
… | |
… | |
68 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
80 | #define ERR_NESTING_EXCEEDED "json text or perl structure exceeds maximum nesting level (max_depth set too low?)" |
69 | |
81 | |
70 | #ifdef USE_ITHREADS |
82 | #ifdef USE_ITHREADS |
71 | # define JSON_SLOW 1 |
83 | # define JSON_SLOW 1 |
72 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
84 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
85 | # define BOOL_STASH (bool_stash ? bool_stash : gv_stashpv ("Types::Serialiser::Boolean", 1)) |
73 | #else |
86 | #else |
74 | # define JSON_SLOW 0 |
87 | # define JSON_SLOW 0 |
75 | # define JSON_STASH json_stash |
88 | # define JSON_STASH json_stash |
|
|
89 | # define BOOL_STASH bool_stash |
76 | #endif |
90 | #endif |
77 | |
91 | |
78 | // the amount of HEs to allocate on the stack, when sorting keys |
92 | // the amount of HEs to allocate on the stack, when sorting keys |
79 | #define STACK_HES 64 |
93 | #define STACK_HES 64 |
80 | |
94 | |
81 | static HV *json_stash, *types_boolean_stash; // JSON::XS:: |
95 | static HV *json_stash, *bool_stash; // JSON::XS::, Types::Serialiser::Boolean:: |
82 | static SV *types_true, *types_false, *sv_json; |
96 | static SV *bool_true, *bool_false, *sv_json; |
83 | |
97 | |
84 | enum { |
98 | enum { |
85 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
99 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
86 | INCR_M_STR, // inside string |
100 | INCR_M_STR, // inside string |
87 | INCR_M_BS, // inside backslash |
101 | INCR_M_BS, // inside backslash |
… | |
… | |
139 | SvPV_shrink_to_cur (sv); |
153 | SvPV_shrink_to_cur (sv); |
140 | #elif defined (SvPV_renew) |
154 | #elif defined (SvPV_renew) |
141 | SvPV_renew (sv, SvCUR (sv) + 1); |
155 | SvPV_renew (sv, SvCUR (sv) + 1); |
142 | #endif |
156 | #endif |
143 | } |
157 | } |
|
|
158 | } |
|
|
159 | |
|
|
160 | /* adds two STRLENs together, slow, and with paranoia */ |
|
|
161 | STRLEN |
|
|
162 | strlen_sum (STRLEN l1, STRLEN l2) |
|
|
163 | { |
|
|
164 | size_t sum = l1 + l2; |
|
|
165 | |
|
|
166 | if (sum < (size_t)l2 || sum != (size_t)(STRLEN)sum) |
|
|
167 | croak ("JSON::XS: string size overflow"); |
|
|
168 | |
|
|
169 | return sum; |
|
|
170 | } |
|
|
171 | |
|
|
172 | /* similar to SvGROW, but somewhat safer and guarantees exponential realloc strategy */ |
|
|
173 | static char * |
|
|
174 | json_sv_grow (SV *sv, size_t len1, size_t len2) |
|
|
175 | { |
|
|
176 | len1 = strlen_sum (len1, len2); |
|
|
177 | len1 = strlen_sum (len1, len1 >> 1); |
|
|
178 | |
|
|
179 | if (len1 > 4096 - 24) |
|
|
180 | len1 = (len1 | 4095) - 24; |
|
|
181 | |
|
|
182 | return SvGROW (sv, len1); |
144 | } |
183 | } |
145 | |
184 | |
146 | // decode an utf-8 character and return it, or (UV)-1 in |
185 | // decode an utf-8 character and return it, or (UV)-1 in |
147 | // case of an error. |
186 | // case of an error. |
148 | // we special-case "safe" characters from U+80 .. U+7FF, |
187 | // we special-case "safe" characters from U+80 .. U+7FF, |
… | |
… | |
288 | // a recursion depth of ten gives us >>500 bits |
327 | // a recursion depth of ten gives us >>500 bits |
289 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
328 | json_atof_scan1 (s, &accum, &expo, 0, 10); |
290 | |
329 | |
291 | return neg ? -accum : accum; |
330 | return neg ? -accum : accum; |
292 | } |
331 | } |
|
|
332 | |
|
|
333 | // target of scalar reference is bool? -1 == nope, 0 == false, 1 == true |
|
|
334 | static int |
|
|
335 | ref_bool_type (SV *sv) |
|
|
336 | { |
|
|
337 | svtype svt = SvTYPE (sv); |
|
|
338 | |
|
|
339 | if (svt < SVt_PVAV) |
|
|
340 | { |
|
|
341 | STRLEN len = 0; |
|
|
342 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
343 | |
|
|
344 | if (len == 1) |
|
|
345 | if (*pv == '1') |
|
|
346 | return 1; |
|
|
347 | else if (*pv == '0') |
|
|
348 | return 0; |
|
|
349 | } |
|
|
350 | |
|
|
351 | return -1; |
|
|
352 | } |
|
|
353 | |
|
|
354 | // returns whether scalar is not a reference in the sense of allow_nonref |
|
|
355 | static int |
|
|
356 | json_nonref (SV *scalar) |
|
|
357 | { |
|
|
358 | if (!SvROK (scalar)) |
|
|
359 | return 1; |
|
|
360 | |
|
|
361 | scalar = SvRV (scalar); |
|
|
362 | |
|
|
363 | if (SvTYPE (scalar) >= SVt_PVMG) |
|
|
364 | { |
|
|
365 | if (SvSTASH (scalar) == bool_stash) |
|
|
366 | return 1; |
|
|
367 | |
|
|
368 | if (!SvOBJECT (scalar) && ref_bool_type (scalar) >= 0) |
|
|
369 | return 1; |
|
|
370 | } |
|
|
371 | |
|
|
372 | return 0; |
|
|
373 | } |
|
|
374 | |
293 | ///////////////////////////////////////////////////////////////////////////// |
375 | ///////////////////////////////////////////////////////////////////////////// |
294 | // encoder |
376 | // encoder |
295 | |
377 | |
296 | // structure used for encoding JSON |
378 | // structure used for encoding JSON |
297 | typedef struct |
379 | typedef struct |
… | |
… | |
305 | } enc_t; |
387 | } enc_t; |
306 | |
388 | |
307 | INLINE void |
389 | INLINE void |
308 | need (enc_t *enc, STRLEN len) |
390 | need (enc_t *enc, STRLEN len) |
309 | { |
391 | { |
310 | if (expect_false (enc->cur + len >= enc->end)) |
392 | if (expect_false ((uintptr_t)(enc->end - enc->cur) < len)) |
311 | { |
393 | { |
312 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
394 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
313 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
395 | char *buf = json_sv_grow (enc->sv, cur, len); |
314 | enc->cur = SvPVX (enc->sv) + cur; |
396 | enc->cur = buf + cur; |
315 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
397 | enc->end = buf + SvLEN (enc->sv) - 1; |
316 | } |
398 | } |
317 | } |
399 | } |
318 | |
400 | |
319 | INLINE void |
401 | INLINE void |
320 | encode_ch (enc_t *enc, char ch) |
402 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
336 | |
418 | |
337 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
419 | if (expect_true (ch >= 0x20 && ch < 0x80)) // most common case |
338 | { |
420 | { |
339 | if (expect_false (ch == '"')) // but with slow exceptions |
421 | if (expect_false (ch == '"')) // but with slow exceptions |
340 | { |
422 | { |
341 | need (enc, len += 1); |
423 | need (enc, len + 1); |
342 | *enc->cur++ = '\\'; |
424 | *enc->cur++ = '\\'; |
343 | *enc->cur++ = '"'; |
425 | *enc->cur++ = '"'; |
344 | } |
426 | } |
345 | else if (expect_false (ch == '\\')) |
427 | else if (expect_false (ch == '\\')) |
346 | { |
428 | { |
347 | need (enc, len += 1); |
429 | need (enc, len + 1); |
348 | *enc->cur++ = '\\'; |
430 | *enc->cur++ = '\\'; |
349 | *enc->cur++ = '\\'; |
431 | *enc->cur++ = '\\'; |
350 | } |
432 | } |
351 | else |
433 | else |
352 | *enc->cur++ = ch; |
434 | *enc->cur++ = ch; |
… | |
… | |
355 | } |
437 | } |
356 | else |
438 | else |
357 | { |
439 | { |
358 | switch (ch) |
440 | switch (ch) |
359 | { |
441 | { |
360 | case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
442 | case '\010': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break; |
361 | case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
443 | case '\011': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break; |
362 | case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
444 | case '\012': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break; |
363 | case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
445 | case '\014': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break; |
364 | case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
446 | case '\015': need (enc, len + 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break; |
365 | |
447 | |
366 | default: |
448 | default: |
367 | { |
449 | { |
368 | STRLEN clen; |
450 | STRLEN clen; |
369 | UV uch; |
451 | UV uch; |
… | |
… | |
385 | if (uch >= 0x10000UL) |
467 | if (uch >= 0x10000UL) |
386 | { |
468 | { |
387 | if (uch >= 0x110000UL) |
469 | if (uch >= 0x110000UL) |
388 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
470 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
389 | |
471 | |
390 | need (enc, len += 11); |
472 | need (enc, len + 11); |
391 | sprintf (enc->cur, "\\u%04x\\u%04x", |
473 | sprintf (enc->cur, "\\u%04x\\u%04x", |
392 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
474 | (int)((uch - 0x10000) / 0x400 + 0xD800), |
393 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
475 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
394 | enc->cur += 12; |
476 | enc->cur += 12; |
395 | } |
477 | } |
396 | else |
478 | else |
397 | { |
479 | { |
398 | need (enc, len += 5); |
480 | need (enc, len + 5); |
399 | *enc->cur++ = '\\'; |
481 | *enc->cur++ = '\\'; |
400 | *enc->cur++ = 'u'; |
482 | *enc->cur++ = 'u'; |
401 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
483 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
402 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
484 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
403 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
485 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
… | |
… | |
411 | *enc->cur++ = uch; |
493 | *enc->cur++ = uch; |
412 | str += clen; |
494 | str += clen; |
413 | } |
495 | } |
414 | else if (is_utf8) |
496 | else if (is_utf8) |
415 | { |
497 | { |
416 | need (enc, len += clen); |
498 | need (enc, len + clen); |
417 | do |
499 | do |
418 | { |
500 | { |
419 | *enc->cur++ = *str++; |
501 | *enc->cur++ = *str++; |
420 | } |
502 | } |
421 | while (--clen); |
503 | while (--clen); |
422 | } |
504 | } |
423 | else |
505 | else |
424 | { |
506 | { |
425 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
507 | need (enc, len + UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
426 | enc->cur = encode_utf8 (enc->cur, uch); |
508 | enc->cur = encode_utf8 (enc->cur, uch); |
427 | ++str; |
509 | ++str; |
428 | } |
510 | } |
429 | } |
511 | } |
430 | } |
512 | } |
… | |
… | |
683 | // encode objects, arrays and special \0=false and \1=true values. |
765 | // encode objects, arrays and special \0=false and \1=true values. |
684 | static void |
766 | static void |
685 | encode_rv (enc_t *enc, SV *sv) |
767 | encode_rv (enc_t *enc, SV *sv) |
686 | { |
768 | { |
687 | svtype svt; |
769 | svtype svt; |
|
|
770 | GV *method; |
688 | |
771 | |
689 | SvGETMAGIC (sv); |
772 | SvGETMAGIC (sv); |
690 | svt = SvTYPE (sv); |
773 | svt = SvTYPE (sv); |
691 | |
774 | |
692 | if (expect_false (SvOBJECT (sv))) |
775 | if (expect_false (SvOBJECT (sv))) |
693 | { |
776 | { |
694 | HV *boolean_stash = !JSON_SLOW || types_boolean_stash |
|
|
695 | ? types_boolean_stash |
|
|
696 | : gv_stashpv ("Types::Serialiser::Boolean", 1); |
|
|
697 | HV *stash = SvSTASH (sv); |
777 | HV *stash = SvSTASH (sv); |
698 | |
778 | |
699 | if (stash == boolean_stash) |
779 | if (stash == bool_stash) |
700 | { |
780 | { |
701 | if (SvIV (sv)) |
781 | if (SvIV (sv)) |
702 | encode_str (enc, "true", 4, 0); |
782 | encode_str (enc, "true", 4, 0); |
703 | else |
783 | else |
704 | encode_str (enc, "false", 5, 0); |
784 | encode_str (enc, "false", 5, 0); |
705 | } |
785 | } |
706 | else if (enc->json.flags & F_CONV_BLESSED) |
786 | else if ((enc->json.flags & F_ALLOW_TAGS) && (method = gv_fetchmethod_autoload (stash, "FREEZE", 0))) |
707 | { |
787 | { |
708 | GV *to_json = gv_fetchmethod_autoload (stash, "TO_JSON", 0); |
788 | int count; |
|
|
789 | dSP; |
709 | |
790 | |
|
|
791 | ENTER; SAVETMPS; |
|
|
792 | SAVESTACK_POS (); |
|
|
793 | PUSHMARK (SP); |
|
|
794 | EXTEND (SP, 2); |
|
|
795 | // we re-bless the reference to get overload and other niceties right |
|
|
796 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
710 | if (to_json) |
797 | PUSHs (sv_json); |
|
|
798 | |
|
|
799 | PUTBACK; |
|
|
800 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
|
|
801 | SPAGAIN; |
|
|
802 | |
|
|
803 | // catch this surprisingly common error |
|
|
804 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
805 | croak ("%s::FREEZE method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
806 | |
|
|
807 | encode_ch (enc, '('); |
|
|
808 | encode_ch (enc, '"'); |
|
|
809 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
810 | encode_ch (enc, '"'); |
|
|
811 | encode_ch (enc, ')'); |
|
|
812 | encode_ch (enc, '['); |
|
|
813 | |
|
|
814 | while (count) |
711 | { |
815 | { |
712 | dSP; |
816 | encode_sv (enc, SP[1 - count--]); |
713 | |
817 | |
714 | ENTER; SAVETMPS; PUSHMARK (SP); |
818 | if (count) |
715 | // we re-bless the reference to get overload and other niceties right |
|
|
716 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
|
|
717 | |
|
|
718 | // calling with G_SCALAR ensures that we always get a 1 return value |
|
|
719 | PUTBACK; |
|
|
720 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
|
|
721 | SPAGAIN; |
|
|
722 | |
|
|
723 | // catch this surprisingly common error |
|
|
724 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
725 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
726 | |
|
|
727 | sv = POPs; |
|
|
728 | PUTBACK; |
|
|
729 | |
|
|
730 | encode_sv (enc, sv); |
819 | encode_ch (enc, ','); |
731 | |
|
|
732 | FREETMPS; LEAVE; |
|
|
733 | } |
820 | } |
734 | else if (enc->json.flags & F_ALLOW_BLESSED) |
821 | |
735 | encode_str (enc, "null", 4, 0); |
822 | encode_ch (enc, ']'); |
736 | else |
823 | |
737 | croak ("encountered object '%s' when conv_object is enabled, but TO_JSON is missing and allow_blessed disabled", |
824 | FREETMPS; LEAVE; |
738 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
739 | } |
825 | } |
740 | else if (enc->json.flags & F_ALLOW_TAGS) |
826 | else if ((enc->json.flags & F_CONV_BLESSED) && (method = gv_fetchmethod_autoload (stash, "TO_JSON", 0))) |
741 | { |
827 | { |
742 | GV *method = gv_fetchmethod_autoload (stash, "FREEZE", 0); |
|
|
743 | |
|
|
744 | if (method) |
|
|
745 | { |
|
|
746 | int count; |
|
|
747 | dSP; |
828 | dSP; |
748 | |
829 | |
749 | ENTER; SAVETMPS; PUSHMARK (SP); |
830 | ENTER; SAVETMPS; |
750 | EXTEND (SP, 2); |
831 | PUSHMARK (SP); |
751 | // we re-bless the reference to get overload and other niceties right |
832 | // we re-bless the reference to get overload and other niceties right |
752 | PUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
833 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), stash)); |
753 | PUSHs (sv_json); |
|
|
754 | |
834 | |
|
|
835 | // calling with G_SCALAR ensures that we always get a 1 return value |
755 | PUTBACK; |
836 | PUTBACK; |
756 | count = call_sv ((SV *)GvCV (method), G_ARRAY); |
837 | call_sv ((SV *)GvCV (method), G_SCALAR); |
757 | SPAGAIN; |
838 | SPAGAIN; |
758 | |
839 | |
759 | // catch this surprisingly common error |
840 | // catch this surprisingly common error |
760 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
841 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
761 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
842 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
762 | |
843 | |
763 | encode_ch (enc, '('); |
844 | sv = POPs; |
764 | encode_ch (enc, '"'); |
|
|
765 | encode_str (enc, HvNAME (stash), HvNAMELEN (stash), HvNAMEUTF8 (stash)); |
|
|
766 | encode_ch (enc, '"'); |
|
|
767 | encode_ch (enc, ')'); |
|
|
768 | encode_ch (enc, '['); |
|
|
769 | |
|
|
770 | while (count) |
|
|
771 | { |
|
|
772 | encode_sv (enc, SP[1 - count--]); |
|
|
773 | |
|
|
774 | if (count) |
|
|
775 | encode_ch (enc, ','); |
|
|
776 | } |
|
|
777 | |
|
|
778 | encode_ch (enc, ']'); |
|
|
779 | |
|
|
780 | PUTBACK; |
845 | PUTBACK; |
781 | |
846 | |
|
|
847 | encode_sv (enc, sv); |
|
|
848 | |
782 | FREETMPS; LEAVE; |
849 | FREETMPS; LEAVE; |
783 | } |
|
|
784 | else if (enc->json.flags & F_ALLOW_BLESSED) |
|
|
785 | encode_str (enc, "null", 4, 0); |
|
|
786 | else |
|
|
787 | croak ("encountered object '%s' when allow_tags is enabled, but FREEZE is missing and allow_blessed disabled", |
|
|
788 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
789 | } |
850 | } |
790 | else if (enc->json.flags & F_ALLOW_BLESSED) |
851 | else if (enc->json.flags & F_ALLOW_BLESSED) |
791 | encode_str (enc, "null", 4, 0); |
852 | encode_str (enc, "null", 4, 0); |
792 | else |
853 | else |
793 | croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled", |
854 | croak ("encountered object '%s', but neither allow_blessed, convert_blessed nor allow_tags settings are enabled (or TO_JSON/FREEZE method missing)", |
794 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
855 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
795 | } |
856 | } |
796 | else if (svt == SVt_PVHV) |
857 | else if (svt == SVt_PVHV) |
797 | encode_hv (enc, (HV *)sv); |
858 | encode_hv (enc, (HV *)sv); |
798 | else if (svt == SVt_PVAV) |
859 | else if (svt == SVt_PVAV) |
799 | encode_av (enc, (AV *)sv); |
860 | encode_av (enc, (AV *)sv); |
800 | else if (svt < SVt_PVAV) |
861 | else if (svt < SVt_PVAV) |
801 | { |
862 | { |
802 | STRLEN len = 0; |
863 | int bool_type = ref_bool_type (sv); |
803 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
804 | |
864 | |
805 | if (len == 1 && *pv == '1') |
865 | if (bool_type == 1) |
806 | encode_str (enc, "true", 4, 0); |
866 | encode_str (enc, "true", 4, 0); |
807 | else if (len == 1 && *pv == '0') |
867 | else if (bool_type == 0) |
808 | encode_str (enc, "false", 5, 0); |
868 | encode_str (enc, "false", 5, 0); |
809 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
869 | else if (enc->json.flags & F_ALLOW_UNKNOWN) |
810 | encode_str (enc, "null", 4, 0); |
870 | encode_str (enc, "null", 4, 0); |
811 | else |
871 | else |
812 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
872 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
… | |
… | |
895 | static SV * |
955 | static SV * |
896 | encode_json (SV *scalar, JSON *json) |
956 | encode_json (SV *scalar, JSON *json) |
897 | { |
957 | { |
898 | enc_t enc; |
958 | enc_t enc; |
899 | |
959 | |
900 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
960 | if (!(json->flags & F_ALLOW_NONREF) && json_nonref (scalar)) |
901 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
961 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
902 | |
962 | |
903 | enc.json = *json; |
963 | enc.json = *json; |
904 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
964 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
905 | enc.cur = SvPVX (enc.sv); |
965 | enc.cur = SvPVX (enc.sv); |
… | |
… | |
1112 | *cur++ = *dec_cur++; |
1172 | *cur++ = *dec_cur++; |
1113 | while (--clen); |
1173 | while (--clen); |
1114 | |
1174 | |
1115 | utf8 = 1; |
1175 | utf8 = 1; |
1116 | } |
1176 | } |
|
|
1177 | else if (ch == '\t' && dec->json.flags & F_RELAXED) |
|
|
1178 | *cur++ = ch; |
1117 | else |
1179 | else |
1118 | { |
1180 | { |
1119 | --dec_cur; |
1181 | --dec_cur; |
1120 | |
1182 | |
1121 | if (!ch) |
1183 | if (!ch) |
… | |
… | |
1131 | |
1193 | |
1132 | if (sv) |
1194 | if (sv) |
1133 | { |
1195 | { |
1134 | STRLEN cur = SvCUR (sv); |
1196 | STRLEN cur = SvCUR (sv); |
1135 | |
1197 | |
1136 | if (SvLEN (sv) <= cur + len) |
1198 | if (SvLEN (sv) - cur <= len) |
1137 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
1199 | json_sv_grow (sv, cur, len); |
1138 | |
1200 | |
1139 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1201 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1140 | SvCUR_set (sv, SvCUR (sv) + len); |
1202 | SvCUR_set (sv, SvCUR (sv) + len); |
1141 | } |
1203 | } |
1142 | else |
1204 | else |
… | |
… | |
1443 | |
1505 | |
1444 | hv_iterinit (hv); |
1506 | hv_iterinit (hv); |
1445 | he = hv_iternext (hv); |
1507 | he = hv_iternext (hv); |
1446 | hv_iterinit (hv); |
1508 | hv_iterinit (hv); |
1447 | |
1509 | |
1448 | // the next line creates a mortal sv each time its called. |
1510 | // the next line creates a mortal sv each time it's called. |
1449 | // might want to optimise this for common cases. |
1511 | // might want to optimise this for common cases. |
1450 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1512 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
1451 | |
1513 | |
1452 | if (cb) |
1514 | if (cb) |
1453 | { |
1515 | { |
1454 | dSP; |
1516 | dSP; |
1455 | int count; |
1517 | int count; |
1456 | |
1518 | |
1457 | ENTER; SAVETMPS; PUSHMARK (SP); |
1519 | ENTER; SAVETMPS; |
|
|
1520 | SAVESTACK_POS (); |
|
|
1521 | PUSHMARK (SP); |
1458 | XPUSHs (HeVAL (he)); |
1522 | XPUSHs (HeVAL (he)); |
1459 | sv_2mortal (sv); |
1523 | sv_2mortal (sv); |
1460 | |
1524 | |
1461 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1525 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1462 | |
1526 | |
… | |
… | |
1475 | if (dec->json.cb_object) |
1539 | if (dec->json.cb_object) |
1476 | { |
1540 | { |
1477 | dSP; |
1541 | dSP; |
1478 | int count; |
1542 | int count; |
1479 | |
1543 | |
1480 | ENTER; SAVETMPS; PUSHMARK (SP); |
1544 | ENTER; SAVETMPS; |
|
|
1545 | SAVESTACK_POS (); |
|
|
1546 | PUSHMARK (SP); |
1481 | XPUSHs (sv_2mortal (sv)); |
1547 | XPUSHs (sv_2mortal (sv)); |
1482 | |
1548 | |
1483 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1549 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1484 | |
1550 | |
1485 | if (count == 1) |
1551 | if (count == 1) |
… | |
… | |
1511 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1577 | if (!(dec->json.flags & F_ALLOW_TAGS)) |
1512 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1578 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
1513 | |
1579 | |
1514 | ++dec->cur; |
1580 | ++dec->cur; |
1515 | |
1581 | |
|
|
1582 | decode_ws (dec); |
|
|
1583 | |
1516 | tag = decode_sv (dec); |
1584 | tag = decode_sv (dec); |
1517 | if (!tag) |
1585 | if (!tag) |
1518 | goto fail; |
1586 | goto fail; |
1519 | |
1587 | |
1520 | if (!SvPOK (tag)) |
1588 | if (!SvPOK (tag)) |
1521 | ERR ("malformed JSON string, (tag) must be a string"); |
1589 | ERR ("malformed JSON string, (tag) must be a string"); |
1522 | |
1590 | |
|
|
1591 | decode_ws (dec); |
|
|
1592 | |
1523 | if (*dec->cur != ')') |
1593 | if (*dec->cur != ')') |
1524 | ERR (") expected after tag"); |
1594 | ERR (") expected after tag"); |
1525 | |
1595 | |
1526 | ++dec->cur; |
1596 | ++dec->cur; |
|
|
1597 | |
|
|
1598 | decode_ws (dec); |
1527 | |
1599 | |
1528 | val = decode_sv (dec); |
1600 | val = decode_sv (dec); |
1529 | if (!val) |
1601 | if (!val) |
1530 | goto fail; |
1602 | goto fail; |
1531 | |
1603 | |
… | |
… | |
1546 | if (!method) |
1618 | if (!method) |
1547 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1619 | ERR ("cannot decode perl-object (package does not have a THAW method)"); |
1548 | |
1620 | |
1549 | dSP; |
1621 | dSP; |
1550 | |
1622 | |
1551 | ENTER; SAVETMPS; PUSHMARK (SP); |
1623 | ENTER; SAVETMPS; |
|
|
1624 | PUSHMARK (SP); |
1552 | EXTEND (SP, len + 2); |
1625 | EXTEND (SP, len + 2); |
1553 | // we re-bless the reference to get overload and other niceties right |
1626 | // we re-bless the reference to get overload and other niceties right |
1554 | PUSHs (tag); |
1627 | PUSHs (tag); |
1555 | PUSHs (sv_json); |
1628 | PUSHs (sv_json); |
1556 | |
1629 | |
… | |
… | |
1598 | case 't': |
1671 | case 't': |
1599 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1672 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1600 | { |
1673 | { |
1601 | dec->cur += 4; |
1674 | dec->cur += 4; |
1602 | #if JSON_SLOW |
1675 | #if JSON_SLOW |
1603 | types_true = get_bool ("Types::Serialiser::true"); |
1676 | bool_true = get_bool ("Types::Serialiser::true"); |
1604 | #endif |
1677 | #endif |
1605 | return newSVsv (types_true); |
1678 | return newSVsv (bool_true); |
1606 | } |
1679 | } |
1607 | else |
1680 | else |
1608 | ERR ("'true' expected"); |
1681 | ERR ("'true' expected"); |
1609 | |
1682 | |
1610 | break; |
1683 | break; |
… | |
… | |
1612 | case 'f': |
1685 | case 'f': |
1613 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1686 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1614 | { |
1687 | { |
1615 | dec->cur += 5; |
1688 | dec->cur += 5; |
1616 | #if JSON_SLOW |
1689 | #if JSON_SLOW |
1617 | types_false = get_bool ("Types::Serialiser::false"); |
1690 | bool_false = get_bool ("Types::Serialiser::false"); |
1618 | #endif |
1691 | #endif |
1619 | return newSVsv (types_false); |
1692 | return newSVsv (bool_false); |
1620 | } |
1693 | } |
1621 | else |
1694 | else |
1622 | ERR ("'false' expected"); |
1695 | ERR ("'false' expected"); |
1623 | |
1696 | |
1624 | break; |
1697 | break; |
… | |
… | |
1642 | fail: |
1715 | fail: |
1643 | return 0; |
1716 | return 0; |
1644 | } |
1717 | } |
1645 | |
1718 | |
1646 | static SV * |
1719 | static SV * |
1647 | decode_json (SV *string, JSON *json, char **offset_return) |
1720 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1648 | { |
1721 | { |
1649 | dec_t dec; |
1722 | dec_t dec; |
1650 | SV *sv; |
1723 | SV *sv; |
1651 | |
1724 | |
1652 | /* work around bugs in 5.10 where manipulating magic values |
1725 | /* work around bugs in 5.10 where manipulating magic values |
1653 | * makes perl ignore the magic in subsequent accesses. |
1726 | * makes perl ignore the magic in subsequent accesses. |
1654 | * also make a copy of non-PV values, to get them into a clean |
1727 | * also make a copy of non-PV values, to get them into a clean |
1655 | * state (SvPV should do that, but it's buggy, see below). |
1728 | * state (SvPV should do that, but it's buggy, see below). |
|
|
1729 | * |
|
|
1730 | * SvIsCOW_shared_hash works around a bug in perl (possibly 5.16), |
|
|
1731 | * as reported by Reini Urban. |
1656 | */ |
1732 | */ |
1657 | /*SvGETMAGIC (string);*/ |
1733 | /*SvGETMAGIC (string);*/ |
1658 | if (SvMAGICAL (string) || !SvPOK (string)) |
1734 | if (SvMAGICAL (string) || !SvPOK (string) || SvIsCOW_shared_hash (string)) |
1659 | string = sv_2mortal (newSVsv (string)); |
1735 | string = sv_2mortal (newSVsv (string)); |
1660 | |
1736 | |
1661 | SvUPGRADE (string, SVt_PV); |
1737 | SvUPGRADE (string, SVt_PV); |
1662 | |
1738 | |
1663 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1739 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
… | |
… | |
1702 | |
1778 | |
1703 | decode_ws (&dec); |
1779 | decode_ws (&dec); |
1704 | sv = decode_sv (&dec); |
1780 | sv = decode_sv (&dec); |
1705 | |
1781 | |
1706 | if (offset_return) |
1782 | if (offset_return) |
1707 | *offset_return = dec.cur; |
1783 | *offset_return = dec.cur - SvPVX (string); |
1708 | |
1784 | else if (sv) |
1709 | if (!(offset_return || !sv)) |
|
|
1710 | { |
1785 | { |
1711 | // check for trailing garbage |
1786 | // check for trailing garbage |
1712 | decode_ws (&dec); |
1787 | decode_ws (&dec); |
1713 | |
1788 | |
1714 | if (*dec.cur) |
1789 | if (*dec.cur) |
… | |
… | |
1738 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1813 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1739 | } |
1814 | } |
1740 | |
1815 | |
1741 | sv = sv_2mortal (sv); |
1816 | sv = sv_2mortal (sv); |
1742 | |
1817 | |
1743 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1818 | if (!(dec.json.flags & F_ALLOW_NONREF) && json_nonref (sv)) |
1744 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1819 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1745 | |
1820 | |
1746 | return sv; |
1821 | return sv; |
1747 | } |
1822 | } |
1748 | |
1823 | |
… | |
… | |
1918 | i >= '0' && i <= '9' ? i - '0' |
1993 | i >= '0' && i <= '9' ? i - '0' |
1919 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1994 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1920 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1995 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1921 | : -1; |
1996 | : -1; |
1922 | |
1997 | |
1923 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1998 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1924 | types_boolean_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1999 | bool_stash = gv_stashpv ("Types::Serialiser::Boolean", 1); |
1925 | |
|
|
1926 | types_true = get_bool ("Types::Serialiser::true"); |
2000 | bool_true = get_bool ("Types::Serialiser::true"); |
1927 | types_false = get_bool ("Types::Serialiser::false"); |
2001 | bool_false = get_bool ("Types::Serialiser::false"); |
1928 | |
2002 | |
1929 | sv_json = newSVpv ("JSON", 0); |
2003 | sv_json = newSVpv ("JSON", 0); |
1930 | SvREADONLY_on (sv_json); |
2004 | SvREADONLY_on (sv_json); |
1931 | |
2005 | |
1932 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
2006 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
… | |
… | |
1934 | |
2008 | |
1935 | PROTOTYPES: DISABLE |
2009 | PROTOTYPES: DISABLE |
1936 | |
2010 | |
1937 | void CLONE (...) |
2011 | void CLONE (...) |
1938 | CODE: |
2012 | CODE: |
1939 | json_stash = 0; |
2013 | json_stash = 0; |
1940 | types_boolean_stash = 0; |
2014 | bool_stash = 0; |
1941 | |
2015 | |
1942 | void new (char *klass) |
2016 | void new (char *klass) |
1943 | PPCODE: |
2017 | PPCODE: |
1944 | { |
2018 | { |
1945 | SV *pv = NEWSV (0, sizeof (JSON)); |
2019 | SV *pv = NEWSV (0, sizeof (JSON)); |
… | |
… | |
2062 | |
2136 | |
2063 | void decode_prefix (JSON *self, SV *jsonstr) |
2137 | void decode_prefix (JSON *self, SV *jsonstr) |
2064 | PPCODE: |
2138 | PPCODE: |
2065 | { |
2139 | { |
2066 | SV *sv; |
2140 | SV *sv; |
2067 | char *offset; |
2141 | STRLEN offset; |
2068 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2142 | PUTBACK; sv = decode_json (jsonstr, self, &offset); SPAGAIN; |
2069 | EXTEND (SP, 2); |
2143 | EXTEND (SP, 2); |
2070 | PUSHs (sv); |
2144 | PUSHs (sv); |
2071 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
2145 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, SvPV_nolen (jsonstr) + offset)))); |
2072 | } |
2146 | } |
2073 | |
2147 | |
2074 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2148 | void incr_parse (JSON *self, SV *jsonstr = 0) |
2075 | PPCODE: |
2149 | PPCODE: |
2076 | { |
2150 | { |
… | |
… | |
2110 | { |
2184 | { |
2111 | STRLEN len; |
2185 | STRLEN len; |
2112 | const char *str = SvPV (jsonstr, len); |
2186 | const char *str = SvPV (jsonstr, len); |
2113 | STRLEN cur = SvCUR (self->incr_text); |
2187 | STRLEN cur = SvCUR (self->incr_text); |
2114 | |
2188 | |
2115 | if (SvLEN (self->incr_text) <= cur + len) |
2189 | if (SvLEN (self->incr_text) - cur <= len) |
2116 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
2190 | json_sv_grow (self->incr_text, cur, len); |
2117 | |
2191 | |
2118 | Move (str, SvEND (self->incr_text), len, char); |
2192 | Move (str, SvEND (self->incr_text), len, char); |
2119 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
2193 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
2120 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
2194 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
2121 | } |
2195 | } |
… | |
… | |
2123 | |
2197 | |
2124 | if (GIMME_V != G_VOID) |
2198 | if (GIMME_V != G_VOID) |
2125 | do |
2199 | do |
2126 | { |
2200 | { |
2127 | SV *sv; |
2201 | SV *sv; |
2128 | char *offset; |
2202 | STRLEN offset; |
2129 | |
2203 | |
2130 | if (!INCR_DONE (self)) |
2204 | if (!INCR_DONE (self)) |
2131 | { |
2205 | { |
2132 | incr_parse (self); |
2206 | incr_parse (self); |
2133 | |
2207 | |
… | |
… | |
2149 | } |
2223 | } |
2150 | |
2224 | |
2151 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2225 | PUTBACK; sv = decode_json (self->incr_text, self, &offset); SPAGAIN; |
2152 | XPUSHs (sv); |
2226 | XPUSHs (sv); |
2153 | |
2227 | |
2154 | self->incr_pos -= offset - SvPVX (self->incr_text); |
2228 | self->incr_pos -= offset; |
2155 | self->incr_nest = 0; |
2229 | self->incr_nest = 0; |
2156 | self->incr_mode = 0; |
2230 | self->incr_mode = 0; |
2157 | |
2231 | |
2158 | sv_chop (self->incr_text, offset); |
2232 | sv_chop (self->incr_text, SvPVX (self->incr_text) + offset); |
2159 | } |
2233 | } |
2160 | while (GIMME_V == G_ARRAY); |
2234 | while (GIMME_V == G_ARRAY); |
2161 | } |
2235 | } |
2162 | |
2236 | |
2163 | SV *incr_text (JSON *self) |
2237 | SV *incr_text (JSON *self) |