… | |
… | |
21 | |
21 | |
22 | // F_SELFCONVERT? <=> to_json/toJson |
22 | // F_SELFCONVERT? <=> to_json/toJson |
23 | // F_BLESSED? <=> { $__class__$ => } |
23 | // F_BLESSED? <=> { $__class__$ => } |
24 | |
24 | |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
25 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
26 | #define F_DEFAULT (12UL << S_MAXDEPTH) |
26 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
27 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
29 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
30 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
… | |
… | |
76 | { |
76 | { |
77 | *clen = 2; |
77 | *clen = 2; |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
78 | return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f); |
79 | } |
79 | } |
80 | else |
80 | else |
|
|
81 | { |
|
|
82 | *clen = (STRLEN)-1; |
81 | return (UV)-1; |
83 | return (UV)-1; |
|
|
84 | } |
82 | } |
85 | } |
83 | |
86 | |
84 | ///////////////////////////////////////////////////////////////////////////// |
87 | ///////////////////////////////////////////////////////////////////////////// |
85 | // encoder |
88 | // encoder |
86 | |
89 | |
… | |
… | |
675 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
678 | UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); |
676 | if (clen == (STRLEN)-1) |
679 | if (clen == (STRLEN)-1) |
677 | ERR ("malformed UTF-8 character in JSON string"); |
680 | ERR ("malformed UTF-8 character in JSON string"); |
678 | |
681 | |
679 | do |
682 | do |
680 | { |
|
|
681 | *cur++ = *dec->cur++; |
683 | *cur++ = *dec->cur++; |
682 | } |
|
|
683 | while (--clen); |
684 | while (--clen); |
684 | |
685 | |
685 | utf8 = 1; |
686 | utf8 = 1; |
686 | } |
687 | } |
687 | else if (!ch) |
|
|
688 | ERR ("unexpected end of string while parsing json string"); |
|
|
689 | else |
688 | else |
|
|
689 | { |
|
|
690 | --dec->cur; |
|
|
691 | |
|
|
692 | if (!ch) |
|
|
693 | ERR ("unexpected end of string while parsing JSON string"); |
|
|
694 | else |
690 | ERR ("invalid character encountered"); |
695 | ERR ("invalid character encountered while parsing JSON string"); |
691 | |
696 | } |
692 | } |
697 | } |
693 | while (cur < buf + SHORT_STRING_LEN); |
698 | while (cur < buf + SHORT_STRING_LEN); |
694 | |
699 | |
695 | STRLEN len = cur - buf; |
700 | STRLEN len = cur - buf; |
696 | |
701 | |
… | |
… | |
953 | ERR ("'null' expected"); |
958 | ERR ("'null' expected"); |
954 | |
959 | |
955 | break; |
960 | break; |
956 | |
961 | |
957 | default: |
962 | default: |
958 | ERR ("malformed json string, neither array, object, number, string or atom"); |
963 | ERR ("malformed JSON string, neither array, object, number, string or atom"); |
959 | break; |
964 | break; |
960 | } |
965 | } |
961 | |
966 | |
962 | fail: |
967 | fail: |
963 | return 0; |
968 | return 0; |
… | |
… | |
965 | |
970 | |
966 | static SV * |
971 | static SV * |
967 | decode_json (SV *string, U32 flags) |
972 | decode_json (SV *string, U32 flags) |
968 | { |
973 | { |
969 | SV *sv; |
974 | SV *sv; |
|
|
975 | |
|
|
976 | SvUPGRADE (string, SVt_PV); |
970 | |
977 | |
971 | if (flags & F_UTF8) |
978 | if (flags & F_UTF8) |
972 | sv_utf8_downgrade (string, 0); |
979 | sv_utf8_downgrade (string, 0); |
973 | else |
980 | else |
974 | sv_utf8_upgrade (string); |
981 | sv_utf8_upgrade (string); |
… | |
… | |
1000 | SAVEVPTR (PL_curcop); |
1007 | SAVEVPTR (PL_curcop); |
1001 | PL_curcop = &cop; |
1008 | PL_curcop = &cop; |
1002 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1009 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1003 | LEAVE; |
1010 | LEAVE; |
1004 | |
1011 | |
1005 | croak ("%s, at character offset %d (%s)", |
1012 | croak ("%s, at character offset %d [\"%s\"]", |
1006 | dec.err, |
1013 | dec.err, |
1007 | (int)offset, |
1014 | (int)offset, |
1008 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1015 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1009 | } |
1016 | } |
1010 | |
1017 | |