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 | |
… | |
… | |
56 | # define inline static |
57 | # define inline static |
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) |
|
|
62 | |
|
|
63 | #ifdef USE_ITHREADS |
|
|
64 | # define JSON_SLOW 1 |
|
|
65 | #else |
|
|
66 | # define JSON_SLOW 0 |
|
|
67 | #endif |
61 | |
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 { |
… | |
… | |
306 | encode_ch (enc, '['); encode_nl (enc); |
313 | encode_ch (enc, '['); encode_nl (enc); |
307 | ++enc->indent; |
314 | ++enc->indent; |
308 | |
315 | |
309 | for (i = 0; i <= len; ++i) |
316 | for (i = 0; i <= len; ++i) |
310 | { |
317 | { |
|
|
318 | SV **svp = av_fetch (av, i, 0); |
|
|
319 | |
311 | encode_indent (enc); |
320 | encode_indent (enc); |
312 | 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); |
313 | |
326 | |
314 | if (i < len) |
327 | if (i < len) |
315 | encode_comma (enc); |
328 | encode_comma (enc); |
316 | } |
329 | } |
317 | |
330 | |
… | |
… | |
471 | SvGETMAGIC (sv); |
484 | SvGETMAGIC (sv); |
472 | svt = SvTYPE (sv); |
485 | svt = SvTYPE (sv); |
473 | |
486 | |
474 | if (expect_false (SvOBJECT (sv))) |
487 | if (expect_false (SvOBJECT (sv))) |
475 | { |
488 | { |
|
|
489 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
490 | ? json_boolean_stash |
|
|
491 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
492 | |
476 | if (SvSTASH (sv) == json_boolean_stash) |
493 | if (SvSTASH (sv) == stash) |
477 | { |
494 | { |
478 | if (SvIV (sv)) |
495 | if (SvIV (sv)) |
479 | encode_str (enc, "true", 4, 0); |
496 | encode_str (enc, "true", 4, 0); |
480 | else |
497 | else |
481 | encode_str (enc, "false", 5, 0); |
498 | encode_str (enc, "false", 5, 0); |
… | |
… | |
499 | |
516 | |
500 | ENTER; SAVETMPS; PUSHMARK (SP); |
517 | ENTER; SAVETMPS; PUSHMARK (SP); |
501 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
518 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
502 | |
519 | |
503 | // calling with G_SCALAR ensures that we always get a 1 return value |
520 | // calling with G_SCALAR ensures that we always get a 1 return value |
504 | // check anyways. |
|
|
505 | PUTBACK; |
521 | PUTBACK; |
506 | assert (1 == call_sv ((SV *)GvCV (to_json), G_SCALAR)); |
522 | call_sv ((SV *)GvCV (to_json), G_SCALAR); |
507 | SPAGAIN; |
523 | SPAGAIN; |
508 | |
524 | |
509 | // catch this surprisingly common error |
525 | // catch this surprisingly common error |
510 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
526 | if (SvROK (TOPs) && SvRV (TOPs) == sv) |
511 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
527 | croak ("%s::TO_JSON method returned same object as was passed instead of a new one", HvNAME (SvSTASH (sv))); |
… | |
… | |
931 | is_nv = 1; |
947 | is_nv = 1; |
932 | } |
948 | } |
933 | |
949 | |
934 | if (!is_nv) |
950 | if (!is_nv) |
935 | { |
951 | { |
|
|
952 | int len = dec->cur - start; |
|
|
953 | |
936 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
954 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
937 | if (*start == '-') |
955 | if (*start == '-') |
938 | switch (dec->cur - start) |
956 | switch (len) |
939 | { |
957 | { |
940 | case 2: return newSViv (-( start [1] - '0' * 1)); |
958 | case 2: return newSViv (-( start [1] - '0' * 1)); |
941 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
959 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
942 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
960 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
943 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
961 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
944 | } |
962 | } |
945 | else |
963 | else |
946 | switch (dec->cur - start) |
964 | switch (len) |
947 | { |
965 | { |
948 | case 1: return newSViv ( start [0] - '0' * 1); |
966 | case 1: return newSViv ( start [0] - '0' * 1); |
949 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
967 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
950 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
968 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
951 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
969 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
952 | } |
970 | } |
953 | |
971 | |
954 | { |
972 | { |
955 | UV uv; |
973 | UV uv; |
956 | int numtype = grok_number (start, dec->cur - start, &uv); |
974 | int numtype = grok_number (start, len, &uv); |
957 | if (numtype & IS_NUMBER_IN_UV) |
975 | if (numtype & IS_NUMBER_IN_UV) |
958 | if (numtype & IS_NUMBER_NEG) |
976 | if (numtype & IS_NUMBER_NEG) |
959 | { |
977 | { |
960 | if (uv < (UV)IV_MIN) |
978 | if (uv < (UV)IV_MIN) |
961 | return newSViv (-(IV)uv); |
979 | return newSViv (-(IV)uv); |
962 | } |
980 | } |
963 | else |
981 | else |
964 | return newSVuv (uv); |
982 | return newSVuv (uv); |
965 | |
|
|
966 | // here would likely be the place for bigint support |
|
|
967 | } |
983 | } |
968 | } |
|
|
969 | |
984 | |
970 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
985 | len -= *start == '-' ? 1 : 0; |
|
|
986 | |
|
|
987 | // does not fit into IV or UV, try NV |
|
|
988 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
989 | #if defined (LDBL_DIG) |
|
|
990 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
991 | #endif |
|
|
992 | ) |
|
|
993 | // fits into NV without loss of precision |
|
|
994 | return newSVnv (Atof (start)); |
|
|
995 | |
|
|
996 | // everything else fails, convert it to a string |
|
|
997 | return newSVpvn (start, dec->cur - start); |
|
|
998 | } |
|
|
999 | |
|
|
1000 | // loss of precision here |
971 | return newSVnv (Atof (start)); |
1001 | return newSVnv (Atof (start)); |
972 | |
1002 | |
973 | fail: |
1003 | fail: |
974 | return 0; |
1004 | return 0; |
975 | } |
1005 | } |
… | |
… | |
1340 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1370 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1341 | } |
1371 | } |
1342 | |
1372 | |
1343 | PROTOTYPES: DISABLE |
1373 | PROTOTYPES: DISABLE |
1344 | |
1374 | |
|
|
1375 | void CLONE (...) |
|
|
1376 | CODE: |
|
|
1377 | json_stash = 0; |
|
|
1378 | json_boolean_stash = 0; |
|
|
1379 | |
1345 | void new (char *klass) |
1380 | void new (char *klass) |
1346 | PPCODE: |
1381 | PPCODE: |
1347 | { |
1382 | { |
|
|
1383 | HV *stash = !JSON_SLOW || json_stash |
|
|
1384 | ? json_stash |
|
|
1385 | : gv_stashpv ("JSON::XS", 1); |
1348 | SV *pv = NEWSV (0, sizeof (JSON)); |
1386 | SV *pv = NEWSV (0, sizeof (JSON)); |
1349 | SvPOK_only (pv); |
1387 | SvPOK_only (pv); |
1350 | Zero (SvPVX (pv), 1, JSON); |
1388 | Zero (SvPVX (pv), 1, JSON); |
1351 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1389 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1352 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1390 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash))); |
1353 | } |
1391 | } |
1354 | |
1392 | |
1355 | void ascii (JSON *self, int enable = 1) |
1393 | void ascii (JSON *self, int enable = 1) |
1356 | ALIAS: |
1394 | ALIAS: |
1357 | ascii = F_ASCII |
1395 | ascii = F_ASCII |