… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
8 | |
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
|
|
13 | |
9 | #define F_ASCII 0x00000001 |
14 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002 |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004 |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008 |
17 | #define F_CANONICAL 0x00000008UL |
13 | #define F_SPACE_BEFORE 0x00000010 |
18 | #define F_SPACE_BEFORE 0x00000010UL |
14 | #define F_SPACE_AFTER 0x00000020 |
19 | #define F_SPACE_AFTER 0x00000020UL |
15 | #define F_ALLOW_NONREF 0x00000080 |
20 | #define F_ALLOW_NONREF 0x00000080UL |
16 | #define F_SHRINK 0x00000100 |
21 | #define F_SHRINK 0x00000100UL |
|
|
22 | #define F_MAXDEPTH 0xf8000000UL |
|
|
23 | #define S_MAXDEPTH 27 |
|
|
24 | |
|
|
25 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
26 | |
|
|
27 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
28 | // F_BLESSED? <=> { $__class__$ => } |
17 | |
29 | |
18 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
19 | #define F_DEFAULT 0 |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
20 | |
32 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
34 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
35 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
36 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
… | |
… | |
69 | { |
81 | { |
70 | *clen = 2; |
82 | *clen = 2; |
71 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
83 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
72 | } |
84 | } |
73 | else |
85 | else |
|
|
86 | { |
|
|
87 | *clen = (STRLEN)-1; |
74 | return (UV)-1; |
88 | return (UV)-1; |
|
|
89 | } |
75 | } |
90 | } |
76 | |
91 | |
77 | ///////////////////////////////////////////////////////////////////////////// |
92 | ///////////////////////////////////////////////////////////////////////////// |
78 | // encoder |
93 | // encoder |
79 | |
94 | |
… | |
… | |
81 | typedef struct |
96 | typedef struct |
82 | { |
97 | { |
83 | char *cur; // SvPVX (sv) + current output position |
98 | char *cur; // SvPVX (sv) + current output position |
84 | char *end; // SvEND (sv) |
99 | char *end; // SvEND (sv) |
85 | SV *sv; // result scalar |
100 | SV *sv; // result scalar |
86 | UV flags; // F_* |
101 | U32 flags; // F_* |
87 | int indent; // indentation level |
102 | U32 indent; // indentation level |
88 | int max_depth; // max. recursion level |
103 | U32 maxdepth; // max. indentation/recursion level |
89 | } enc_t; |
104 | } enc_t; |
90 | |
105 | |
91 | static void |
106 | static void |
92 | need (enc_t *enc, STRLEN len) |
107 | need (enc_t *enc, STRLEN len) |
93 | { |
108 | { |
94 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
95 | { |
110 | { |
96 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
97 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
98 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
99 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
100 | } |
115 | } |
101 | } |
116 | } |
102 | |
117 | |
103 | static void |
118 | static void |
104 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
261 | static void |
276 | static void |
262 | encode_av (enc_t *enc, AV *av) |
277 | encode_av (enc_t *enc, AV *av) |
263 | { |
278 | { |
264 | int i, len = av_len (av); |
279 | int i, len = av_len (av); |
265 | |
280 | |
|
|
281 | if (enc->indent >= enc->maxdepth) |
|
|
282 | croak ("data structure too deep (hit recursion limit)"); |
|
|
283 | |
266 | encode_ch (enc, '['); encode_nl (enc); |
284 | encode_ch (enc, '['); encode_nl (enc); |
267 | ++enc->indent; |
285 | ++enc->indent; |
268 | |
286 | |
269 | for (i = 0; i <= len; ++i) |
287 | for (i = 0; i <= len; ++i) |
270 | { |
288 | { |
… | |
… | |
335 | |
353 | |
336 | static void |
354 | static void |
337 | encode_hv (enc_t *enc, HV *hv) |
355 | encode_hv (enc_t *enc, HV *hv) |
338 | { |
356 | { |
339 | int count, i; |
357 | int count, i; |
|
|
358 | |
|
|
359 | if (enc->indent >= enc->maxdepth) |
|
|
360 | croak ("data structure too deep (hit recursion limit)"); |
340 | |
361 | |
341 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
362 | encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; |
342 | |
363 | |
343 | if ((count = hv_iterinit (hv))) |
364 | if ((count = hv_iterinit (hv))) |
344 | { |
365 | { |
… | |
… | |
392 | |
413 | |
393 | encode_nl (enc); |
414 | encode_nl (enc); |
394 | } |
415 | } |
395 | else |
416 | else |
396 | { |
417 | { |
397 | SV *sv; |
|
|
398 | HE *he = hv_iternext (hv); |
418 | HE *he = hv_iternext (hv); |
399 | |
419 | |
400 | for (;;) |
420 | for (;;) |
401 | { |
421 | { |
402 | encode_indent (enc); |
422 | encode_indent (enc); |
… | |
… | |
411 | encode_nl (enc); |
431 | encode_nl (enc); |
412 | } |
432 | } |
413 | } |
433 | } |
414 | |
434 | |
415 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
435 | --enc->indent; encode_indent (enc); encode_ch (enc, '}'); |
|
|
436 | } |
|
|
437 | |
|
|
438 | // encode objects, arrays and special \0=false and \1=true values. |
|
|
439 | static void |
|
|
440 | encode_rv (enc_t *enc, SV *sv) |
|
|
441 | { |
|
|
442 | svtype svt; |
|
|
443 | |
|
|
444 | SvGETMAGIC (sv); |
|
|
445 | svt = SvTYPE (sv); |
|
|
446 | |
|
|
447 | if (svt == SVt_PVHV) |
|
|
448 | encode_hv (enc, (HV *)sv); |
|
|
449 | else if (svt == SVt_PVAV) |
|
|
450 | encode_av (enc, (AV *)sv); |
|
|
451 | else if (svt < SVt_PVAV) |
|
|
452 | { |
|
|
453 | if (SvNIOK (sv) && SvIV (sv) == 0) |
|
|
454 | encode_str (enc, "false", 5, 0); |
|
|
455 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
456 | encode_str (enc, "true", 4, 0); |
|
|
457 | else |
|
|
458 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
|
|
459 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
460 | } |
|
|
461 | else |
|
|
462 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
463 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
416 | } |
464 | } |
417 | |
465 | |
418 | static void |
466 | static void |
419 | encode_sv (enc_t *enc, SV *sv) |
467 | encode_sv (enc_t *enc, SV *sv) |
420 | { |
468 | { |
… | |
… | |
441 | SvIsUV(sv) |
489 | SvIsUV(sv) |
442 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
490 | ? snprintf (enc->cur, 64, "%"UVuf, (UV)SvUVX (sv)) |
443 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
491 | : snprintf (enc->cur, 64, "%"IVdf, (IV)SvIVX (sv)); |
444 | } |
492 | } |
445 | else if (SvROK (sv)) |
493 | else if (SvROK (sv)) |
446 | { |
494 | encode_rv (enc, SvRV (sv)); |
447 | SV *rv = SvRV (sv); |
|
|
448 | |
|
|
449 | if (enc->indent >= enc->max_depth) |
|
|
450 | croak ("data structure too deep (hit recursion limit)"); |
|
|
451 | |
|
|
452 | switch (SvTYPE (rv)) |
|
|
453 | { |
|
|
454 | case SVt_PVAV: encode_av (enc, (AV *)rv); break; |
|
|
455 | case SVt_PVHV: encode_hv (enc, (HV *)rv); break; |
|
|
456 | |
|
|
457 | default: |
|
|
458 | croak ("encountered %s, but JSON can only represent references to arrays or hashes", |
|
|
459 | SvPV_nolen (sv)); |
|
|
460 | } |
|
|
461 | } |
|
|
462 | else if (!SvOK (sv)) |
495 | else if (!SvOK (sv)) |
463 | encode_str (enc, "null", 4, 0); |
496 | encode_str (enc, "null", 4, 0); |
464 | else |
497 | else |
465 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
498 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
466 | SvPV_nolen (sv), SvFLAGS (sv)); |
499 | SvPV_nolen (sv), SvFLAGS (sv)); |
467 | } |
500 | } |
468 | |
501 | |
469 | static SV * |
502 | static SV * |
470 | encode_json (SV *scalar, UV flags) |
503 | encode_json (SV *scalar, U32 flags) |
471 | { |
504 | { |
|
|
505 | enc_t enc; |
|
|
506 | |
472 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
507 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
473 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
508 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
474 | |
509 | |
475 | enc_t enc; |
|
|
476 | enc.flags = flags; |
510 | enc.flags = flags; |
477 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
511 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
478 | enc.cur = SvPVX (enc.sv); |
512 | enc.cur = SvPVX (enc.sv); |
479 | enc.end = SvEND (enc.sv); |
513 | enc.end = SvEND (enc.sv); |
480 | enc.indent = 0; |
514 | enc.indent = 0; |
481 | enc.max_depth = 0x7fffffffUL; |
515 | enc.maxdepth = DEC_DEPTH (flags); |
482 | |
516 | |
483 | SvPOK_only (enc.sv); |
517 | SvPOK_only (enc.sv); |
484 | encode_sv (&enc, scalar); |
518 | encode_sv (&enc, scalar); |
485 | |
519 | |
486 | if (!(flags & (F_ASCII | F_UTF8))) |
520 | if (!(flags & (F_ASCII | F_UTF8))) |
487 | SvUTF8_on (enc.sv); |
521 | SvUTF8_on (enc.sv); |
488 | |
522 | |
489 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
523 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
524 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
490 | |
525 | |
491 | if (enc.flags & F_SHRINK) |
526 | if (enc.flags & F_SHRINK) |
492 | shrink (enc.sv); |
527 | shrink (enc.sv); |
493 | |
528 | |
494 | return enc.sv; |
529 | return enc.sv; |
… | |
… | |
501 | typedef struct |
536 | typedef struct |
502 | { |
537 | { |
503 | char *cur; // current parser pointer |
538 | char *cur; // current parser pointer |
504 | char *end; // end of input string |
539 | char *end; // end of input string |
505 | const char *err; // parse error, if != 0 |
540 | const char *err; // parse error, if != 0 |
506 | UV flags; // F_* |
541 | U32 flags; // F_* |
|
|
542 | U32 depth; // recursion depth |
|
|
543 | U32 maxdepth; // recursion depth limit |
507 | } dec_t; |
544 | } dec_t; |
508 | |
545 | |
509 | static void |
546 | static void |
510 | decode_ws (dec_t *dec) |
547 | decode_ws (dec_t *dec) |
511 | { |
548 | { |
… | |
… | |
520 | ++dec->cur; |
557 | ++dec->cur; |
521 | } |
558 | } |
522 | } |
559 | } |
523 | |
560 | |
524 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
561 | #define ERR(reason) SB dec->err = reason; goto fail; SE |
|
|
562 | |
525 | #define EXPECT_CH(ch) SB \ |
563 | #define EXPECT_CH(ch) SB \ |
526 | if (*dec->cur != ch) \ |
564 | if (*dec->cur != ch) \ |
527 | ERR (# ch " expected"); \ |
565 | ERR (# ch " expected"); \ |
528 | ++dec->cur; \ |
566 | ++dec->cur; \ |
529 | SE |
567 | SE |
|
|
568 | |
|
|
569 | #define DEC_INC_DEPTH if (++dec->depth > dec->maxdepth) ERR ("json datastructure exceeds maximum nesting level (set a higher max_depth)") |
|
|
570 | #define DEC_DEC_DEPTH --dec->depth |
530 | |
571 | |
531 | static SV *decode_sv (dec_t *dec); |
572 | static SV *decode_sv (dec_t *dec); |
532 | |
573 | |
533 | static signed char decode_hexdigit[256]; |
574 | static signed char decode_hexdigit[256]; |
534 | |
575 | |
… | |
… | |
636 | } |
677 | } |
637 | else if (ch >= 0x20 && ch <= 0x7f) |
678 | else if (ch >= 0x20 && ch <= 0x7f) |
638 | *cur++ = ch; |
679 | *cur++ = ch; |
639 | else if (ch >= 0x80) |
680 | else if (ch >= 0x80) |
640 | { |
681 | { |
|
|
682 | STRLEN clen; |
|
|
683 | UV uch; |
|
|
684 | |
641 | --dec->cur; |
685 | --dec->cur; |
642 | |
686 | |
643 | STRLEN clen; |
|
|
644 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
687 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
645 | if (clen == (STRLEN)-1) |
688 | if (clen == (STRLEN)-1) |
646 | ERR ("malformed UTF-8 character in JSON string"); |
689 | ERR ("malformed UTF-8 character in JSON string"); |
647 | |
690 | |
648 | do |
691 | do |
649 | { |
|
|
650 | *cur++ = *dec->cur++; |
692 | *cur++ = *dec->cur++; |
651 | } |
|
|
652 | while (--clen); |
693 | while (--clen); |
653 | |
694 | |
654 | utf8 = 1; |
695 | utf8 = 1; |
655 | } |
696 | } |
656 | else if (!ch) |
|
|
657 | ERR ("unexpected end of string while parsing json string"); |
|
|
658 | else |
697 | else |
|
|
698 | { |
|
|
699 | --dec->cur; |
|
|
700 | |
|
|
701 | if (!ch) |
|
|
702 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
703 | else |
659 | ERR ("invalid character encountered"); |
704 | ERR ("invalid character encountered while parsing JSON string"); |
660 | |
705 | } |
661 | } |
706 | } |
662 | while (cur < buf + SHORT_STRING_LEN); |
707 | while (cur < buf + SHORT_STRING_LEN); |
663 | |
708 | |
|
|
709 | { |
664 | STRLEN len = cur - buf; |
710 | STRLEN len = cur - buf; |
665 | |
711 | |
666 | if (sv) |
712 | if (sv) |
667 | { |
713 | { |
668 | SvGROW (sv, SvCUR (sv) + len + 1); |
714 | SvGROW (sv, SvCUR (sv) + len + 1); |
669 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
715 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
670 | SvCUR_set (sv, SvCUR (sv) + len); |
716 | SvCUR_set (sv, SvCUR (sv) + len); |
671 | } |
717 | } |
672 | else |
718 | else |
673 | sv = newSVpvn (buf, len); |
719 | sv = newSVpvn (buf, len); |
|
|
720 | } |
674 | } |
721 | } |
675 | while (*dec->cur != '"'); |
722 | while (*dec->cur != '"'); |
676 | |
723 | |
677 | ++dec->cur; |
724 | ++dec->cur; |
678 | |
725 | |
… | |
… | |
778 | static SV * |
825 | static SV * |
779 | decode_av (dec_t *dec) |
826 | decode_av (dec_t *dec) |
780 | { |
827 | { |
781 | AV *av = newAV (); |
828 | AV *av = newAV (); |
782 | |
829 | |
|
|
830 | DEC_INC_DEPTH; |
783 | decode_ws (dec); |
831 | decode_ws (dec); |
|
|
832 | |
784 | if (*dec->cur == ']') |
833 | if (*dec->cur == ']') |
785 | ++dec->cur; |
834 | ++dec->cur; |
786 | else |
835 | else |
787 | for (;;) |
836 | for (;;) |
788 | { |
837 | { |
… | |
… | |
806 | ERR (", or ] expected while parsing array"); |
855 | ERR (", or ] expected while parsing array"); |
807 | |
856 | |
808 | ++dec->cur; |
857 | ++dec->cur; |
809 | } |
858 | } |
810 | |
859 | |
|
|
860 | DEC_DEC_DEPTH; |
811 | return newRV_noinc ((SV *)av); |
861 | return newRV_noinc ((SV *)av); |
812 | |
862 | |
813 | fail: |
863 | fail: |
814 | SvREFCNT_dec (av); |
864 | SvREFCNT_dec (av); |
|
|
865 | DEC_DEC_DEPTH; |
815 | return 0; |
866 | return 0; |
816 | } |
867 | } |
817 | |
868 | |
818 | static SV * |
869 | static SV * |
819 | decode_hv (dec_t *dec) |
870 | decode_hv (dec_t *dec) |
820 | { |
871 | { |
821 | HV *hv = newHV (); |
872 | HV *hv = newHV (); |
822 | |
873 | |
|
|
874 | DEC_INC_DEPTH; |
823 | decode_ws (dec); |
875 | decode_ws (dec); |
|
|
876 | |
824 | if (*dec->cur == '}') |
877 | if (*dec->cur == '}') |
825 | ++dec->cur; |
878 | ++dec->cur; |
826 | else |
879 | else |
827 | for (;;) |
880 | for (;;) |
828 | { |
881 | { |
… | |
… | |
841 | { |
894 | { |
842 | SvREFCNT_dec (key); |
895 | SvREFCNT_dec (key); |
843 | goto fail; |
896 | goto fail; |
844 | } |
897 | } |
845 | |
898 | |
846 | //TODO: optimise |
|
|
847 | hv_store_ent (hv, key, value, 0); |
899 | hv_store_ent (hv, key, value, 0); |
|
|
900 | SvREFCNT_dec (key); |
848 | |
901 | |
849 | decode_ws (dec); |
902 | decode_ws (dec); |
850 | |
903 | |
851 | if (*dec->cur == '}') |
904 | if (*dec->cur == '}') |
852 | { |
905 | { |
… | |
… | |
858 | ERR (", or } expected while parsing object/hash"); |
911 | ERR (", or } expected while parsing object/hash"); |
859 | |
912 | |
860 | ++dec->cur; |
913 | ++dec->cur; |
861 | } |
914 | } |
862 | |
915 | |
|
|
916 | DEC_DEC_DEPTH; |
863 | return newRV_noinc ((SV *)hv); |
917 | return newRV_noinc ((SV *)hv); |
864 | |
918 | |
865 | fail: |
919 | fail: |
866 | SvREFCNT_dec (hv); |
920 | SvREFCNT_dec (hv); |
|
|
921 | DEC_DEC_DEPTH; |
867 | return 0; |
922 | return 0; |
868 | } |
923 | } |
869 | |
924 | |
870 | static SV * |
925 | static SV * |
871 | decode_sv (dec_t *dec) |
926 | decode_sv (dec_t *dec) |
… | |
… | |
914 | ERR ("'null' expected"); |
969 | ERR ("'null' expected"); |
915 | |
970 | |
916 | break; |
971 | break; |
917 | |
972 | |
918 | default: |
973 | default: |
919 | ERR ("malformed json string, neither array, object, number, string or atom"); |
974 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
920 | break; |
975 | break; |
921 | } |
976 | } |
922 | |
977 | |
923 | fail: |
978 | fail: |
924 | return 0; |
979 | return 0; |
925 | } |
980 | } |
926 | |
981 | |
927 | static SV * |
982 | static SV * |
928 | decode_json (SV *string, UV flags) |
983 | decode_json (SV *string, U32 flags) |
929 | { |
984 | { |
|
|
985 | dec_t dec; |
930 | SV *sv; |
986 | SV *sv; |
|
|
987 | |
|
|
988 | SvUPGRADE (string, SVt_PV); |
931 | |
989 | |
932 | if (flags & F_UTF8) |
990 | if (flags & F_UTF8) |
933 | sv_utf8_downgrade (string, 0); |
991 | sv_utf8_downgrade (string, 0); |
934 | else |
992 | else |
935 | sv_utf8_upgrade (string); |
993 | sv_utf8_upgrade (string); |
936 | |
994 | |
937 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
995 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
938 | |
996 | |
939 | dec_t dec; |
|
|
940 | dec.flags = flags; |
997 | dec.flags = flags; |
941 | dec.cur = SvPVX (string); |
998 | dec.cur = SvPVX (string); |
942 | dec.end = SvEND (string); |
999 | dec.end = SvEND (string); |
943 | dec.err = 0; |
1000 | dec.err = 0; |
|
|
1001 | dec.depth = 0; |
|
|
1002 | dec.maxdepth = DEC_DEPTH (dec.flags); |
944 | |
1003 | |
|
|
1004 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
945 | sv = decode_sv (&dec); |
1005 | sv = decode_sv (&dec); |
946 | |
1006 | |
947 | if (!sv) |
1007 | if (!sv) |
948 | { |
1008 | { |
949 | IV offset = dec.flags & F_UTF8 |
1009 | IV offset = dec.flags & F_UTF8 |
… | |
… | |
958 | SAVEVPTR (PL_curcop); |
1018 | SAVEVPTR (PL_curcop); |
959 | PL_curcop = &cop; |
1019 | PL_curcop = &cop; |
960 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1020 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
961 | LEAVE; |
1021 | LEAVE; |
962 | |
1022 | |
963 | croak ("%s, at character offset %d (%s)", |
1023 | croak ("%s, at character offset %d [\"%s\"]", |
964 | dec.err, |
1024 | dec.err, |
965 | (int)offset, |
1025 | (int)offset, |
966 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1026 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
967 | } |
1027 | } |
968 | |
1028 | |
… | |
… | |
982 | BOOT: |
1042 | BOOT: |
983 | { |
1043 | { |
984 | int i; |
1044 | int i; |
985 | |
1045 | |
986 | memset (decode_hexdigit, 0xff, 256); |
1046 | memset (decode_hexdigit, 0xff, 256); |
|
|
1047 | |
987 | for (i = 10; i--; ) |
1048 | for (i = 0; i < 256; ++i) |
988 | decode_hexdigit ['0' + i] = i; |
1049 | decode_hexdigit [i] = |
989 | |
1050 | i >= '0' && i <= '9' ? i - '0' |
990 | for (i = 7; i--; ) |
1051 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
991 | { |
1052 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
992 | decode_hexdigit ['a' + i] = 10 + i; |
1053 | : -1; |
993 | decode_hexdigit ['A' + i] = 10 + i; |
|
|
994 | } |
|
|
995 | |
1054 | |
996 | json_stash = gv_stashpv ("JSON::XS", 1); |
1055 | json_stash = gv_stashpv ("JSON::XS", 1); |
997 | } |
1056 | } |
998 | |
1057 | |
999 | PROTOTYPES: DISABLE |
1058 | PROTOTYPES: DISABLE |
… | |
… | |
1026 | RETVAL = newSVsv (self); |
1085 | RETVAL = newSVsv (self); |
1027 | } |
1086 | } |
1028 | OUTPUT: |
1087 | OUTPUT: |
1029 | RETVAL |
1088 | RETVAL |
1030 | |
1089 | |
|
|
1090 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
|
|
1091 | CODE: |
|
|
1092 | { |
|
|
1093 | UV *uv = SvJSON (self); |
|
|
1094 | UV log2 = 0; |
|
|
1095 | |
|
|
1096 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
|
|
1097 | |
|
|
1098 | while ((1UL << log2) < max_depth) |
|
|
1099 | ++log2; |
|
|
1100 | |
|
|
1101 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
|
|
1102 | |
|
|
1103 | RETVAL = newSVsv (self); |
|
|
1104 | } |
|
|
1105 | OUTPUT: |
|
|
1106 | RETVAL |
|
|
1107 | |
1031 | void encode (SV *self, SV *scalar) |
1108 | void encode (SV *self, SV *scalar) |
1032 | PPCODE: |
1109 | PPCODE: |
1033 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1110 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1034 | |
1111 | |
1035 | void decode (SV *self, SV *jsonstr) |
1112 | void decode (SV *self, SV *jsonstr) |
… | |
… | |
1037 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1114 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1038 | |
1115 | |
1039 | PROTOTYPES: ENABLE |
1116 | PROTOTYPES: ENABLE |
1040 | |
1117 | |
1041 | void to_json (SV *scalar) |
1118 | void to_json (SV *scalar) |
|
|
1119 | ALIAS: |
|
|
1120 | objToJson = 0 |
1042 | PPCODE: |
1121 | PPCODE: |
1043 | XPUSHs (encode_json (scalar, F_UTF8)); |
1122 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1044 | |
1123 | |
1045 | void from_json (SV *jsonstr) |
1124 | void from_json (SV *jsonstr) |
|
|
1125 | ALIAS: |
|
|
1126 | jsonToObj = 0 |
1046 | PPCODE: |
1127 | PPCODE: |
1047 | XPUSHs (decode_json (jsonstr, F_UTF8)); |
1128 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1048 | |
1129 | |