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