… | |
… | |
983 | fail: |
983 | fail: |
984 | return 0; |
984 | return 0; |
985 | } |
985 | } |
986 | |
986 | |
987 | static SV * |
987 | static SV * |
988 | decode_json (SV *string, U32 flags) |
988 | decode_json (SV *string, U32 flags, UV *offset_return) |
989 | { |
989 | { |
990 | dec_t dec; |
990 | dec_t dec; |
|
|
991 | UV offset; |
991 | SV *sv; |
992 | SV *sv; |
992 | |
993 | |
993 | SvGETMAGIC (string); |
994 | SvGETMAGIC (string); |
994 | SvUPGRADE (string, SVt_PV); |
995 | SvUPGRADE (string, SVt_PV); |
995 | |
996 | |
… | |
… | |
1005 | dec.end = SvEND (string); |
1006 | dec.end = SvEND (string); |
1006 | dec.err = 0; |
1007 | dec.err = 0; |
1007 | dec.depth = 0; |
1008 | dec.depth = 0; |
1008 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1009 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1009 | |
1010 | |
1010 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1011 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1011 | sv = decode_sv (&dec); |
1012 | sv = decode_sv (&dec); |
1012 | |
1013 | |
|
|
1014 | if (!(offset_return || !sv)) |
|
|
1015 | { |
|
|
1016 | // check for trailing garbage |
|
|
1017 | decode_ws (&dec); |
|
|
1018 | |
|
|
1019 | if (*dec.cur) |
|
|
1020 | { |
|
|
1021 | dec.err = "garbage after JSON object"; |
|
|
1022 | SvREFCNT_dec (sv); |
|
|
1023 | sv = 0; |
|
|
1024 | } |
|
|
1025 | } |
|
|
1026 | |
|
|
1027 | if (offset_return || !sv) |
|
|
1028 | { |
|
|
1029 | offset = dec.flags & F_UTF8 |
|
|
1030 | ? dec.cur - SvPVX (string) |
|
|
1031 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1032 | |
|
|
1033 | if (offset_return) |
|
|
1034 | *offset_return = offset; |
|
|
1035 | } |
|
|
1036 | |
1013 | if (!sv) |
1037 | if (!sv) |
1014 | { |
1038 | { |
1015 | IV offset = dec.flags & F_UTF8 |
|
|
1016 | ? dec.cur - SvPVX (string) |
|
|
1017 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1018 | SV *uni = sv_newmortal (); |
1039 | SV *uni = sv_newmortal (); |
1019 | |
1040 | |
1020 | // horrible hack to silence warning inside pv_uni_display |
1041 | // horrible hack to silence warning inside pv_uni_display |
1021 | COP cop = *PL_curcop; |
1042 | COP cop = *PL_curcop; |
1022 | cop.cop_warnings = pWARN_NONE; |
1043 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1116 | PPCODE: |
1137 | PPCODE: |
1117 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1138 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1118 | |
1139 | |
1119 | void decode (SV *self, SV *jsonstr) |
1140 | void decode (SV *self, SV *jsonstr) |
1120 | PPCODE: |
1141 | PPCODE: |
1121 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1142 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1143 | |
|
|
1144 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1145 | PPCODE: |
|
|
1146 | { |
|
|
1147 | UV offset; |
|
|
1148 | EXTEND (SP, 2); |
|
|
1149 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1150 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1151 | } |
1122 | |
1152 | |
1123 | PROTOTYPES: ENABLE |
1153 | PROTOTYPES: ENABLE |
1124 | |
1154 | |
1125 | void to_json (SV *scalar) |
1155 | void to_json (SV *scalar) |
1126 | ALIAS: |
1156 | ALIAS: |
… | |
… | |
1130 | |
1160 | |
1131 | void from_json (SV *jsonstr) |
1161 | void from_json (SV *jsonstr) |
1132 | ALIAS: |
1162 | ALIAS: |
1133 | jsonToObj = 0 |
1163 | jsonToObj = 0 |
1134 | PPCODE: |
1164 | PPCODE: |
1135 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1165 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1136 | |
1166 | |