… | |
… | |
28 | #define F_SHRINK 0x00000200UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
31 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
33 | |
35 | |
34 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
|
|
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
35 | |
38 | |
36 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
37 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
38 | |
41 | |
39 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
56 | #define expect_true(expr) expect ((expr) != 0, 1) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
57 | |
60 | |
58 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
59 | static SV *json_true, *json_false; |
62 | static SV *json_true, *json_false; |
60 | |
63 | |
|
|
64 | typedef struct { |
|
|
65 | U32 flags; |
|
|
66 | } JSON; |
|
|
67 | |
61 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
62 | // utility functions |
69 | // utility functions |
63 | |
70 | |
64 | static UV * |
71 | inline void |
65 | SvJSON (SV *sv) |
|
|
66 | { |
|
|
67 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
68 | croak ("object is not of type JSON::XS"); |
|
|
69 | |
|
|
70 | return &SvUVX (SvRV (sv)); |
|
|
71 | } |
|
|
72 | |
|
|
73 | static void |
|
|
74 | shrink (SV *sv) |
72 | shrink (SV *sv) |
75 | { |
73 | { |
76 | sv_utf8_downgrade (sv, 1); |
74 | sv_utf8_downgrade (sv, 1); |
77 | if (SvLEN (sv) > SvCUR (sv) + 1) |
75 | if (SvLEN (sv) > SvCUR (sv) + 1) |
78 | { |
76 | { |
… | |
… | |
113 | typedef struct |
111 | typedef struct |
114 | { |
112 | { |
115 | char *cur; // SvPVX (sv) + current output position |
113 | char *cur; // SvPVX (sv) + current output position |
116 | char *end; // SvEND (sv) |
114 | char *end; // SvEND (sv) |
117 | SV *sv; // result scalar |
115 | SV *sv; // result scalar |
118 | U32 flags; // F_* |
116 | JSON json; |
119 | U32 indent; // indentation level |
117 | U32 indent; // indentation level |
120 | U32 maxdepth; // max. indentation/recursion level |
118 | U32 maxdepth; // max. indentation/recursion level |
121 | } enc_t; |
119 | } enc_t; |
122 | |
120 | |
123 | inline void |
121 | inline void |
… | |
… | |
197 | } |
195 | } |
198 | |
196 | |
199 | if (uch > 0x10FFFFUL) |
197 | if (uch > 0x10FFFFUL) |
200 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
198 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
201 | |
199 | |
202 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
200 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
203 | { |
201 | { |
204 | if (uch > 0xFFFFUL) |
202 | if (uch > 0xFFFFUL) |
205 | { |
203 | { |
206 | need (enc, len += 11); |
204 | need (enc, len += 11); |
207 | sprintf (enc->cur, "\\u%04x\\u%04x", |
205 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
221 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
219 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
222 | } |
220 | } |
223 | |
221 | |
224 | str += clen; |
222 | str += clen; |
225 | } |
223 | } |
226 | else if (enc->flags & F_LATIN1) |
224 | else if (enc->json.flags & F_LATIN1) |
227 | { |
225 | { |
228 | *enc->cur++ = uch; |
226 | *enc->cur++ = uch; |
229 | str += clen; |
227 | str += clen; |
230 | } |
228 | } |
231 | else if (is_utf8) |
229 | else if (is_utf8) |
… | |
… | |
252 | } |
250 | } |
253 | |
251 | |
254 | inline void |
252 | inline void |
255 | encode_indent (enc_t *enc) |
253 | encode_indent (enc_t *enc) |
256 | { |
254 | { |
257 | if (enc->flags & F_INDENT) |
255 | if (enc->json.flags & F_INDENT) |
258 | { |
256 | { |
259 | int spaces = enc->indent * INDENT_STEP; |
257 | int spaces = enc->indent * INDENT_STEP; |
260 | |
258 | |
261 | need (enc, spaces); |
259 | need (enc, spaces); |
262 | memset (enc->cur, ' ', spaces); |
260 | memset (enc->cur, ' ', spaces); |
… | |
… | |
272 | } |
270 | } |
273 | |
271 | |
274 | inline void |
272 | inline void |
275 | encode_nl (enc_t *enc) |
273 | encode_nl (enc_t *enc) |
276 | { |
274 | { |
277 | if (enc->flags & F_INDENT) |
275 | if (enc->json.flags & F_INDENT) |
278 | { |
276 | { |
279 | need (enc, 1); |
277 | need (enc, 1); |
280 | encode_ch (enc, '\n'); |
278 | encode_ch (enc, '\n'); |
281 | } |
279 | } |
282 | } |
280 | } |
… | |
… | |
284 | inline void |
282 | inline void |
285 | encode_comma (enc_t *enc) |
283 | encode_comma (enc_t *enc) |
286 | { |
284 | { |
287 | encode_ch (enc, ','); |
285 | encode_ch (enc, ','); |
288 | |
286 | |
289 | if (enc->flags & F_INDENT) |
287 | if (enc->json.flags & F_INDENT) |
290 | encode_nl (enc); |
288 | encode_nl (enc); |
291 | else if (enc->flags & F_SPACE_AFTER) |
289 | else if (enc->json.flags & F_SPACE_AFTER) |
292 | encode_space (enc); |
290 | encode_space (enc); |
293 | } |
291 | } |
294 | |
292 | |
295 | static void encode_sv (enc_t *enc, SV *sv); |
293 | static void encode_sv (enc_t *enc, SV *sv); |
296 | |
294 | |
… | |
… | |
339 | else |
337 | else |
340 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
338 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
341 | |
339 | |
342 | encode_ch (enc, '"'); |
340 | encode_ch (enc, '"'); |
343 | |
341 | |
344 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
342 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
345 | encode_ch (enc, ':'); |
343 | encode_ch (enc, ':'); |
346 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
344 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
347 | encode_sv (enc, HeVAL (he)); |
345 | encode_sv (enc, HeVAL (he)); |
348 | } |
346 | } |
349 | |
347 | |
350 | // compare hash entries, used when all keys are bytestrings |
348 | // compare hash entries, used when all keys are bytestrings |
351 | static int |
349 | static int |
… | |
… | |
386 | { |
384 | { |
387 | // for canonical output we have to sort by keys first |
385 | // for canonical output we have to sort by keys first |
388 | // actually, this is mostly due to the stupid so-called |
386 | // actually, this is mostly due to the stupid so-called |
389 | // security workaround added somewhere in 5.8.x. |
387 | // security workaround added somewhere in 5.8.x. |
390 | // that randomises hash orderings |
388 | // that randomises hash orderings |
391 | if (enc->flags & F_CANONICAL) |
389 | if (enc->json.flags & F_CANONICAL) |
392 | { |
390 | { |
393 | int fast = 1; |
391 | int fast = 1; |
394 | HE *he; |
392 | HE *he; |
395 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
393 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
396 | HE **hes = _alloca (count * sizeof (HE)); |
394 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
485 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
483 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
486 | { |
484 | { |
487 | // not yet |
485 | // not yet |
488 | } |
486 | } |
489 | #endif |
487 | #endif |
490 | if (enc->flags & F_CONV_BLESSED) |
488 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
489 | { |
492 | // we re-bless the reference to get overload and other niceties right |
490 | // we re-bless the reference to get overload and other niceties right |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
491 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
494 | |
492 | |
495 | if (to_json) |
493 | if (to_json) |
… | |
… | |
509 | encode_sv (enc, POPs); |
507 | encode_sv (enc, POPs); |
510 | |
508 | |
511 | FREETMPS; |
509 | FREETMPS; |
512 | LEAVE; |
510 | LEAVE; |
513 | } |
511 | } |
514 | else if (enc->flags & F_ALLOW_BLESSED) |
512 | else if (enc->json.flags & F_ALLOW_BLESSED) |
515 | encode_str (enc, "null", 4, 0); |
513 | encode_str (enc, "null", 4, 0); |
516 | else |
514 | else |
517 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
515 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
518 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
516 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
519 | } |
517 | } |
520 | else if (enc->flags & F_ALLOW_BLESSED) |
518 | else if (enc->json.flags & F_ALLOW_BLESSED) |
521 | encode_str (enc, "null", 4, 0); |
519 | encode_str (enc, "null", 4, 0); |
522 | else |
520 | else |
523 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
521 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
524 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
522 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
525 | } |
523 | } |
… | |
… | |
611 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
609 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
612 | SvPV_nolen (sv), SvFLAGS (sv)); |
610 | SvPV_nolen (sv), SvFLAGS (sv)); |
613 | } |
611 | } |
614 | |
612 | |
615 | static SV * |
613 | static SV * |
616 | encode_json (SV *scalar, U32 flags) |
614 | encode_json (SV *scalar, JSON *json) |
617 | { |
615 | { |
618 | enc_t enc; |
616 | enc_t enc; |
619 | |
617 | |
620 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
618 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
621 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
619 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
622 | |
620 | |
623 | enc.flags = flags; |
621 | enc.json = *json; |
624 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
622 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
625 | enc.cur = SvPVX (enc.sv); |
623 | enc.cur = SvPVX (enc.sv); |
626 | enc.end = SvEND (enc.sv); |
624 | enc.end = SvEND (enc.sv); |
627 | enc.indent = 0; |
625 | enc.indent = 0; |
628 | enc.maxdepth = DEC_DEPTH (flags); |
626 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
629 | |
627 | |
630 | SvPOK_only (enc.sv); |
628 | SvPOK_only (enc.sv); |
631 | encode_sv (&enc, scalar); |
629 | encode_sv (&enc, scalar); |
632 | |
630 | |
633 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
631 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
634 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
632 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
635 | |
633 | |
636 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
634 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
637 | SvUTF8_on (enc.sv); |
635 | SvUTF8_on (enc.sv); |
638 | |
636 | |
639 | if (enc.flags & F_SHRINK) |
637 | if (enc.json.flags & F_SHRINK) |
640 | shrink (enc.sv); |
638 | shrink (enc.sv); |
641 | |
639 | |
642 | return enc.sv; |
640 | return enc.sv; |
643 | } |
641 | } |
644 | |
642 | |
… | |
… | |
649 | typedef struct |
647 | typedef struct |
650 | { |
648 | { |
651 | char *cur; // current parser pointer |
649 | char *cur; // current parser pointer |
652 | char *end; // end of input string |
650 | char *end; // end of input string |
653 | const char *err; // parse error, if != 0 |
651 | const char *err; // parse error, if != 0 |
654 | U32 flags; // F_* |
652 | JSON json; |
655 | U32 depth; // recursion depth |
653 | U32 depth; // recursion depth |
656 | U32 maxdepth; // recursion depth limit |
654 | U32 maxdepth; // recursion depth limit |
657 | } dec_t; |
655 | } dec_t; |
658 | |
656 | |
659 | inline void |
657 | inline void |
… | |
… | |
1020 | if (*dec->cur == '}') |
1018 | if (*dec->cur == '}') |
1021 | ++dec->cur; |
1019 | ++dec->cur; |
1022 | else |
1020 | else |
1023 | for (;;) |
1021 | for (;;) |
1024 | { |
1022 | { |
1025 | SV *key, *value; |
|
|
1026 | |
|
|
1027 | decode_ws (dec); EXPECT_CH ('"'); |
1023 | decode_ws (dec); EXPECT_CH ('"'); |
1028 | |
1024 | |
1029 | key = decode_str (dec); |
1025 | // heuristic: assume that |
1030 | if (!key) |
1026 | // a) decode_str + hv_store_ent are abysmally slow. |
1031 | goto fail; |
1027 | // b) most hash keys are short, simple ascii text. |
|
|
1028 | // => try to "fast-match" such strings to avoid |
|
|
1029 | // the overhead of decode_str + hv_store_ent. |
|
|
1030 | { |
|
|
1031 | SV *value; |
|
|
1032 | char *p = dec->cur; |
|
|
1033 | char *e = p + 24; // only try up to 24 bytes |
1032 | |
1034 | |
1033 | decode_ws (dec); EXPECT_CH (':'); |
1035 | for (;;) |
1034 | |
|
|
1035 | value = decode_sv (dec); |
|
|
1036 | if (!value) |
|
|
1037 | { |
1036 | { |
|
|
1037 | // the >= 0x80 is true on most architectures |
|
|
1038 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1039 | { |
|
|
1040 | // slow path, back up and use decode_str |
|
|
1041 | SV *key = decode_str (dec); |
|
|
1042 | if (!key) |
|
|
1043 | goto fail; |
|
|
1044 | |
|
|
1045 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1046 | |
|
|
1047 | value = decode_sv (dec); |
|
|
1048 | if (!value) |
|
|
1049 | { |
|
|
1050 | SvREFCNT_dec (key); |
|
|
1051 | goto fail; |
|
|
1052 | } |
|
|
1053 | |
|
|
1054 | hv_store_ent (hv, key, value, 0); |
1038 | SvREFCNT_dec (key); |
1055 | SvREFCNT_dec (key); |
|
|
1056 | |
|
|
1057 | break; |
|
|
1058 | } |
|
|
1059 | else if (*p == '"') |
|
|
1060 | { |
|
|
1061 | // fast path, got a simple key |
|
|
1062 | char *key = dec->cur; |
|
|
1063 | int len = p - key; |
|
|
1064 | dec->cur = p + 1; |
|
|
1065 | |
|
|
1066 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1067 | |
|
|
1068 | value = decode_sv (dec); |
|
|
1069 | if (!value) |
1039 | goto fail; |
1070 | goto fail; |
|
|
1071 | |
|
|
1072 | hv_store (hv, key, len, value, 0); |
|
|
1073 | |
|
|
1074 | break; |
|
|
1075 | } |
|
|
1076 | |
|
|
1077 | ++p; |
1040 | } |
1078 | } |
1041 | |
1079 | } |
1042 | hv_store_ent (hv, key, value, 0); |
|
|
1043 | SvREFCNT_dec (key); |
|
|
1044 | |
1080 | |
1045 | decode_ws (dec); |
1081 | decode_ws (dec); |
1046 | |
1082 | |
1047 | if (*dec->cur == '}') |
1083 | if (*dec->cur == '}') |
1048 | { |
1084 | { |
… | |
… | |
1124 | fail: |
1160 | fail: |
1125 | return 0; |
1161 | return 0; |
1126 | } |
1162 | } |
1127 | |
1163 | |
1128 | static SV * |
1164 | static SV * |
1129 | decode_json (SV *string, U32 flags, UV *offset_return) |
1165 | decode_json (SV *string, JSON *json, UV *offset_return) |
1130 | { |
1166 | { |
1131 | dec_t dec; |
1167 | dec_t dec; |
1132 | UV offset; |
1168 | UV offset; |
1133 | SV *sv; |
1169 | SV *sv; |
1134 | |
1170 | |
1135 | SvGETMAGIC (string); |
1171 | SvGETMAGIC (string); |
1136 | SvUPGRADE (string, SVt_PV); |
1172 | SvUPGRADE (string, SVt_PV); |
1137 | |
1173 | |
|
|
1174 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
|
|
1175 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
|
|
1176 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
|
|
1177 | |
1138 | if (flags & F_UTF8) |
1178 | if (json->flags & F_UTF8) |
1139 | sv_utf8_downgrade (string, 0); |
1179 | sv_utf8_downgrade (string, 0); |
1140 | else |
1180 | else |
1141 | sv_utf8_upgrade (string); |
1181 | sv_utf8_upgrade (string); |
1142 | |
1182 | |
1143 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1183 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1144 | |
1184 | |
1145 | dec.flags = flags; |
1185 | dec.json = *json; |
1146 | dec.cur = SvPVX (string); |
1186 | dec.cur = SvPVX (string); |
1147 | dec.end = SvEND (string); |
1187 | dec.end = SvEND (string); |
1148 | dec.err = 0; |
1188 | dec.err = 0; |
1149 | dec.depth = 0; |
1189 | dec.depth = 0; |
1150 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1190 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1151 | |
1191 | |
1152 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1192 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1153 | sv = decode_sv (&dec); |
1193 | sv = decode_sv (&dec); |
1154 | |
1194 | |
1155 | if (!(offset_return || !sv)) |
1195 | if (!(offset_return || !sv)) |
… | |
… | |
1165 | } |
1205 | } |
1166 | } |
1206 | } |
1167 | |
1207 | |
1168 | if (offset_return || !sv) |
1208 | if (offset_return || !sv) |
1169 | { |
1209 | { |
1170 | offset = dec.flags & F_UTF8 |
1210 | offset = dec.json.flags & F_UTF8 |
1171 | ? dec.cur - SvPVX (string) |
1211 | ? dec.cur - SvPVX (string) |
1172 | : utf8_distance (dec.cur, SvPVX (string)); |
1212 | : utf8_distance (dec.cur, SvPVX (string)); |
1173 | |
1213 | |
1174 | if (offset_return) |
1214 | if (offset_return) |
1175 | *offset_return = offset; |
1215 | *offset_return = offset; |
… | |
… | |
1194 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1234 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1195 | } |
1235 | } |
1196 | |
1236 | |
1197 | sv = sv_2mortal (sv); |
1237 | sv = sv_2mortal (sv); |
1198 | |
1238 | |
1199 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1239 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1200 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1240 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1201 | |
1241 | |
1202 | return sv; |
1242 | return sv; |
1203 | } |
1243 | } |
1204 | |
1244 | |
… | |
… | |
1225 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1265 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1226 | } |
1266 | } |
1227 | |
1267 | |
1228 | PROTOTYPES: DISABLE |
1268 | PROTOTYPES: DISABLE |
1229 | |
1269 | |
1230 | SV *new (char *dummy) |
1270 | void new (char *klass) |
1231 | CODE: |
1271 | PPCODE: |
1232 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1272 | { |
1233 | OUTPUT: |
1273 | SV *pv = NEWSV (0, sizeof (JSON)); |
1234 | RETVAL |
1274 | SvPOK_only (pv); |
|
|
1275 | Zero (SvPVX (pv), 1, sizeof (JSON)); |
|
|
1276 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1277 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
|
|
1278 | } |
1235 | |
1279 | |
1236 | SV *ascii (SV *self, int enable = 1) |
1280 | void ascii (JSON *self, int enable = 1) |
1237 | ALIAS: |
1281 | ALIAS: |
1238 | ascii = F_ASCII |
1282 | ascii = F_ASCII |
1239 | latin1 = F_LATIN1 |
1283 | latin1 = F_LATIN1 |
1240 | utf8 = F_UTF8 |
1284 | utf8 = F_UTF8 |
1241 | indent = F_INDENT |
1285 | indent = F_INDENT |
… | |
… | |
1245 | pretty = F_PRETTY |
1289 | pretty = F_PRETTY |
1246 | allow_nonref = F_ALLOW_NONREF |
1290 | allow_nonref = F_ALLOW_NONREF |
1247 | shrink = F_SHRINK |
1291 | shrink = F_SHRINK |
1248 | allow_blessed = F_ALLOW_BLESSED |
1292 | allow_blessed = F_ALLOW_BLESSED |
1249 | convert_blessed = F_CONV_BLESSED |
1293 | convert_blessed = F_CONV_BLESSED |
1250 | CODE: |
1294 | PPCODE: |
1251 | { |
1295 | { |
1252 | UV *uv = SvJSON (self); |
|
|
1253 | if (enable) |
1296 | if (enable) |
1254 | *uv |= ix; |
1297 | self->flags |= ix; |
1255 | else |
1298 | else |
1256 | *uv &= ~ix; |
1299 | self->flags &= ~ix; |
1257 | |
1300 | |
1258 | RETVAL = newSVsv (self); |
1301 | XPUSHs (ST (0)); |
1259 | } |
1302 | } |
1260 | OUTPUT: |
|
|
1261 | RETVAL |
|
|
1262 | |
1303 | |
1263 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1304 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1264 | CODE: |
1305 | PPCODE: |
1265 | { |
1306 | { |
1266 | UV *uv = SvJSON (self); |
|
|
1267 | UV log2 = 0; |
1307 | UV log2 = 0; |
1268 | |
1308 | |
1269 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1309 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1270 | |
1310 | |
1271 | while ((1UL << log2) < max_depth) |
1311 | while ((1UL << log2) < max_depth) |
1272 | ++log2; |
1312 | ++log2; |
1273 | |
1313 | |
1274 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1314 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1275 | |
1315 | |
1276 | RETVAL = newSVsv (self); |
1316 | XPUSHs (ST (0)); |
1277 | } |
1317 | } |
1278 | OUTPUT: |
|
|
1279 | RETVAL |
|
|
1280 | |
1318 | |
1281 | void encode (SV *self, SV *scalar) |
1319 | void max_size (JSON *self, UV max_size = 0) |
1282 | PPCODE: |
1320 | PPCODE: |
1283 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1321 | { |
|
|
1322 | UV log2 = 0; |
1284 | |
1323 | |
|
|
1324 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1325 | if (max_size == 1) max_size = 2; |
|
|
1326 | |
|
|
1327 | while ((1UL << log2) < max_size) |
|
|
1328 | ++log2; |
|
|
1329 | |
|
|
1330 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1331 | |
|
|
1332 | XPUSHs (ST (0)); |
|
|
1333 | } |
|
|
1334 | |
1285 | void decode (SV *self, SV *jsonstr) |
1335 | void encode (JSON *self, SV *scalar) |
1286 | PPCODE: |
1336 | PPCODE: |
|
|
1337 | XPUSHs (encode_json (scalar, self)); |
|
|
1338 | |
|
|
1339 | void decode (JSON *self, SV *jsonstr) |
|
|
1340 | PPCODE: |
1287 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1341 | XPUSHs (decode_json (jsonstr, self, 0)); |
1288 | |
1342 | |
1289 | void decode_prefix (SV *self, SV *jsonstr) |
1343 | void decode_prefix (JSON *self, SV *jsonstr) |
1290 | PPCODE: |
1344 | PPCODE: |
1291 | { |
1345 | { |
1292 | UV offset; |
1346 | UV offset; |
1293 | EXTEND (SP, 2); |
1347 | EXTEND (SP, 2); |
1294 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1348 | PUSHs (decode_json (jsonstr, self, &offset)); |
1295 | PUSHs (sv_2mortal (newSVuv (offset))); |
1349 | PUSHs (sv_2mortal (newSVuv (offset))); |
1296 | } |
1350 | } |
1297 | |
1351 | |
1298 | PROTOTYPES: ENABLE |
1352 | PROTOTYPES: ENABLE |
1299 | |
1353 | |
1300 | void to_json (SV *scalar) |
1354 | void to_json (SV *scalar) |
1301 | ALIAS: |
|
|
1302 | objToJson = 0 |
|
|
1303 | PPCODE: |
1355 | PPCODE: |
|
|
1356 | { |
|
|
1357 | JSON json = { F_DEFAULT | F_UTF8 }; |
1304 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1358 | XPUSHs (encode_json (scalar, &json)); |
|
|
1359 | } |
1305 | |
1360 | |
1306 | void from_json (SV *jsonstr) |
1361 | void from_json (SV *jsonstr) |
1307 | ALIAS: |
|
|
1308 | jsonToObj = 0 |
|
|
1309 | PPCODE: |
1362 | PPCODE: |
|
|
1363 | { |
|
|
1364 | JSON json = { F_DEFAULT | F_UTF8 }; |
1310 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1365 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1366 | } |
1311 | |
1367 | |