1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include <assert.h> |
6 | #include "string.h" |
6 | #include <string.h> |
7 | #include "stdlib.h" |
7 | #include <stdlib.h> |
8 | #include "stdio.h" |
8 | #include <stdio.h> |
|
|
9 | #include <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
… | |
… | |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
31 | #define F_CONV_BLESSED 0x00000800UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
33 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
34 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
35 | #define S_MAXSIZE 20 |
|
|
36 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
35 | |
37 | |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
38 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
39 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | |
40 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
41 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
… | |
… | |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
62 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
64 | static SV *json_true, *json_false; |
63 | |
65 | |
|
|
66 | typedef struct { |
|
|
67 | U32 flags; |
|
|
68 | SV *cb_object; |
|
|
69 | HV *cb_sk_object; |
|
|
70 | } JSON; |
|
|
71 | |
64 | ///////////////////////////////////////////////////////////////////////////// |
72 | ///////////////////////////////////////////////////////////////////////////// |
65 | // utility functions |
73 | // utility functions |
66 | |
74 | |
67 | static UV * |
75 | 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) |
76 | shrink (SV *sv) |
78 | { |
77 | { |
79 | sv_utf8_downgrade (sv, 1); |
78 | sv_utf8_downgrade (sv, 1); |
80 | if (SvLEN (sv) > SvCUR (sv) + 1) |
79 | if (SvLEN (sv) > SvCUR (sv) + 1) |
81 | { |
80 | { |
… | |
… | |
116 | typedef struct |
115 | typedef struct |
117 | { |
116 | { |
118 | char *cur; // SvPVX (sv) + current output position |
117 | char *cur; // SvPVX (sv) + current output position |
119 | char *end; // SvEND (sv) |
118 | char *end; // SvEND (sv) |
120 | SV *sv; // result scalar |
119 | SV *sv; // result scalar |
121 | U32 flags; // F_* |
120 | JSON json; |
122 | U32 indent; // indentation level |
121 | U32 indent; // indentation level |
123 | U32 maxdepth; // max. indentation/recursion level |
122 | U32 maxdepth; // max. indentation/recursion level |
124 | } enc_t; |
123 | } enc_t; |
125 | |
124 | |
126 | inline void |
125 | inline void |
… | |
… | |
200 | } |
199 | } |
201 | |
200 | |
202 | if (uch > 0x10FFFFUL) |
201 | if (uch > 0x10FFFFUL) |
203 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
202 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
204 | |
203 | |
205 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
204 | if (uch < 0x80 || enc->json.flags & F_ASCII || (enc->json.flags & F_LATIN1 && uch > 0xFF)) |
206 | { |
205 | { |
207 | if (uch > 0xFFFFUL) |
206 | if (uch > 0xFFFFUL) |
208 | { |
207 | { |
209 | need (enc, len += 11); |
208 | need (enc, len += 11); |
210 | sprintf (enc->cur, "\\u%04x\\u%04x", |
209 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
224 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
223 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
225 | } |
224 | } |
226 | |
225 | |
227 | str += clen; |
226 | str += clen; |
228 | } |
227 | } |
229 | else if (enc->flags & F_LATIN1) |
228 | else if (enc->json.flags & F_LATIN1) |
230 | { |
229 | { |
231 | *enc->cur++ = uch; |
230 | *enc->cur++ = uch; |
232 | str += clen; |
231 | str += clen; |
233 | } |
232 | } |
234 | else if (is_utf8) |
233 | else if (is_utf8) |
… | |
… | |
255 | } |
254 | } |
256 | |
255 | |
257 | inline void |
256 | inline void |
258 | encode_indent (enc_t *enc) |
257 | encode_indent (enc_t *enc) |
259 | { |
258 | { |
260 | if (enc->flags & F_INDENT) |
259 | if (enc->json.flags & F_INDENT) |
261 | { |
260 | { |
262 | int spaces = enc->indent * INDENT_STEP; |
261 | int spaces = enc->indent * INDENT_STEP; |
263 | |
262 | |
264 | need (enc, spaces); |
263 | need (enc, spaces); |
265 | memset (enc->cur, ' ', spaces); |
264 | memset (enc->cur, ' ', spaces); |
… | |
… | |
275 | } |
274 | } |
276 | |
275 | |
277 | inline void |
276 | inline void |
278 | encode_nl (enc_t *enc) |
277 | encode_nl (enc_t *enc) |
279 | { |
278 | { |
280 | if (enc->flags & F_INDENT) |
279 | if (enc->json.flags & F_INDENT) |
281 | { |
280 | { |
282 | need (enc, 1); |
281 | need (enc, 1); |
283 | encode_ch (enc, '\n'); |
282 | encode_ch (enc, '\n'); |
284 | } |
283 | } |
285 | } |
284 | } |
… | |
… | |
287 | inline void |
286 | inline void |
288 | encode_comma (enc_t *enc) |
287 | encode_comma (enc_t *enc) |
289 | { |
288 | { |
290 | encode_ch (enc, ','); |
289 | encode_ch (enc, ','); |
291 | |
290 | |
292 | if (enc->flags & F_INDENT) |
291 | if (enc->json.flags & F_INDENT) |
293 | encode_nl (enc); |
292 | encode_nl (enc); |
294 | else if (enc->flags & F_SPACE_AFTER) |
293 | else if (enc->json.flags & F_SPACE_AFTER) |
295 | encode_space (enc); |
294 | encode_space (enc); |
296 | } |
295 | } |
297 | |
296 | |
298 | static void encode_sv (enc_t *enc, SV *sv); |
297 | static void encode_sv (enc_t *enc, SV *sv); |
299 | |
298 | |
… | |
… | |
308 | encode_ch (enc, '['); encode_nl (enc); |
307 | encode_ch (enc, '['); encode_nl (enc); |
309 | ++enc->indent; |
308 | ++enc->indent; |
310 | |
309 | |
311 | for (i = 0; i <= len; ++i) |
310 | for (i = 0; i <= len; ++i) |
312 | { |
311 | { |
|
|
312 | SV **svp = av_fetch (av, i, 0); |
|
|
313 | |
313 | encode_indent (enc); |
314 | encode_indent (enc); |
314 | encode_sv (enc, *av_fetch (av, i, 0)); |
315 | |
|
|
316 | if (svp) |
|
|
317 | encode_sv (enc, *svp); |
|
|
318 | else |
|
|
319 | encode_str (enc, "null", 4, 0); |
315 | |
320 | |
316 | if (i < len) |
321 | if (i < len) |
317 | encode_comma (enc); |
322 | encode_comma (enc); |
318 | } |
323 | } |
319 | |
324 | |
… | |
… | |
342 | else |
347 | else |
343 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
348 | encode_str (enc, HeKEY (he), HeKLEN (he), HeKUTF8 (he)); |
344 | |
349 | |
345 | encode_ch (enc, '"'); |
350 | encode_ch (enc, '"'); |
346 | |
351 | |
347 | if (enc->flags & F_SPACE_BEFORE) encode_space (enc); |
352 | if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); |
348 | encode_ch (enc, ':'); |
353 | encode_ch (enc, ':'); |
349 | if (enc->flags & F_SPACE_AFTER ) encode_space (enc); |
354 | if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); |
350 | encode_sv (enc, HeVAL (he)); |
355 | encode_sv (enc, HeVAL (he)); |
351 | } |
356 | } |
352 | |
357 | |
353 | // compare hash entries, used when all keys are bytestrings |
358 | // compare hash entries, used when all keys are bytestrings |
354 | static int |
359 | static int |
… | |
… | |
389 | { |
394 | { |
390 | // for canonical output we have to sort by keys first |
395 | // for canonical output we have to sort by keys first |
391 | // actually, this is mostly due to the stupid so-called |
396 | // actually, this is mostly due to the stupid so-called |
392 | // security workaround added somewhere in 5.8.x. |
397 | // security workaround added somewhere in 5.8.x. |
393 | // that randomises hash orderings |
398 | // that randomises hash orderings |
394 | if (enc->flags & F_CANONICAL) |
399 | if (enc->json.flags & F_CANONICAL) |
395 | { |
400 | { |
396 | int fast = 1; |
401 | int fast = 1; |
397 | HE *he; |
402 | HE *he; |
398 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
403 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
399 | HE **hes = _alloca (count * sizeof (HE)); |
404 | HE **hes = _alloca (count * sizeof (HE)); |
… | |
… | |
475 | |
480 | |
476 | if (expect_false (SvOBJECT (sv))) |
481 | if (expect_false (SvOBJECT (sv))) |
477 | { |
482 | { |
478 | if (SvSTASH (sv) == json_boolean_stash) |
483 | if (SvSTASH (sv) == json_boolean_stash) |
479 | { |
484 | { |
480 | if (SvIV (sv) == 0) |
485 | if (SvIV (sv)) |
|
|
486 | encode_str (enc, "true", 4, 0); |
|
|
487 | else |
481 | encode_str (enc, "false", 5, 0); |
488 | encode_str (enc, "false", 5, 0); |
482 | else |
|
|
483 | encode_str (enc, "true", 4, 0); |
|
|
484 | } |
489 | } |
485 | else |
490 | else |
486 | { |
491 | { |
487 | #if 0 |
492 | #if 0 |
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
493 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
489 | { |
494 | { |
490 | // not yet |
495 | // not yet |
491 | } |
496 | } |
492 | #endif |
497 | #endif |
493 | if (enc->flags & F_CONV_BLESSED) |
498 | if (enc->json.flags & F_CONV_BLESSED) |
494 | { |
499 | { |
495 | // we re-bless the reference to get overload and other niceties right |
500 | // we re-bless the reference to get overload and other niceties right |
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
501 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
497 | |
502 | |
498 | if (to_json) |
503 | if (to_json) |
499 | { |
504 | { |
|
|
505 | int count; |
500 | dSP; |
506 | dSP; |
501 | ENTER; |
507 | |
502 | SAVETMPS; |
|
|
503 | PUSHMARK (SP); |
508 | ENTER; SAVETMPS; PUSHMARK (SP); |
504 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
505 | |
510 | |
506 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
511 | // calling with G_SCALAR ensures that we always get a 1 return value |
507 | // check anyways. |
|
|
508 | PUTBACK; |
512 | PUTBACK; |
509 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
513 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
510 | SPAGAIN; |
514 | SPAGAIN; |
511 | |
515 | |
|
|
516 | // catch this surprisingly common error |
|
|
517 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
518 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
519 | |
|
|
520 | sv = POPs; |
|
|
521 | PUTBACK; |
|
|
522 | |
512 | encode_sv (enc, POPs); |
523 | encode_sv (enc, sv); |
513 | |
524 | |
514 | FREETMPS; |
525 | FREETMPS; LEAVE; |
515 | LEAVE; |
|
|
516 | } |
526 | } |
517 | else if (enc->flags & F_ALLOW_BLESSED) |
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
518 | encode_str (enc, "null", 4, 0); |
528 | encode_str (enc, "null", 4, 0); |
519 | else |
529 | else |
520 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
530 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
531 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
522 | } |
532 | } |
523 | else if (enc->flags & F_ALLOW_BLESSED) |
533 | else if (enc->json.flags & F_ALLOW_BLESSED) |
524 | encode_str (enc, "null", 4, 0); |
534 | encode_str (enc, "null", 4, 0); |
525 | else |
535 | else |
526 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
536 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
537 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
528 | } |
538 | } |
… | |
… | |
531 | encode_hv (enc, (HV *)sv); |
541 | encode_hv (enc, (HV *)sv); |
532 | else if (svt == SVt_PVAV) |
542 | else if (svt == SVt_PVAV) |
533 | encode_av (enc, (AV *)sv); |
543 | encode_av (enc, (AV *)sv); |
534 | else if (svt < SVt_PVAV) |
544 | else if (svt < SVt_PVAV) |
535 | { |
545 | { |
536 | if (SvNIOK (sv) && SvIV (sv) == 0) |
546 | STRLEN len = 0; |
|
|
547 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
548 | |
|
|
549 | if (len == 1 && *pv == '1') |
|
|
550 | encode_str (enc, "true", 4, 0); |
|
|
551 | else if (len == 1 && *pv == '0') |
537 | encode_str (enc, "false", 5, 0); |
552 | encode_str (enc, "false", 5, 0); |
538 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
539 | encode_str (enc, "true", 4, 0); |
|
|
540 | else |
553 | else |
541 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
554 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
542 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
555 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
543 | } |
556 | } |
544 | else |
557 | else |
… | |
… | |
614 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
627 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
615 | SvPV_nolen (sv), SvFLAGS (sv)); |
628 | SvPV_nolen (sv), SvFLAGS (sv)); |
616 | } |
629 | } |
617 | |
630 | |
618 | static SV * |
631 | static SV * |
619 | encode_json (SV *scalar, U32 flags) |
632 | encode_json (SV *scalar, JSON *json) |
620 | { |
633 | { |
621 | enc_t enc; |
634 | enc_t enc; |
622 | |
635 | |
623 | if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
636 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
624 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
637 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
625 | |
638 | |
626 | enc.flags = flags; |
639 | enc.json = *json; |
627 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
640 | enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); |
628 | enc.cur = SvPVX (enc.sv); |
641 | enc.cur = SvPVX (enc.sv); |
629 | enc.end = SvEND (enc.sv); |
642 | enc.end = SvEND (enc.sv); |
630 | enc.indent = 0; |
643 | enc.indent = 0; |
631 | enc.maxdepth = DEC_DEPTH (flags); |
644 | enc.maxdepth = DEC_DEPTH (enc.json.flags); |
632 | |
645 | |
633 | SvPOK_only (enc.sv); |
646 | SvPOK_only (enc.sv); |
634 | encode_sv (&enc, scalar); |
647 | encode_sv (&enc, scalar); |
635 | |
648 | |
636 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
649 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
637 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
650 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
638 | |
651 | |
639 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
652 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
640 | SvUTF8_on (enc.sv); |
653 | SvUTF8_on (enc.sv); |
641 | |
654 | |
642 | if (enc.flags & F_SHRINK) |
655 | if (enc.json.flags & F_SHRINK) |
643 | shrink (enc.sv); |
656 | shrink (enc.sv); |
644 | |
657 | |
645 | return enc.sv; |
658 | return enc.sv; |
646 | } |
659 | } |
647 | |
660 | |
… | |
… | |
652 | typedef struct |
665 | typedef struct |
653 | { |
666 | { |
654 | char *cur; // current parser pointer |
667 | char *cur; // current parser pointer |
655 | char *end; // end of input string |
668 | char *end; // end of input string |
656 | const char *err; // parse error, if != 0 |
669 | const char *err; // parse error, if != 0 |
657 | U32 flags; // F_* |
670 | JSON json; |
658 | U32 depth; // recursion depth |
671 | U32 depth; // recursion depth |
659 | U32 maxdepth; // recursion depth limit |
672 | U32 maxdepth; // recursion depth limit |
660 | } dec_t; |
673 | } dec_t; |
661 | |
674 | |
662 | inline void |
675 | inline void |
… | |
… | |
925 | is_nv = 1; |
938 | is_nv = 1; |
926 | } |
939 | } |
927 | |
940 | |
928 | if (!is_nv) |
941 | if (!is_nv) |
929 | { |
942 | { |
|
|
943 | int len = dec->cur - start; |
|
|
944 | |
930 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
945 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
931 | if (*start == '-') |
946 | if (*start == '-') |
932 | switch (dec->cur - start) |
947 | switch (len) |
933 | { |
948 | { |
934 | case 2: return newSViv (-( start [1] - '0' * 1)); |
949 | case 2: return newSViv (-( start [1] - '0' * 1)); |
935 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
950 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
936 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
951 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
937 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
952 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
938 | } |
953 | } |
939 | else |
954 | else |
940 | switch (dec->cur - start) |
955 | switch (len) |
941 | { |
956 | { |
942 | case 1: return newSViv ( start [0] - '0' * 1); |
957 | case 1: return newSViv ( start [0] - '0' * 1); |
943 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
958 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
944 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
959 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
945 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
960 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
946 | } |
961 | } |
947 | |
962 | |
948 | { |
963 | { |
949 | UV uv; |
964 | UV uv; |
950 | int numtype = grok_number (start, dec->cur - start, &uv); |
965 | int numtype = grok_number (start, len, &uv); |
951 | if (numtype & IS_NUMBER_IN_UV) |
966 | if (numtype & IS_NUMBER_IN_UV) |
952 | if (numtype & IS_NUMBER_NEG) |
967 | if (numtype & IS_NUMBER_NEG) |
953 | { |
968 | { |
954 | if (uv < (UV)IV_MIN) |
969 | if (uv < (UV)IV_MIN) |
955 | return newSViv (-(IV)uv); |
970 | return newSViv (-(IV)uv); |
956 | } |
971 | } |
957 | else |
972 | else |
958 | return newSVuv (uv); |
973 | return newSVuv (uv); |
959 | |
|
|
960 | // here would likely be the place for bigint support |
|
|
961 | } |
974 | } |
962 | } |
|
|
963 | |
975 | |
964 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
976 | len -= *start == '-' ? 1 : 0; |
|
|
977 | |
|
|
978 | // does not fit into IV or UV, try NV |
|
|
979 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
980 | #if defined (LDBL_DIG) |
|
|
981 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
982 | #endif |
|
|
983 | ) |
|
|
984 | // fits into NV without loss of precision |
|
|
985 | return newSVnv (Atof (start)); |
|
|
986 | |
|
|
987 | // everything else fails, convert it to a string |
|
|
988 | return newSVpvn (start, dec->cur - start); |
|
|
989 | } |
|
|
990 | |
|
|
991 | // loss of precision here |
965 | return newSVnv (Atof (start)); |
992 | return newSVnv (Atof (start)); |
966 | |
993 | |
967 | fail: |
994 | fail: |
968 | return 0; |
995 | return 0; |
969 | } |
996 | } |
… | |
… | |
1013 | } |
1040 | } |
1014 | |
1041 | |
1015 | static SV * |
1042 | static SV * |
1016 | decode_hv (dec_t *dec) |
1043 | decode_hv (dec_t *dec) |
1017 | { |
1044 | { |
|
|
1045 | SV *sv; |
1018 | HV *hv = newHV (); |
1046 | HV *hv = newHV (); |
1019 | |
1047 | |
1020 | DEC_INC_DEPTH; |
1048 | DEC_INC_DEPTH; |
1021 | decode_ws (dec); |
1049 | decode_ws (dec); |
1022 | |
1050 | |
1023 | if (*dec->cur == '}') |
1051 | if (*dec->cur == '}') |
1024 | ++dec->cur; |
1052 | ++dec->cur; |
1025 | else |
1053 | else |
1026 | for (;;) |
1054 | for (;;) |
1027 | { |
1055 | { |
1028 | SV *key, *value; |
|
|
1029 | |
|
|
1030 | decode_ws (dec); EXPECT_CH ('"'); |
1056 | decode_ws (dec); EXPECT_CH ('"'); |
1031 | |
1057 | |
1032 | key = decode_str (dec); |
1058 | // heuristic: assume that |
1033 | if (!key) |
1059 | // a) decode_str + hv_store_ent are abysmally slow. |
1034 | goto fail; |
1060 | // b) most hash keys are short, simple ascii text. |
|
|
1061 | // => try to "fast-match" such strings to avoid |
|
|
1062 | // the overhead of decode_str + hv_store_ent. |
|
|
1063 | { |
|
|
1064 | SV *value; |
|
|
1065 | char *p = dec->cur; |
|
|
1066 | char *e = p + 24; // only try up to 24 bytes |
1035 | |
1067 | |
1036 | decode_ws (dec); EXPECT_CH (':'); |
1068 | for (;;) |
1037 | |
|
|
1038 | value = decode_sv (dec); |
|
|
1039 | if (!value) |
|
|
1040 | { |
1069 | { |
|
|
1070 | // the >= 0x80 is true on most architectures |
|
|
1071 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1072 | { |
|
|
1073 | // slow path, back up and use decode_str |
|
|
1074 | SV *key = decode_str (dec); |
|
|
1075 | if (!key) |
|
|
1076 | goto fail; |
|
|
1077 | |
|
|
1078 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1079 | |
|
|
1080 | value = decode_sv (dec); |
|
|
1081 | if (!value) |
|
|
1082 | { |
|
|
1083 | SvREFCNT_dec (key); |
|
|
1084 | goto fail; |
|
|
1085 | } |
|
|
1086 | |
|
|
1087 | hv_store_ent (hv, key, value, 0); |
1041 | SvREFCNT_dec (key); |
1088 | SvREFCNT_dec (key); |
|
|
1089 | |
|
|
1090 | break; |
|
|
1091 | } |
|
|
1092 | else if (*p == '"') |
|
|
1093 | { |
|
|
1094 | // fast path, got a simple key |
|
|
1095 | char *key = dec->cur; |
|
|
1096 | int len = p - key; |
|
|
1097 | dec->cur = p + 1; |
|
|
1098 | |
|
|
1099 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1100 | |
|
|
1101 | value = decode_sv (dec); |
|
|
1102 | if (!value) |
1042 | goto fail; |
1103 | goto fail; |
|
|
1104 | |
|
|
1105 | hv_store (hv, key, len, value, 0); |
|
|
1106 | |
|
|
1107 | break; |
|
|
1108 | } |
|
|
1109 | |
|
|
1110 | ++p; |
1043 | } |
1111 | } |
1044 | |
1112 | } |
1045 | hv_store_ent (hv, key, value, 0); |
|
|
1046 | SvREFCNT_dec (key); |
|
|
1047 | |
1113 | |
1048 | decode_ws (dec); |
1114 | decode_ws (dec); |
1049 | |
1115 | |
1050 | if (*dec->cur == '}') |
1116 | if (*dec->cur == '}') |
1051 | { |
1117 | { |
… | |
… | |
1058 | |
1124 | |
1059 | ++dec->cur; |
1125 | ++dec->cur; |
1060 | } |
1126 | } |
1061 | |
1127 | |
1062 | DEC_DEC_DEPTH; |
1128 | DEC_DEC_DEPTH; |
1063 | return newRV_noinc ((SV *)hv); |
1129 | sv = newRV_noinc ((SV *)hv); |
|
|
1130 | |
|
|
1131 | // check filter callbacks |
|
|
1132 | if (dec->json.flags & F_HOOK) |
|
|
1133 | { |
|
|
1134 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1135 | { |
|
|
1136 | HE *cb, *he; |
|
|
1137 | |
|
|
1138 | hv_iterinit (hv); |
|
|
1139 | he = hv_iternext (hv); |
|
|
1140 | hv_iterinit (hv); |
|
|
1141 | |
|
|
1142 | // the next line creates a mortal sv each time its called. |
|
|
1143 | // might want to optimise this for common cases. |
|
|
1144 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1145 | |
|
|
1146 | if (cb) |
|
|
1147 | { |
|
|
1148 | dSP; |
|
|
1149 | int count; |
|
|
1150 | |
|
|
1151 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1152 | XPUSHs (HeVAL (he)); |
|
|
1153 | |
|
|
1154 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1155 | |
|
|
1156 | if (count == 1) |
|
|
1157 | { |
|
|
1158 | sv = newSVsv (POPs); |
|
|
1159 | FREETMPS; LEAVE; |
|
|
1160 | return sv; |
|
|
1161 | } |
|
|
1162 | |
|
|
1163 | FREETMPS; LEAVE; |
|
|
1164 | } |
|
|
1165 | } |
|
|
1166 | |
|
|
1167 | if (dec->json.cb_object) |
|
|
1168 | { |
|
|
1169 | dSP; |
|
|
1170 | int count; |
|
|
1171 | |
|
|
1172 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1173 | XPUSHs (sv_2mortal (sv)); |
|
|
1174 | |
|
|
1175 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1176 | |
|
|
1177 | if (count == 1) |
|
|
1178 | { |
|
|
1179 | sv = newSVsv (POPs); |
|
|
1180 | FREETMPS; LEAVE; |
|
|
1181 | return sv; |
|
|
1182 | } |
|
|
1183 | |
|
|
1184 | SvREFCNT_inc (sv); |
|
|
1185 | FREETMPS; LEAVE; |
|
|
1186 | } |
|
|
1187 | } |
|
|
1188 | |
|
|
1189 | return sv; |
1064 | |
1190 | |
1065 | fail: |
1191 | fail: |
1066 | SvREFCNT_dec (hv); |
1192 | SvREFCNT_dec (hv); |
1067 | DEC_DEC_DEPTH; |
1193 | DEC_DEC_DEPTH; |
1068 | return 0; |
1194 | return 0; |
… | |
… | |
1127 | fail: |
1253 | fail: |
1128 | return 0; |
1254 | return 0; |
1129 | } |
1255 | } |
1130 | |
1256 | |
1131 | static SV * |
1257 | static SV * |
1132 | decode_json (SV *string, U32 flags, UV *offset_return) |
1258 | decode_json (SV *string, JSON *json, UV *offset_return) |
1133 | { |
1259 | { |
1134 | dec_t dec; |
1260 | dec_t dec; |
1135 | UV offset; |
1261 | UV offset; |
1136 | SV *sv; |
1262 | SV *sv; |
1137 | |
1263 | |
1138 | SvGETMAGIC (string); |
1264 | SvGETMAGIC (string); |
1139 | SvUPGRADE (string, SVt_PV); |
1265 | SvUPGRADE (string, SVt_PV); |
1140 | |
1266 | |
1141 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
1267 | if (json->flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (json->flags)) |
1142 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1268 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1143 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (flags)); |
1269 | (unsigned long)SvCUR (string), (unsigned long)DEC_SIZE (json->flags)); |
1144 | |
1270 | |
1145 | if (flags & F_UTF8) |
1271 | if (json->flags & F_UTF8) |
1146 | sv_utf8_downgrade (string, 0); |
1272 | sv_utf8_downgrade (string, 0); |
1147 | else |
1273 | else |
1148 | sv_utf8_upgrade (string); |
1274 | sv_utf8_upgrade (string); |
1149 | |
1275 | |
1150 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1276 | SvGROW (string, SvCUR (string) + 1); // should basically be a NOP |
1151 | |
1277 | |
1152 | dec.flags = flags; |
1278 | dec.json = *json; |
1153 | dec.cur = SvPVX (string); |
1279 | dec.cur = SvPVX (string); |
1154 | dec.end = SvEND (string); |
1280 | dec.end = SvEND (string); |
1155 | dec.err = 0; |
1281 | dec.err = 0; |
1156 | dec.depth = 0; |
1282 | dec.depth = 0; |
1157 | dec.maxdepth = DEC_DEPTH (dec.flags); |
1283 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1284 | |
|
|
1285 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1286 | dec.json.flags |= F_HOOK; |
1158 | |
1287 | |
1159 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1288 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1160 | sv = decode_sv (&dec); |
1289 | sv = decode_sv (&dec); |
1161 | |
1290 | |
1162 | if (!(offset_return || !sv)) |
1291 | if (!(offset_return || !sv)) |
… | |
… | |
1172 | } |
1301 | } |
1173 | } |
1302 | } |
1174 | |
1303 | |
1175 | if (offset_return || !sv) |
1304 | if (offset_return || !sv) |
1176 | { |
1305 | { |
1177 | offset = dec.flags & F_UTF8 |
1306 | offset = dec.json.flags & F_UTF8 |
1178 | ? dec.cur - SvPVX (string) |
1307 | ? dec.cur - SvPVX (string) |
1179 | : utf8_distance (dec.cur, SvPVX (string)); |
1308 | : utf8_distance (dec.cur, SvPVX (string)); |
1180 | |
1309 | |
1181 | if (offset_return) |
1310 | if (offset_return) |
1182 | *offset_return = offset; |
1311 | *offset_return = offset; |
… | |
… | |
1201 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1330 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1202 | } |
1331 | } |
1203 | |
1332 | |
1204 | sv = sv_2mortal (sv); |
1333 | sv = sv_2mortal (sv); |
1205 | |
1334 | |
1206 | if (!(dec.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1335 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1207 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1336 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1208 | |
1337 | |
1209 | return sv; |
1338 | return sv; |
1210 | } |
1339 | } |
1211 | |
1340 | |
… | |
… | |
1232 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1361 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1233 | } |
1362 | } |
1234 | |
1363 | |
1235 | PROTOTYPES: DISABLE |
1364 | PROTOTYPES: DISABLE |
1236 | |
1365 | |
1237 | SV *new (char *dummy) |
1366 | void new (char *klass) |
1238 | CODE: |
1367 | PPCODE: |
1239 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1368 | { |
1240 | OUTPUT: |
1369 | SV *pv = NEWSV (0, sizeof (JSON)); |
1241 | RETVAL |
1370 | SvPOK_only (pv); |
|
|
1371 | Zero (SvPVX (pv), 1, JSON); |
|
|
1372 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1373 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
|
|
1374 | } |
1242 | |
1375 | |
1243 | SV *ascii (SV *self, int enable = 1) |
1376 | void ascii (JSON *self, int enable = 1) |
1244 | ALIAS: |
1377 | ALIAS: |
1245 | ascii = F_ASCII |
1378 | ascii = F_ASCII |
1246 | latin1 = F_LATIN1 |
1379 | latin1 = F_LATIN1 |
1247 | utf8 = F_UTF8 |
1380 | utf8 = F_UTF8 |
1248 | indent = F_INDENT |
1381 | indent = F_INDENT |
… | |
… | |
1252 | pretty = F_PRETTY |
1385 | pretty = F_PRETTY |
1253 | allow_nonref = F_ALLOW_NONREF |
1386 | allow_nonref = F_ALLOW_NONREF |
1254 | shrink = F_SHRINK |
1387 | shrink = F_SHRINK |
1255 | allow_blessed = F_ALLOW_BLESSED |
1388 | allow_blessed = F_ALLOW_BLESSED |
1256 | convert_blessed = F_CONV_BLESSED |
1389 | convert_blessed = F_CONV_BLESSED |
1257 | CODE: |
1390 | PPCODE: |
1258 | { |
1391 | { |
1259 | UV *uv = SvJSON (self); |
|
|
1260 | if (enable) |
1392 | if (enable) |
1261 | *uv |= ix; |
1393 | self->flags |= ix; |
1262 | else |
1394 | else |
1263 | *uv &= ~ix; |
1395 | self->flags &= ~ix; |
1264 | |
1396 | |
1265 | RETVAL = newSVsv (self); |
1397 | XPUSHs (ST (0)); |
1266 | } |
1398 | } |
1267 | OUTPUT: |
|
|
1268 | RETVAL |
|
|
1269 | |
1399 | |
1270 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1400 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1271 | CODE: |
1401 | PPCODE: |
1272 | { |
1402 | { |
1273 | UV *uv = SvJSON (self); |
|
|
1274 | UV log2 = 0; |
1403 | UV log2 = 0; |
1275 | |
1404 | |
1276 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1405 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1277 | |
1406 | |
1278 | while ((1UL << log2) < max_depth) |
1407 | while ((1UL << log2) < max_depth) |
1279 | ++log2; |
1408 | ++log2; |
1280 | |
1409 | |
1281 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1410 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1282 | |
1411 | |
1283 | RETVAL = newSVsv (self); |
1412 | XPUSHs (ST (0)); |
1284 | } |
1413 | } |
1285 | OUTPUT: |
|
|
1286 | RETVAL |
|
|
1287 | |
1414 | |
1288 | SV *max_size (SV *self, UV max_size = 0) |
1415 | void max_size (JSON *self, UV max_size = 0) |
1289 | CODE: |
1416 | PPCODE: |
1290 | { |
1417 | { |
1291 | UV *uv = SvJSON (self); |
|
|
1292 | UV log2 = 0; |
1418 | UV log2 = 0; |
1293 | |
1419 | |
1294 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1420 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1295 | if (max_size == 1) max_size = 2; |
1421 | if (max_size == 1) max_size = 2; |
1296 | |
1422 | |
1297 | while ((1UL << log2) < max_size) |
1423 | while ((1UL << log2) < max_size) |
1298 | ++log2; |
1424 | ++log2; |
1299 | |
1425 | |
1300 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1426 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1301 | |
1427 | |
1302 | RETVAL = newSVsv (self); |
1428 | XPUSHs (ST (0)); |
1303 | } |
1429 | } |
1304 | OUTPUT: |
|
|
1305 | RETVAL |
|
|
1306 | |
1430 | |
1307 | void encode (SV *self, SV *scalar) |
1431 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1308 | PPCODE: |
1432 | PPCODE: |
1309 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1433 | { |
|
|
1434 | SvREFCNT_dec (self->cb_object); |
|
|
1435 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1310 | |
1436 | |
1311 | void decode (SV *self, SV *jsonstr) |
1437 | XPUSHs (ST (0)); |
|
|
1438 | } |
|
|
1439 | |
|
|
1440 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1312 | PPCODE: |
1441 | PPCODE: |
|
|
1442 | { |
|
|
1443 | if (!self->cb_sk_object) |
|
|
1444 | self->cb_sk_object = newHV (); |
|
|
1445 | |
|
|
1446 | if (SvOK (cb)) |
|
|
1447 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1448 | else |
|
|
1449 | { |
|
|
1450 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1451 | |
|
|
1452 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1453 | { |
|
|
1454 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1455 | self->cb_sk_object = 0; |
|
|
1456 | } |
|
|
1457 | } |
|
|
1458 | |
|
|
1459 | XPUSHs (ST (0)); |
|
|
1460 | } |
|
|
1461 | |
|
|
1462 | void encode (JSON *self, SV *scalar) |
|
|
1463 | PPCODE: |
|
|
1464 | XPUSHs (encode_json (scalar, self)); |
|
|
1465 | |
|
|
1466 | void decode (JSON *self, SV *jsonstr) |
|
|
1467 | PPCODE: |
1313 | XPUSHs (decode_json (jsonstr, *SvJSON (self), 0)); |
1468 | XPUSHs (decode_json (jsonstr, self, 0)); |
1314 | |
1469 | |
1315 | void decode_prefix (SV *self, SV *jsonstr) |
1470 | void decode_prefix (JSON *self, SV *jsonstr) |
1316 | PPCODE: |
1471 | PPCODE: |
1317 | { |
1472 | { |
1318 | UV offset; |
1473 | UV offset; |
1319 | EXTEND (SP, 2); |
1474 | EXTEND (SP, 2); |
1320 | PUSHs (decode_json (jsonstr, *SvJSON (self), &offset)); |
1475 | PUSHs (decode_json (jsonstr, self, &offset)); |
1321 | PUSHs (sv_2mortal (newSVuv (offset))); |
1476 | PUSHs (sv_2mortal (newSVuv (offset))); |
1322 | } |
1477 | } |
1323 | |
1478 | |
|
|
1479 | void DESTROY (JSON *self) |
|
|
1480 | CODE: |
|
|
1481 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1482 | SvREFCNT_dec (self->cb_object); |
|
|
1483 | |
1324 | PROTOTYPES: ENABLE |
1484 | PROTOTYPES: ENABLE |
1325 | |
1485 | |
1326 | void to_json (SV *scalar) |
1486 | void to_json (SV *scalar) |
1327 | ALIAS: |
|
|
1328 | objToJson = 0 |
|
|
1329 | PPCODE: |
1487 | PPCODE: |
|
|
1488 | { |
|
|
1489 | JSON json = { F_DEFAULT | F_UTF8 }; |
1330 | XPUSHs (encode_json (scalar, F_DEFAULT | F_UTF8)); |
1490 | XPUSHs (encode_json (scalar, &json)); |
|
|
1491 | } |
1331 | |
1492 | |
1332 | void from_json (SV *jsonstr) |
1493 | void from_json (SV *jsonstr) |
1333 | ALIAS: |
|
|
1334 | jsonToObj = 0 |
|
|
1335 | PPCODE: |
1494 | PPCODE: |
|
|
1495 | { |
|
|
1496 | JSON json = { F_DEFAULT | F_UTF8 }; |
1336 | XPUSHs (decode_json (jsonstr, F_DEFAULT | F_UTF8, 0)); |
1497 | XPUSHs (decode_json (jsonstr, &json, 0)); |
|
|
1498 | } |
1337 | |
1499 | |