… | |
… | |
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" |
|
|
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
8 | |
13 | |
9 | #define F_ASCII 0x00000001UL |
14 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002UL |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004UL |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008UL |
17 | #define F_CANONICAL 0x00000008UL |
… | |
… | |
21 | |
26 | |
22 | // F_SELFCONVERT? <=> to_json/toJson |
27 | // F_SELFCONVERT? <=> to_json/toJson |
23 | // F_BLESSED? <=> { $__class__$ => } |
28 | // F_BLESSED? <=> { $__class__$ => } |
24 | |
29 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
30 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (12UL << S_MAXDEPTH) |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
32 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
34 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
35 | |
31 | #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 |
… | |
… | |
104 | if (enc->cur + len >= enc->end) |
109 | if (enc->cur + len >= enc->end) |
105 | { |
110 | { |
106 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
111 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
107 | SvGROW (enc->sv, cur + len + 1); |
112 | SvGROW (enc->sv, cur + len + 1); |
108 | enc->cur = SvPVX (enc->sv) + cur; |
113 | enc->cur = SvPVX (enc->sv) + cur; |
109 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
114 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
110 | } |
115 | } |
111 | } |
116 | } |
112 | |
117 | |
113 | static void |
118 | static void |
114 | encode_ch (enc_t *enc, char ch) |
119 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
408 | |
413 | |
409 | encode_nl (enc); |
414 | encode_nl (enc); |
410 | } |
415 | } |
411 | else |
416 | else |
412 | { |
417 | { |
413 | SV *sv; |
|
|
414 | HE *he = hv_iternext (hv); |
418 | HE *he = hv_iternext (hv); |
415 | |
419 | |
416 | for (;;) |
420 | for (;;) |
417 | { |
421 | { |
418 | encode_indent (enc); |
422 | encode_indent (enc); |
… | |
… | |
433 | |
437 | |
434 | // encode objects, arrays and special \0=false and \1=true values. |
438 | // encode objects, arrays and special \0=false and \1=true values. |
435 | static void |
439 | static void |
436 | encode_rv (enc_t *enc, SV *sv) |
440 | encode_rv (enc_t *enc, SV *sv) |
437 | { |
441 | { |
|
|
442 | svtype svt; |
|
|
443 | |
438 | SvGETMAGIC (sv); |
444 | SvGETMAGIC (sv); |
439 | |
|
|
440 | svtype svt = SvTYPE (sv); |
445 | svt = SvTYPE (sv); |
441 | |
446 | |
442 | if (svt == SVt_PVHV) |
447 | if (svt == SVt_PVHV) |
443 | encode_hv (enc, (HV *)sv); |
448 | encode_hv (enc, (HV *)sv); |
444 | else if (svt == SVt_PVAV) |
449 | else if (svt == SVt_PVAV) |
445 | encode_av (enc, (AV *)sv); |
450 | encode_av (enc, (AV *)sv); |
… | |
… | |
495 | } |
500 | } |
496 | |
501 | |
497 | static SV * |
502 | static SV * |
498 | encode_json (SV *scalar, U32 flags) |
503 | encode_json (SV *scalar, U32 flags) |
499 | { |
504 | { |
|
|
505 | enc_t enc; |
|
|
506 | |
500 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
507 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
501 | 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)"); |
502 | |
509 | |
503 | enc_t enc; |
|
|
504 | enc.flags = flags; |
510 | enc.flags = flags; |
505 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
511 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
506 | enc.cur = SvPVX (enc.sv); |
512 | enc.cur = SvPVX (enc.sv); |
507 | enc.end = SvEND (enc.sv); |
513 | enc.end = SvEND (enc.sv); |
508 | enc.indent = 0; |
514 | enc.indent = 0; |
… | |
… | |
513 | |
519 | |
514 | if (!(flags & (F_ASCII | F_UTF8))) |
520 | if (!(flags & (F_ASCII | F_UTF8))) |
515 | SvUTF8_on (enc.sv); |
521 | SvUTF8_on (enc.sv); |
516 | |
522 | |
517 | 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 |
518 | |
525 | |
519 | if (enc.flags & F_SHRINK) |
526 | if (enc.flags & F_SHRINK) |
520 | shrink (enc.sv); |
527 | shrink (enc.sv); |
521 | |
528 | |
522 | return enc.sv; |
529 | return enc.sv; |
… | |
… | |
670 | } |
677 | } |
671 | else if (ch >= 0x20 && ch <= 0x7f) |
678 | else if (ch >= 0x20 && ch <= 0x7f) |
672 | *cur++ = ch; |
679 | *cur++ = ch; |
673 | else if (ch >= 0x80) |
680 | else if (ch >= 0x80) |
674 | { |
681 | { |
|
|
682 | STRLEN clen; |
|
|
683 | UV uch; |
|
|
684 | |
675 | --dec->cur; |
685 | --dec->cur; |
676 | |
686 | |
677 | STRLEN clen; |
|
|
678 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
687 | uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
679 | if (clen == (STRLEN)-1) |
688 | if (clen == (STRLEN)-1) |
680 | ERR ("malformed UTF-8 character in JSON string"); |
689 | ERR ("malformed UTF-8 character in JSON string"); |
681 | |
690 | |
682 | do |
691 | do |
683 | *cur++ = *dec->cur++; |
692 | *cur++ = *dec->cur++; |
… | |
… | |
688 | else |
697 | else |
689 | { |
698 | { |
690 | --dec->cur; |
699 | --dec->cur; |
691 | |
700 | |
692 | if (!ch) |
701 | if (!ch) |
693 | ERR ("unexpected end of string while parsing json string"); |
702 | ERR ("unexpected end of string while parsing JSON string"); |
694 | else |
703 | else |
695 | ERR ("invalid character encountered while parsing json string"); |
704 | ERR ("invalid character encountered while parsing JSON string"); |
696 | } |
705 | } |
697 | } |
706 | } |
698 | while (cur < buf + SHORT_STRING_LEN); |
707 | while (cur < buf + SHORT_STRING_LEN); |
699 | |
708 | |
|
|
709 | { |
700 | STRLEN len = cur - buf; |
710 | STRLEN len = cur - buf; |
701 | |
711 | |
702 | if (sv) |
712 | if (sv) |
703 | { |
713 | { |
704 | SvGROW (sv, SvCUR (sv) + len + 1); |
714 | SvGROW (sv, SvCUR (sv) + len + 1); |
705 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
715 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
706 | SvCUR_set (sv, SvCUR (sv) + len); |
716 | SvCUR_set (sv, SvCUR (sv) + len); |
707 | } |
717 | } |
708 | else |
718 | else |
709 | sv = newSVpvn (buf, len); |
719 | sv = newSVpvn (buf, len); |
|
|
720 | } |
710 | } |
721 | } |
711 | while (*dec->cur != '"'); |
722 | while (*dec->cur != '"'); |
712 | |
723 | |
713 | ++dec->cur; |
724 | ++dec->cur; |
714 | |
725 | |
… | |
… | |
958 | ERR ("'null' expected"); |
969 | ERR ("'null' expected"); |
959 | |
970 | |
960 | break; |
971 | break; |
961 | |
972 | |
962 | default: |
973 | default: |
963 | ERR ("malformed json string, neither array, object, number, string or atom"); |
974 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
964 | break; |
975 | break; |
965 | } |
976 | } |
966 | |
977 | |
967 | fail: |
978 | fail: |
968 | return 0; |
979 | return 0; |
969 | } |
980 | } |
970 | |
981 | |
971 | static SV * |
982 | static SV * |
972 | decode_json (SV *string, U32 flags) |
983 | decode_json (SV *string, U32 flags) |
973 | { |
984 | { |
|
|
985 | dec_t dec; |
974 | SV *sv; |
986 | SV *sv; |
975 | |
987 | |
976 | SvUPGRADE (string, SVt_PV); |
988 | SvUPGRADE (string, SVt_PV); |
977 | |
989 | |
978 | if (flags & F_UTF8) |
990 | if (flags & F_UTF8) |
… | |
… | |
980 | else |
992 | else |
981 | sv_utf8_upgrade (string); |
993 | sv_utf8_upgrade (string); |
982 | |
994 | |
983 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
995 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
984 | |
996 | |
985 | dec_t dec; |
|
|
986 | dec.flags = flags; |
997 | dec.flags = flags; |
987 | dec.cur = SvPVX (string); |
998 | dec.cur = SvPVX (string); |
988 | dec.end = SvEND (string); |
999 | dec.end = SvEND (string); |
989 | dec.err = 0; |
1000 | dec.err = 0; |
990 | dec.depth = 0; |
1001 | dec.depth = 0; |
… | |
… | |
1074 | RETVAL = newSVsv (self); |
1085 | RETVAL = newSVsv (self); |
1075 | } |
1086 | } |
1076 | OUTPUT: |
1087 | OUTPUT: |
1077 | RETVAL |
1088 | RETVAL |
1078 | |
1089 | |
1079 | SV *max_depth (SV *self, int max_depth = 0x80000000UL) |
1090 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1080 | CODE: |
1091 | CODE: |
1081 | { |
1092 | { |
1082 | UV *uv = SvJSON (self); |
1093 | UV *uv = SvJSON (self); |
1083 | UV log2 = 0; |
1094 | UV log2 = 0; |
1084 | |
1095 | |