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 | |
… | |
… | |
57 | #endif |
58 | #endif |
58 | |
59 | |
59 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_false(expr) expect ((expr) != 0, 0) |
60 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | #define expect_true(expr) expect ((expr) != 0, 1) |
61 | |
62 | |
|
|
63 | #ifdef USE_ITHREADS |
|
|
64 | # define JSON_SLOW 1 |
|
|
65 | #else |
|
|
66 | # define JSON_SLOW 1 |
|
|
67 | #endif |
|
|
68 | |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
69 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static SV *json_true, *json_false; |
70 | static SV *json_true, *json_false; |
64 | |
71 | |
65 | typedef struct { |
72 | typedef struct { |
66 | U32 flags; |
73 | U32 flags; |
67 | SV *cb_object, *cb_sk_object; |
74 | SV *cb_object; |
|
|
75 | HV *cb_sk_object; |
68 | } JSON; |
76 | } JSON; |
69 | |
77 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
78 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
79 | // utility functions |
72 | |
80 | |
… | |
… | |
305 | encode_ch (enc, '['); encode_nl (enc); |
313 | encode_ch (enc, '['); encode_nl (enc); |
306 | ++enc->indent; |
314 | ++enc->indent; |
307 | |
315 | |
308 | for (i = 0; i <= len; ++i) |
316 | for (i = 0; i <= len; ++i) |
309 | { |
317 | { |
|
|
318 | SV **svp = av_fetch (av, i, 0); |
|
|
319 | |
310 | encode_indent (enc); |
320 | encode_indent (enc); |
311 | encode_sv (enc, *av_fetch (av, i, 0)); |
321 | |
|
|
322 | if (svp) |
|
|
323 | encode_sv (enc, *svp); |
|
|
324 | else |
|
|
325 | encode_str (enc, "null", 4, 0); |
312 | |
326 | |
313 | if (i < len) |
327 | if (i < len) |
314 | encode_comma (enc); |
328 | encode_comma (enc); |
315 | } |
329 | } |
316 | |
330 | |
… | |
… | |
470 | SvGETMAGIC (sv); |
484 | SvGETMAGIC (sv); |
471 | svt = SvTYPE (sv); |
485 | svt = SvTYPE (sv); |
472 | |
486 | |
473 | if (expect_false (SvOBJECT (sv))) |
487 | if (expect_false (SvOBJECT (sv))) |
474 | { |
488 | { |
|
|
489 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
490 | ? json_boolean_stash |
|
|
491 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
492 | |
475 | if (SvSTASH (sv) == json_boolean_stash) |
493 | if (SvSTASH (sv) == stash) |
476 | { |
494 | { |
477 | if (SvIV (sv) == 0) |
495 | if (SvIV (sv)) |
|
|
496 | encode_str (enc, "true", 4, 0); |
|
|
497 | else |
478 | encode_str (enc, "false", 5, 0); |
498 | encode_str (enc, "false", 5, 0); |
479 | else |
|
|
480 | encode_str (enc, "true", 4, 0); |
|
|
481 | } |
499 | } |
482 | else |
500 | else |
483 | { |
501 | { |
484 | #if 0 |
502 | #if 0 |
485 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
503 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
488 | } |
506 | } |
489 | #endif |
507 | #endif |
490 | if (enc->json.flags & F_CONV_BLESSED) |
508 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
509 | { |
492 | // we re-bless the reference to get overload and other niceties right |
510 | // we re-bless the reference to get overload and other niceties right |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
511 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
494 | |
512 | |
495 | if (to_json) |
513 | if (to_json) |
496 | { |
514 | { |
|
|
515 | int count; |
|
|
516 | dSP; |
|
|
517 | |
497 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
518 | ENTER; SAVETMPS; PUSHMARK (SP); |
498 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
519 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
499 | |
520 | |
500 | // calling with G_SCALAR ensures that we always get a 1 reutrn value |
521 | // calling with G_SCALAR ensures that we always get a 1 return value |
501 | // check anyways. |
|
|
502 | PUTBACK; |
522 | PUTBACK; |
503 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
523 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
504 | SPAGAIN; |
524 | SPAGAIN; |
505 | |
525 | |
|
|
526 | // catch this surprisingly common error |
|
|
527 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
|
|
528 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
|
|
529 | |
|
|
530 | sv = POPs; |
|
|
531 | PUTBACK; |
|
|
532 | |
506 | encode_sv (enc, POPs); |
533 | encode_sv (enc, sv); |
507 | |
534 | |
508 | FREETMPS; LEAVE; |
535 | FREETMPS; LEAVE; |
509 | } |
536 | } |
510 | else if (enc->json.flags & F_ALLOW_BLESSED) |
537 | else if (enc->json.flags & F_ALLOW_BLESSED) |
511 | encode_str (enc, "null", 4, 0); |
538 | encode_str (enc, "null", 4, 0); |
… | |
… | |
527 | else if (svt < SVt_PVAV) |
554 | else if (svt < SVt_PVAV) |
528 | { |
555 | { |
529 | STRLEN len = 0; |
556 | STRLEN len = 0; |
530 | char *pv = svt ? SvPV (sv, len) : 0; |
557 | char *pv = svt ? SvPV (sv, len) : 0; |
531 | |
558 | |
532 | if (len == 1 && *pv == '0') |
559 | if (len == 1 && *pv == '1') |
|
|
560 | encode_str (enc, "true", 4, 0); |
|
|
561 | else if (len == 1 && *pv == '0') |
533 | encode_str (enc, "false", 5, 0); |
562 | encode_str (enc, "false", 5, 0); |
534 | else if (len == 1 && *pv == '1') |
|
|
535 | encode_str (enc, "true", 4, 0); |
|
|
536 | else |
563 | else |
537 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
564 | croak ("cannot encode reference to scalar '%s' unless the scalar is 0 or 1", |
538 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
565 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
539 | } |
566 | } |
540 | else |
567 | else |
… | |
… | |
921 | is_nv = 1; |
948 | is_nv = 1; |
922 | } |
949 | } |
923 | |
950 | |
924 | if (!is_nv) |
951 | if (!is_nv) |
925 | { |
952 | { |
|
|
953 | int len = dec->cur - start; |
|
|
954 | |
926 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
955 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
927 | if (*start == '-') |
956 | if (*start == '-') |
928 | switch (dec->cur - start) |
957 | switch (len) |
929 | { |
958 | { |
930 | case 2: return newSViv (-( start [1] - '0' * 1)); |
959 | case 2: return newSViv (-( start [1] - '0' * 1)); |
931 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
960 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
932 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
961 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
933 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
962 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
934 | } |
963 | } |
935 | else |
964 | else |
936 | switch (dec->cur - start) |
965 | switch (len) |
937 | { |
966 | { |
938 | case 1: return newSViv ( start [0] - '0' * 1); |
967 | case 1: return newSViv ( start [0] - '0' * 1); |
939 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
968 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
940 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
969 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
941 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
970 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
942 | } |
971 | } |
943 | |
972 | |
944 | { |
973 | { |
945 | UV uv; |
974 | UV uv; |
946 | int numtype = grok_number (start, dec->cur - start, &uv); |
975 | int numtype = grok_number (start, len, &uv); |
947 | if (numtype & IS_NUMBER_IN_UV) |
976 | if (numtype & IS_NUMBER_IN_UV) |
948 | if (numtype & IS_NUMBER_NEG) |
977 | if (numtype & IS_NUMBER_NEG) |
949 | { |
978 | { |
950 | if (uv < (UV)IV_MIN) |
979 | if (uv < (UV)IV_MIN) |
951 | return newSViv (-(IV)uv); |
980 | return newSViv (-(IV)uv); |
952 | } |
981 | } |
953 | else |
982 | else |
954 | return newSVuv (uv); |
983 | return newSVuv (uv); |
955 | |
|
|
956 | // here would likely be the place for bigint support |
|
|
957 | } |
984 | } |
958 | } |
|
|
959 | |
985 | |
960 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
986 | len -= *start == '-' ? 1 : 0; |
|
|
987 | |
|
|
988 | // does not fit into IV or UV, try NV |
|
|
989 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
990 | #if defined (LDBL_DIG) |
|
|
991 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
992 | #endif |
|
|
993 | ) |
|
|
994 | // fits into NV without loss of precision |
|
|
995 | return newSVnv (Atof (start)); |
|
|
996 | |
|
|
997 | // everything else fails, convert it to a string |
|
|
998 | return newSVpvn (start, dec->cur - start); |
|
|
999 | } |
|
|
1000 | |
|
|
1001 | // loss of precision here |
961 | return newSVnv (Atof (start)); |
1002 | return newSVnv (Atof (start)); |
962 | |
1003 | |
963 | fail: |
1004 | fail: |
964 | return 0; |
1005 | return 0; |
965 | } |
1006 | } |
… | |
… | |
1098 | sv = newRV_noinc ((SV *)hv); |
1139 | sv = newRV_noinc ((SV *)hv); |
1099 | |
1140 | |
1100 | // check filter callbacks |
1141 | // check filter callbacks |
1101 | if (dec->json.flags & F_HOOK) |
1142 | if (dec->json.flags & F_HOOK) |
1102 | { |
1143 | { |
1103 | ENTER; SAVETMPS; |
1144 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1104 | |
|
|
1105 | if (HvKEYS (hv) == 1 && dec->json.cb_sk_object) |
|
|
1106 | { |
1145 | { |
|
|
1146 | HE *cb, *he; |
|
|
1147 | |
|
|
1148 | hv_iterinit (hv); |
|
|
1149 | he = hv_iternext (hv); |
|
|
1150 | hv_iterinit (hv); |
|
|
1151 | |
|
|
1152 | // the next line creates a mortal sv each time its called. |
|
|
1153 | // might want to optimise this for common cases. |
|
|
1154 | cb = hv_fetch_ent (dec->json.cb_sk_object, hv_iterkeysv (he), 0, 0); |
|
|
1155 | |
|
|
1156 | if (cb) |
|
|
1157 | { |
|
|
1158 | dSP; |
1107 | int count; |
1159 | int count; |
1108 | |
1160 | |
1109 | dSP; PUSHMARK (SP); |
1161 | ENTER; SAVETMPS; PUSHMARK (SP); |
1110 | XPUSHs (sv_2mortal (sv)); |
1162 | XPUSHs (HeVAL (he)); |
1111 | |
1163 | |
1112 | PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; |
1164 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1113 | |
1165 | |
1114 | if (count == 1) |
1166 | if (count == 1) |
|
|
1167 | { |
1115 | sv = newSVsv (POPs); |
1168 | sv = newSVsv (POPs); |
1116 | else |
1169 | FREETMPS; LEAVE; |
1117 | SvREFCNT_inc (sv); |
1170 | return sv; |
|
|
1171 | } |
|
|
1172 | |
|
|
1173 | FREETMPS; LEAVE; |
|
|
1174 | } |
1118 | } |
1175 | } |
1119 | |
1176 | |
1120 | if (dec->json.cb_object) |
1177 | if (dec->json.cb_object) |
1121 | { |
1178 | { |
|
|
1179 | dSP; |
1122 | int count; |
1180 | int count; |
1123 | |
1181 | |
1124 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
1182 | ENTER; SAVETMPS; PUSHMARK (SP); |
1125 | XPUSHs (sv_2mortal (sv)); |
1183 | XPUSHs (sv_2mortal (sv)); |
1126 | |
1184 | |
1127 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1185 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1128 | |
1186 | |
1129 | if (count == 1) |
1187 | if (count == 1) |
|
|
1188 | { |
1130 | sv = newSVsv (POPs); |
1189 | sv = newSVsv (POPs); |
1131 | else |
1190 | FREETMPS; LEAVE; |
|
|
1191 | return sv; |
|
|
1192 | } |
|
|
1193 | |
1132 | SvREFCNT_inc (sv); |
1194 | SvREFCNT_inc (sv); |
|
|
1195 | FREETMPS; LEAVE; |
1133 | } |
1196 | } |
1134 | |
|
|
1135 | FREETMPS; LEAVE; |
|
|
1136 | } |
1197 | } |
1137 | |
1198 | |
1138 | return newRV_noinc ((SV *)hv); |
1199 | return sv; |
1139 | |
1200 | |
1140 | fail: |
1201 | fail: |
1141 | SvREFCNT_dec (hv); |
1202 | SvREFCNT_dec (hv); |
1142 | DEC_DEC_DEPTH; |
1203 | DEC_DEC_DEPTH; |
1143 | return 0; |
1204 | return 0; |
… | |
… | |
1310 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1371 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1311 | } |
1372 | } |
1312 | |
1373 | |
1313 | PROTOTYPES: DISABLE |
1374 | PROTOTYPES: DISABLE |
1314 | |
1375 | |
|
|
1376 | void CLONE (...) |
|
|
1377 | CODE: |
|
|
1378 | json_stash = 0; |
|
|
1379 | json_boolean_stash = 0; |
|
|
1380 | |
1315 | void new (char *klass) |
1381 | void new (char *klass) |
1316 | PPCODE: |
1382 | PPCODE: |
1317 | { |
1383 | { |
|
|
1384 | HV *stash = !JSON_SLOW || json_stash |
|
|
1385 | ? json_stash |
|
|
1386 | : gv_stashpv ("JSON::XS", 1); |
1318 | SV *pv = NEWSV (0, sizeof (JSON)); |
1387 | SV *pv = NEWSV (0, sizeof (JSON)); |
1319 | SvPOK_only (pv); |
1388 | SvPOK_only (pv); |
1320 | Zero (SvPVX (pv), 1, JSON); |
1389 | Zero (SvPVX (pv), 1, JSON); |
1321 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1390 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1322 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1391 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); |
1323 | } |
1392 | } |
1324 | |
1393 | |
1325 | void ascii (JSON *self, int enable = 1) |
1394 | void ascii (JSON *self, int enable = 1) |
1326 | ALIAS: |
1395 | ALIAS: |
1327 | ascii = F_ASCII |
1396 | ascii = F_ASCII |
… | |
… | |
1375 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1444 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1376 | |
1445 | |
1377 | XPUSHs (ST (0)); |
1446 | XPUSHs (ST (0)); |
1378 | } |
1447 | } |
1379 | |
1448 | |
1380 | void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef) |
1449 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1381 | ALIAS: |
|
|
1382 | filter_sk_json_objects = 1 |
|
|
1383 | PPCODE: |
1450 | PPCODE: |
1384 | { |
1451 | { |
|
|
1452 | SvREFCNT_dec (self->cb_object); |
|
|
1453 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1454 | |
|
|
1455 | XPUSHs (ST (0)); |
|
|
1456 | } |
|
|
1457 | |
|
|
1458 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1459 | PPCODE: |
|
|
1460 | { |
|
|
1461 | if (!self->cb_sk_object) |
|
|
1462 | self->cb_sk_object = newHV (); |
|
|
1463 | |
1385 | if (!SvOK (cb)) |
1464 | if (SvOK (cb)) |
1386 | cb = 0; |
1465 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1387 | |
1466 | else |
1388 | switch (ix) |
|
|
1389 | { |
1467 | { |
1390 | case 0: self->cb_object = cb; break; |
1468 | hv_delete_ent (self->cb_sk_object, key, G_DISCARD, 0); |
|
|
1469 | |
|
|
1470 | if (!HvKEYS (self->cb_sk_object)) |
|
|
1471 | { |
|
|
1472 | SvREFCNT_dec (self->cb_sk_object); |
1391 | case 1: self->cb_sk_object = cb; break; |
1473 | self->cb_sk_object = 0; |
|
|
1474 | } |
1392 | } |
1475 | } |
1393 | |
1476 | |
1394 | XPUSHs (ST (0)); |
1477 | XPUSHs (ST (0)); |
1395 | } |
1478 | } |
1396 | |
1479 | |
… | |
… | |
1409 | EXTEND (SP, 2); |
1492 | EXTEND (SP, 2); |
1410 | PUSHs (decode_json (jsonstr, self, &offset)); |
1493 | PUSHs (decode_json (jsonstr, self, &offset)); |
1411 | PUSHs (sv_2mortal (newSVuv (offset))); |
1494 | PUSHs (sv_2mortal (newSVuv (offset))); |
1412 | } |
1495 | } |
1413 | |
1496 | |
|
|
1497 | void DESTROY (JSON *self) |
|
|
1498 | CODE: |
|
|
1499 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1500 | SvREFCNT_dec (self->cb_object); |
|
|
1501 | |
1414 | PROTOTYPES: ENABLE |
1502 | PROTOTYPES: ENABLE |
1415 | |
1503 | |
1416 | void to_json (SV *scalar) |
1504 | void to_json (SV *scalar) |
1417 | PPCODE: |
1505 | PPCODE: |
1418 | { |
1506 | { |