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 | |
… | |
… | |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static SV *json_true, *json_false; |
64 | static SV *json_true, *json_false; |
64 | |
65 | |
65 | typedef struct { |
66 | typedef struct { |
66 | U32 flags; |
67 | U32 flags; |
67 | SV *cb_object, *cb_sk_object; |
68 | SV *cb_object; |
|
|
69 | HV *cb_sk_object; |
68 | } JSON; |
70 | } JSON; |
69 | |
71 | |
70 | ///////////////////////////////////////////////////////////////////////////// |
72 | ///////////////////////////////////////////////////////////////////////////// |
71 | // utility functions |
73 | // utility functions |
72 | |
74 | |
… | |
… | |
305 | encode_ch (enc, '['); encode_nl (enc); |
307 | encode_ch (enc, '['); encode_nl (enc); |
306 | ++enc->indent; |
308 | ++enc->indent; |
307 | |
309 | |
308 | for (i = 0; i <= len; ++i) |
310 | for (i = 0; i <= len; ++i) |
309 | { |
311 | { |
|
|
312 | SV **svp = av_fetch (av, i, 0); |
|
|
313 | |
310 | encode_indent (enc); |
314 | encode_indent (enc); |
311 | 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); |
312 | |
320 | |
313 | if (i < len) |
321 | if (i < len) |
314 | encode_comma (enc); |
322 | encode_comma (enc); |
315 | } |
323 | } |
316 | |
324 | |
… | |
… | |
472 | |
480 | |
473 | if (expect_false (SvOBJECT (sv))) |
481 | if (expect_false (SvOBJECT (sv))) |
474 | { |
482 | { |
475 | if (SvSTASH (sv) == json_boolean_stash) |
483 | if (SvSTASH (sv) == json_boolean_stash) |
476 | { |
484 | { |
477 | if (SvIV (sv) == 0) |
485 | if (SvIV (sv)) |
|
|
486 | encode_str (enc, "true", 4, 0); |
|
|
487 | else |
478 | encode_str (enc, "false", 5, 0); |
488 | encode_str (enc, "false", 5, 0); |
479 | else |
|
|
480 | encode_str (enc, "true", 4, 0); |
|
|
481 | } |
489 | } |
482 | else |
490 | else |
483 | { |
491 | { |
484 | #if 0 |
492 | #if 0 |
485 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
493 | if (0 && sv_derived_from (rv, "JSON::Literal")) |
… | |
… | |
488 | } |
496 | } |
489 | #endif |
497 | #endif |
490 | if (enc->json.flags & F_CONV_BLESSED) |
498 | if (enc->json.flags & F_CONV_BLESSED) |
491 | { |
499 | { |
492 | // 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 |
493 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 1); |
501 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
494 | |
502 | |
495 | if (to_json) |
503 | if (to_json) |
496 | { |
504 | { |
|
|
505 | int count; |
|
|
506 | dSP; |
|
|
507 | |
497 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
508 | ENTER; SAVETMPS; PUSHMARK (SP); |
498 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
509 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
499 | |
510 | |
500 | // 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 |
501 | // check anyways. |
|
|
502 | PUTBACK; |
512 | PUTBACK; |
503 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
513 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
504 | SPAGAIN; |
514 | SPAGAIN; |
505 | |
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 | |
506 | encode_sv (enc, POPs); |
523 | encode_sv (enc, sv); |
507 | |
524 | |
508 | FREETMPS; LEAVE; |
525 | FREETMPS; LEAVE; |
509 | } |
526 | } |
510 | else if (enc->json.flags & F_ALLOW_BLESSED) |
527 | else if (enc->json.flags & F_ALLOW_BLESSED) |
511 | encode_str (enc, "null", 4, 0); |
528 | encode_str (enc, "null", 4, 0); |
… | |
… | |
527 | else if (svt < SVt_PVAV) |
544 | else if (svt < SVt_PVAV) |
528 | { |
545 | { |
529 | STRLEN len = 0; |
546 | STRLEN len = 0; |
530 | char *pv = svt ? SvPV (sv, len) : 0; |
547 | char *pv = svt ? SvPV (sv, len) : 0; |
531 | |
548 | |
532 | if (len == 1 && *pv == '0') |
549 | if (len == 1 && *pv == '1') |
|
|
550 | encode_str (enc, "true", 4, 0); |
|
|
551 | else if (len == 1 && *pv == '0') |
533 | encode_str (enc, "false", 5, 0); |
552 | encode_str (enc, "false", 5, 0); |
534 | else if (len == 1 && *pv == '1') |
|
|
535 | encode_str (enc, "true", 4, 0); |
|
|
536 | else |
553 | else |
537 | 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", |
538 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
555 | SvPV_nolen (sv_2mortal (newRV_inc (sv)))); |
539 | } |
556 | } |
540 | else |
557 | else |
… | |
… | |
921 | is_nv = 1; |
938 | is_nv = 1; |
922 | } |
939 | } |
923 | |
940 | |
924 | if (!is_nv) |
941 | if (!is_nv) |
925 | { |
942 | { |
|
|
943 | int len = dec->cur - start; |
|
|
944 | |
926 | // 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 |
927 | if (*start == '-') |
946 | if (*start == '-') |
928 | switch (dec->cur - start) |
947 | switch (len) |
929 | { |
948 | { |
930 | case 2: return newSViv (-( start [1] - '0' * 1)); |
949 | case 2: return newSViv (-( start [1] - '0' * 1)); |
931 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
950 | 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)); |
951 | 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)); |
952 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
934 | } |
953 | } |
935 | else |
954 | else |
936 | switch (dec->cur - start) |
955 | switch (len) |
937 | { |
956 | { |
938 | case 1: return newSViv ( start [0] - '0' * 1); |
957 | case 1: return newSViv ( start [0] - '0' * 1); |
939 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
958 | 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); |
959 | 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); |
960 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
942 | } |
961 | } |
943 | |
962 | |
944 | { |
963 | { |
945 | UV uv; |
964 | UV uv; |
946 | int numtype = grok_number (start, dec->cur - start, &uv); |
965 | int numtype = grok_number (start, len, &uv); |
947 | if (numtype & IS_NUMBER_IN_UV) |
966 | if (numtype & IS_NUMBER_IN_UV) |
948 | if (numtype & IS_NUMBER_NEG) |
967 | if (numtype & IS_NUMBER_NEG) |
949 | { |
968 | { |
950 | if (uv < (UV)IV_MIN) |
969 | if (uv < (UV)IV_MIN) |
951 | return newSViv (-(IV)uv); |
970 | return newSViv (-(IV)uv); |
952 | } |
971 | } |
953 | else |
972 | else |
954 | return newSVuv (uv); |
973 | return newSVuv (uv); |
955 | |
|
|
956 | // here would likely be the place for bigint support |
|
|
957 | } |
974 | } |
958 | } |
|
|
959 | |
975 | |
960 | // 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 |
961 | return newSVnv (Atof (start)); |
992 | return newSVnv (Atof (start)); |
962 | |
993 | |
963 | fail: |
994 | fail: |
964 | return 0; |
995 | return 0; |
965 | } |
996 | } |
… | |
… | |
1098 | sv = newRV_noinc ((SV *)hv); |
1129 | sv = newRV_noinc ((SV *)hv); |
1099 | |
1130 | |
1100 | // check filter callbacks |
1131 | // check filter callbacks |
1101 | if (dec->json.flags & F_HOOK) |
1132 | if (dec->json.flags & F_HOOK) |
1102 | { |
1133 | { |
1103 | ENTER; SAVETMPS; |
1134 | if (dec->json.cb_sk_object && HvKEYS (hv) == 1) |
1104 | |
|
|
1105 | if (HvKEYS (hv) == 1 && dec->json.cb_sk_object) |
|
|
1106 | { |
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; |
1107 | int count; |
1149 | int count; |
1108 | |
1150 | |
1109 | dSP; PUSHMARK (SP); |
1151 | ENTER; SAVETMPS; PUSHMARK (SP); |
1110 | XPUSHs (sv_2mortal (sv)); |
1152 | XPUSHs (HeVAL (he)); |
1111 | |
1153 | |
1112 | PUTBACK; count = call_sv (dec->json.cb_sk_object, G_ARRAY); SPAGAIN; |
1154 | PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; |
1113 | |
1155 | |
1114 | if (count == 1) |
1156 | if (count == 1) |
|
|
1157 | { |
1115 | sv = newSVsv (POPs); |
1158 | sv = newSVsv (POPs); |
1116 | else |
1159 | FREETMPS; LEAVE; |
1117 | SvREFCNT_inc (sv); |
1160 | return sv; |
|
|
1161 | } |
|
|
1162 | |
|
|
1163 | FREETMPS; LEAVE; |
|
|
1164 | } |
1118 | } |
1165 | } |
1119 | |
1166 | |
1120 | if (dec->json.cb_object) |
1167 | if (dec->json.cb_object) |
1121 | { |
1168 | { |
|
|
1169 | dSP; |
1122 | int count; |
1170 | int count; |
1123 | |
1171 | |
1124 | dSP; ENTER; SAVETMPS; PUSHMARK (SP); |
1172 | ENTER; SAVETMPS; PUSHMARK (SP); |
1125 | XPUSHs (sv_2mortal (sv)); |
1173 | XPUSHs (sv_2mortal (sv)); |
1126 | |
1174 | |
1127 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1175 | PUTBACK; count = call_sv (dec->json.cb_object, G_ARRAY); SPAGAIN; |
1128 | |
1176 | |
1129 | if (count == 1) |
1177 | if (count == 1) |
|
|
1178 | { |
1130 | sv = newSVsv (POPs); |
1179 | sv = newSVsv (POPs); |
1131 | else |
1180 | FREETMPS; LEAVE; |
|
|
1181 | return sv; |
|
|
1182 | } |
|
|
1183 | |
1132 | SvREFCNT_inc (sv); |
1184 | SvREFCNT_inc (sv); |
|
|
1185 | FREETMPS; LEAVE; |
1133 | } |
1186 | } |
1134 | |
|
|
1135 | FREETMPS; LEAVE; |
|
|
1136 | } |
1187 | } |
1137 | |
1188 | |
1138 | return newRV_noinc ((SV *)hv); |
1189 | return sv; |
1139 | |
1190 | |
1140 | fail: |
1191 | fail: |
1141 | SvREFCNT_dec (hv); |
1192 | SvREFCNT_dec (hv); |
1142 | DEC_DEC_DEPTH; |
1193 | DEC_DEC_DEPTH; |
1143 | return 0; |
1194 | return 0; |
… | |
… | |
1375 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1426 | self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); |
1376 | |
1427 | |
1377 | XPUSHs (ST (0)); |
1428 | XPUSHs (ST (0)); |
1378 | } |
1429 | } |
1379 | |
1430 | |
1380 | void filter_json_objects (JSON *self, SV *cb = &PL_sv_undef) |
1431 | void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) |
1381 | ALIAS: |
|
|
1382 | filter_sk_json_objects = 1 |
|
|
1383 | PPCODE: |
1432 | PPCODE: |
1384 | { |
1433 | { |
|
|
1434 | SvREFCNT_dec (self->cb_object); |
|
|
1435 | self->cb_object = SvOK (cb) ? newSVsv (cb) : 0; |
|
|
1436 | |
|
|
1437 | XPUSHs (ST (0)); |
|
|
1438 | } |
|
|
1439 | |
|
|
1440 | void filter_json_single_key_object (JSON *self, SV *key, SV *cb = &PL_sv_undef) |
|
|
1441 | PPCODE: |
|
|
1442 | { |
|
|
1443 | if (!self->cb_sk_object) |
|
|
1444 | self->cb_sk_object = newHV (); |
|
|
1445 | |
1385 | if (!SvOK (cb)) |
1446 | if (SvOK (cb)) |
1386 | cb = 0; |
1447 | hv_store_ent (self->cb_sk_object, key, newSVsv (cb), 0); |
1387 | |
1448 | else |
1388 | switch (ix) |
|
|
1389 | { |
1449 | { |
1390 | case 0: self->cb_object = cb; break; |
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); |
1391 | case 1: self->cb_sk_object = cb; break; |
1455 | self->cb_sk_object = 0; |
|
|
1456 | } |
1392 | } |
1457 | } |
1393 | |
1458 | |
1394 | XPUSHs (ST (0)); |
1459 | XPUSHs (ST (0)); |
1395 | } |
1460 | } |
1396 | |
1461 | |
… | |
… | |
1409 | EXTEND (SP, 2); |
1474 | EXTEND (SP, 2); |
1410 | PUSHs (decode_json (jsonstr, self, &offset)); |
1475 | PUSHs (decode_json (jsonstr, self, &offset)); |
1411 | PUSHs (sv_2mortal (newSVuv (offset))); |
1476 | PUSHs (sv_2mortal (newSVuv (offset))); |
1412 | } |
1477 | } |
1413 | |
1478 | |
|
|
1479 | void DESTROY (JSON *self) |
|
|
1480 | CODE: |
|
|
1481 | SvREFCNT_dec (self->cb_sk_object); |
|
|
1482 | SvREFCNT_dec (self->cb_object); |
|
|
1483 | |
1414 | PROTOTYPES: ENABLE |
1484 | PROTOTYPES: ENABLE |
1415 | |
1485 | |
1416 | void to_json (SV *scalar) |
1486 | void to_json (SV *scalar) |
1417 | PPCODE: |
1487 | PPCODE: |
1418 | { |
1488 | { |