… | |
… | |
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 | offset = dec.flags & F_UTF8 |
|
|
1017 | ? dec.cur - SvPVX (string) |
|
|
1018 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1019 | |
|
|
1020 | if (offset_return) |
|
|
1021 | *offset_return = offset; |
|
|
1022 | } |
|
|
1023 | else |
|
|
1024 | { |
|
|
1025 | // check for trailing garbage |
|
|
1026 | decode_ws (&dec); |
|
|
1027 | |
|
|
1028 | if (*dec.cur) |
|
|
1029 | { |
|
|
1030 | dec.err = "garbage after JSON object"; |
|
|
1031 | SvREFCNT_dec (sv); |
|
|
1032 | sv = 0; |
|
|
1033 | } |
|
|
1034 | } |
|
|
1035 | |
1013 | if (!sv) |
1036 | if (!sv) |
1014 | { |
1037 | { |
1015 | IV offset = dec.flags & F_UTF8 |
|
|
1016 | ? dec.cur - SvPVX (string) |
|
|
1017 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1018 | SV *uni = sv_newmortal (); |
1038 | SV *uni = sv_newmortal (); |
1019 | |
1039 | |
1020 | // horrible hack to silence warning inside pv_uni_display |
1040 | // horrible hack to silence warning inside pv_uni_display |
1021 | COP cop = *PL_curcop; |
1041 | COP cop = *PL_curcop; |
1022 | cop.cop_warnings = pWARN_NONE; |
1042 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1116 | PPCODE: |
1136 | PPCODE: |
1117 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1137 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1118 | |
1138 | |
1119 | void decode (SV *self, SV *jsonstr) |
1139 | void decode (SV *self, SV *jsonstr) |
1120 | PPCODE: |
1140 | PPCODE: |
1121 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1141 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1142 | |
|
|
1143 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1144 | PPCODE: |
|
|
1145 | { |
|
|
1146 | UV offset; |
|
|
1147 | EXTEND (SP, 2); |
|
|
1148 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1149 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1150 | } |
1122 | |
1151 | |
1123 | PROTOTYPES: ENABLE |
1152 | PROTOTYPES: ENABLE |
1124 | |
1153 | |
1125 | void to_json (SV *scalar) |
1154 | void to_json (SV *scalar) |
1126 | ALIAS: |
1155 | ALIAS: |
… | |
… | |
1130 | |
1159 | |
1131 | void from_json (SV *jsonstr) |
1160 | void from_json (SV *jsonstr) |
1132 | ALIAS: |
1161 | ALIAS: |
1133 | jsonToObj = 0 |
1162 | jsonToObj = 0 |
1134 | PPCODE: |
1163 | PPCODE: |
1135 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1164 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1136 | |
1165 | |