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 | # define JSON_STASH (json_stash ? json_stash : gv_stashpv ("JSON::XS", 1)) |
|
|
66 | #else |
|
|
67 | # define JSON_SLOW 0 |
|
|
68 | # define JSON_STASH json_stash |
|
|
69 | #endif |
61 | |
70 | |
62 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
71 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
63 | static SV *json_true, *json_false; |
72 | static SV *json_true, *json_false; |
64 | |
73 | |
65 | typedef struct { |
74 | typedef struct { |
… | |
… | |
306 | encode_ch (enc, '['); encode_nl (enc); |
315 | encode_ch (enc, '['); encode_nl (enc); |
307 | ++enc->indent; |
316 | ++enc->indent; |
308 | |
317 | |
309 | for (i = 0; i <= len; ++i) |
318 | for (i = 0; i <= len; ++i) |
310 | { |
319 | { |
|
|
320 | SV **svp = av_fetch (av, i, 0); |
|
|
321 | |
311 | encode_indent (enc); |
322 | encode_indent (enc); |
312 | encode_sv (enc, *av_fetch (av, i, 0)); |
323 | |
|
|
324 | if (svp) |
|
|
325 | encode_sv (enc, *svp); |
|
|
326 | else |
|
|
327 | encode_str (enc, "null", 4, 0); |
313 | |
328 | |
314 | if (i < len) |
329 | if (i < len) |
315 | encode_comma (enc); |
330 | encode_comma (enc); |
316 | } |
331 | } |
317 | |
332 | |
… | |
… | |
471 | SvGETMAGIC (sv); |
486 | SvGETMAGIC (sv); |
472 | svt = SvTYPE (sv); |
487 | svt = SvTYPE (sv); |
473 | |
488 | |
474 | if (expect_false (SvOBJECT (sv))) |
489 | if (expect_false (SvOBJECT (sv))) |
475 | { |
490 | { |
|
|
491 | HV *stash = !JSON_SLOW || json_boolean_stash |
|
|
492 | ? json_boolean_stash |
|
|
493 | : gv_stashpv ("JSON::XS::Boolean", 1); |
|
|
494 | |
476 | if (SvSTASH (sv) == json_boolean_stash) |
495 | if (SvSTASH (sv) == stash) |
477 | { |
496 | { |
478 | if (SvIV (sv)) |
497 | if (SvIV (sv)) |
479 | encode_str (enc, "true", 4, 0); |
498 | encode_str (enc, "true", 4, 0); |
480 | else |
499 | else |
481 | encode_str (enc, "false", 5, 0); |
500 | encode_str (enc, "false", 5, 0); |
… | |
… | |
493 | // we re-bless the reference to get overload and other niceties right |
512 | // we re-bless the reference to get overload and other niceties right |
494 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
513 | GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); |
495 | |
514 | |
496 | if (to_json) |
515 | if (to_json) |
497 | { |
516 | { |
498 | int count; |
|
|
499 | dSP; |
517 | dSP; |
500 | |
518 | |
501 | ENTER; SAVETMPS; PUSHMARK (SP); |
519 | ENTER; SAVETMPS; PUSHMARK (SP); |
502 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
520 | XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); |
503 | |
521 | |
… | |
… | |
931 | is_nv = 1; |
949 | is_nv = 1; |
932 | } |
950 | } |
933 | |
951 | |
934 | if (!is_nv) |
952 | if (!is_nv) |
935 | { |
953 | { |
|
|
954 | int len = dec->cur - start; |
|
|
955 | |
936 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
956 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
937 | if (*start == '-') |
957 | if (*start == '-') |
938 | switch (dec->cur - start) |
958 | switch (len) |
939 | { |
959 | { |
940 | case 2: return newSViv (-( start [1] - '0' * 1)); |
960 | case 2: return newSViv (-( start [1] - '0' * 1)); |
941 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
961 | 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)); |
962 | 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)); |
963 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
944 | } |
964 | } |
945 | else |
965 | else |
946 | switch (dec->cur - start) |
966 | switch (len) |
947 | { |
967 | { |
948 | case 1: return newSViv ( start [0] - '0' * 1); |
968 | case 1: return newSViv ( start [0] - '0' * 1); |
949 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
969 | 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); |
970 | 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); |
971 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
952 | } |
972 | } |
953 | |
973 | |
954 | { |
974 | { |
955 | UV uv; |
975 | UV uv; |
956 | int numtype = grok_number (start, dec->cur - start, &uv); |
976 | int numtype = grok_number (start, len, &uv); |
957 | if (numtype & IS_NUMBER_IN_UV) |
977 | if (numtype & IS_NUMBER_IN_UV) |
958 | if (numtype & IS_NUMBER_NEG) |
978 | if (numtype & IS_NUMBER_NEG) |
959 | { |
979 | { |
960 | if (uv < (UV)IV_MIN) |
980 | if (uv < (UV)IV_MIN) |
961 | return newSViv (-(IV)uv); |
981 | return newSViv (-(IV)uv); |
962 | } |
982 | } |
963 | else |
983 | else |
964 | return newSVuv (uv); |
984 | return newSVuv (uv); |
965 | |
|
|
966 | // here would likely be the place for bigint support |
|
|
967 | } |
985 | } |
968 | } |
|
|
969 | |
986 | |
970 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
987 | len -= *start == '-' ? 1 : 0; |
|
|
988 | |
|
|
989 | // does not fit into IV or UV, try NV |
|
|
990 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
991 | #if defined (LDBL_DIG) |
|
|
992 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
993 | #endif |
|
|
994 | ) |
|
|
995 | // fits into NV without loss of precision |
|
|
996 | return newSVnv (Atof (start)); |
|
|
997 | |
|
|
998 | // everything else fails, convert it to a string |
|
|
999 | return newSVpvn (start, dec->cur - start); |
|
|
1000 | } |
|
|
1001 | |
|
|
1002 | // loss of precision here |
971 | return newSVnv (Atof (start)); |
1003 | return newSVnv (Atof (start)); |
972 | |
1004 | |
973 | fail: |
1005 | fail: |
974 | return 0; |
1006 | return 0; |
975 | } |
1007 | } |
… | |
… | |
1340 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1372 | json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); |
1341 | } |
1373 | } |
1342 | |
1374 | |
1343 | PROTOTYPES: DISABLE |
1375 | PROTOTYPES: DISABLE |
1344 | |
1376 | |
|
|
1377 | void CLONE (...) |
|
|
1378 | CODE: |
|
|
1379 | json_stash = 0; |
|
|
1380 | json_boolean_stash = 0; |
|
|
1381 | |
1345 | void new (char *klass) |
1382 | void new (char *klass) |
1346 | PPCODE: |
1383 | PPCODE: |
1347 | { |
1384 | { |
1348 | SV *pv = NEWSV (0, sizeof (JSON)); |
1385 | SV *pv = NEWSV (0, sizeof (JSON)); |
1349 | SvPOK_only (pv); |
1386 | SvPOK_only (pv); |
1350 | Zero (SvPVX (pv), 1, JSON); |
1387 | Zero (SvPVX (pv), 1, JSON); |
1351 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1388 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1352 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); |
1389 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
1353 | } |
1390 | } |
1354 | |
1391 | |
1355 | void ascii (JSON *self, int enable = 1) |
1392 | void ascii (JSON *self, int enable = 1) |
1356 | ALIAS: |
1393 | ALIAS: |
1357 | ascii = F_ASCII |
1394 | ascii = F_ASCII |