… | |
… | |
1000 | |
1000 | |
1001 | if (!is_nv) |
1001 | if (!is_nv) |
1002 | { |
1002 | { |
1003 | int len = dec->cur - start; |
1003 | int len = dec->cur - start; |
1004 | |
1004 | |
1005 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
1005 | // special case the rather common 1..5-digit-int case |
1006 | if (*start == '-') |
1006 | if (*start == '-') |
1007 | switch (len) |
1007 | switch (len) |
1008 | { |
1008 | { |
1009 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1009 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1010 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1010 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1011 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1011 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1012 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1012 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
|
|
1013 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1013 | } |
1014 | } |
1014 | else |
1015 | else |
1015 | switch (len) |
1016 | switch (len) |
1016 | { |
1017 | { |
1017 | case 1: return newSViv ( start [0] - '0' * 1); |
1018 | case 1: return newSViv ( start [0] - '0' * 1); |
1018 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1019 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1019 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1020 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1020 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1021 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
|
|
1022 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1021 | } |
1023 | } |
1022 | |
1024 | |
1023 | { |
1025 | { |
1024 | UV uv; |
1026 | UV uv; |
1025 | int numtype = grok_number (start, len, &uv); |
1027 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1457 | { |
1459 | { |
1458 | SV *pv = NEWSV (0, sizeof (JSON)); |
1460 | SV *pv = NEWSV (0, sizeof (JSON)); |
1459 | SvPOK_only (pv); |
1461 | SvPOK_only (pv); |
1460 | Zero (SvPVX (pv), 1, JSON); |
1462 | Zero (SvPVX (pv), 1, JSON); |
1461 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1463 | ((JSON *)SvPVX (pv))->flags = F_DEFAULT; |
1462 | XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH))); |
1464 | XPUSHs (sv_2mortal (sv_bless ( |
|
|
1465 | newRV_noinc (pv), |
|
|
1466 | strEQ (klass, "JSON::XS") ? JSON_STASH : gv_stashpv (klass, 1) |
|
|
1467 | ))); |
1463 | } |
1468 | } |
1464 | |
1469 | |
1465 | void ascii (JSON *self, int enable = 1) |
1470 | void ascii (JSON *self, int enable = 1) |
1466 | ALIAS: |
1471 | ALIAS: |
1467 | ascii = F_ASCII |
1472 | ascii = F_ASCII |
… | |
… | |
1494 | get_utf8 = F_UTF8 |
1499 | get_utf8 = F_UTF8 |
1495 | get_indent = F_INDENT |
1500 | get_indent = F_INDENT |
1496 | get_canonical = F_CANONICAL |
1501 | get_canonical = F_CANONICAL |
1497 | get_space_before = F_SPACE_BEFORE |
1502 | get_space_before = F_SPACE_BEFORE |
1498 | get_space_after = F_SPACE_AFTER |
1503 | get_space_after = F_SPACE_AFTER |
1499 | get_pretty = F_PRETTY |
|
|
1500 | get_allow_nonref = F_ALLOW_NONREF |
1504 | get_allow_nonref = F_ALLOW_NONREF |
1501 | get_shrink = F_SHRINK |
1505 | get_shrink = F_SHRINK |
1502 | get_allow_blessed = F_ALLOW_BLESSED |
1506 | get_allow_blessed = F_ALLOW_BLESSED |
1503 | get_convert_blessed = F_CONV_BLESSED |
1507 | get_convert_blessed = F_CONV_BLESSED |
1504 | get_relaxed = F_RELAXED |
1508 | get_relaxed = F_RELAXED |
… | |
… | |
1518 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1522 | self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); |
1519 | |
1523 | |
1520 | XPUSHs (ST (0)); |
1524 | XPUSHs (ST (0)); |
1521 | } |
1525 | } |
1522 | |
1526 | |
1523 | int get_max_depth (JSON *self) |
1527 | U32 get_max_depth (JSON *self) |
1524 | CODE: |
1528 | CODE: |
1525 | RETVAL = DEC_DEPTH (self->flags); |
1529 | RETVAL = DEC_DEPTH (self->flags); |
1526 | OUTPUT: |
1530 | OUTPUT: |
1527 | RETVAL |
1531 | RETVAL |
1528 | |
1532 | |
… | |
… | |
1601 | SvREFCNT_dec (self->cb_sk_object); |
1605 | SvREFCNT_dec (self->cb_sk_object); |
1602 | SvREFCNT_dec (self->cb_object); |
1606 | SvREFCNT_dec (self->cb_object); |
1603 | |
1607 | |
1604 | PROTOTYPES: ENABLE |
1608 | PROTOTYPES: ENABLE |
1605 | |
1609 | |
1606 | void to_json (SV *scalar) |
1610 | void encode_json (SV *scalar) |
1607 | PPCODE: |
1611 | PPCODE: |
1608 | { |
1612 | { |
1609 | JSON json = { F_DEFAULT | F_UTF8 }; |
1613 | JSON json = { F_DEFAULT | F_UTF8 }; |
1610 | XPUSHs (encode_json (scalar, &json)); |
1614 | XPUSHs (encode_json (scalar, &json)); |
1611 | } |
1615 | } |
1612 | |
1616 | |
1613 | void from_json (SV *jsonstr) |
1617 | void decode_json (SV *jsonstr) |
1614 | PPCODE: |
1618 | PPCODE: |
1615 | { |
1619 | { |
1616 | JSON json = { F_DEFAULT | F_UTF8 }; |
1620 | JSON json = { F_DEFAULT | F_UTF8 }; |
1617 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1621 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1618 | } |
1622 | } |