… | |
… | |
25 | #define F_SPACE_BEFORE 0x00000020UL |
25 | #define F_SPACE_BEFORE 0x00000020UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
26 | #define F_SPACE_AFTER 0x00000040UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
27 | #define F_ALLOW_NONREF 0x00000100UL |
28 | #define F_SHRINK 0x00000200UL |
28 | #define F_SHRINK 0x00000200UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
29 | #define F_ALLOW_BLESSED 0x00000400UL |
30 | #define F_CONV_BLESSED 0x00000800UL // NYI |
30 | #define F_CONV_BLESSED 0x00000800UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
31 | #define F_MAXDEPTH 0xf8000000UL |
32 | #define S_MAXDEPTH 27 |
32 | #define S_MAXDEPTH 27 |
33 | #define F_MAXSIZE 0x01f00000UL |
33 | #define F_MAXSIZE 0x01f00000UL |
34 | #define S_MAXSIZE 20 |
34 | #define S_MAXSIZE 20 |
|
|
35 | #define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing |
35 | |
36 | |
36 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
37 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | #define DEC_SIZE(flags) (1UL << ((flags & F_MAXSIZE ) >> S_MAXSIZE )) |
38 | |
39 | |
39 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
40 | #define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER |
… | |
… | |
59 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | #define expect_true(expr) expect ((expr) != 0, 1) |
60 | |
61 | |
61 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
62 | static SV *json_true, *json_false; |
63 | static SV *json_true, *json_false; |
63 | |
64 | |
64 | typedef struct json { |
65 | typedef struct { |
65 | U32 flags; |
66 | U32 flags; |
|
|
67 | SV *cb_object; |
|
|
68 | HV *cb_sk_object; |
66 | } JSON__XS; |
69 | } JSON; |
67 | |
70 | |
68 | ///////////////////////////////////////////////////////////////////////////// |
71 | ///////////////////////////////////////////////////////////////////////////// |
69 | // utility functions |
72 | // utility functions |
70 | |
73 | |
71 | static UV * |
74 | 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) |
75 | shrink (SV *sv) |
82 | { |
76 | { |
83 | sv_utf8_downgrade (sv, 1); |
77 | sv_utf8_downgrade (sv, 1); |
84 | if (SvLEN (sv) > SvCUR (sv) + 1) |
78 | if (SvLEN (sv) > SvCUR (sv) + 1) |
85 | { |
79 | { |
… | |
… | |
120 | typedef struct |
114 | typedef struct |
121 | { |
115 | { |
122 | char *cur; // SvPVX (sv) + current output position |
116 | char *cur; // SvPVX (sv) + current output position |
123 | char *end; // SvEND (sv) |
117 | char *end; // SvEND (sv) |
124 | SV *sv; // result scalar |
118 | SV *sv; // result scalar |
125 | struct json json; |
119 | JSON json; |
126 | U32 indent; // indentation level |
120 | U32 indent; // indentation level |
127 | U32 maxdepth; // max. indentation/recursion level |
121 | U32 maxdepth; // max. indentation/recursion level |
128 | } enc_t; |
122 | } enc_t; |
129 | |
123 | |
130 | inline void |
124 | inline void |
… | |
… | |
312 | encode_ch (enc, '['); encode_nl (enc); |
306 | encode_ch (enc, '['); encode_nl (enc); |
313 | ++enc->indent; |
307 | ++enc->indent; |
314 | |
308 | |
315 | for (i = 0; i <= len; ++i) |
309 | for (i = 0; i <= len; ++i) |
316 | { |
310 | { |
|
|
311 | SV **svp = av_fetch (av, i, 0); |
|
|
312 | |
317 | encode_indent (enc); |
313 | encode_indent (enc); |
318 | encode_sv (enc, *av_fetch (av, i, 0)); |
314 | |
|
|
315 | if (svp) |
|
|
316 | encode_sv (enc, *svp); |
|
|
317 | else |
|
|
318 | encode_str (enc, "null", 4, 0); |
319 | |
319 | |
320 | if (i < len) |
320 | if (i < len) |
321 | encode_comma (enc); |
321 | encode_comma (enc); |
322 | } |
322 | } |
323 | |
323 | |
… | |
… | |
479 | |
479 | |
480 | if (expect_false (SvOBJECT (sv))) |
480 | if (expect_false (SvOBJECT (sv))) |
481 | { |
481 | { |
482 | if (SvSTASH (sv) == json_boolean_stash) |
482 | if (SvSTASH (sv) == json_boolean_stash) |
483 | { |
483 | { |
484 | if (SvIV (sv) == 0) |
484 | if (SvIV (sv)) |
|
|
485 | encode_str (enc, "true", 4, 0); |
|
|
486 | else |
485 | encode_str (enc, "false", 5, 0); |
487 | encode_str (enc, "false", 5, 0); |
486 | else |
|
|
487 | encode_str (enc, "true", 4, 0); |
|
|
488 | } |
488 | } |
489 | else |
489 | else |
490 | { |
490 | { |
491 | #if 0 |
491 | #if 0 |
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
492 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
495 | } |
495 | } |
496 | #endif |
496 | #endif |
497 | if (enc->json.flags & F_CONV_BLESSED) |
497 | if (enc->json.flags & F_CONV_BLESSED) |
498 | { |
498 | { |
499 | // we re-bless the reference to get overload and other niceties right |
499 | // we re-bless the reference to get overload and other niceties right |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
500 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
501 | |
501 | |
502 | if (to_json) |
502 | if (to_json) |
503 | { |
503 | { |
|
|
504 | int count; |
504 | dSP; |
505 | dSP; |
505 | ENTER; |
506 | |
506 | SAVETMPS; |
|
|
507 | PUSHMARK (SP); |
507 | ENTER; SAVETMPS; PUSHMARK (SP); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
508 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | |
509 | |
510 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
510 | // calling with G_SCALAR ensures that we always get a 1 return value |
511 | // check anyways. |
|
|
512 | PUTBACK; |
511 | PUTBACK; |
513 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
512 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
514 | SPAGAIN; |
513 | SPAGAIN; |
515 | |
514 | |
|
|
515 | // catch this surprisingly common error |
|
|
516 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
517 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
518 | |
|
|
519 | sv = POPs; |
|
|
520 | PUTBACK; |
|
|
521 | |
516 | encode_sv (enc, POPs); |
522 | encode_sv (enc, sv); |
517 | |
523 | |
518 | FREETMPS; |
524 | FREETMPS; LEAVE; |
519 | LEAVE; |
|
|
520 | } |
525 | } |
521 | else if (enc->json.flags & F_ALLOW_BLESSED) |
526 | else if (enc->json.flags & F_ALLOW_BLESSED) |
522 | encode_str (enc, "null", 4, 0); |
527 | encode_str (enc, "null", 4, 0); |
523 | else |
528 | else |
524 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
529 | croak ("encountered object '%s', but neither allow_blessed enabled nor TO_JSON method available on it", |
… | |
… | |
535 | encode_hv (enc, (HV *)sv); |
540 | encode_hv (enc, (HV *)sv); |
536 | else if (svt == SVt_PVAV) |
541 | else if (svt == SVt_PVAV) |
537 | encode_av (enc, (AV *)sv); |
542 | encode_av (enc, (AV *)sv); |
538 | else if (svt < SVt_PVAV) |
543 | else if (svt < SVt_PVAV) |
539 | { |
544 | { |
540 | if (SvNIOK (sv) && SvIV (sv) == 0) |
545 | STRLEN len = 0; |
|
|
546 | char *pv = svt ? SvPV (sv, len) : 0; |
|
|
547 | |
|
|
548 | if (len == 1 && *pv == '1') |
|
|
549 | encode_str (enc, "true", 4, 0); |
|
|
550 | else if (len == 1 && *pv == '0') |
541 | encode_str (enc, "false", 5, 0); |
551 | encode_str (enc, "false", 5, 0); |
542 | else if (SvNIOK (sv) && SvIV (sv) == 1) |
|
|
543 | encode_str (enc, "true", 4, 0); |
|
|
544 | else |
552 | else |
545 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
553 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
546 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
554 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
547 | } |
555 | } |
548 | else |
556 | else |
… | |
… | |
618 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
626 | croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", |
619 | SvPV_nolen (sv), SvFLAGS (sv)); |
627 | SvPV_nolen (sv), SvFLAGS (sv)); |
620 | } |
628 | } |
621 | |
629 | |
622 | static SV * |
630 | static SV * |
623 | encode_json (SV *scalar, struct json *json) |
631 | encode_json (SV *scalar, JSON *json) |
624 | { |
632 | { |
625 | enc_t enc; |
633 | enc_t enc; |
626 | |
634 | |
627 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
635 | if (!(json->flags & F_ALLOW_NONREF) && !SvROK (scalar)) |
628 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
636 | croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); |
… | |
… | |
656 | typedef struct |
664 | typedef struct |
657 | { |
665 | { |
658 | char *cur; // current parser pointer |
666 | char *cur; // current parser pointer |
659 | char *end; // end of input string |
667 | char *end; // end of input string |
660 | const char *err; // parse error, if != 0 |
668 | const char *err; // parse error, if != 0 |
661 | struct json json; |
669 | JSON json; |
662 | U32 depth; // recursion depth |
670 | U32 depth; // recursion depth |
663 | U32 maxdepth; // recursion depth limit |
671 | U32 maxdepth; // recursion depth limit |
664 | } dec_t; |
672 | } dec_t; |
665 | |
673 | |
666 | inline void |
674 | inline void |
… | |
… | |
1017 | } |
1025 | } |
1018 | |
1026 | |
1019 | static SV * |
1027 | static SV * |
1020 | decode_hv (dec_t *dec) |
1028 | decode_hv (dec_t *dec) |
1021 | { |
1029 | { |
|
|
1030 | SV *sv; |
1022 | HV *hv = newHV (); |
1031 | HV *hv = newHV (); |
1023 | |
1032 | |
1024 | DEC_INC_DEPTH; |
1033 | DEC_INC_DEPTH; |
1025 | decode_ws (dec); |
1034 | decode_ws (dec); |
1026 | |
1035 | |
… | |
… | |
1100 | |
1109 | |
1101 | ++dec->cur; |
1110 | ++dec->cur; |
1102 | } |
1111 | } |
1103 | |
1112 | |
1104 | DEC_DEC_DEPTH; |
1113 | DEC_DEC_DEPTH; |
1105 | return newRV_noinc ((SV *)hv); |
1114 | sv = newRV_noinc ((SV *)hv); |
|
|
1115 | |
|
|
1116 | // check filter callbacks |
|
|
1117 | if (dec->json.flags & F_HOOK) |
|
|
1118 | { |
|
|
1119 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
|
|
1120 | { |
|
|
1121 | HE *cb, *he; |
|
|
1122 | |
|
|
1123 | hv_iterinit (hv); |
|
|
1124 | he = hv_iternext (hv); |
|
|
1125 | hv_iterinit (hv); |
|
|
1126 | |
|
|
1127 | // the next line creates a mortal sv each time its called. |
|
|
1128 | // might want to optimise this for common cases. |
|
|
1129 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1130 | |
|
|
1131 | if (cb) |
|
|
1132 | { |
|
|
1133 | dSP; |
|
|
1134 | int count; |
|
|
1135 | |
|
|
1136 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1137 | XPUSHs (HeVAL (he)); |
|
|
1138 | |
|
|
1139 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
|
|
1140 | |
|
|
1141 | if (count == 1) |
|
|
1142 | { |
|
|
1143 | sv = newSVsv (POPs); |
|
|
1144 | FREETMPS; LEAVE; |
|
|
1145 | return sv; |
|
|
1146 | } |
|
|
1147 | |
|
|
1148 | FREETMPS; LEAVE; |
|
|
1149 | } |
|
|
1150 | } |
|
|
1151 | |
|
|
1152 | if (dec->json.cb_object) |
|
|
1153 | { |
|
|
1154 | dSP; |
|
|
1155 | int count; |
|
|
1156 | |
|
|
1157 | ENTER; SAVETMPS; PUSHMARK (SP); |
|
|
1158 | XPUSHs (sv_2mortal (sv)); |
|
|
1159 | |
|
|
1160 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
|
|
1161 | |
|
|
1162 | if (count == 1) |
|
|
1163 | { |
|
|
1164 | sv = newSVsv (POPs); |
|
|
1165 | FREETMPS; LEAVE; |
|
|
1166 | return sv; |
|
|
1167 | } |
|
|
1168 | |
|
|
1169 | SvREFCNT_inc (sv); |
|
|
1170 | FREETMPS; LEAVE; |
|
|
1171 | } |
|
|
1172 | } |
|
|
1173 | |
|
|
1174 | return sv; |
1106 | |
1175 | |
1107 | fail: |
1176 | fail: |
1108 | SvREFCNT_dec (hv); |
1177 | SvREFCNT_dec (hv); |
1109 | DEC_DEC_DEPTH; |
1178 | DEC_DEC_DEPTH; |
1110 | return 0; |
1179 | return 0; |
… | |
… | |
1169 | fail: |
1238 | fail: |
1170 | return 0; |
1239 | return 0; |
1171 | } |
1240 | } |
1172 | |
1241 | |
1173 | static SV * |
1242 | static SV * |
1174 | decode_json (SV *string, struct json *json, UV *offset_return) |
1243 | decode_json (SV *string, JSON *json, UV *offset_return) |
1175 | { |
1244 | { |
1176 | dec_t dec; |
1245 | dec_t dec; |
1177 | UV offset; |
1246 | UV offset; |
1178 | SV *sv; |
1247 | SV *sv; |
1179 | |
1248 | |
… | |
… | |
1195 | dec.cur = SvPVX (string); |
1264 | dec.cur = SvPVX (string); |
1196 | dec.end = SvEND (string); |
1265 | dec.end = SvEND (string); |
1197 | dec.err = 0; |
1266 | dec.err = 0; |
1198 | dec.depth = 0; |
1267 | dec.depth = 0; |
1199 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
1268 | dec.maxdepth = DEC_DEPTH (dec.json.flags); |
|
|
1269 | |
|
|
1270 | if (dec.json.cb_object || dec.json.cb_sk_object) |
|
|
1271 | dec.json.flags |= F_HOOK; |
1200 | |
1272 | |
1201 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1273 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1202 | sv = decode_sv (&dec); |
1274 | sv = decode_sv (&dec); |
1203 | |
1275 | |
1204 | if (!(offset_return || !sv)) |
1276 | if (!(offset_return || !sv)) |
… | |
… | |
1274 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1346 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1275 | } |
1347 | } |
1276 | |
1348 | |
1277 | PROTOTYPES: DISABLE |
1349 | PROTOTYPES: DISABLE |
1278 | |
1350 | |
1279 | SV *new (char *dummy) |
1351 | void new (char *klass) |
1280 | CODE: |
1352 | PPCODE: |
1281 | RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); |
1353 | { |
1282 | OUTPUT: |
1354 | SV *pv = NEWSV (0, sizeof (JSON)); |
1283 | RETVAL |
1355 | SvPOK_only (pv); |
|
|
1356 | Zero (SvPVX (pv), 1, JSON); |
|
|
1357 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
|
|
1358 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
|
|
1359 | } |
1284 | |
1360 | |
1285 | SV *ascii (SV *self, int enable = 1) |
1361 | void ascii (JSON *self, int enable = 1) |
1286 | ALIAS: |
1362 | ALIAS: |
1287 | ascii = F_ASCII |
1363 | ascii = F_ASCII |
1288 | latin1 = F_LATIN1 |
1364 | latin1 = F_LATIN1 |
1289 | utf8 = F_UTF8 |
1365 | utf8 = F_UTF8 |
1290 | indent = F_INDENT |
1366 | indent = F_INDENT |
… | |
… | |
1294 | pretty = F_PRETTY |
1370 | pretty = F_PRETTY |
1295 | allow_nonref = F_ALLOW_NONREF |
1371 | allow_nonref = F_ALLOW_NONREF |
1296 | shrink = F_SHRINK |
1372 | shrink = F_SHRINK |
1297 | allow_blessed = F_ALLOW_BLESSED |
1373 | allow_blessed = F_ALLOW_BLESSED |
1298 | convert_blessed = F_CONV_BLESSED |
1374 | convert_blessed = F_CONV_BLESSED |
1299 | CODE: |
1375 | PPCODE: |
1300 | { |
1376 | { |
1301 | UV *uv = SvJSON (self); |
|
|
1302 | if (enable) |
1377 | if (enable) |
1303 | *uv |= ix; |
1378 | self->flags |= ix; |
1304 | else |
1379 | else |
1305 | *uv &= ~ix; |
1380 | self->flags &= ~ix; |
1306 | |
1381 | |
1307 | RETVAL = newSVsv (self); |
1382 | XPUSHs (ST (0)); |
1308 | } |
1383 | } |
1309 | OUTPUT: |
|
|
1310 | RETVAL |
|
|
1311 | |
1384 | |
1312 | SV *max_depth (SV *self, UV max_depth = 0x80000000UL) |
1385 | void max_depth (JSON *self, UV max_depth = 0x80000000UL) |
1313 | CODE: |
1386 | PPCODE: |
1314 | { |
1387 | { |
1315 | UV *uv = SvJSON (self); |
|
|
1316 | UV log2 = 0; |
1388 | UV log2 = 0; |
1317 | |
1389 | |
1318 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1390 | if (max_depth > 0x80000000UL) max_depth = 0x80000000UL; |
1319 | |
1391 | |
1320 | while ((1UL << log2) < max_depth) |
1392 | while ((1UL << log2) < max_depth) |
1321 | ++log2; |
1393 | ++log2; |
1322 | |
1394 | |
1323 | *uv = *uv & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1395 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1324 | |
1396 | |
1325 | RETVAL = newSVsv (self); |
1397 | XPUSHs (ST (0)); |
1326 | } |
1398 | } |
1327 | OUTPUT: |
|
|
1328 | RETVAL |
|
|
1329 | |
1399 | |
1330 | SV *max_size (SV *self, UV max_size = 0) |
1400 | void max_size (JSON *self, UV max_size = 0) |
1331 | CODE: |
1401 | PPCODE: |
1332 | { |
1402 | { |
1333 | UV *uv = SvJSON (self); |
|
|
1334 | UV log2 = 0; |
1403 | UV log2 = 0; |
1335 | |
1404 | |
1336 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1405 | if (max_size > 0x80000000UL) max_size = 0x80000000UL; |
1337 | if (max_size == 1) max_size = 2; |
1406 | if (max_size == 1) max_size = 2; |
1338 | |
1407 | |
1339 | while ((1UL << log2) < max_size) |
1408 | while ((1UL << log2) < max_size) |
1340 | ++log2; |
1409 | ++log2; |
1341 | |
1410 | |
1342 | *uv = *uv & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1411 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1343 | |
1412 | |
1344 | RETVAL = newSVsv (self); |
1413 | XPUSHs (ST (0)); |
1345 | } |
1414 | } |
1346 | OUTPUT: |
|
|
1347 | RETVAL |
|
|
1348 | |
1415 | |
1349 | void encode (SV *self, SV *scalar) |
1416 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1350 | PPCODE: |
1417 | PPCODE: |
1351 | { |
1418 | { |
1352 | struct json json = { *SvJSON (self) }; |
1419 | SvREFCNT_dec (self->cb_object); |
1353 | XPUSHs (encode_json (scalar, &json)); |
1420 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
1354 | } |
|
|
1355 | |
1421 | |
1356 | void decode (SV *self, SV *jsonstr) |
1422 | XPUSHs (ST (0)); |
|
|
1423 | } |
|
|
1424 | |
|
|
1425 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
1357 | PPCODE: |
1426 | PPCODE: |
1358 | { |
1427 | { |
1359 | struct json json = { *SvJSON (self) }; |
1428 | if (!self->cb_sk_object) |
1360 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1429 | self->cb_sk_object = newHV (); |
1361 | } |
|
|
1362 | |
1430 | |
1363 | void decode_prefix (SV *self, SV *jsonstr) |
1431 | if (SvOK (cb)) |
|
|
1432 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
|
|
1433 | else |
|
|
1434 | { |
|
|
1435 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1436 | |
|
|
1437 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1438 | { |
|
|
1439 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1440 | self->cb_sk_object = 0; |
|
|
1441 | } |
|
|
1442 | } |
|
|
1443 | |
|
|
1444 | XPUSHs (ST (0)); |
|
|
1445 | } |
|
|
1446 | |
|
|
1447 | void encode (JSON *self, SV *scalar) |
1364 | PPCODE: |
1448 | PPCODE: |
|
|
1449 | XPUSHs (encode_json (scalar, self)); |
|
|
1450 | |
|
|
1451 | void decode (JSON *self, SV *jsonstr) |
|
|
1452 | PPCODE: |
|
|
1453 | XPUSHs (decode_json (jsonstr, self, 0)); |
|
|
1454 | |
|
|
1455 | void decode_prefix (JSON *self, SV *jsonstr) |
|
|
1456 | PPCODE: |
1365 | { |
1457 | { |
1366 | UV offset; |
1458 | UV offset; |
1367 | struct json json = { *SvJSON (self) }; |
|
|
1368 | EXTEND (SP, 2); |
1459 | EXTEND (SP, 2); |
1369 | PUSHs (decode_json (jsonstr, &json, &offset)); |
1460 | PUSHs (decode_json (jsonstr, self, &offset)); |
1370 | PUSHs (sv_2mortal (newSVuv (offset))); |
1461 | PUSHs (sv_2mortal (newSVuv (offset))); |
1371 | } |
1462 | } |
|
|
1463 | |
|
|
1464 | void DESTROY (JSON *self) |
|
|
1465 | CODE: |
|
|
1466 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1467 | SvREFCNT_dec (self->cb_object); |
1372 | |
1468 | |
1373 | PROTOTYPES: ENABLE |
1469 | PROTOTYPES: ENABLE |
1374 | |
1470 | |
1375 | void to_json (SV *scalar) |
1471 | void to_json (SV *scalar) |
1376 | PPCODE: |
1472 | PPCODE: |
1377 | { |
1473 | { |
1378 | struct json json = { F_DEFAULT | F_UTF8 }; |
1474 | JSON json = { F_DEFAULT | F_UTF8 }; |
1379 | XPUSHs (encode_json (scalar, &json)); |
1475 | XPUSHs (encode_json (scalar, &json)); |
1380 | } |
1476 | } |
1381 | |
1477 | |
1382 | void from_json (SV *jsonstr) |
1478 | void from_json (SV *jsonstr) |
1383 | PPCODE: |
1479 | PPCODE: |
1384 | { |
1480 | { |
1385 | struct json json = { F_DEFAULT | F_UTF8 }; |
1481 | JSON json = { F_DEFAULT | F_UTF8 }; |
1386 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1482 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1387 | } |
1483 | } |
1388 | |
1484 | |