… | |
… | |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
60 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
62 | static SV *json_true, *json_false; |
63 | |
63 | |
|
|
64 | typedef struct { |
|
|
65 | U32 flags; |
|
|
66 | } JSON; |
|
|
67 | |
64 | ///////////////////////////////////////////////////////////////////////////// |
68 | ///////////////////////////////////////////////////////////////////////////// |
65 | // utility functions |
69 | // utility functions |
66 | |
70 | |
67 | static UV * |
71 | inline void |
68 | SvJSON (SV *sv) |
|
|
69 | { |
|
|
70 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
71 | croak ("object is not of type JSON::XS"); |
|
|
72 | |
|
|
73 | return &SvUVX (SvRV (sv)); |
|
|
74 | } |
|
|
75 | |
|
|
76 | static void |
|
|
77 | shrink (SV *sv) |
72 | shrink (SV *sv) |
78 | { |
73 | { |
79 | sv_utf8_downgrade (sv, 1); |
74 | sv_utf8_downgrade (sv, 1); |
80 | if (SvLEN (sv) > SvCUR (sv) + 1) |
75 | if (SvLEN (sv) > SvCUR (sv) + 1) |
81 | { |
76 | { |
… | |
… | |
116 | typedef struct |
111 | typedef struct |
117 | { |
112 | { |
118 | char *cur; // SvPVX (sv) + current output position |
113 | char *cur; // SvPVX (sv) + current output position |
119 | char *end; // SvEND (sv) |
114 | char *end; // SvEND (sv) |
120 | SV *sv; // result scalar |
115 | SV *sv; // result scalar |
121 | U32 flags; // F_* |
116 | JSON json; |
122 | U32 indent; // indentation level |
117 | U32 indent; // indentation level |
123 | U32 maxdepth; // max. indentation/recursion level |
118 | U32 maxdepth; // max. indentation/recursion level |
124 | } enc_t; |
119 | } enc_t; |
125 | |
120 | |
126 | inline void |
121 | inline void |
… | |
… | |
200 | } |
195 | } |
201 | |
196 | |
202 | if (uch > 0x10FFFFUL) |
197 | if (uch > 0x10FFFFUL) |
203 | 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); |
204 | |
199 | |
205 | 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)) |
206 | { |
201 | { |
207 | if (uch > 0xFFFFUL) |
202 | if (uch > 0xFFFFUL) |
208 | { |
203 | { |
209 | need (enc, len += 11); |
204 | need (enc, len += 11); |
210 | sprintf (enc->cur, "\\u%04x\\u%04x", |
205 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
224 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
219 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
225 | } |
220 | } |
226 | |
221 | |
227 | str += clen; |
222 | str += clen; |
228 | } |
223 | } |
229 | else if (enc->flags & F_LATIN1) |
224 | else if (enc->json.flags & F_LATIN1) |
230 | { |
225 | { |
231 | *enc->cur++ = uch; |
226 | *enc->cur++ = uch; |
232 | str += clen; |
227 | str += clen; |
233 | } |
228 | } |
234 | else if (is_utf8) |
229 | else if (is_utf8) |
… | |
… | |
255 | } |
250 | } |
256 | |
251 | |
257 | inline void |
252 | inline void |
258 | encode_indent (enc_t *enc) |
253 | encode_indent (enc_t *enc) |
259 | { |
254 | { |
260 | if (enc->flags & F_INDENT) |
255 | if (enc->json.flags & F_INDENT) |
261 | { |
256 | { |
262 | int spaces = enc->indent * INDENT_STEP; |
257 | int spaces = enc->indent * INDENT_STEP; |
263 | |
258 | |
264 | need (enc, spaces); |
259 | need (enc, spaces); |
265 | memset (enc->cur, ' ', spaces); |
260 | memset (enc->cur, ' ', spaces); |
… | |
… | |
275 | } |
270 | } |
276 | |
271 | |
277 | inline void |
272 | inline void |
278 | encode_nl (enc_t *enc) |
273 | encode_nl (enc_t *enc) |
279 | { |
274 | { |
280 | if (enc->flags & F_INDENT) |
275 | if (enc->json.flags & F_INDENT) |
281 | { |
276 | { |
282 | need (enc, 1); |
277 | need (enc, 1); |
283 | encode_ch (enc, '\n'); |
278 | encode_ch (enc, '\n'); |
284 | } |
279 | } |
285 | } |
280 | } |
… | |
… | |
287 | inline void |
282 | inline void |
288 | encode_comma (enc_t *enc) |
283 | encode_comma (enc_t *enc) |
289 | { |
284 | { |
290 | encode_ch (enc, ','); |
285 | encode_ch (enc, ','); |
291 | |
286 | |
292 | if (enc->flags & F_INDENT) |
287 | if (enc->json.flags & F_INDENT) |
293 | encode_nl (enc); |
288 | encode_nl (enc); |
294 | else if (enc->flags & F_SPACE_AFTER) |
289 | else if (enc->json.flags & F_SPACE_AFTER) |
295 | encode_space (enc); |
290 | encode_space (enc); |
296 | } |
291 | } |
297 | |
292 | |
298 | static void encode_sv (enc_t *enc, SV *sv); |
293 | static void encode_sv (enc_t *enc, SV *sv); |
299 | |
294 | |
… | |
… | |
342 | else |
337 | else |
343 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
338 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
344 | |
339 | |
345 | encode_ch (enc, '"'); |
340 | encode_ch (enc, '"'); |
346 | |
341 | |
347 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
342 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
348 | encode_ch (enc, ':'); |
343 | encode_ch (enc, ':'); |
349 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
344 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
350 | encode_sv (enc, HeVAL (he)); |
345 | encode_sv (enc, HeVAL (he)); |
351 | } |
346 | } |
352 | |
347 | |
353 | // compare hash entries, used when all keys are bytestrings |
348 | // compare hash entries, used when all keys are bytestrings |
354 | static int |
349 | static int |
… | |
… | |
389 | { |
384 | { |
390 | // for canonical output we have to sort by keys first |
385 | // for canonical output we have to sort by keys first |
391 | // actually, this is mostly due to the stupid so-called |
386 | // actually, this is mostly due to the stupid so-called |
392 | // security workaround added somewhere in 5.8.x. |
387 | // security workaround added somewhere in 5.8.x. |
393 | // that randomises hash orderings |
388 | // that randomises hash orderings |
394 | if (enc->flags & F_CANONICAL) |
389 | if (enc->json.flags & F_CANONICAL) |
395 | { |
390 | { |
396 | int fast = 1; |
391 | int fast = 1; |
397 | HE *he; |
392 | HE *he; |
398 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
393 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
399 | HE **hes = _alloca (count * sizeof (HE)); |
394 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
483 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
489 | { |
484 | { |
490 | // not yet |
485 | // not yet |
491 | } |
486 | } |
492 | #endif |
487 | #endif |
493 | if (enc->flags & F_CONV_BLESSED) |
488 | if (enc->json.flags & F_CONV_BLESSED) |
494 | { |
489 | { |
495 | // 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 |
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
491 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
497 | |
492 | |
498 | if (to_json) |
493 | if (to_json) |
… | |
… | |
512 | encode_sv (enc, POPs); |
507 | encode_sv (enc, POPs); |
513 | |
508 | |
514 | FREETMPS; |
509 | FREETMPS; |
515 | LEAVE; |
510 | LEAVE; |
516 | } |
511 | } |
517 | else if (enc->flags & F_ALLOW_BLESSED) |
512 | else if (enc->json.flags & F_ALLOW_BLESSED) |
518 | encode_str (enc, "null", 4, 0); |
513 | encode_str (enc, "null", 4, 0); |
519 | else |
514 | else |
520 | 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", |
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
516 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
522 | } |
517 | } |
523 | else if (enc->flags & F_ALLOW_BLESSED) |
518 | else if (enc->json.flags & F_ALLOW_BLESSED) |
524 | encode_str (enc, "null", 4, 0); |
519 | encode_str (enc, "null", 4, 0); |
525 | else |
520 | else |
526 | 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", |
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
522 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
528 | } |
523 | } |
… | |
… | |
614 | 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", |
615 | SvPV_nolen (sv), SvFLAGS (sv)); |
610 | SvPV_nolen (sv), SvFLAGS (sv)); |
616 | } |
611 | } |
617 | |
612 | |
618 | static SV * |
613 | static SV * |
619 | encode_json (SV *scalar, U32 flags) |
614 | encode_json (SV *scalar, JSON *json) |
620 | { |
615 | { |
621 | enc_t enc; |
616 | enc_t enc; |
622 | |
617 | |
623 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
618 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
624 | 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)"); |
625 | |
620 | |
626 | enc.flags = flags; |
621 | enc.json = *json; |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
622 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
628 | enc.cur = SvPVX (enc.sv); |
623 | enc.cur = SvPVX (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
624 | enc.end = SvEND (enc.sv); |
630 | enc.indent = 0; |
625 | enc.indent = 0; |
631 | enc.maxdepth = DEC_DEPTH (flags); |
626 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
632 | |
627 | |
633 | SvPOK_only (enc.sv); |
628 | SvPOK_only (enc.sv); |
634 | encode_sv (&enc, scalar); |
629 | encode_sv (&enc, scalar); |
635 | |
630 | |
636 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
631 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
637 | *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 |
638 | |
633 | |
639 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
634 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
640 | SvUTF8_on (enc.sv); |
635 | SvUTF8_on (enc.sv); |
641 | |
636 | |
642 | if (enc.flags & F_SHRINK) |
637 | if (enc.json.flags & F_SHRINK) |
643 | shrink (enc.sv); |
638 | shrink (enc.sv); |
644 | |
639 | |
645 | return enc.sv; |
640 | return enc.sv; |
646 | } |
641 | } |
647 | |
642 | |
… | |
… | |
652 | typedef struct |
647 | typedef struct |
653 | { |
648 | { |
654 | char *cur; // current parser pointer |
649 | char *cur; // current parser pointer |
655 | char *end; // end of input string |
650 | char *end; // end of input string |
656 | const char *err; // parse error, if != 0 |
651 | const char *err; // parse error, if != 0 |
657 | U32 flags; // F_* |
652 | JSON json; |
658 | U32 depth; // recursion depth |
653 | U32 depth; // recursion depth |
659 | U32 maxdepth; // recursion depth limit |
654 | U32 maxdepth; // recursion depth limit |
660 | } dec_t; |
655 | } dec_t; |
661 | |
656 | |
662 | inline void |
657 | inline void |
… | |
… | |
1026 | for (;;) |
1021 | for (;;) |
1027 | { |
1022 | { |
1028 | decode_ws (dec); EXPECT_CH ('"'); |
1023 | decode_ws (dec); EXPECT_CH ('"'); |
1029 | |
1024 | |
1030 | // heuristic: assume that |
1025 | // heuristic: assume that |
1031 | // a) decode_str + hv_store_ent are abysmally slow |
1026 | // a) decode_str + hv_store_ent are abysmally slow. |
1032 | // b) most hash keys are short, simple ascii text |
1027 | // b) most hash keys are short, simple ascii text. |
1033 | // so try to "fast-match" such strings to avoid |
1028 | // => try to "fast-match" such strings to avoid |
1034 | // the overhead of hv_store_ent. |
1029 | // the overhead of decode_str + hv_store_ent. |
1035 | { |
1030 | { |
1036 | SV *value; |
1031 | SV *value; |
1037 | char *p = dec->cur; |
1032 | char *p = dec->cur; |
1038 | char *e = p + 24; // only try up to 24 bytes |
1033 | char *e = p + 24; // only try up to 24 bytes |
1039 | |
1034 | |
1040 | for (;;) |
1035 | for (;;) |
1041 | { |
1036 | { |
|
|
1037 | // the >= 0x80 is true on most architectures |
1042 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1038 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1043 | { |
1039 | { |
1044 | // slow path, back up and use decode_str |
1040 | // slow path, back up and use decode_str |
1045 | SV *key = decode_str (dec); |
1041 | SV *key = decode_str (dec); |
1046 | if (!key) |
1042 | if (!key) |
… | |
… | |
1164 | fail: |
1160 | fail: |
1165 | return 0; |
1161 | return 0; |
1166 | } |
1162 | } |
1167 | |
1163 | |
1168 | static SV * |
1164 | static SV * |
1169 | decode_json (SV *string, U32 flags, UV *offset_return) |
1165 | decode_json (SV *string, JSON *json, UV *offset_return) |
1170 | { |
1166 | { |
1171 | dec_t dec; |
1167 | dec_t dec; |
1172 | UV offset; |
1168 | UV offset; |
1173 | SV *sv; |
1169 | SV *sv; |
1174 | |
1170 | |
1175 | SvGETMAGIC (string); |
1171 | SvGETMAGIC (string); |
1176 | SvUPGRADE (string, SVt_PV); |
1172 | SvUPGRADE (string, SVt_PV); |
1177 | |
1173 | |
1178 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
1174 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
1179 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1175 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1180 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); |
1176 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
1181 | |
1177 | |
1182 | if (flags & F_UTF8) |
1178 | if (json->flags & F_UTF8) |
1183 | sv_utf8_downgrade (string, 0); |
1179 | sv_utf8_downgrade (string, 0); |
1184 | else |
1180 | else |
1185 | sv_utf8_upgrade (string); |
1181 | sv_utf8_upgrade (string); |
1186 | |
1182 | |
1187 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1183 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1188 | |
1184 | |
1189 | dec.flags = flags; |
1185 | dec.json = *json; |
1190 | dec.cur = SvPVX (string); |
1186 | dec.cur = SvPVX (string); |
1191 | dec.end = SvEND (string); |
1187 | dec.end = SvEND (string); |
1192 | dec.err = 0; |
1188 | dec.err = 0; |
1193 | dec.depth = 0; |
1189 | dec.depth = 0; |
1194 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1190 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1195 | |
1191 | |
1196 | *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 |
1197 | sv = decode_sv (&dec); |
1193 | sv = decode_sv (&dec); |
1198 | |
1194 | |
1199 | if (!(offset_return || !sv)) |
1195 | if (!(offset_return || !sv)) |
… | |
… | |
1209 | } |
1205 | } |
1210 | } |
1206 | } |
1211 | |
1207 | |
1212 | if (offset_return || !sv) |
1208 | if (offset_return || !sv) |
1213 | { |
1209 | { |
1214 | offset = dec.flags & F_UTF8 |
1210 | offset = dec.json.flags & F_UTF8 |
1215 | ? dec.cur - SvPVX (string) |
1211 | ? dec.cur - SvPVX (string) |
1216 | : utf8_distance (dec.cur, SvPVX (string)); |
1212 | : utf8_distance (dec.cur, SvPVX (string)); |
1217 | |
1213 | |
1218 | if (offset_return) |
1214 | if (offset_return) |
1219 | *offset_return = offset; |
1215 | *offset_return = offset; |
… | |
… | |
1238 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1234 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1239 | } |
1235 | } |
1240 | |
1236 | |
1241 | sv = sv_2mortal (sv); |
1237 | sv = sv_2mortal (sv); |
1242 | |
1238 | |
1243 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1239 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1244 | 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)"); |
1245 | |
1241 | |
1246 | return sv; |
1242 | return sv; |
1247 | } |
1243 | } |
1248 | |
1244 | |
… | |
… | |
1269 | 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); |
1270 | } |
1266 | } |
1271 | |
1267 | |
1272 | PROTOTYPES: DISABLE |
1268 | PROTOTYPES: DISABLE |
1273 | |
1269 | |
1274 | SV *new (char *dummy) |
1270 | void new (char *klass) |
1275 | CODE: |
1271 | PPCODE: |
1276 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1272 | { |
1277 | OUTPUT: |
1273 | SV *pv = NEWSV (0, sizeof (JSON)); |
1278 | 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 | } |
1279 | |
1279 | |
1280 | SV *ascii (SV *self, int enable = 1) |
1280 | void ascii (JSON *self, int enable = 1) |
1281 | ALIAS: |
1281 | ALIAS: |
1282 | ascii = F_ASCII |
1282 | ascii = F_ASCII |
1283 | latin1 = F_LATIN1 |
1283 | latin1 = F_LATIN1 |
1284 | utf8 = F_UTF8 |
1284 | utf8 = F_UTF8 |
1285 | indent = F_INDENT |
1285 | indent = F_INDENT |
… | |
… | |
1289 | pretty = F_PRETTY |
1289 | pretty = F_PRETTY |
1290 | allow_nonref = F_ALLOW_NONREF |
1290 | allow_nonref = F_ALLOW_NONREF |
1291 | shrink = F_SHRINK |
1291 | shrink = F_SHRINK |
1292 | allow_blessed = F_ALLOW_BLESSED |
1292 | allow_blessed = F_ALLOW_BLESSED |
1293 | convert_blessed = F_CONV_BLESSED |
1293 | convert_blessed = F_CONV_BLESSED |
1294 | CODE: |
1294 | PPCODE: |
1295 | { |
1295 | { |
1296 | UV *uv = SvJSON (self); |
|
|
1297 | if (enable) |
1296 | if (enable) |
1298 | *uv |= ix; |
1297 | self->flags |= ix; |
1299 | else |
1298 | else |
1300 | *uv &= ~ix; |
1299 | self->flags &= ~ix; |
1301 | |
1300 | |
1302 | RETVAL = newSVsv (self); |
1301 | XPUSHs (ST (0)); |
1303 | } |
1302 | } |
1304 | OUTPUT: |
|
|
1305 | RETVAL |
|
|
1306 | |
1303 | |
1307 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1304 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1308 | CODE: |
1305 | PPCODE: |
1309 | { |
1306 | { |
1310 | UV *uv = SvJSON (self); |
|
|
1311 | UV log2 = 0; |
1307 | UV log2 = 0; |
1312 | |
1308 | |
1313 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1309 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1314 | |
1310 | |
1315 | while ((1UL << log2) < max_depth) |
1311 | while ((1UL << log2) < max_depth) |
1316 | ++log2; |
1312 | ++log2; |
1317 | |
1313 | |
1318 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1314 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1319 | |
1315 | |
1320 | RETVAL = newSVsv (self); |
1316 | XPUSHs (ST (0)); |
1321 | } |
1317 | } |
1322 | OUTPUT: |
|
|
1323 | RETVAL |
|
|
1324 | |
1318 | |
1325 | SV *max_size (SV *self, UV max_size = 0) |
1319 | void max_size (JSON *self, UV max_size = 0) |
1326 | CODE: |
1320 | PPCODE: |
1327 | { |
1321 | { |
1328 | UV *uv = SvJSON (self); |
|
|
1329 | UV log2 = 0; |
1322 | UV log2 = 0; |
1330 | |
1323 | |
1331 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1324 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1332 | if (max_size == 1) max_size = 2; |
1325 | if (max_size == 1) max_size = 2; |
1333 | |
1326 | |
1334 | while ((1UL << log2) < max_size) |
1327 | while ((1UL << log2) < max_size) |
1335 | ++log2; |
1328 | ++log2; |
1336 | |
1329 | |
1337 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1330 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1338 | |
1331 | |
1339 | RETVAL = newSVsv (self); |
1332 | XPUSHs (ST (0)); |
1340 | } |
1333 | } |
1341 | OUTPUT: |
|
|
1342 | RETVAL |
|
|
1343 | |
1334 | |
1344 | void encode (SV *self, SV *scalar) |
1335 | void encode (JSON *self, SV *scalar) |
1345 | PPCODE: |
1336 | PPCODE: |
1346 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1337 | XPUSHs (encode_json (scalar, self)); |
1347 | |
1338 | |
1348 | void decode (SV *self, SV *jsonstr) |
1339 | void decode (JSON *self, SV *jsonstr) |
1349 | PPCODE: |
1340 | PPCODE: |
1350 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1341 | XPUSHs (decode_json (jsonstr, self, 0)); |
1351 | |
1342 | |
1352 | void decode_prefix (SV *self, SV *jsonstr) |
1343 | void decode_prefix (JSON *self, SV *jsonstr) |
1353 | PPCODE: |
1344 | PPCODE: |
1354 | { |
1345 | { |
1355 | UV offset; |
1346 | UV offset; |
1356 | EXTEND (SP, 2); |
1347 | EXTEND (SP, 2); |
1357 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1348 | PUSHs (decode_json (jsonstr, self, &offset)); |
1358 | PUSHs (sv_2mortal (newSVuv (offset))); |
1349 | PUSHs (sv_2mortal (newSVuv (offset))); |
1359 | } |
1350 | } |
1360 | |
1351 | |
1361 | PROTOTYPES: ENABLE |
1352 | PROTOTYPES: ENABLE |
1362 | |
1353 | |
1363 | void to_json (SV *scalar) |
1354 | void to_json (SV *scalar) |
1364 | ALIAS: |
|
|
1365 | objToJson = 0 |
|
|
1366 | PPCODE: |
1355 | PPCODE: |
|
|
1356 | { |
|
|
1357 | JSON json = { F_DEFAULT | F_UTF8 }; |
1367 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1358 | XPUSHs (encode_json (scalar, &json)); |
|
|
1359 | } |
1368 | |
1360 | |
1369 | void from_json (SV *jsonstr) |
1361 | void from_json (SV *jsonstr) |
1370 | ALIAS: |
|
|
1371 | jsonToObj = 0 |
|
|
1372 | PPCODE: |
1362 | PPCODE: |
|
|
1363 | { |
|
|
1364 | JSON json = { F_DEFAULT | F_UTF8 }; |
1373 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1365 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1366 | } |
1374 | |
1367 | |