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 | |
64 | typedef struct json { |
66 | typedef struct { |
65 | U32 flags; |
67 | U32 flags; |
|
|
68 | SV *cb_object; |
|
|
69 | HV *cb_sk_object; |
66 | } JSON__XS; |
70 | } JSON; |
67 | |
71 | |
68 | ///////////////////////////////////////////////////////////////////////////// |
72 | ///////////////////////////////////////////////////////////////////////////// |
69 | // utility functions |
73 | // utility functions |
70 | |
74 | |
71 | static UV * |
75 | inline void |
72 | SvJSON (SV *sv) |
|
|
73 | { |
|
|
74 | if (!(SvROK (sv) && SvOBJECT (SvRV (sv)) && SvSTASH (SvRV (sv)) == json_stash)) |
|
|
75 | croak ("object is not of type JSON::XS"); |
|
|
76 | |
|
|
77 | return &SvUVX (SvRV (sv)); |
|
|
78 | } |
|
|
79 | |
|
|
80 | static void |
|
|
81 | shrink (SV *sv) |
76 | shrink (SV *sv) |
82 | { |
77 | { |
83 | sv_utf8_downgrade (sv, 1); |
78 | sv_utf8_downgrade (sv, 1); |
84 | if (SvLEN (sv) > SvCUR (sv) + 1) |
79 | if (SvLEN (sv) > SvCUR (sv) + 1) |
85 | { |
80 | { |
… | |
… | |
120 | typedef struct |
115 | typedef struct |
121 | { |
116 | { |
122 | char *cur; // SvPVX (sv) + current output position |
117 | char *cur; // SvPVX (sv) + current output position |
123 | char *end; // SvEND (sv) |
118 | char *end; // SvEND (sv) |
124 | SV *sv; // result scalar |
119 | SV *sv; // result scalar |
125 | struct json json; |
120 | JSON json; |
126 | U32 indent; // indentation level |
121 | U32 indent; // indentation level |
127 | U32 maxdepth; // max. indentation/recursion level |
122 | U32 maxdepth; // max. indentation/recursion level |
128 | } enc_t; |
123 | } enc_t; |
129 | |
124 | |
130 | inline void |
125 | inline void |
… | |
… | |
312 | encode_ch (enc, '['); encode_nl (enc); |
307 | encode_ch (enc, '['); encode_nl (enc); |
313 | ++enc->indent; |
308 | ++enc->indent; |
314 | |
309 | |
315 | for (i = 0; i <= len; ++i) |
310 | for (i = 0; i <= len; ++i) |
316 | { |
311 | { |
|
|
312 | SV **svp = av_fetch (av, i, 0); |
|
|
313 | |
317 | encode_indent (enc); |
314 | encode_indent (enc); |
318 | 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); |
319 | |
320 | |
320 | if (i < len) |
321 | if (i < len) |
321 | encode_comma (enc); |
322 | encode_comma (enc); |
322 | } |
323 | } |
323 | |
324 | |
… | |
… | |
479 | |
480 | |
480 | if (expect_false (SvOBJECT (sv))) |
481 | if (expect_false (SvOBJECT (sv))) |
481 | { |
482 | { |
482 | if (SvSTASH (sv) == json_boolean_stash) |
483 | if (SvSTASH (sv) == json_boolean_stash) |
483 | { |
484 | { |
484 | if (SvIV (sv) == 0) |
485 | if (SvIV (sv)) |
|
|
486 | encode_str (enc, "true", 4, 0); |
|
|
487 | else |
485 | encode_str (enc, "false", 5, 0); |
488 | encode_str (enc, "false", 5, 0); |
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
489 | } |
489 | else |
490 | else |
490 | { |
491 | { |
491 | #if 0 |
492 | #if 0 |
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
493 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
495 | } |
496 | } |
496 | #endif |
497 | #endif |
497 | if (enc->json.flags & F_CONV_BLESSED) |
498 | if (enc->json.flags & F_CONV_BLESSED) |
498 | { |
499 | { |
499 | // 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 |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
501 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
501 | |
502 | |
502 | if (to_json) |
503 | if (to_json) |
503 | { |
504 | { |
|
|
505 | int count; |
504 | dSP; |
506 | dSP; |
505 | ENTER; |
507 | |
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
508 | ENTER; SAVETMPS; PUSHMARK (SP); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | |
510 | |
510 | // 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 |
511 | // check anyways. |
|
|
512 | PUTBACK; |
512 | PUTBACK; |
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
513 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
514 | SPAGAIN; |
514 | SPAGAIN; |
515 | |
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 | |
516 | encode_sv (enc, POPs); |
523 | encode_sv (enc, sv); |
517 | |
524 | |
518 | FREETMPS; |
525 | FREETMPS; LEAVE; |
519 | LEAVE; |
|
|
520 | } |
526 | } |
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
522 | encode_str (enc, "null", 4, 0); |
528 | encode_str (enc, "null", 4, 0); |
523 | else |
529 | else |
524 | 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", |
… | |
… | |
535 | encode_hv (enc, (HV *)sv); |
541 | encode_hv (enc, (HV *)sv); |
536 | else if (svt == SVt_PVAV) |
542 | else if (svt == SVt_PVAV) |
537 | encode_av (enc, (AV *)sv); |
543 | encode_av (enc, (AV *)sv); |
538 | else if (svt < SVt_PVAV) |
544 | else if (svt < SVt_PVAV) |
539 | { |
545 | { |
540 | 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') |
541 | encode_str (enc, "false", 5, 0); |
552 | encode_str (enc, "false", 5, 0); |
542 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
543 | encode_str (enc, "true", 4, 0); |
|
|
544 | else |
553 | else |
545 | 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", |
546 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
555 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
547 | } |
556 | } |
548 | else |
557 | else |
… | |
… | |
618 | 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", |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
628 | SvPV_nolen (sv), SvFLAGS (sv)); |
620 | } |
629 | } |
621 | |
630 | |
622 | static SV * |
631 | static SV * |
623 | encode_json (SV *scalar, struct json *json) |
632 | encode_json (SV *scalar, JSON *json) |
624 | { |
633 | { |
625 | enc_t enc; |
634 | enc_t enc; |
626 | |
635 | |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
636 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
628 | 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)"); |
… | |
… | |
656 | typedef struct |
665 | typedef struct |
657 | { |
666 | { |
658 | char *cur; // current parser pointer |
667 | char *cur; // current parser pointer |
659 | char *end; // end of input string |
668 | char *end; // end of input string |
660 | const char *err; // parse error, if != 0 |
669 | const char *err; // parse error, if != 0 |
661 | struct json json; |
670 | JSON json; |
662 | U32 depth; // recursion depth |
671 | U32 depth; // recursion depth |
663 | U32 maxdepth; // recursion depth limit |
672 | U32 maxdepth; // recursion depth limit |
664 | } dec_t; |
673 | } dec_t; |
665 | |
674 | |
666 | inline void |
675 | inline void |
… | |
… | |
929 | is_nv = 1; |
938 | is_nv = 1; |
930 | } |
939 | } |
931 | |
940 | |
932 | if (!is_nv) |
941 | if (!is_nv) |
933 | { |
942 | { |
|
|
943 | int len = dec->cur - start; |
|
|
944 | |
934 | // 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 |
935 | if (*start == '-') |
946 | if (*start == '-') |
936 | switch (dec->cur - start) |
947 | switch (len) |
937 | { |
948 | { |
938 | case 2: return newSViv (-( start [1] - '0' * 1)); |
949 | case 2: return newSViv (-( start [1] - '0' * 1)); |
939 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
950 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
940 | 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)); |
941 | 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)); |
942 | } |
953 | } |
943 | else |
954 | else |
944 | switch (dec->cur - start) |
955 | switch (len) |
945 | { |
956 | { |
946 | case 1: return newSViv ( start [0] - '0' * 1); |
957 | case 1: return newSViv ( start [0] - '0' * 1); |
947 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
958 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
948 | 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); |
949 | 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); |
950 | } |
961 | } |
951 | |
962 | |
952 | { |
963 | { |
953 | UV uv; |
964 | UV uv; |
954 | int numtype = grok_number (start, dec->cur - start, &uv); |
965 | int numtype = grok_number (start, len, &uv); |
955 | if (numtype & IS_NUMBER_IN_UV) |
966 | if (numtype & IS_NUMBER_IN_UV) |
956 | if (numtype & IS_NUMBER_NEG) |
967 | if (numtype & IS_NUMBER_NEG) |
957 | { |
968 | { |
958 | if (uv < (UV)IV_MIN) |
969 | if (uv < (UV)IV_MIN) |
959 | return newSViv (-(IV)uv); |
970 | return newSViv (-(IV)uv); |
960 | } |
971 | } |
961 | else |
972 | else |
962 | return newSVuv (uv); |
973 | return newSVuv (uv); |
963 | |
|
|
964 | // here would likely be the place for bigint support |
|
|
965 | } |
974 | } |
966 | } |
|
|
967 | |
975 | |
968 | // 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 |
969 | return newSVnv (Atof (start)); |
992 | return newSVnv (Atof (start)); |
970 | |
993 | |
971 | fail: |
994 | fail: |
972 | return 0; |
995 | return 0; |
973 | } |
996 | } |
… | |
… | |
1017 | } |
1040 | } |
1018 | |
1041 | |
1019 | static SV * |
1042 | static SV * |
1020 | decode_hv (dec_t *dec) |
1043 | decode_hv (dec_t *dec) |
1021 | { |
1044 | { |
|
|
1045 | SV *sv; |
1022 | HV *hv = newHV (); |
1046 | HV *hv = newHV (); |
1023 | |
1047 | |
1024 | DEC_INC_DEPTH; |
1048 | DEC_INC_DEPTH; |
1025 | decode_ws (dec); |
1049 | decode_ws (dec); |
1026 | |
1050 | |
… | |
… | |
1100 | |
1124 | |
1101 | ++dec->cur; |
1125 | ++dec->cur; |
1102 | } |
1126 | } |
1103 | |
1127 | |
1104 | DEC_DEC_DEPTH; |
1128 | DEC_DEC_DEPTH; |
1105 | 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; |
1106 | |
1190 | |
1107 | fail: |
1191 | fail: |
1108 | SvREFCNT_dec (hv); |
1192 | SvREFCNT_dec (hv); |
1109 | DEC_DEC_DEPTH; |
1193 | DEC_DEC_DEPTH; |
1110 | return 0; |
1194 | return 0; |
… | |
… | |
1169 | fail: |
1253 | fail: |
1170 | return 0; |
1254 | return 0; |
1171 | } |
1255 | } |
1172 | |
1256 | |
1173 | static SV * |
1257 | static SV * |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
1258 | decode_json (SV *string, JSON *json, UV *offset_return) |
1175 | { |
1259 | { |
1176 | dec_t dec; |
1260 | dec_t dec; |
1177 | UV offset; |
1261 | UV offset; |
1178 | SV *sv; |
1262 | SV *sv; |
1179 | |
1263 | |
… | |
… | |
1195 | dec.cur = SvPVX (string); |
1279 | dec.cur = SvPVX (string); |
1196 | dec.end = SvEND (string); |
1280 | dec.end = SvEND (string); |
1197 | dec.err = 0; |
1281 | dec.err = 0; |
1198 | dec.depth = 0; |
1282 | dec.depth = 0; |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.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; |
1200 | |
1287 | |
1201 | *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 |
1202 | sv = decode_sv (&dec); |
1289 | sv = decode_sv (&dec); |
1203 | |
1290 | |
1204 | if (!(offset_return || !sv)) |
1291 | if (!(offset_return || !sv)) |
… | |
… | |
1274 | 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); |
1275 | } |
1362 | } |
1276 | |
1363 | |
1277 | PROTOTYPES: DISABLE |
1364 | PROTOTYPES: DISABLE |
1278 | |
1365 | |
1279 | SV *new (char *dummy) |
1366 | void new (char *klass) |
1280 | CODE: |
1367 | PPCODE: |
1281 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1368 | { |
1282 | OUTPUT: |
1369 | SV *pv = NEWSV (0, sizeof (JSON)); |
1283 | 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 | } |
1284 | |
1375 | |
1285 | SV *ascii (SV *self, int enable = 1) |
1376 | void ascii (JSON *self, int enable = 1) |
1286 | ALIAS: |
1377 | ALIAS: |
1287 | ascii = F_ASCII |
1378 | ascii = F_ASCII |
1288 | latin1 = F_LATIN1 |
1379 | latin1 = F_LATIN1 |
1289 | utf8 = F_UTF8 |
1380 | utf8 = F_UTF8 |
1290 | indent = F_INDENT |
1381 | indent = F_INDENT |
… | |
… | |
1294 | pretty = F_PRETTY |
1385 | pretty = F_PRETTY |
1295 | allow_nonref = F_ALLOW_NONREF |
1386 | allow_nonref = F_ALLOW_NONREF |
1296 | shrink = F_SHRINK |
1387 | shrink = F_SHRINK |
1297 | allow_blessed = F_ALLOW_BLESSED |
1388 | allow_blessed = F_ALLOW_BLESSED |
1298 | convert_blessed = F_CONV_BLESSED |
1389 | convert_blessed = F_CONV_BLESSED |
1299 | CODE: |
1390 | PPCODE: |
1300 | { |
1391 | { |
1301 | UV *uv = SvJSON (self); |
|
|
1302 | if (enable) |
1392 | if (enable) |
1303 | *uv |= ix; |
1393 | self->flags |= ix; |
1304 | else |
1394 | else |
1305 | *uv &= ~ix; |
1395 | self->flags &= ~ix; |
1306 | |
1396 | |
1307 | RETVAL = newSVsv (self); |
1397 | XPUSHs (ST (0)); |
1308 | } |
1398 | } |
1309 | OUTPUT: |
|
|
1310 | RETVAL |
|
|
1311 | |
1399 | |
1312 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1400 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1313 | CODE: |
1401 | PPCODE: |
1314 | { |
1402 | { |
1315 | UV *uv = SvJSON (self); |
|
|
1316 | UV log2 = 0; |
1403 | UV log2 = 0; |
1317 | |
1404 | |
1318 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1405 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1319 | |
1406 | |
1320 | while ((1UL << log2) < max_depth) |
1407 | while ((1UL << log2) < max_depth) |
1321 | ++log2; |
1408 | ++log2; |
1322 | |
1409 | |
1323 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1410 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1324 | |
1411 | |
1325 | RETVAL = newSVsv (self); |
1412 | XPUSHs (ST (0)); |
1326 | } |
1413 | } |
1327 | OUTPUT: |
|
|
1328 | RETVAL |
|
|
1329 | |
1414 | |
1330 | SV *max_size (SV *self, UV max_size = 0) |
1415 | void max_size (JSON *self, UV max_size = 0) |
1331 | CODE: |
1416 | PPCODE: |
1332 | { |
1417 | { |
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
1418 | UV log2 = 0; |
1335 | |
1419 | |
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1420 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1337 | if (max_size == 1) max_size = 2; |
1421 | if (max_size == 1) max_size = 2; |
1338 | |
1422 | |
1339 | while ((1UL << log2) < max_size) |
1423 | while ((1UL << log2) < max_size) |
1340 | ++log2; |
1424 | ++log2; |
1341 | |
1425 | |
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1426 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1343 | |
1427 | |
1344 | RETVAL = newSVsv (self); |
1428 | XPUSHs (ST (0)); |
1345 | } |
1429 | } |
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1430 | |
1349 | void encode (SV *self, SV *scalar) |
1431 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1350 | PPCODE: |
1432 | PPCODE: |
1351 | { |
1433 | { |
1352 | struct json json = { *SvJSON (self) }; |
1434 | SvREFCNT_dec (self->cb_object); |
1353 | XPUSHs (encode_json (scalar, &json)); |
1435 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1354 | } |
|
|
1355 | |
1436 | |
1356 | 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) |
1357 | PPCODE: |
1441 | PPCODE: |
1358 | { |
1442 | { |
1359 | struct json json = { *SvJSON (self) }; |
1443 | if (!self->cb_sk_object) |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1444 | self->cb_sk_object = newHV (); |
1361 | } |
|
|
1362 | |
1445 | |
1363 | void decode_prefix (SV *self, SV *jsonstr) |
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) |
1364 | PPCODE: |
1463 | PPCODE: |
|
|
1464 | XPUSHs (encode_json (scalar, self)); |
|
|
1465 | |
|
|
1466 | void decode (JSON *self, SV *jsonstr) |
|
|
1467 | PPCODE: |
|
|
1468 | XPUSHs (decode_json (jsonstr, self, 0)); |
|
|
1469 | |
|
|
1470 | void decode_prefix (JSON *self, SV *jsonstr) |
|
|
1471 | PPCODE: |
1365 | { |
1472 | { |
1366 | UV offset; |
1473 | UV offset; |
1367 | struct json json = { *SvJSON (self) }; |
|
|
1368 | EXTEND (SP, 2); |
1474 | EXTEND (SP, 2); |
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
1475 | PUSHs (decode_json (jsonstr, self, &offset)); |
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
1476 | PUSHs (sv_2mortal (newSVuv (offset))); |
1371 | } |
1477 | } |
|
|
1478 | |
|
|
1479 | void DESTROY (JSON *self) |
|
|
1480 | CODE: |
|
|
1481 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1482 | SvREFCNT_dec (self->cb_object); |
1372 | |
1483 | |
1373 | PROTOTYPES: ENABLE |
1484 | PROTOTYPES: ENABLE |
1374 | |
1485 | |
1375 | void to_json (SV *scalar) |
1486 | void to_json (SV *scalar) |
1376 | PPCODE: |
1487 | PPCODE: |
1377 | { |
1488 | { |
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1489 | JSON json = { F_DEFAULT | F_UTF8 }; |
1379 | XPUSHs (encode_json (scalar, &json)); |
1490 | XPUSHs (encode_json (scalar, &json)); |
1380 | } |
1491 | } |
1381 | |
1492 | |
1382 | void from_json (SV *jsonstr) |
1493 | void from_json (SV *jsonstr) |
1383 | PPCODE: |
1494 | PPCODE: |
1384 | { |
1495 | { |
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1496 | JSON json = { F_DEFAULT | F_UTF8 }; |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1497 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1387 | } |
1498 | } |
1388 | |
1499 | |