… | |
… | |
9 | |
9 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
12 | #endif |
13 | |
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. if it breaks, you get to keep the pieces. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
|
|
18 | #endif |
|
|
19 | |
14 | #define F_ASCII 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
15 | #define F_LATIN1 0x00000002UL |
21 | #define F_LATIN1 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
22 | #define F_UTF8 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
23 | #define F_INDENT 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
24 | #define F_CANONICAL 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
28 | #define F_SHRINK 0x00000200UL |
|
|
29 | #define F_ALLOW_BLESSED 0x00000400UL |
|
|
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
23 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
24 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
|
|
33 | #define F_MAXSIZE 0x01f00000UL |
|
|
34 | #define S_MAXSIZE 20 |
25 | |
35 | |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
27 | |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
28 | // F_SELFCONVERT? <=> to_json/toJson |
|
|
29 | // F_BLESSED? <=> { $__class__$ => } |
|
|
30 | |
38 | |
31 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
32 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
40 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
33 | |
41 | |
34 | #define INIT_SIZE 32 // initial scalar size to be allocated |
42 | #define INIT_SIZE 32 // initial scalar size to be allocated |
… | |
… | |
48 | #endif |
56 | #endif |
49 | |
57 | |
50 | #define expect_false(expr) expect ((expr) != 0, 0) |
58 | #define expect_false(expr) expect ((expr) != 0, 0) |
51 | #define expect_true(expr) expect ((expr) != 0, 1) |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
52 | |
60 | |
53 | static HV *json_stash; // JSON::XS:: |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
|
|
62 | static SV *json_true, *json_false; |
54 | |
63 | |
55 | ///////////////////////////////////////////////////////////////////////////// |
64 | ///////////////////////////////////////////////////////////////////////////// |
56 | // utility functions |
65 | // utility functions |
57 | |
66 | |
58 | static UV * |
67 | static UV * |
… | |
… | |
178 | STRLEN clen; |
187 | STRLEN clen; |
179 | UV uch; |
188 | UV uch; |
180 | |
189 | |
181 | if (is_utf8) |
190 | if (is_utf8) |
182 | { |
191 | { |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
192 | uch = decode_utf8 (str, end - str, &clen); |
185 | if (clen == (STRLEN)-1) |
193 | if (clen == (STRLEN)-1) |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
194 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
187 | } |
195 | } |
188 | else |
196 | else |
… | |
… | |
463 | svtype svt; |
471 | svtype svt; |
464 | |
472 | |
465 | SvGETMAGIC (sv); |
473 | SvGETMAGIC (sv); |
466 | svt = SvTYPE (sv); |
474 | svt = SvTYPE (sv); |
467 | |
475 | |
|
|
476 | if (expect_false (SvOBJECT (sv))) |
|
|
477 | { |
|
|
478 | if (SvSTASH (sv) == json_boolean_stash) |
|
|
479 | { |
|
|
480 | if (SvIV (sv) == 0) |
|
|
481 | encode_str (enc, "false", 5, 0); |
|
|
482 | else |
|
|
483 | encode_str (enc, "true", 4, 0); |
|
|
484 | } |
|
|
485 | else |
|
|
486 | { |
|
|
487 | #if 0 |
|
|
488 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
|
|
489 | { |
|
|
490 | // not yet |
|
|
491 | } |
|
|
492 | #endif |
|
|
493 | if (enc->flags & F_CONV_BLESSED) |
|
|
494 | { |
|
|
495 | // we re-bless the reference to get overload and other niceties right |
|
|
496 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
|
|
497 | |
|
|
498 | if (to_json) |
|
|
499 | { |
|
|
500 | dSP; |
|
|
501 | ENTER; |
|
|
502 | SAVETMPS; |
|
|
503 | PUSHMARK (SP); |
|
|
504 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
|
|
505 | |
|
|
506 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
|
|
507 | // check anyways. |
|
|
508 | PUTBACK; |
|
|
509 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
|
|
510 | SPAGAIN; |
|
|
511 | |
|
|
512 | encode_sv (enc, POPs); |
|
|
513 | |
|
|
514 | FREETMPS; |
|
|
515 | LEAVE; |
|
|
516 | } |
|
|
517 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
518 | encode_str (enc, "null", 4, 0); |
|
|
519 | else |
|
|
520 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
|
|
521 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
522 | } |
|
|
523 | else if (enc->flags & F_ALLOW_BLESSED) |
|
|
524 | encode_str (enc, "null", 4, 0); |
|
|
525 | else |
|
|
526 | croak ("encountered object '%s', but neither allow_blessed nor convert_blessed settings are enabled", |
|
|
527 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
|
|
528 | } |
|
|
529 | } |
468 | if (svt == SVt_PVHV) |
530 | else if (svt == SVt_PVHV) |
469 | encode_hv (enc, (HV *)sv); |
531 | encode_hv (enc, (HV *)sv); |
470 | else if (svt == SVt_PVAV) |
532 | else if (svt == SVt_PVAV) |
471 | encode_av (enc, (AV *)sv); |
533 | encode_av (enc, (AV *)sv); |
472 | else if (svt < SVt_PVAV) |
534 | else if (svt < SVt_PVAV) |
473 | { |
535 | { |
… | |
… | |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
559 | encode_str (enc, str, len, SvUTF8 (sv)); |
498 | encode_ch (enc, '"'); |
560 | encode_ch (enc, '"'); |
499 | } |
561 | } |
500 | else if (SvNOKp (sv)) |
562 | else if (SvNOKp (sv)) |
501 | { |
563 | { |
|
|
564 | // trust that perl will do the right thing w.r.t. JSON syntax. |
502 | need (enc, NV_DIG + 32); |
565 | need (enc, NV_DIG + 32); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
566 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | enc->cur += strlen (enc->cur); |
567 | enc->cur += strlen (enc->cur); |
505 | } |
568 | } |
506 | else if (SvIOKp (sv)) |
569 | else if (SvIOKp (sv)) |
507 | { |
570 | { |
508 | // we assume we can always read an IV as a UV |
571 | // we assume we can always read an IV as a UV |
509 | if (SvUV (sv) & ~(UV)0x7fff) |
572 | if (SvUV (sv) & ~(UV)0x7fff) |
510 | { |
573 | { |
|
|
574 | // large integer, use the (rather slow) snprintf way. |
511 | need (enc, sizeof (UV) * 3); |
575 | need (enc, sizeof (UV) * 3); |
512 | enc->cur += |
576 | enc->cur += |
513 | SvIsUV(sv) |
577 | SvIsUV(sv) |
514 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
578 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
515 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
579 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
… | |
… | |
518 | { |
582 | { |
519 | // optimise the "small number case" |
583 | // optimise the "small number case" |
520 | // code will likely be branchless and use only a single multiplication |
584 | // code will likely be branchless and use only a single multiplication |
521 | I32 i = SvIV (sv); |
585 | I32 i = SvIV (sv); |
522 | U32 u; |
586 | U32 u; |
|
|
587 | char digit, nz = 0; |
523 | |
588 | |
524 | need (enc, 6); |
589 | need (enc, 6); |
525 | |
590 | |
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
591 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
527 | u = i < 0 ? -i : i; |
592 | u = i < 0 ? -i : i; |
528 | |
593 | |
529 | // convert to 4.28 fixed-point representation |
594 | // convert to 4.28 fixed-point representation |
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
595 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
531 | |
596 | |
532 | char digit, nz = 0; |
597 | // now output digit by digit, each time masking out the integer part |
533 | |
598 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
599 | // resulting in a net multiplication by 10. |
|
|
600 | // we always write the digit to memory but conditionally increment |
|
|
601 | // the pointer, to ease the usage of conditional move instructions. |
534 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
602 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
535 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
603 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
536 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
604 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
537 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
605 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
538 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; |
606 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
539 | } |
607 | } |
540 | } |
608 | } |
541 | else if (SvROK (sv)) |
609 | else if (SvROK (sv)) |
542 | encode_rv (enc, SvRV (sv)); |
610 | encode_rv (enc, SvRV (sv)); |
543 | else if (!SvOK (sv)) |
611 | else if (!SvOK (sv)) |
… | |
… | |
625 | decode_4hex (dec_t *dec) |
693 | decode_4hex (dec_t *dec) |
626 | { |
694 | { |
627 | signed char d1, d2, d3, d4; |
695 | signed char d1, d2, d3, d4; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
696 | unsigned char *cur = (unsigned char *)dec->cur; |
629 | |
697 | |
630 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
698 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
631 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
699 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
632 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
700 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
633 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
701 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
634 | |
702 | |
635 | dec->cur += 4; |
703 | dec->cur += 4; |
636 | |
704 | |
637 | return ((UV)d1) << 12 |
705 | return ((UV)d1) << 12 |
638 | | ((UV)d2) << 8 |
706 | | ((UV)d2) << 8 |
… | |
… | |
861 | { |
929 | { |
862 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
930 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
863 | if (*start == '-') |
931 | if (*start == '-') |
864 | switch (dec->cur - start) |
932 | switch (dec->cur - start) |
865 | { |
933 | { |
866 | case 2: return newSViv (-( start [1] - '0' )); |
934 | case 2: return newSViv (-( start [1] - '0' * 1)); |
867 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
935 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
868 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
936 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
869 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
937 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
870 | } |
938 | } |
871 | else |
939 | else |
872 | switch (dec->cur - start) |
940 | switch (dec->cur - start) |
873 | { |
941 | { |
874 | case 1: return newSViv ( start [0] - '0' ); |
942 | case 1: return newSViv ( start [0] - '0' * 1); |
875 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
943 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
876 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
944 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
877 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
945 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
878 | } |
946 | } |
879 | |
947 | |
… | |
… | |
886 | if (uv < (UV)IV_MIN) |
954 | if (uv < (UV)IV_MIN) |
887 | return newSViv (-(IV)uv); |
955 | return newSViv (-(IV)uv); |
888 | } |
956 | } |
889 | else |
957 | else |
890 | return newSVuv (uv); |
958 | return newSVuv (uv); |
|
|
959 | |
|
|
960 | // here would likely be the place for bigint support |
891 | } |
961 | } |
892 | } |
962 | } |
893 | |
963 | |
|
|
964 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
894 | return newSVnv (Atof (start)); |
965 | return newSVnv (Atof (start)); |
895 | |
966 | |
896 | fail: |
967 | fail: |
897 | return 0; |
968 | return 0; |
898 | } |
969 | } |
… | |
… | |
952 | if (*dec->cur == '}') |
1023 | if (*dec->cur == '}') |
953 | ++dec->cur; |
1024 | ++dec->cur; |
954 | else |
1025 | else |
955 | for (;;) |
1026 | for (;;) |
956 | { |
1027 | { |
957 | SV *key, *value; |
|
|
958 | |
|
|
959 | decode_ws (dec); EXPECT_CH ('"'); |
1028 | decode_ws (dec); EXPECT_CH ('"'); |
960 | |
1029 | |
961 | key = decode_str (dec); |
1030 | // heuristic: assume that |
962 | if (!key) |
1031 | // a) decode_str + hv_store_ent are abysmally slow |
963 | goto fail; |
1032 | // b) most hash keys are short, simple ascii text |
|
|
1033 | // so try to "fast-match" such strings to avoid |
|
|
1034 | // the overhead of hv_store_ent. |
|
|
1035 | { |
|
|
1036 | SV *value; |
|
|
1037 | char *p = dec->cur; |
|
|
1038 | char *e = p + 24; // only try up to 24 bytes |
964 | |
1039 | |
965 | decode_ws (dec); EXPECT_CH (':'); |
1040 | for (;;) |
966 | |
|
|
967 | value = decode_sv (dec); |
|
|
968 | if (!value) |
|
|
969 | { |
1041 | { |
|
|
1042 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
|
|
1043 | { |
|
|
1044 | // slow path, back up and use decode_str |
|
|
1045 | SV *key = decode_str (dec); |
|
|
1046 | if (!key) |
|
|
1047 | goto fail; |
|
|
1048 | |
|
|
1049 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1050 | |
|
|
1051 | value = decode_sv (dec); |
|
|
1052 | if (!value) |
|
|
1053 | { |
|
|
1054 | SvREFCNT_dec (key); |
|
|
1055 | goto fail; |
|
|
1056 | } |
|
|
1057 | |
|
|
1058 | hv_store_ent (hv, key, value, 0); |
970 | SvREFCNT_dec (key); |
1059 | SvREFCNT_dec (key); |
|
|
1060 | |
|
|
1061 | break; |
|
|
1062 | } |
|
|
1063 | else if (*p == '"') |
|
|
1064 | { |
|
|
1065 | // fast path, got a simple key |
|
|
1066 | char *key = dec->cur; |
|
|
1067 | int len = p - key; |
|
|
1068 | dec->cur = p + 1; |
|
|
1069 | |
|
|
1070 | decode_ws (dec); EXPECT_CH (':'); |
|
|
1071 | |
|
|
1072 | value = decode_sv (dec); |
|
|
1073 | if (!value) |
971 | goto fail; |
1074 | goto fail; |
|
|
1075 | |
|
|
1076 | hv_store (hv, key, len, value, 0); |
|
|
1077 | |
|
|
1078 | break; |
|
|
1079 | } |
|
|
1080 | |
|
|
1081 | ++p; |
972 | } |
1082 | } |
973 | |
1083 | } |
974 | hv_store_ent (hv, key, value, 0); |
|
|
975 | SvREFCNT_dec (key); |
|
|
976 | |
1084 | |
977 | decode_ws (dec); |
1085 | decode_ws (dec); |
978 | |
1086 | |
979 | if (*dec->cur == '}') |
1087 | if (*dec->cur == '}') |
980 | { |
1088 | { |
… | |
… | |
999 | |
1107 | |
1000 | static SV * |
1108 | static SV * |
1001 | decode_sv (dec_t *dec) |
1109 | decode_sv (dec_t *dec) |
1002 | { |
1110 | { |
1003 | decode_ws (dec); |
1111 | decode_ws (dec); |
|
|
1112 | |
|
|
1113 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1114 | // to parse anything. |
1004 | switch (*dec->cur) |
1115 | switch (*dec->cur) |
1005 | { |
1116 | { |
1006 | case '"': ++dec->cur; return decode_str (dec); |
1117 | case '"': ++dec->cur; return decode_str (dec); |
1007 | case '[': ++dec->cur; return decode_av (dec); |
1118 | case '[': ++dec->cur; return decode_av (dec); |
1008 | case '{': ++dec->cur; return decode_hv (dec); |
1119 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
1014 | |
1125 | |
1015 | case 't': |
1126 | case 't': |
1016 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1127 | if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) |
1017 | { |
1128 | { |
1018 | dec->cur += 4; |
1129 | dec->cur += 4; |
1019 | return newSViv (1); |
1130 | return SvREFCNT_inc (json_true); |
1020 | } |
1131 | } |
1021 | else |
1132 | else |
1022 | ERR ("'true' expected"); |
1133 | ERR ("'true' expected"); |
1023 | |
1134 | |
1024 | break; |
1135 | break; |
1025 | |
1136 | |
1026 | case 'f': |
1137 | case 'f': |
1027 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1138 | if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) |
1028 | { |
1139 | { |
1029 | dec->cur += 5; |
1140 | dec->cur += 5; |
1030 | return newSViv (0); |
1141 | return SvREFCNT_inc (json_false); |
1031 | } |
1142 | } |
1032 | else |
1143 | else |
1033 | ERR ("'false' expected"); |
1144 | ERR ("'false' expected"); |
1034 | |
1145 | |
1035 | break; |
1146 | break; |
… | |
… | |
1062 | SV *sv; |
1173 | SV *sv; |
1063 | |
1174 | |
1064 | SvGETMAGIC (string); |
1175 | SvGETMAGIC (string); |
1065 | SvUPGRADE (string, SVt_PV); |
1176 | SvUPGRADE (string, SVt_PV); |
1066 | |
1177 | |
|
|
1178 | if (flags & F_MAXSIZE && SvCUR (string) > DEC_SIZE (flags)) |
|
|
1179 | 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)); |
|
|
1181 | |
1067 | if (flags & F_UTF8) |
1182 | if (flags & F_UTF8) |
1068 | sv_utf8_downgrade (string, 0); |
1183 | sv_utf8_downgrade (string, 0); |
1069 | else |
1184 | else |
1070 | sv_utf8_upgrade (string); |
1185 | sv_utf8_upgrade (string); |
1071 | |
1186 | |
… | |
… | |
1138 | |
1253 | |
1139 | BOOT: |
1254 | BOOT: |
1140 | { |
1255 | { |
1141 | int i; |
1256 | int i; |
1142 | |
1257 | |
1143 | memset (decode_hexdigit, 0xff, 256); |
|
|
1144 | |
|
|
1145 | for (i = 0; i < 256; ++i) |
1258 | for (i = 0; i < 256; ++i) |
1146 | decode_hexdigit [i] = |
1259 | decode_hexdigit [i] = |
1147 | i >= '0' && i <= '9' ? i - '0' |
1260 | i >= '0' && i <= '9' ? i - '0' |
1148 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1261 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1149 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1262 | : i >= 'A' && i <= 'F' ? i - 'A' + 10 |
1150 | : -1; |
1263 | : -1; |
1151 | |
1264 | |
1152 | json_stash = gv_stashpv ("JSON::XS", 1); |
1265 | json_stash = gv_stashpv ("JSON::XS" , 1); |
|
|
1266 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
1267 | |
|
|
1268 | json_true = get_sv ("JSON::XS::true" , 1); SvREADONLY_on (json_true ); |
|
|
1269 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1153 | } |
1270 | } |
1154 | |
1271 | |
1155 | PROTOTYPES: DISABLE |
1272 | PROTOTYPES: DISABLE |
1156 | |
1273 | |
1157 | SV *new (char *dummy) |
1274 | SV *new (char *dummy) |
… | |
… | |
1160 | OUTPUT: |
1277 | OUTPUT: |
1161 | RETVAL |
1278 | RETVAL |
1162 | |
1279 | |
1163 | SV *ascii (SV *self, int enable = 1) |
1280 | SV *ascii (SV *self, int enable = 1) |
1164 | ALIAS: |
1281 | ALIAS: |
1165 | ascii = F_ASCII |
1282 | ascii = F_ASCII |
1166 | latin1 = F_LATIN1 |
1283 | latin1 = F_LATIN1 |
1167 | utf8 = F_UTF8 |
1284 | utf8 = F_UTF8 |
1168 | indent = F_INDENT |
1285 | indent = F_INDENT |
1169 | canonical = F_CANONICAL |
1286 | canonical = F_CANONICAL |
1170 | space_before = F_SPACE_BEFORE |
1287 | space_before = F_SPACE_BEFORE |
1171 | space_after = F_SPACE_AFTER |
1288 | space_after = F_SPACE_AFTER |
1172 | pretty = F_PRETTY |
1289 | pretty = F_PRETTY |
1173 | allow_nonref = F_ALLOW_NONREF |
1290 | allow_nonref = F_ALLOW_NONREF |
1174 | shrink = F_SHRINK |
1291 | shrink = F_SHRINK |
|
|
1292 | allow_blessed = F_ALLOW_BLESSED |
|
|
1293 | convert_blessed = F_CONV_BLESSED |
1175 | CODE: |
1294 | CODE: |
1176 | { |
1295 | { |
1177 | UV *uv = SvJSON (self); |
1296 | UV *uv = SvJSON (self); |
1178 | if (enable) |
1297 | if (enable) |
1179 | *uv |= ix; |
1298 | *uv |= ix; |
… | |
… | |
1201 | RETVAL = newSVsv (self); |
1320 | RETVAL = newSVsv (self); |
1202 | } |
1321 | } |
1203 | OUTPUT: |
1322 | OUTPUT: |
1204 | RETVAL |
1323 | RETVAL |
1205 | |
1324 | |
|
|
1325 | SV *max_size (SV *self, UV max_size = 0) |
|
|
1326 | CODE: |
|
|
1327 | { |
|
|
1328 | UV *uv = SvJSON (self); |
|
|
1329 | UV log2 = 0; |
|
|
1330 | |
|
|
1331 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
|
|
1332 | if (max_size == 1) max_size = 2; |
|
|
1333 | |
|
|
1334 | while ((1UL << log2) < max_size) |
|
|
1335 | ++log2; |
|
|
1336 | |
|
|
1337 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
|
|
1338 | |
|
|
1339 | RETVAL = newSVsv (self); |
|
|
1340 | } |
|
|
1341 | OUTPUT: |
|
|
1342 | RETVAL |
|
|
1343 | |
1206 | void encode (SV *self, SV *scalar) |
1344 | void encode (SV *self, SV *scalar) |
1207 | PPCODE: |
1345 | PPCODE: |
1208 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1346 | XPUSHs (encode_json (scalar, *SvJSON (self))); |
1209 | |
1347 | |
1210 | void decode (SV *self, SV *jsonstr) |
1348 | void decode (SV *self, SV *jsonstr) |