… | |
… | |
372 | // actually, this is mostly due to the stupid so-called |
372 | // actually, this is mostly due to the stupid so-called |
373 | // security workaround added somewhere in 5.8.x. |
373 | // security workaround added somewhere in 5.8.x. |
374 | // that randomises hash orderings |
374 | // that randomises hash orderings |
375 | if (enc->flags & F_CANONICAL) |
375 | if (enc->flags & F_CANONICAL) |
376 | { |
376 | { |
377 | HE *he, *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
378 | int fast = 1; |
377 | int fast = 1; |
|
|
378 | HE *he; |
|
|
379 | #if WIN32 |
|
|
380 | HE **hes = _alloca (count * sizeof (HE)); |
|
|
381 | #else |
|
|
382 | HE *hes [count]; // if your compiler dies here, you need to enable C99 mode |
|
|
383 | #endif |
379 | |
384 | |
380 | i = 0; |
385 | i = 0; |
381 | while ((he = hv_iternext (hv))) |
386 | while ((he = hv_iternext (hv))) |
382 | { |
387 | { |
383 | hes [i++] = he; |
388 | hes [i++] = he; |
… | |
… | |
983 | fail: |
988 | fail: |
984 | return 0; |
989 | return 0; |
985 | } |
990 | } |
986 | |
991 | |
987 | static SV * |
992 | static SV * |
988 | decode_json (SV *string, U32 flags) |
993 | decode_json (SV *string, U32 flags, UV *offset_return) |
989 | { |
994 | { |
990 | dec_t dec; |
995 | dec_t dec; |
|
|
996 | UV offset; |
991 | SV *sv; |
997 | SV *sv; |
992 | |
998 | |
993 | SvGETMAGIC (string); |
999 | SvGETMAGIC (string); |
994 | SvUPGRADE (string, SVt_PV); |
1000 | SvUPGRADE (string, SVt_PV); |
995 | |
1001 | |
… | |
… | |
1005 | dec.end = SvEND (string); |
1011 | dec.end = SvEND (string); |
1006 | dec.err = 0; |
1012 | dec.err = 0; |
1007 | dec.depth = 0; |
1013 | dec.depth = 0; |
1008 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1014 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1009 | |
1015 | |
1010 | *dec.end = 0; // this should basically be a nop, too, but make sure its there |
1016 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1011 | sv = decode_sv (&dec); |
1017 | sv = decode_sv (&dec); |
1012 | |
1018 | |
|
|
1019 | if (!(offset_return || !sv)) |
|
|
1020 | { |
|
|
1021 | // check for trailing garbage |
|
|
1022 | decode_ws (&dec); |
|
|
1023 | |
|
|
1024 | if (*dec.cur) |
|
|
1025 | { |
|
|
1026 | dec.err = "garbage after JSON object"; |
|
|
1027 | SvREFCNT_dec (sv); |
|
|
1028 | sv = 0; |
|
|
1029 | } |
|
|
1030 | } |
|
|
1031 | |
|
|
1032 | if (offset_return || !sv) |
|
|
1033 | { |
|
|
1034 | offset = dec.flags & F_UTF8 |
|
|
1035 | ? dec.cur - SvPVX (string) |
|
|
1036 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1037 | |
|
|
1038 | if (offset_return) |
|
|
1039 | *offset_return = offset; |
|
|
1040 | } |
|
|
1041 | |
1013 | if (!sv) |
1042 | if (!sv) |
1014 | { |
1043 | { |
1015 | IV offset = dec.flags & F_UTF8 |
|
|
1016 | ? dec.cur - SvPVX (string) |
|
|
1017 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1018 | SV *uni = sv_newmortal (); |
1044 | SV *uni = sv_newmortal (); |
1019 | |
1045 | |
1020 | // horrible hack to silence warning inside pv_uni_display |
1046 | // horrible hack to silence warning inside pv_uni_display |
1021 | COP cop = *PL_curcop; |
1047 | COP cop = *PL_curcop; |
1022 | cop.cop_warnings = pWARN_NONE; |
1048 | cop.cop_warnings = pWARN_NONE; |
… | |
… | |
1116 | PPCODE: |
1142 | PPCODE: |
1117 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1143 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1118 | |
1144 | |
1119 | void decode (SV *self, SV *jsonstr) |
1145 | void decode (SV *self, SV *jsonstr) |
1120 | PPCODE: |
1146 | PPCODE: |
1121 | XPUSHs (decode_json (jsonstr, *SvJSON (self))); |
1147 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
|
|
1148 | |
|
|
1149 | void decode_prefix (SV *self, SV *jsonstr) |
|
|
1150 | PPCODE: |
|
|
1151 | { |
|
|
1152 | UV offset; |
|
|
1153 | EXTEND (SP, 2); |
|
|
1154 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
|
|
1155 | PUSHs (sv_2mortal (newSVuv (offset))); |
|
|
1156 | } |
1122 | |
1157 | |
1123 | PROTOTYPES: ENABLE |
1158 | PROTOTYPES: ENABLE |
1124 | |
1159 | |
1125 | void to_json (SV *scalar) |
1160 | void to_json (SV *scalar) |
1126 | ALIAS: |
1161 | ALIAS: |
… | |
… | |
1130 | |
1165 | |
1131 | void from_json (SV *jsonstr) |
1166 | void from_json (SV *jsonstr) |
1132 | ALIAS: |
1167 | ALIAS: |
1133 | jsonToObj = 0 |
1168 | jsonToObj = 0 |
1134 | PPCODE: |
1169 | PPCODE: |
1135 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8)); |
1170 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1136 | |
1171 | |